UNPKG

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
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;