also looking at this
fix(cli): validate scaffold templates before creating projects
#298
4 files
+53
-7
| @@ -31,7 +31,7 @@ | ||
| 31 | 31 | "verify": "bun scripts/verify.ts", |
| 32 | 32 | "verify:packages": "bun scripts/verify-packages.ts", |
| 33 | 33 | "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", | |
| 35 | 35 | "verify:create": "bun scripts/verify-create-craft.ts", |
| 36 | 36 | "verify:cimports": "bun scripts/audit-cimports.ts", |
| 37 | 37 | "verify:docs": "bun scripts/verify-doc-links.ts", |
| @@ -66,6 +66,11 @@ cli | ||
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | 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 | } | |
| 69 | 74 | const skipInstall = options?.skipInstall || false |
| 70 | 75 | |
| 71 | 76 | console.log(`\n✨ Creating a new Craft app: ${projectName}`) |
| @@ -92,11 +97,6 @@ cli | ||
| 92 | 97 | else if (template === 'todo-app') { |
| 93 | 98 | createTodoAppTemplate(projectPath, projectName) |
| 94 | 99 | } |
| 95 | else { | |
| 96 | console.error(`Error: Unknown template "${template}"`) | |
| 97 | console.log('\nAvailable templates: minimal, full-featured, todo-app') | |
| 98 | process.exit(1) | |
| 99 | } | |
| 100 | 100 | |
| 101 | 101 | // Install dependencies unless skipped |
| 102 | 102 | if (!skipInstall) { |
| @@ -729,9 +729,15 @@ cli | ||
| 729 | 729 | .example('craft init MyApp --template dashboard') |
| 730 | 730 | .example('craft init MyApp --template all') |
| 731 | 731 | .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 | } | |
| 732 | 739 | console.log(`\n⚡ Creating new Craft project: ${name}\n`) |
| 733 | 740 | |
| 734 | const template = options?.template || 'blank' | |
| 735 | 741 | const bundleId = options?.bundleId || `com.example.${name.toLowerCase().replace(/[^a-z0-9]/g, '')}` |
| 736 | 742 | const appNameSlug = name.toLowerCase().replace(/[^a-z0-9]/g, '-') |
| 737 | 743 | |
| @@ -0,0 +1,40 @@ | ||
| 1 | import { expect, test } from 'bun:test' | |
| 2 | import { existsSync, mkdirSync, mkdtempSync, rmSync, symlinkSync } from 'node:fs' | |
| 3 | import { tmpdir } from 'node:os' | |
| 4 | import { join, resolve } from 'node:path' | |
| 5 | ||
| 6 | for (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 | } | |