diff --git a/packages/cli/commands/create.ts b/packages/cli/commands/create.ts index 44ce0498c..fcdc6cd21 100644 --- a/packages/cli/commands/create.ts +++ b/packages/cli/commands/create.ts @@ -144,6 +144,8 @@ async function createProject(cwd: ProjectPath, options: Options) { force: async ({ results: { directory } }) => { if (!options.dirCheck) return; + if (!fs.existsSync(directory!)) return; + const files = fs.readdirSync(directory!); const hasNonIgnoredFiles = files.some((file) => !file.startsWith('.git')); if (!hasNonIgnoredFiles) return; diff --git a/packages/cli/tests/cli.ts b/packages/cli/tests/cli.ts new file mode 100644 index 000000000..e93a21c4f --- /dev/null +++ b/packages/cli/tests/cli.ts @@ -0,0 +1,50 @@ +import { beforeAll, describe, expect, it } from 'vitest'; + +import { exec } from 'tinyexec'; +import path from 'node:path'; +import fs from 'node:fs'; +import { parseJson } from '../../core/tooling/index.ts'; + +const monoRepoPath = path.resolve(__dirname, '..', '..', '..'); + +beforeAll(() => { + const testOutputCliPath = path.resolve(monoRepoPath, '.test-output', 'cli'); + + if (fs.existsSync(testOutputCliPath)) { + fs.rmSync(testOutputCliPath, { force: true, recursive: true }); + } +}); + +describe('cli', () => { + it('should be able to create a new project with cli command', async () => { + const svBinPath = path.resolve(monoRepoPath, 'packages', 'cli', 'dist', 'bin.js'); + const testOutputPath = path.resolve(monoRepoPath, '.test-output', 'cli', 'test-project'); + + const result = await exec( + 'node', + [ + svBinPath, + 'create', + testOutputPath, + '--template', + 'minimal', + '--types', + 'ts', + '--no-install', + '--no-add-ons' + ], + { nodeOptions: { stdio: 'ignore' } } + ); + + // cli finished well + expect(result.exitCode).toBe(0); + + // test output path exists + expect(fs.existsSync(testOutputPath)).toBe(true); + + // package.json has a name + const packageJsonPath = path.resolve(testOutputPath, 'package.json'); + const packageJson = parseJson(fs.readFileSync(packageJsonPath, 'utf-8')); + expect(packageJson.name).toBe('test-project'); + }); +});