quick-node-server
Version:
A CLI tool to generate a Node.js project with optional MongoDB setup.
58 lines (45 loc) • 1.66 kB
JavaScript
import { execSync } from "child_process";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import readline from "readline";
import setupMongoDB from "../lib/setupMongoDB.js";
import setupNode from "../lib/setupNode.js";
// Set __dirname (Required for ESM)
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Setup CLI Input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Get project name from CLI input or set a default name
const projectName = process.argv[2] || "my-node-app";
const projectPath = path.join(process.cwd(), projectName);
console.log(`🚀 Creating Node.js project: ${projectName}...`);
fs.mkdirSync(projectPath, { recursive: true });
process.chdir(projectPath);
// Initialize package.json
execSync("npm init -y", { stdio: "ignore" });
// Modify package.json
const packageJsonPath = path.join(projectPath, "package.json");
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
packageJson.type = "module";
packageJson.scripts = {
start: "node app.js",
dev: "nodemon app.js"
};
// Write updated package.json back to file
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
// Install necessary dependencies
execSync("npm install express dotenv cors", { stdio: "ignore" });
// Ask the user whether to set up MongoDB
rl.question("⚡ Do you want to set up MongoDB? (Y/N): ", (answer) => {
const useMongoDB = answer.toLowerCase() === "y";
if (useMongoDB) {
setupMongoDB(rl, projectPath);
} else {
setupNode(rl, projectPath);
}
});