Skip to content

Commit fbcd964

Browse files
morinokamiclaude
andauthored
fix(init): clarify starter messages and cd into a new subfolder (#853)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 363bf31 commit fbcd964

5 files changed

Lines changed: 117 additions & 28 deletions

File tree

‎packages/void/src/cli/init-git.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { spawnSync } from 'node:child_process';
22
import { existsSync } from 'node:fs';
33
import { cancel, confirm, isCancel, log } from '@voidzero-dev/vite-plus-prompts';
44
import { dirname, join, resolve } from 'pathslash';
5+
import { quoteShellPath, shellDirectory } from './init-shell.ts';
56

67
export async function setupGit(root: string, git?: boolean): Promise<boolean> {
78
if (git === false) {
@@ -58,14 +59,8 @@ export async function setupGit(root: string, git?: boolean): Promise<boolean> {
5859
}
5960

6061
export function initialCommitCommand(root: string): string {
61-
// Quote paths for the user's shell, including names containing quotes, $ or
62-
// backticks. Use the absolute path because init can create a subfolder.
62+
// Use the absolute path because init can create a subfolder.
6363
const directory = resolve(root);
64-
const argument = /^[A-Za-z0-9_@./:-]+$/.test(directory)
65-
? directory
66-
: process.platform === 'win32'
67-
? `"${directory}"`
68-
: `'${directory.replaceAll("'", "'\\''")}'`;
69-
const git = directory === resolve(process.cwd()) ? 'git' : `git -C ${argument}`;
64+
const git = directory === shellDirectory() ? 'git' : `git -C ${quoteShellPath(directory)}`;
7065
return `${git} add -A && ${git} commit -m "chore: initial commit"`;
7166
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { relative, resolve } from 'pathslash';
2+
3+
/**
4+
* The directory of the user's shell. Printed commands start there, which is
5+
* not the project root when init creates a subfolder.
6+
*/
7+
export function shellDirectory(): string {
8+
return resolve(process.cwd());
9+
}
10+
11+
/** Quote a path for the user's shell, including names containing quotes, $ or backticks. */
12+
export function quoteShellPath(path: string): string {
13+
if (/^[A-Za-z0-9_@./:-]+$/.test(path)) {
14+
return path;
15+
}
16+
return process.platform === 'win32' ? `"${path}"` : `'${path.replaceAll("'", "'\\''")}'`;
17+
}
18+
19+
/** Prefix `command` with a `cd` from the user's shell into `root` when they differ. */
20+
export function commandInProject(root: string, command: string): string {
21+
const shell = shellDirectory();
22+
const directory = resolve(root);
23+
if (directory === shell) {
24+
return command;
25+
}
26+
const path = relative(shell, directory);
27+
// `cd` would read a leading dash as an option, and `cd -` as the previous directory.
28+
return `cd ${quoteShellPath(path.startsWith('-') ? `./${path}` : path)} && ${command}`;
29+
}

‎packages/void/src/cli/init.ts‎

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { maybeSetupDeployProject } from './deploy-setup.ts';
3535
import { maybeScaffoldEnvTs } from './env-scaffold.ts';
3636
import { migrateFrameworkAdapterPaths } from './framework-config-migrate.ts';
3737
import { initialCommitCommand, setupGit } from './init-git.ts';
38+
import { commandInProject } from './init-shell.ts';
3839
import { configureVitePlusScaffold } from './init-vite-plus.ts';
3940
import { migrateLegacyVoidConfig } from './migrate.ts';
4041
import { formatUrl, note, printSuccess } from './output.ts';
@@ -1423,19 +1424,27 @@ function writeDatabaseDialect(root: string, config: VoidConfig, dialect: Databas
14231424
writeVoidProjectConfig(root, next);
14241425
}
14251426

1427+
/** `quiet` is for a new starter, whose prompt already showed the database. */
1428+
type DatabaseDialectOptions = { quiet?: boolean };
1429+
14261430
function clearConfiguredDatabase(
14271431
root: string,
14281432
messages: { unchanged: string; updated: string },
1433+
options: DatabaseDialectOptions,
14291434
): void {
14301435
const config = readConfig(root);
14311436

14321437
if (getDatabaseDialect(config) === 'sqlite') {
1433-
log.info(messages.unchanged);
1438+
if (!options.quiet) {
1439+
log.info(messages.unchanged);
1440+
}
14341441
return;
14351442
}
14361443

14371444
writeDatabaseDialect(root, config, 'sqlite');
1438-
log.step(messages.updated);
1445+
if (!options.quiet) {
1446+
log.step(messages.updated);
1447+
}
14391448
}
14401449

14411450
/** Remove `database` entirely: its casing alone still makes Void serve `void/db`. */
@@ -1451,35 +1460,51 @@ function removeDatabaseConfig(root: string): void {
14511460
log.step('Removed database config from void.config.ts');
14521461
}
14531462

1454-
function applyDatabaseDialect(root: string, dialect: DatabaseDialect): void {
1463+
function applyDatabaseDialect(
1464+
root: string,
1465+
dialect: DatabaseDialect,
1466+
options: DatabaseDialectOptions = {},
1467+
): void {
14551468
const config = readConfig(root);
14561469

14571470
if (dialect === 'postgresql') {
14581471
if (getDatabaseDialect(config) === 'postgresql') {
1459-
log.info('PostgreSQL already configured in void.config.ts');
1472+
if (!options.quiet) {
1473+
log.info('PostgreSQL already configured in void.config.ts');
1474+
}
14601475
return;
14611476
}
14621477

14631478
writeDatabaseDialect(root, config, 'postgresql');
1464-
log.step('Configured PostgreSQL in void.config.ts');
1479+
if (!options.quiet) {
1480+
log.step('Configured PostgreSQL in void.config.ts');
1481+
}
14651482
return;
14661483
}
14671484

14681485
if (dialect === 'mysql') {
14691486
if (getDatabaseDialect(config) === 'mysql') {
1470-
log.info('MySQL already configured in void.config.ts');
1487+
if (!options.quiet) {
1488+
log.info('MySQL already configured in void.config.ts');
1489+
}
14711490
return;
14721491
}
14731492

14741493
writeDatabaseDialect(root, config, 'mysql');
1475-
log.step('Configured MySQL in void.config.ts');
1494+
if (!options.quiet) {
1495+
log.step('Configured MySQL in void.config.ts');
1496+
}
14761497
return;
14771498
}
14781499

1479-
clearConfiguredDatabase(root, {
1480-
unchanged: 'Using default D1 (SQLite) configuration',
1481-
updated: 'Updated void.config.ts to use the default D1 (SQLite) configuration',
1482-
});
1500+
clearConfiguredDatabase(
1501+
root,
1502+
{
1503+
unchanged: 'Using default D1 (SQLite) configuration',
1504+
updated: 'Updated void.config.ts to use the default D1 (SQLite) configuration',
1505+
},
1506+
options,
1507+
);
14831508
}
14841509

14851510
function detectDatabaseSetup(root: string): DatabaseSetupChoice {
@@ -2330,7 +2355,7 @@ function scaffoldPagesApp(
23302355

23312356
ensureGitignore(root);
23322357
if (starterMode !== 'static') {
2333-
applyDatabaseDialect(root, starterMode);
2358+
applyDatabaseDialect(root, starterMode, { quiet: true });
23342359
}
23352360

23362361
if (starterMode === 'postgresql') {
@@ -2768,7 +2793,7 @@ export async function runInitCommand(root: string, initArgs?: InitArgs): Promise
27682793
}
27692794

27702795
if (!targeted && !framework && isScaffoldableEmptyProject(root)) {
2771-
log.info('Empty project detected. Scaffolding a Pages starter...');
2796+
log.info('Empty project detected. Creating a Pages starter.');
27722797
const scaffoldToolchain = await promptScaffoldToolchain(initArgs?.toolchain);
27732798
if (detectedPagesFramework) {
27742799
scaffoldedPagesFramework = detectedPagesFramework;
@@ -2891,10 +2916,11 @@ export async function runInitCommand(root: string, initArgs?: InitArgs): Promise
28912916
log.info('Bundled Void docs: node_modules/void/docs/');
28922917
}
28932918

2894-
// Demo code only in the full flow, and not for meta-frameworks.
2895-
if (!targeted && !framework) {
2919+
// Demo code only in the full flow, and not for meta-frameworks or a new
2920+
// starter, which already has its own pages.
2921+
if (!targeted && !framework && !didScaffoldStarter) {
28962922
const existingDemoFiles = Object.values(demoFiles(root)).filter((file) => existsSync(file));
2897-
if (didScaffoldStarter || state.hasRoutes || state.hasMigrations || state.hasPages) {
2923+
if (state.hasRoutes || state.hasMigrations || state.hasPages) {
28982924
log.info('Project already has pages, routes, or migrations, skipping demo');
28992925
} else if (hasDrizzleSchema(root)) {
29002926
log.info('Project already has a database schema, skipping demo');
@@ -3038,8 +3064,7 @@ export async function runInitCommand(root: string, initArgs?: InitArgs): Promise
30383064
printSuccess('Project initialized. Run your workspace install to finish setup.');
30393065
}
30403066
} else {
3041-
printSuccess(
3042-
`Project initialized. Run \`${getDevCommand(root, framework)}\` to start developing.`,
3043-
);
3067+
const devCommand = commandInProject(root, getDevCommand(root, framework));
3068+
printSuccess(`Project initialized. Run \`${devCommand}\` to start developing.`);
30443069
}
30453070
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { join } from 'node:path';
2+
import { describe, expect, it } from 'vite-plus/test';
3+
import { commandInProject } from '../../src/cli/init-shell.ts';
4+
5+
describe('commandInProject', () => {
6+
it('keeps the command when the shell is in the project', () => {
7+
expect(commandInProject(process.cwd(), 'vp dev')).toBe('vp dev');
8+
});
9+
10+
it('changes into a project folder relative to the shell', () => {
11+
expect(commandInProject(join(process.cwd(), 'my-app'), 'vp dev')).toBe('cd my-app && vp dev');
12+
});
13+
14+
it('quotes the folder for the shell', () => {
15+
const quoted = process.platform === 'win32' ? '"my app"' : "'my app'";
16+
expect(commandInProject(join(process.cwd(), 'my app'), 'vp dev')).toBe(
17+
`cd ${quoted} && vp dev`,
18+
);
19+
});
20+
21+
it.each(['-', '-app'])('keeps cd from reading %s as an option', (folder) => {
22+
expect(commandInProject(join(process.cwd(), folder), 'vp dev')).toBe(
23+
`cd ./${folder} && vp dev`,
24+
);
25+
});
26+
});

‎packages/void/test/unit/init.test.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const mockNote = vi.fn();
3838
const mockOutro = vi.fn();
3939
const mockLogWarn = vi.fn();
4040
const mockLogInfo = vi.fn();
41+
const mockLogStep = vi.fn();
4142
const mockGetToken = vi.fn();
4243
const mockGetApiUrl = vi.fn();
4344
const mockRunLogin = vi.fn();
@@ -334,7 +335,7 @@ vi.mock('@voidzero-dev/vite-plus-prompts', () => ({
334335
warn: (...args: Array<unknown>) => mockLogWarn(...args),
335336
error: vi.fn(),
336337
info: (...args: Array<unknown>) => mockLogInfo(...args),
337-
step: vi.fn(),
338+
step: (...args: Array<unknown>) => mockLogStep(...args),
338339
},
339340
confirm: (...args: Array<unknown>) => mockConfirm(...args),
340341
select: (...args: Array<unknown>) => mockSelect(...args),
@@ -622,6 +623,7 @@ afterEach(() => {
622623
mockOutro.mockReset();
623624
mockLogWarn.mockReset();
624625
mockLogInfo.mockReset();
626+
mockLogStep.mockReset();
625627
mockGetToken.mockReset();
626628
mockGetApiUrl.mockReset();
627629
mockRunLogin.mockReset();
@@ -1448,6 +1450,11 @@ describe('isScaffoldableEmptyProject', () => {
14481450
expect(tsconfig.compilerOptions.jsx).toBe('react-jsx');
14491451
expect(tsconfig.include).toContain('pages');
14501452
expect(tsconfig.include).toContain('db');
1453+
1454+
expect(mockLogInfo).toHaveBeenCalledWith('Empty project detected. Creating a Pages starter.');
1455+
// The starter prompt already showed the database, and the starter has its own pages.
1456+
expect(mockLogInfo).not.toHaveBeenCalledWith('Using default D1 (SQLite) configuration');
1457+
expect(mockLogInfo).not.toHaveBeenCalledWith(expect.stringContaining('skipping demo'));
14511458
} finally {
14521459
rmSync(root, { recursive: true, force: true });
14531460
}
@@ -1494,6 +1501,7 @@ describe('isScaffoldableEmptyProject', () => {
14941501
expect(readFileSync(join(root, 'db', 'migrations', '0000_generated.sql'), 'utf-8')).toContain(
14951502
'CREATE TABLE "messages"',
14961503
);
1504+
expect(mockLogStep).not.toHaveBeenCalledWith('Configured PostgreSQL in void.config.ts');
14971505

14981506
const toolchainPrompt = mockSelect.mock.calls[0]?.[0];
14991507
expect(toolchainPrompt.message).toBe('Choose a Pages scaffold toolchain:');
@@ -1743,6 +1751,8 @@ describe('isScaffoldableEmptyProject', () => {
17431751

17441752
it('offers to scaffold in a subfolder from a non-project parent directory', async () => {
17451753
const root = mkdtempSync(join(tmpdir(), 'void-init-'));
1754+
// The user's shell stays in the parent directory.
1755+
const cwd = vi.spyOn(process, 'cwd').mockReturnValue(root);
17461756
try {
17471757
mkdirSync(join(root, 'existing-project'));
17481758
mockSelect
@@ -1794,7 +1804,11 @@ describe('isScaffoldableEmptyProject', () => {
17941804
shell: process.platform === 'win32',
17951805
}),
17961806
);
1807+
expect(String(mockOutro.mock.calls.at(-1)?.[0])).toContain(
1808+
'Run `cd my-new-app && vp dev` to start developing.',
1809+
);
17971810
} finally {
1811+
cwd.mockRestore();
17981812
rmSync(root, { recursive: true, force: true });
17991813
}
18001814
});

0 commit comments

Comments
 (0)