|
| 1 | +import prompts from 'prompts'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { fileURLToPath } from 'url'; |
| 5 | + |
| 6 | +export async function createProject(): Promise<void> { |
| 7 | + console.log('\n⚡ Zeus Project Creator\n'); |
| 8 | + |
| 9 | + // 1. Prompt for template |
| 10 | + const { template } = await prompts({ |
| 11 | + type: 'select', |
| 12 | + name: 'template', |
| 13 | + message: 'Which template would you like to use?', |
| 14 | + choices: [ |
| 15 | + { title: 'Next.js', value: 'nextjs', description: 'Next.js 16 + React 19 + Tailwind CSS 4' }, |
| 16 | + { title: 'Vite', value: 'vite', description: 'Vite 7 + React 19 + Tailwind CSS 4' }, |
| 17 | + ], |
| 18 | + }); |
| 19 | + |
| 20 | + if (!template) { |
| 21 | + console.log('Cancelled.'); |
| 22 | + process.exit(0); |
| 23 | + } |
| 24 | + |
| 25 | + // 2. Prompt for project name |
| 26 | + const { projectName } = await prompts({ |
| 27 | + type: 'text', |
| 28 | + name: 'projectName', |
| 29 | + message: 'What is your project name?', |
| 30 | + initial: 'my-zeus-app', |
| 31 | + validate: (value: string) => { |
| 32 | + if (!value) return 'Project name is required'; |
| 33 | + if (!/^[a-z0-9-_]+$/i.test(value)) |
| 34 | + return 'Project name can only contain letters, numbers, hyphens, and underscores'; |
| 35 | + if (fs.existsSync(path.resolve(process.cwd(), value))) return `Directory "${value}" already exists`; |
| 36 | + return true; |
| 37 | + }, |
| 38 | + }); |
| 39 | + |
| 40 | + if (!projectName) { |
| 41 | + console.log('Cancelled.'); |
| 42 | + process.exit(0); |
| 43 | + } |
| 44 | + |
| 45 | + // 3. Copy template to target directory |
| 46 | + // __dirname will be lib/commands when running compiled code |
| 47 | + // So we need to go up to package root (../../) to find starters |
| 48 | + const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 49 | + const packageRoot = path.join(__dirname, '..', '..'); |
| 50 | + const startersDir = path.join(packageRoot, 'starters', template); |
| 51 | + const targetDir = path.resolve(process.cwd(), projectName); |
| 52 | + |
| 53 | + // Check if starters directory exists |
| 54 | + if (!fs.existsSync(startersDir)) { |
| 55 | + console.error(`Error: Starter template "${template}" not found at ${startersDir}`); |
| 56 | + process.exit(1); |
| 57 | + } |
| 58 | + |
| 59 | + copyDirRecursive(startersDir, targetDir); |
| 60 | + |
| 61 | + // 4. Update package.json with project name |
| 62 | + const pkgPath = path.join(targetDir, 'package.json'); |
| 63 | + const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')); |
| 64 | + pkg.name = projectName; |
| 65 | + fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2)); |
| 66 | + |
| 67 | + // 5. Print success message |
| 68 | + const isNextjs = template === 'nextjs'; |
| 69 | + const zeusOutputPath = isNextjs ? './lib' : './src/lib'; |
| 70 | + const devPort = isNextjs ? '3000' : '5173'; |
| 71 | + |
| 72 | + console.log(` |
| 73 | +✅ Created "${projectName}" successfully! |
| 74 | +
|
| 75 | +Next steps: |
| 76 | +
|
| 77 | + cd ${projectName} |
| 78 | + npm install |
| 79 | + npm run dev |
| 80 | +
|
| 81 | +To generate Zeus client from your GraphQL schema: |
| 82 | +
|
| 83 | + npx graphql-zeus <schema-url-or-path> ${zeusOutputPath}${isNextjs ? ' --node' : ''} |
| 84 | +
|
| 85 | +Your app will be running at http://localhost:${devPort} |
| 86 | +
|
| 87 | +Happy coding! ⚡ |
| 88 | +`); |
| 89 | +} |
| 90 | + |
| 91 | +function copyDirRecursive(src: string, dest: string): void { |
| 92 | + fs.mkdirSync(dest, { recursive: true }); |
| 93 | + for (const entry of fs.readdirSync(src, { withFileTypes: true })) { |
| 94 | + const srcPath = path.join(src, entry.name); |
| 95 | + const destPath = path.join(dest, entry.name); |
| 96 | + if (entry.isDirectory()) { |
| 97 | + copyDirRecursive(srcPath, destPath); |
| 98 | + } else { |
| 99 | + fs.copyFileSync(srcPath, destPath); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments