Skip to content

Commit 0ac1310

Browse files
committed
fix(cli): preserve raw auth client reruns
1 parent 815cdf6 commit 0ac1310

4 files changed

Lines changed: 89 additions & 11 deletions

File tree

.changeset/ten-bees-swim.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@
77
- Fix `bunx --bun kitcn init -t start --yes` so Bun-native parse-time imports
88
no longer bypass project aliases and crash first-run codegen on scaffolded
99
Start files.
10+
- Fix raw auth reruns so `http.ts` import detection respects both quote styles,
11+
`registerRoutes(http, getAuth, ...)` accepts Better Auth route contracts
12+
without a type cast, and raw auth clients keep the app `SITE_URL` while
13+
preserving user-edited raw `auth-client.ts` files on reruns.

packages/kitcn/src/cli/cli.commands.ts

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,9 +2370,7 @@ describe('cli/cli', () => {
23702370
path.join(dir, 'lib', 'convex', 'auth-client.ts'),
23712371
'utf8'
23722372
);
2373-
expect(authClientSource).toContain(
2374-
'process.env.NEXT_PUBLIC_CONVEX_SITE_URL!'
2375-
);
2373+
expect(authClientSource).toContain('process.env.NEXT_PUBLIC_SITE_URL!');
23762374
expect(authClientSource).not.toContain('createAuthMutations');
23772375

23782376
const schemaSource = fs.readFileSync(
@@ -2474,9 +2472,7 @@ describe('cli/cli', () => {
24742472
path.join(dir, 'src', 'lib', 'convex', 'auth-client.ts'),
24752473
'utf8'
24762474
);
2477-
expect(authClientSource).toContain(
2478-
'import.meta.env.VITE_CONVEX_SITE_URL!'
2479-
);
2475+
expect(authClientSource).toContain('import.meta.env.VITE_SITE_URL!');
24802476
expect(authClientSource).not.toContain('createAuthMutations');
24812477
expect(
24822478
execaStub.mock.calls.some((call) => {
@@ -2566,9 +2562,7 @@ describe('cli/cli', () => {
25662562
path.join(dir, 'src', 'lib', 'convex', 'auth-client.ts'),
25672563
'utf8'
25682564
);
2569-
expect(authClientSource).toContain(
2570-
'import.meta.env.VITE_CONVEX_SITE_URL!'
2571-
);
2565+
expect(authClientSource).toContain('import.meta.env.VITE_SITE_URL!');
25722566
expect(authClientSource).not.toContain('createAuthMutations');
25732567

25742568
const httpSource = fs.readFileSync(
@@ -2683,6 +2677,84 @@ describe('cli/cli', () => {
26832677
}
26842678
});
26852679

2680+
test('run(add auth --preset convex --yes) preserves a user-edited raw start auth client on rerun', async () => {
2681+
const dir = fs.mkdtempSync(
2682+
path.join(os.tmpdir(), 'kitcn-cli-add-auth-convex-start-auth-client-')
2683+
);
2684+
const oldCwd = process.cwd();
2685+
writeRawConvexStartApp(dir);
2686+
fs.writeFileSync(
2687+
path.join(dir, '.env.local'),
2688+
'CONVEX_DEPLOYMENT=local:demo\nVITE_CONVEX_URL=http://127.0.0.1:3210\n'
2689+
);
2690+
process.chdir(dir);
2691+
2692+
try {
2693+
const execaStub = mock(async (_cmd: string, args: string[]) => {
2694+
if (args[0] === '/fake/convex/main.js' && args[1] === 'codegen') {
2695+
return { exitCode: 0, stdout: '', stderr: '' } as any;
2696+
}
2697+
2698+
return { exitCode: 0 } as any;
2699+
});
2700+
const generateMetaStub = mock(async () => {});
2701+
const syncEnvStub = mock(async () => {});
2702+
const loadConfigStub = mock(() => createDefaultConfig());
2703+
2704+
await run(['add', 'auth', '--preset', 'convex', '--yes'], {
2705+
realConvex: '/fake/convex/main.js',
2706+
execa: execaStub as any,
2707+
generateMeta: generateMetaStub as any,
2708+
syncEnv: syncEnvStub as any,
2709+
loadCliConfig: loadConfigStub as any,
2710+
});
2711+
2712+
const authClientPath = path.join(
2713+
dir,
2714+
'src',
2715+
'lib',
2716+
'convex',
2717+
'auth-client.ts'
2718+
);
2719+
const editedAuthClientSource = `${fs.readFileSync(authClientPath, 'utf8')}
2720+
// user plugin
2721+
`;
2722+
fs.writeFileSync(authClientPath, editedAuthClientSource, 'utf8');
2723+
const nodeModulesDir = path.join(dir, 'node_modules');
2724+
fs.mkdirSync(nodeModulesDir, { recursive: true });
2725+
fs.symlinkSync(
2726+
path.join(oldCwd, 'packages', 'kitcn'),
2727+
path.join(nodeModulesDir, 'kitcn')
2728+
);
2729+
fs.symlinkSync(
2730+
path.join(oldCwd, 'node_modules', 'better-auth'),
2731+
path.join(nodeModulesDir, 'better-auth')
2732+
);
2733+
fs.symlinkSync(
2734+
path.join(oldCwd, 'node_modules', 'zod'),
2735+
path.join(nodeModulesDir, 'zod')
2736+
);
2737+
2738+
const exitCode = await run(
2739+
['add', 'auth', '--preset', 'convex', '--yes', '--no-codegen'],
2740+
{
2741+
realConvex: '/fake/convex/main.js',
2742+
execa: execaStub as any,
2743+
generateMeta: generateMetaStub as any,
2744+
syncEnv: syncEnvStub as any,
2745+
loadCliConfig: loadConfigStub as any,
2746+
}
2747+
);
2748+
2749+
expect(exitCode).toBe(0);
2750+
expect(fs.readFileSync(authClientPath, 'utf8')).toBe(
2751+
editedAuthClientSource
2752+
);
2753+
} finally {
2754+
process.chdir(oldCwd);
2755+
}
2756+
});
2757+
26862758
test('run(add ratelimit/auth/resend --yes --no-codegen) keeps auth in root schema and other plugins in one ordered extend call', async () => {
26872759
const dir = fs.mkdtempSync(
26882760
path.join(os.tmpdir(), 'kitcn-cli-add-plugin-stack-')

packages/kitcn/src/cli/registry/items/auth/auth-client.template.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const AUTH_CONVEX_CLIENT_TEMPLATE = `import { createAuthClient } from 'be
5353
import { convexClient } from 'kitcn/auth/client';
5454
5555
export const authClient = createAuthClient({
56-
baseURL: process.env.NEXT_PUBLIC_CONVEX_SITE_URL!,
56+
baseURL: process.env.NEXT_PUBLIC_SITE_URL!,
5757
plugins: [convexClient()],
5858
});
5959
`;
@@ -62,7 +62,7 @@ export const AUTH_CONVEX_REACT_CLIENT_TEMPLATE = `import { createAuthClient } fr
6262
import { convexClient } from 'kitcn/auth/client';
6363
6464
export const authClient = createAuthClient({
65-
baseURL: import.meta.env.VITE_CONVEX_SITE_URL!,
65+
baseURL: import.meta.env.VITE_SITE_URL!,
6666
plugins: [convexClient()],
6767
});
6868
`;

packages/kitcn/src/cli/registry/items/auth/reconcile-auth-schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type AuthSchemaTemplateId = 'auth-schema' | 'auth-schema-convex';
1313
type UserOwnedAuthTemplateId =
1414
| 'auth-config'
1515
| 'auth-config-convex'
16+
| 'auth-client-convex'
1617
| 'auth-runtime'
1718
| 'auth-runtime-convex';
1819

@@ -30,6 +31,7 @@ const AUTH_SCHEMA_TEMPLATE_IDS = new Set<AuthSchemaTemplateId>([
3031
const USER_OWNED_AUTH_TEMPLATE_IDS = new Set<UserOwnedAuthTemplateId>([
3132
'auth-config',
3233
'auth-config-convex',
34+
'auth-client-convex',
3335
'auth-runtime',
3436
'auth-runtime-convex',
3537
]);

0 commit comments

Comments
 (0)