also looking at this
fix(cli): validate scaffold templates before creating projects
#298
4 files
+53
-7
| @@ -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 | } | |