create-bodhi-node-boilerplate
Version:
Create a Node.js project with basic folder structure and server setup
96 lines (82 loc) • 2.93 kB
JavaScript
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
async function createProject() {
const projectName = process.argv[2] || 'my-node-app';
const projectPath = path.join(process.cwd(), projectName);
const templatePath = path.join(__dirname, 'templates');
// Create project directory
console.log(chalk.blue(`Creating a new Node.js app in ${projectPath}`));
await fs.ensureDir(projectPath);
// Copy template files
await fs.copy(path.join(templatePath, 'src'), path.join(projectPath, 'src'), {
overwrite: true,
errorOnExist: false
});
// Create postman directory and copy collection
await fs.copy(
path.join(templatePath, 'postman'),
path.join(projectPath, 'postman'),
{
overwrite: true,
errorOnExist: false
}
);
// Create package.json for the new project
const packageJson = {
name: projectName,
version: '1.0.0',
description: 'A Node.js application created with bodhi-node-boilerplate',
main: 'src/app.js',
scripts: {
start: 'node src/app.js',
dev: 'nodemon src/app.js'
},
dependencies: {
express: '^4.18.2',
mongoose: '^7.0.3',
bcryptjs: '^2.4.3',
jsonwebtoken: '^9.0.0',
dotenv: '^16.0.3',
cors: '^2.8.5',
winston: '^3.8.2'
},
devDependencies: {
nodemon: '^2.0.22'
}
};
await fs.writeJSON(path.join(projectPath, 'package.json'), packageJson, { spaces: 2 });
// Create .env file
const envContent = `PORT=7000
NODE_ENV=development
MONGODB_URI=mongodb://127.0.0.1:27017/${projectName}
JWT_SECRET=your-super-secret-jwt-key
JWT_REFRESH_SECRET=your-super-secret-refresh-key
JWT_ACCESS_EXPIRE=15m
JWT_REFRESH_EXPIRE=7d`;
await fs.writeFile(path.join(projectPath, '.env'), envContent);
// Create .gitignore
const gitignore = `node_modules
.env
*.log
logs
npm-debug.log*`;
await fs.writeFile(path.join(projectPath, '.gitignore'), gitignore);
console.log(chalk.green('\nSuccess! Created', projectName));
console.log('\nInside that directory, you can run several commands:');
console.log(chalk.cyan('\n npm install'));
console.log(' Installs the dependencies');
console.log(chalk.cyan('\n npm start'));
console.log(' Starts the production server');
console.log(chalk.cyan('\n npm run dev'));
console.log(' Starts the development server');
console.log('\nWe suggest that you begin by typing:');
console.log(chalk.cyan(`\n cd ${projectName}`));
console.log(chalk.cyan(' npm install'));
console.log(chalk.cyan(' npm run dev'));
}
createProject().catch(err => {
console.error(chalk.red('Error creating project:'), err);
process.exit(1);
});