UNPKG

@bodheesh/create-bodhi-node-app

Version:

Create a production-ready Node.js REST API with zero configuration

70 lines (57 loc) • 2.28 kB
#!/usr/bin/env node const fs = require('fs'); const path = require('path'); const { execSync } = require('child_process'); const chalk = require('chalk'); // Get project name from command line arguments const projectName = process.argv[2]; if (!projectName) { console.error(chalk.red('Please specify the project name:')); console.log(chalk.blue('\n npx create-bodhi-node-app'), chalk.green('<project-name>\n')); process.exit(1); } // Create project directory const currentDir = process.cwd(); const projectDir = path.resolve(currentDir, projectName); // Check if directory already exists if (fs.existsSync(projectDir)) { console.error(chalk.red(`Directory ${projectName} already exists.`)); process.exit(1); } // Create project directory fs.mkdirSync(projectDir, { recursive: true }); // Copy template files const templateDir = path.resolve(__dirname, '../src/templates/rest-api'); fs.cpSync(templateDir, projectDir, { recursive: true }); // Create .env file const envContent = `NODE_ENV=development PORT=3000 MONGODB_URI=mongodb://localhost:27017/${projectName} JWT_SECRET=your-jwt-secret JWT_EXPIRES_IN=1d`; fs.writeFileSync(path.join(projectDir, '.env'), envContent); // Initialize git repository try { execSync('git init', { cwd: projectDir }); } catch (error) { console.log(chalk.yellow('Git initialization skipped.')); } // Install dependencies console.log(chalk.blue('\nInstalling dependencies...\n')); try { execSync('npm install', { cwd: projectDir, stdio: 'inherit' }); } catch (error) { console.error(chalk.red('Error installing dependencies:'), error); process.exit(1); } // Success message with Bodhi branding console.log(chalk.green('\n✨ Success! Created'), chalk.blue(projectName), chalk.green('at'), chalk.blue(projectDir)); console.log(chalk.green('\nInside that directory, you can run:\n')); console.log(chalk.blue(' npm run dev')); console.log(chalk.gray(' Starts the development server.\n')); console.log(chalk.blue(' npm start')); console.log(chalk.gray(' Starts the production server.\n')); console.log(chalk.green('\nWe suggest that you begin by typing:\n')); console.log(chalk.blue(` cd ${projectName}`)); console.log(chalk.blue(' npm run dev\n')); console.log(chalk.green('Happy coding with Bodhi! 🚀\n'));