ReviewOS

also looking at this

craft-native/craft

fix(cli): validate scaffold templates before creating projects

#298
Merged glennmichael123 wants to merge codex/scaffold-template-validation into main
4 files +53 -7

Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.

package.jsonmodified+1-1
Changes to package.json
@@ -31,7 +31,7 @@
3131 "verify": "bun scripts/verify.ts",
3232 "verify:packages": "bun scripts/verify-packages.ts",
3333 "verify:npm": "bun scripts/npm-packages.ts",
34 "test:packages": "bun test scripts/npm-packages.test.ts scripts/mobile-package.test.ts scripts/svelte-package.test.ts",
34 "test:packages": "bun test scripts/npm-packages.test.ts scripts/mobile-package.test.ts scripts/svelte-package.test.ts scripts/scaffold-package.test.ts",
3535 "verify:create": "bun scripts/verify-create-craft.ts",
3636 "verify:cimports": "bun scripts/audit-cimports.ts",
3737 "verify:docs": "bun scripts/verify-doc-links.ts",
packages/create-craft/bin/cli.tsmodified+5-5
Changes to packages/create-craft/bin/cli.ts
@@ -66,6 +66,11 @@ cli
6666 }
6767
6868 const template = options?.template || 'minimal'
69 if (!['minimal', 'full-featured', 'todo-app'].includes(template)) {
70 console.error(`Error: Unknown template "${template}"`)
71 console.error('Available templates: minimal, full-featured, todo-app')
72 process.exit(1)
73 }
6974 const skipInstall = options?.skipInstall || false
7075
7176 console.log(`\n✨ Creating a new Craft app: ${projectName}`)
@@ -92,11 +97,6 @@ cli
9297 else if (template === 'todo-app') {
9398 createTodoAppTemplate(projectPath, projectName)
9499 }
95 else {
96 console.error(`Error: Unknown template "${template}"`)
97 console.log('\nAvailable templates: minimal, full-featured, todo-app')
98 process.exit(1)
99 }
100100
101101 // Install dependencies unless skipped
102102 if (!skipInstall) {
packages/typescript/bin/cli.tsmodified+7-1
Changes to packages/typescript/bin/cli.ts
@@ -729,9 +729,15 @@ cli
729729 .example('craft init MyApp --template dashboard')
730730 .example('craft init MyApp --template all')
731731 .action(async (name: string, options?: any) => {
732 const template = options?.template || 'blank'
733 const templates = ['blank', 'tabs', 'drawer', 'dashboard', 'desktop', 'ios', 'android', 'all']
734 if (!templates.includes(template)) {
735 console.error(`Error: Unknown template "${template}"`)
736 console.error(`Available templates: ${templates.join(', ')}`)
737 process.exit(1)
738 }
732739 console.log(`\n⚡ Creating new Craft project: ${name}\n`)
733740
734 const template = options?.template || 'blank'
735741 const bundleId = options?.bundleId || `com.example.${name.toLowerCase().replace(/[^a-z0-9]/g, '')}`
736742 const appNameSlug = name.toLowerCase().replace(/[^a-z0-9]/g, '-')
737743
scripts/scaffold-package.test.tsadded+40-0
Changes to scripts/scaffold-package.test.ts
@@ -0,0 +1,40 @@
1import { expect, test } from 'bun:test'
2import { existsSync, mkdirSync, mkdtempSync, rmSync, symlinkSync } from 'node:fs'
3import { tmpdir } from 'node:os'
4import { join, resolve } from 'node:path'
5
6for (const tool of ['create-craft', 'craft-sdk']) {
7 test(`${tool} rejects an unknown template without preventing a corrected retry`, () => {
8 const root = resolve(import.meta.dir, '..')
9 const temp = mkdtempSync(join(tmpdir(), 'craft-scaffold-package-'))
10 const packageDir = join(root, 'packages', tool === 'create-craft' ? 'create-craft' : 'typescript')
11 const run = (args: string[], cwd: string) => Bun.spawnSync(args, { cwd, stdout: 'pipe', stderr: 'pipe' })
12 try {
13 if (tool === 'craft-sdk') expect(run([process.execPath, 'run', 'build'], packageDir).exitCode).toBe(0)
14 const archive = join(temp, 'package.tgz')
15 expect(run([process.execPath, 'pm', 'pack', '--filename', archive], packageDir).exitCode).toBe(0)
16 const installed = join(temp, 'installed')
17 mkdirSync(installed)
18 expect(run(['tar', '-xzf', archive, '--strip-components=1', '-C', installed], temp).exitCode).toBe(0)
19 // Only create-craft's external CLI parser comes from the workspace.
20 mkdirSync(join(installed, 'node_modules/@stacksjs'), { recursive: true })
21 symlinkSync(join(root, 'node_modules/@stacksjs/clapp'), join(installed, 'node_modules/@stacksjs/clapp'), 'dir')
22 const cli = join(installed, tool === 'create-craft' ? 'bin/cli.ts' : 'dist/cli.js')
23 const work = join(temp, 'consumer')
24 mkdirSync(work)
25 const args = tool === 'create-craft' ? ['demo', '--skip-install'] : ['init', 'demo']
26 const invalid = run([process.execPath, cli, ...args, '--template', 'nope'], work)
27 expect(invalid.exitCode).not.toBe(0)
28 expect(`${invalid.stdout}\n${invalid.stderr}`).toContain('Unknown template "nope"')
29 expect(`${invalid.stdout}\n${invalid.stderr}`).toContain('Available templates:')
30 expect(invalid.stdout.toString()).not.toContain('Next steps:')
31 expect(existsSync(join(work, 'demo'))).toBe(false)
32 const valid = run([process.execPath, cli, ...args, '--template', tool === 'create-craft' ? 'minimal' : 'blank'], work)
33 expect(valid.exitCode).toBe(0)
34 expect(existsSync(join(work, 'demo/package.json'))).toBe(true)
35 }
36 finally {
37 rmSync(temp, { recursive: true, force: true })
38 }
39 }, 60_000)
40}