|
| 1 | +#!/usr/bin/env node |
| 2 | +import { createInterface } from "readline"; |
| 3 | +import { execSync } from "child_process"; |
| 4 | +import fs from "fs"; |
| 5 | +import path from "path"; |
| 6 | +import { fileURLToPath } from "url"; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const __dirname = path.dirname(__filename); |
| 10 | + |
| 11 | +const rl = createInterface({ |
| 12 | + input: process.stdin, |
| 13 | + output: process.stdout, |
| 14 | +}); |
| 15 | + |
| 16 | +const ask = (q) => new Promise((res) => rl.question(q, res)); |
| 17 | + |
| 18 | +async function main() { |
| 19 | + console.log("🚀 Welcome to create-node-backend!"); |
| 20 | + |
| 21 | + const projectName = await ask("Project name: "); |
| 22 | + const useAuth = await ask("Include auth? (y/n): "); |
| 23 | + const useMulter = await ask("Include Multer (file uploads)? (y/n): "); |
| 24 | + |
| 25 | + rl.close(); |
| 26 | + |
| 27 | + const projectPath = path.join(process.cwd(), projectName); |
| 28 | + if (fs.existsSync(projectPath)) { |
| 29 | + console.error(`❌ Project folder already exists. Choose a different name.`); |
| 30 | + process.exit(1); |
| 31 | + } |
| 32 | + fs.mkdirSync(projectPath); |
| 33 | + |
| 34 | + process.chdir(projectPath); |
| 35 | + |
| 36 | + // Step 1: Initialize npm and Install Dependencies |
| 37 | + console.log("📦 Initializing npm..."); |
| 38 | + execSync("npm init -y", { stdio: "inherit" }); |
| 39 | + |
| 40 | + // Step 2: Install Dependencies |
| 41 | + console.log("📥 Installing required packages..."); |
| 42 | + execSync("npm install express dotenv", { stdio: "inherit" }); |
| 43 | + |
| 44 | + if (useAuth.toLowerCase() === "y") { |
| 45 | + console.log("🔐 Adding auth dependencies..."); |
| 46 | + execSync("npm install argon2 jsonwebtoken", { stdio: "inherit" }); |
| 47 | + } |
| 48 | + |
| 49 | + if (useMulter.toLowerCase() === "y") { |
| 50 | + console.log("📂 Adding Multer..."); |
| 51 | + execSync("npm install multer", { stdio: "inherit" }); |
| 52 | + } |
| 53 | + |
| 54 | + console.log("⚙️ Installing dev dependencies..."); |
| 55 | + execSync("npm install --save-dev nodemon", { stdio: "inherit" }); |
| 56 | + |
| 57 | + // Step 3: Modify package.json for ES Modules |
| 58 | + const packageJsonPath = path.join(projectPath, "package.json"); |
| 59 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")); |
| 60 | + packageJson.type = "module"; // Set the project to use ES Modules |
| 61 | + |
| 62 | + packageJson.scripts = { |
| 63 | + ...packageJson.scripts, |
| 64 | + dev: "nodemon src/index.js", // Add this line for dev script |
| 65 | + }; |
| 66 | + |
| 67 | + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); |
| 68 | + |
| 69 | + // Step 4: Setup Folder Structure |
| 70 | + console.log("📁 Creating project structure..."); |
| 71 | + |
| 72 | + // Create necessary directories |
| 73 | + fs.mkdirSync(path.join(projectPath, "src")); |
| 74 | + fs.mkdirSync(path.join(projectPath, "src", "controllers")); |
| 75 | + fs.mkdirSync(path.join(projectPath, "src", "models")); |
| 76 | + fs.mkdirSync(path.join(projectPath, "src", "routes")); |
| 77 | + fs.mkdirSync(path.join(projectPath, "src", "middlewares")); |
| 78 | + fs.mkdirSync(path.join(projectPath, "src", "config")); |
| 79 | + |
| 80 | + // Step 5: Write environment configuration |
| 81 | + const envContent = ` |
| 82 | +PORT=3000 |
| 83 | +JWT_SECRET=your_jwt_secret |
| 84 | +`; |
| 85 | + |
| 86 | + fs.writeFileSync(path.join(projectPath, ".env"), envContent.trim()); |
| 87 | + |
| 88 | + // Step 6: Write basic project files |
| 89 | + |
| 90 | + // src/index.js (updated to reflect src folder) |
| 91 | + const indexContent = ` |
| 92 | +import express from 'express'; |
| 93 | +import dotenv from 'dotenv'; |
| 94 | +import routes from './routes/userRoutes.js'; |
| 95 | +
|
| 96 | +dotenv.config(); |
| 97 | +
|
| 98 | +const app = express(); |
| 99 | +app.use(express.json()); |
| 100 | +
|
| 101 | +// Basic route for health check |
| 102 | +app.get('/', (req, res) => res.send('Hello from ${projectName} backend!')); |
| 103 | +
|
| 104 | +// Use Routes |
| 105 | +app.use('/api', routes); |
| 106 | +
|
| 107 | +const port = process.env.PORT || 3000; |
| 108 | +app.listen(port, () => console.log('🚀 Server running on ' + 'http://localhost:' + port)); |
| 109 | +`; |
| 110 | + |
| 111 | + fs.writeFileSync( |
| 112 | + path.join(projectPath, "src", "index.js"), |
| 113 | + indexContent.trim() |
| 114 | + ); |
| 115 | + |
| 116 | + // Create controllers, routes, and models for an example user API |
| 117 | + |
| 118 | + const userController = ` |
| 119 | +export const getUsers = (req, res) => { |
| 120 | + res.json({ message: "List of users" }); |
| 121 | +}; |
| 122 | +`; |
| 123 | + |
| 124 | + fs.writeFileSync( |
| 125 | + path.join(projectPath, "src", "controllers", "userController.js"), |
| 126 | + userController.trim() |
| 127 | + ); |
| 128 | + |
| 129 | + const userRoutes = ` |
| 130 | +import express from 'express'; |
| 131 | +import { getUsers } from '../controllers/userController.js'; |
| 132 | +
|
| 133 | +const router = express.Router(); |
| 134 | +
|
| 135 | +router.get('/users', getUsers); |
| 136 | +
|
| 137 | +export default router; |
| 138 | +`; |
| 139 | + |
| 140 | + fs.writeFileSync( |
| 141 | + path.join(projectPath, "src", "routes", "userRoutes.js"), |
| 142 | + userRoutes.trim() |
| 143 | + ); |
| 144 | + |
| 145 | + // Step 7: Optionally Add Auth (JWT) |
| 146 | + if (useAuth.toLowerCase() === "yes") { |
| 147 | + const authMiddleware = ` |
| 148 | +import jwt from 'jsonwebtoken'; |
| 149 | +
|
| 150 | +export const authenticate = (req, res, next) => { |
| 151 | + const token = req.header('x-auth-token'); |
| 152 | + if (!token) { |
| 153 | + return res.status(401).json({ msg: 'No token, authorization denied' }); |
| 154 | + } |
| 155 | + try { |
| 156 | + const decoded = jwt.verify(token, process.env.JWT_SECRET); |
| 157 | + req.user = decoded.user; |
| 158 | + next(); |
| 159 | + } catch (err) { |
| 160 | + res.status(401).json({ msg: 'Token is not valid' }); |
| 161 | + } |
| 162 | +}; |
| 163 | +`; |
| 164 | + |
| 165 | + fs.writeFileSync( |
| 166 | + path.join(projectPath, "src", "middlewares", "authMiddleware.js"), |
| 167 | + authMiddleware.trim() |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + console.log(`👉 Project "${projectName}" created!`); |
| 172 | + console.log(`👉 Run: cd ${projectName} && npm run dev`); |
| 173 | +} |
| 174 | + |
| 175 | +main(); |
0 commit comments