express-gny
Version:
A CLI tool that magically set up Node with Express MVC app folder structure
60 lines (46 loc) • 1.55 kB
JavaScript
#!/usr/bin/env node
import fs from "fs";
import { execSync } from "child_process";
const projectName = process.argv[2];
if (!projectName) {
console.log(`
❌ Project name is missing!
👉 Please add your project name after the command.
For example:
npx express-gny my-app
`);
process.exit(1);
}
// Step 1 - Create a folder as : Project Name
fs.mkdirSync(projectName);
process.chdir(projectName);
// Step 2 - npm init and install express
execSync("npm init -y", { stdio: "inherit" });
execSync("npm i express", { stdio: "inherit" });
// Step 3 - create folder structure
fs.mkdirSync("src", { recursive: true });
fs.mkdirSync("src/routes", { recursive: true });
fs.mkdirSync("src/controllers", { recursive: true });
fs.mkdirSync("src/models", { recursive: true });
fs.mkdirSync("src/utils", { recursive: true });
fs.mkdirSync("src/connection", { recursive: true });
fs.writeFileSync(
"index.js",
`import express from 'express';
const port = 3000;
const app = express();
app.use(express.json());
// app.use(router); // <- You'll need to define and import a router
// Example GET route
// app.get('/hello', (req, res) => {
// res.send('Hello from /hello');
// });
// Example POST route
// app.post('/submit', (req, res) => {
// res.json({ data: req.body });
// });
app.listen(port, () => {
console.log(\`Server is running on http://localhost:\${port}\`);
});`
);
console.log("Express Project with MVC Folder Structure created successfully!");