Skip to content

Commit 3b14bfc

Browse files
authored
Merge pull request #238 from udecode/codex/auth-schema-import-idempotent
2 parents 82a1e09 + f43fc36 commit 3b14bfc

3 files changed

Lines changed: 67 additions & 1 deletion

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"kitcn": patch
3+
---
4+
5+
## Patches
6+
7+
- Fix `kitcn add auth --preset convex` so schema registration reuses existing `authSchema` imports.

packages/kitcn/src/cli/registry/items/auth/auth-item.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,4 +549,61 @@ ${userUnit.relations},
549549
owner: 'managed',
550550
});
551551
});
552+
553+
test('convex auth schema registration reuses existing double-quoted authSchema import', async () => {
554+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'kitcn-auth-item-'));
555+
const functionsDir = path.join(dir, 'convex', 'functions');
556+
const schemaPath = path.join(functionsDir, 'schema.ts');
557+
const schemaSource = `
558+
import { authSchema } from "./authSchema";
559+
import { defineSchema } from "convex/server";
560+
561+
export default defineSchema({
562+
messages: {},
563+
});
564+
`.trim();
565+
566+
fs.mkdirSync(functionsDir, { recursive: true });
567+
fs.writeFileSync(schemaPath, schemaSource, 'utf8');
568+
569+
const descriptor = getPluginCatalogEntry('auth');
570+
const plan =
571+
await descriptor.integration?.buildSchemaRegistrationPlanFile?.({
572+
config: createDefaultConfig(),
573+
functionsDir,
574+
lockfile: {
575+
plugins: {},
576+
},
577+
overwrite: true,
578+
preset: 'convex',
579+
preview: false,
580+
promptAdapter: {
581+
confirm: async () => false,
582+
isInteractive: () => false,
583+
multiselect: async () => [],
584+
select: async () => 'ignored',
585+
},
586+
roots: {
587+
appRootDir: null,
588+
clientLibRootDir: null,
589+
crpcFilePath: path.join(dir, 'convex', 'lib', 'crpc.ts'),
590+
envFilePath: path.join(dir, 'convex', 'lib', 'get-env.ts'),
591+
functionsRootDir: functionsDir,
592+
libRootDir: path.join(dir, 'convex', 'lib'),
593+
projectContext: null,
594+
sharedApiFilePath: path.join(dir, 'convex', 'shared', 'api.ts'),
595+
},
596+
yes: true,
597+
});
598+
599+
expect(
600+
plan?.content.match(
601+
/import \{ authSchema \} from ['"]\.\/authSchema['"];/g
602+
)
603+
).toHaveLength(1);
604+
expect(plan?.content).toContain(
605+
'import { authSchema } from "./authSchema";'
606+
);
607+
expect(plan?.content).toContain('...authSchema,');
608+
});
552609
});

packages/kitcn/src/cli/registry/items/auth/auth-item.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ const AUTH_CONVEX_HTTP_IMPORT_RE = /from\s+['"]kitcn\/auth\/http['"]/;
7070
const AUTH_CONVEX_HTTP_GET_AUTH_IMPORT_RE =
7171
/from\s+['"]\.\/generated\/auth['"]/;
7272
const AUTH_CONVEX_SCHEMA_CALL_RE = /defineSchema\(\s*\{/;
73+
const AUTH_CONVEX_SCHEMA_AUTH_SCHEMA_IMPORT_RE =
74+
/import\s+\{\s*authSchema\s*\}\s+from\s+['"]\.\/authSchema['"];?/;
7375
const AUTH_CONVEX_APP_IMPORT_RE = /import App from ['"][^'"]+['"];?/;
7476
const AUTH_CONVEX_PROVIDER_IMPORT_RE =
7577
/import\s+\{\s*ConvexProvider,\s*ConvexReactClient\s*\}\s+from\s+['"]convex\/react['"];?/;
@@ -624,7 +626,7 @@ function buildAuthConvexSchemaPlanFile(
624626
const schemaPath = getSchemaFilePath(params.functionsDir);
625627
let source = fs.readFileSync(schemaPath, 'utf8');
626628

627-
if (!source.includes("import { authSchema } from './authSchema';")) {
629+
if (!AUTH_CONVEX_SCHEMA_AUTH_SCHEMA_IMPORT_RE.test(source)) {
628630
source = `import { authSchema } from './authSchema';\n${source}`;
629631
}
630632
if (!source.includes('...authSchema')) {

0 commit comments

Comments
 (0)