|
1 | | -import { existsSync, rmSync } from 'node:fs'; |
| 1 | +import { existsSync, rmSync, readFileSync, writeFileSync } from 'node:fs'; |
| 2 | +import { basename, resolve } from 'node:path'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
2 | 4 |
|
3 | | -// When installed via degit (no .git directory), remove CI-specific files |
4 | | -// that are only needed for the template repository itself |
5 | | -if (!existsSync('.git') && existsSync('.github/workflows')) { |
6 | | - rmSync('.github/workflows', { recursive: true }); |
| 5 | +// Only runs for degit-cloned projects (no .git directory) |
| 6 | +if (!existsSync('.git')) { |
| 7 | + // 1. Remove CI workflows (template-repo only) |
| 8 | + if (existsSync('.github/workflows')) { |
| 9 | + rmSync('.github/workflows', { recursive: true }); |
| 10 | + } |
| 11 | + |
| 12 | + // 2. Ask for project name (interactive) or use directory name (CI/non-TTY) |
| 13 | + const dirName = basename(resolve('.')); |
| 14 | + let projectName = dirName; |
| 15 | + |
| 16 | + if (process.stdin.isTTY) { |
| 17 | + try { |
| 18 | + const { createInterface } = await import('node:readline/promises'); |
| 19 | + const rl = createInterface({ input: process.stdin, output: process.stdout }); |
| 20 | + const answer = await rl.question(`\n📦 Project name (${dirName}): `); |
| 21 | + if (answer.trim()) projectName = answer.trim(); |
| 22 | + rl.close(); |
| 23 | + } catch { |
| 24 | + // Non-interactive fallback — use directory name |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + // 3. Update package.json for the new project |
| 29 | + const pkg = JSON.parse(readFileSync('package.json', 'utf8')); |
| 30 | + pkg.name = projectName; |
| 31 | + pkg.version = '1.0.0'; |
| 32 | + pkg.description = ''; |
| 33 | + pkg.author = ''; |
| 34 | + delete pkg.license; |
| 35 | + delete pkg.scripts.prepare; |
| 36 | + writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n'); |
| 37 | + |
| 38 | + // 4. Self-cleanup |
| 39 | + try { |
| 40 | + rmSync(fileURLToPath(import.meta.url), { force: true }); |
| 41 | + } catch {} |
| 42 | + |
| 43 | + console.log(`\n✅ Project "${projectName}" is ready! Run: pnpm dev\n`); |
7 | 44 | } |
0 commit comments