Skip to content

Commit fd9fa18

Browse files
committed
fix: Project creation in an "empty" pnpm project, fixes void-sdk/void#44
1 parent 3126fd3 commit fd9fa18

2 files changed

Lines changed: 133 additions & 0 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,23 @@ function createVoidTsconfigPatch(root: string, outputBaseDir: string): TsconfigI
438438
);
439439
}
440440

441+
function hasTopLevelYamlKey(yaml: string, key: string): boolean {
442+
const keyPattern = new RegExp(`^${key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*:`);
443+
return yaml.split(/\r?\n/).some((rawLine) => {
444+
const line = stripYamlLineComment(rawLine);
445+
return keyPattern.test(line);
446+
});
447+
}
448+
449+
function isEmptyProjectAllowedPnpmWorkspaceFile(root: string): boolean {
450+
try {
451+
const yaml = readFileSync(join(root, 'pnpm-workspace.yaml'), 'utf-8');
452+
return !hasTopLevelYamlKey(yaml, 'packages');
453+
} catch {
454+
return false;
455+
}
456+
}
457+
441458
export function isScaffoldableEmptyProject(root: string): boolean {
442459
for (const dir of [
443460
'src',
@@ -466,6 +483,9 @@ export function isScaffoldableEmptyProject(root: string): boolean {
466483
if (EMPTY_PROJECT_ALLOWED_ENTRIES.has(entry.name)) {
467484
continue;
468485
}
486+
if (entry.name === 'pnpm-workspace.yaml' && isEmptyProjectAllowedPnpmWorkspaceFile(root)) {
487+
continue;
488+
}
469489
if (entry.name.startsWith('README')) {
470490
continue;
471491
}

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

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,41 @@ describe('isScaffoldableEmptyProject', () => {
893893
});
894894
});
895895

896+
it('returns true for pnpm add -D void shape with generated pnpm-workspace.yaml', () => {
897+
withTempDir((root) => {
898+
writeFileSync(
899+
join(root, 'package.json'),
900+
JSON.stringify(
901+
{
902+
devDependencies: {
903+
void: '^0.8.9',
904+
},
905+
},
906+
null,
907+
2,
908+
) + '\n',
909+
);
910+
writeFileSync(join(root, 'pnpm-lock.yaml'), "lockfileVersion: '9.0'\n");
911+
writeFileSync(
912+
join(root, 'pnpm-workspace.yaml'),
913+
[
914+
'allowBuilds:',
915+
' better-sqlite3: set this to true or false',
916+
' esbuild: set this to true or false',
917+
' sharp: set this to true or false',
918+
' workerd: set this to true or false',
919+
'minimumReleaseAgeExclude:',
920+
' - void@0.8.9',
921+
'',
922+
].join('\n'),
923+
);
924+
mkdirSync(join(root, 'node_modules', 'void'), { recursive: true });
925+
writeFileSync(join(root, 'node_modules', 'void', 'package.json'), '{"name":"void"}\n');
926+
927+
expect(isScaffoldableEmptyProject(root)).toBe(true);
928+
});
929+
});
930+
896931
it('returns true when only void and a single Pages adapter are installed', () => {
897932
withTempDir((root) => {
898933
writeFileSync(
@@ -915,6 +950,27 @@ describe('isScaffoldableEmptyProject', () => {
915950
});
916951
});
917952

953+
it('returns false when pnpm-workspace.yaml declares workspace packages', () => {
954+
withTempDir((root) => {
955+
writeFileSync(
956+
join(root, 'package.json'),
957+
JSON.stringify(
958+
{
959+
devDependencies: {
960+
void: '^0.8.9',
961+
},
962+
},
963+
null,
964+
2,
965+
) + '\n',
966+
);
967+
writeFileSync(join(root, 'pnpm-lock.yaml'), "lockfileVersion: '9.0'\n");
968+
writeFileSync(join(root, 'pnpm-workspace.yaml'), "packages:\n - 'packages/*'\n");
969+
970+
expect(isScaffoldableEmptyProject(root)).toBe(false);
971+
});
972+
});
973+
918974
it('returns true for a deleted starter that only has manifest and generated state left', () => {
919975
withTempDir((root) => {
920976
mkdirSync(join(root, '.void'), { recursive: true });
@@ -1270,6 +1326,63 @@ describe('isScaffoldableEmptyProject', () => {
12701326
}
12711327
});
12721328

1329+
it('prompts for the scaffold toolchain after pnpm add -D void creates pnpm-workspace.yaml', async () => {
1330+
const root = mkdtempSync(join(tmpdir(), 'void-init-'));
1331+
try {
1332+
writeFileSync(
1333+
join(root, 'package.json'),
1334+
JSON.stringify(
1335+
{
1336+
devDependencies: {
1337+
void: '^0.8.9',
1338+
},
1339+
},
1340+
null,
1341+
2,
1342+
) + '\n',
1343+
);
1344+
writeFileSync(join(root, 'pnpm-lock.yaml'), "lockfileVersion: '9.0'\n");
1345+
writeFileSync(
1346+
join(root, 'pnpm-workspace.yaml'),
1347+
[
1348+
'allowBuilds:',
1349+
' better-sqlite3: set this to true or false',
1350+
' esbuild: set this to true or false',
1351+
' sharp: set this to true or false',
1352+
' workerd: set this to true or false',
1353+
'minimumReleaseAgeExclude:',
1354+
' - void@0.8.9',
1355+
'',
1356+
].join('\n'),
1357+
);
1358+
mkdirSync(join(root, 'node_modules', 'void'), { recursive: true });
1359+
writeFileSync(join(root, 'node_modules', 'void', 'package.json'), '{"name":"void"}\n');
1360+
1361+
mockSelect
1362+
.mockResolvedValueOnce('vite-plus')
1363+
.mockResolvedValueOnce('react')
1364+
.mockResolvedValueOnce('static')
1365+
.mockResolvedValueOnce('generic');
1366+
mockConfirm.mockResolvedValueOnce(false);
1367+
1368+
await runInitCommand(root);
1369+
1370+
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf-8'));
1371+
expect(pkg.scripts.dev).toBe('vp dev');
1372+
expect(pkg.devDependencies['vite-plus']).toBe('^0.1.21');
1373+
expect(pkg.devDependencies.void).toBe('^0.8.9');
1374+
expect(readFileSync(join(root, 'vite.config.ts'), 'utf-8')).toContain('voidReact()');
1375+
expectPnpmAllowBuilds(root);
1376+
1377+
expect(mockSelect.mock.calls[0]?.[0].message).toBe('Choose a Pages scaffold toolchain:');
1378+
expect(mockSelect.mock.calls[0]?.[0].initialValue).toBe('vite-plus');
1379+
expect(mockSelect.mock.calls[1]?.[0].message).toBe('Choose a Pages framework to scaffold:');
1380+
expect(mockSelect.mock.calls[2]?.[0].message).toBe('Choose a Pages starter:');
1381+
} finally {
1382+
rmSync(root, { recursive: true, force: true });
1383+
}
1384+
});
1385+
12731386
it('offers to scaffold in a subfolder from a non-project parent directory', async () => {
12741387
const root = mkdtempSync(join(tmpdir(), 'void-init-'));
12751388
try {

0 commit comments

Comments
 (0)