UNPKG

forgejs-cli

Version:

CLI for init project

80 lines (66 loc) • 2.31 kB
#!/usr/bin/env node import { Command } from 'commander' import { execa } from 'execa' import { existsSync, readFileSync, writeFileSync } from 'node:fs' import path from 'node:path' const packageJson = JSON.parse( readFileSync(new URL('package.json', import.meta.url), 'utf8'), ) const templates = { 'node-grpc': 'https://github.com/florob95/node-grpc-starter-kit.git', 'node-nest': 'https://github.com/florob95/node-nest-starter-kit.git', 'node-ts': 'https://github.com/florob95/node-ts-starter-kit.git', 'web-react': 'https://github.com/florob95/react-starter-kit.git', } async function initProject(template, projectName) { const repoUrl = templates[template] if (!repoUrl) { console.error(`\u001B[31mUnknown template: ${template}\u001B[0m`) process.exit(1) } const projectPath = path.resolve(process.cwd(), projectName) if (existsSync(projectPath)) { console.error( `\u001B[31mDirectory "${projectName}" already exists.\u001B[0m`, ) process.exit(1) } try { console.log(`\u001B[36m> Cloning template...\u001B[0m`) await execa('git', ['clone', repoUrl, projectPath], { stdio: 'inherit' }) await execa('rm', ['-rf', `${projectPath}/.git`]) const readmePath = path.join(projectPath, 'README.md') if (existsSync(readmePath)) { try { const content = readFileSync(readmePath, 'utf8') const updated = `${content.trim()}\n\n---\nGenerated by Forge CLI 🚀\n` writeFileSync(readmePath, updated, 'utf8') } catch (error) { console.warn( '\u001B[33m> Could not update README.md\u001B[0m', error.message, ) } } console.log( `\u001B[32m✔ Project "${projectName}" created successfully!\u001B[0m`, ) } catch (error) { console.error(`\u001B[31mError: ${error.message}\u001B[0m`) process.exit(1) } } const program = new Command() program .name('forge') .description('Project CLI') .version(packageJson.version, '-v, --version') program .command('init') .description('Initialize a new project') .argument('<template>', 'Template to use (node-ts, node-grpc, node-nest)') .argument('<project-name>', 'Project name') .action(async (template, projectName) => { await initProject(template, projectName) }) program.parse()