express-api-kit
Version:
š Production-Ready Express API Generator - Create enterprise-grade APIs with authentication, file uploads, cron jobs, database pooling, and advanced security features. Supports MongoDB, MySQL, and PostgreSQL.
110 lines (92 loc) ⢠3.63 kB
JavaScript
const fs = require('fs-extra');
const path = require('path');
const { exec } = require('child_process');
const templatesDir = path.join(__dirname, 'templates');
async function generateProject(projectName, database) {
const targetDir = path.join(process.cwd(), projectName);
const template = database === 'MongoDB' ? 'express-mongo-template' : 'express-sql-template';
const templateDir = path.join(templatesDir, template);
try {
if (fs.existsSync(targetDir)) {
console.error(`ā Error: Directory '${projectName}' already exists! Choose a different name.`);
process.exit(1);
}
await fs.copy(templateDir, targetDir);
console.log(`\nā
Successfully created ${projectName} with ${database} support!\n`);
const envContent =
database === 'MongoDB'
? `PORT=5000
MONGO_URI=mongodb://localhost:27017/${projectName}
JWT_SECRET=your_jwt_secret_here_minimum_32_characters
EMAIL_USER=your_email@gmail.com
EMAIL_PASS=your_app_password
APP_URL=http://localhost:5000
# MongoDB Connection Pool Settings
MONGO_MAX_POOL_SIZE=10
MONGO_MIN_POOL_SIZE=5
MONGO_MAX_IDLE_TIME=30000
MONGO_SERVER_SELECTION_TIMEOUT=5000
MONGO_SOCKET_TIMEOUT=45000
MONGO_CONNECT_TIMEOUT=10000
MONGO_HEARTBEAT_FREQUENCY=10000
MONGO_READ_PREFERENCE=primary
MONGO_WRITE_CONCERN=majority
MONGO_READ_CONCERN=majority
MONGO_WRITE_TIMEOUT=5000
# Cron Jobs
ENABLE_CRON_JOBS=true
# Node Environment
NODE_ENV=development`
: `PORT=5000
DB_HOST=localhost
DB_USER=root
DB_PASS=password
DB_NAME=${projectName}
DB_DIALECT=mysql
JWT_SECRET=your_jwt_secret_here_minimum_32_characters
EMAIL_USER=your_email@gmail.com
EMAIL_PASS=your_app_password
APP_URL=http://localhost:5000
# Database Connection Pool Settings
DB_POOL_MAX=5
DB_POOL_MIN=0
DB_POOL_ACQUIRE=30000
DB_POOL_IDLE=10000
# Cron Jobs
ENABLE_CRON_JOBS=true
# Node Environment
NODE_ENV=development`;
await fs.writeFile(path.join(targetDir, '.env'), envContent);
console.log('ā
.env file created successfully!');
console.log('š¦ Installing dependencies...');
exec(`cd ${targetDir} && npm install`, (error, stdout, stderr) => {
if (error) {
console.error(`ā Error installing dependencies: ${error.message}`);
return;
}
console.log(stdout);
console.error(stderr);
console.log('ā
Installation complete! You can now run your project.');
// Display feature information
console.log('\nš Your project includes these features:');
console.log(' ā
Authentication with JWT & Role-based access');
console.log(' ā
File upload middleware with validation');
console.log(' ā
Database connection pooling');
console.log(' ā
Cron job scheduler system');
console.log(' ā
Email service integration');
console.log(' ā
Request logging & response handler');
console.log(' ā
Security & validation middlewares');
console.log('\nš Next steps:');
console.log(' 1. cd ' + projectName);
console.log(' 2. Update .env file with your database credentials');
console.log(' 3. npm run dev');
// Display the creator message
console.log(`\nš Project successfully generated by Express API Kit!\n`);
console.log(`šØāš» Developed by: Muhammad Ahmad`);
console.log(`š¢ Support: If you like this tool, give it a star ā on GitHub!\n`);
});
} catch (error) {
console.error('ā Error creating project:', error);
}
}
module.exports = generateProject;