create-auth-backend-cli
Version:
CLI to scaffold a Node.js Auth backend with Express, JWT, MongoDB
54 lines (44 loc) • 1.54 kB
JavaScript
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const args = process.argv.slice(2);
const projectName = args[0];
if (!projectName) {
console.error("❌ Please provide a project name.");
console.log("Example: npx create-auth-backend my-app");
process.exit(1);
}
const targetDir = path.join(process.cwd(), projectName);
const templateDir = path.join(__dirname, "../template");
if (fs.existsSync(targetDir)) {
console.error(`❌ Directory "${projectName}" already exists.`);
process.exit(1);
}
function copyDir(src, dest) {
if (!fs.existsSync(dest)) fs.mkdirSync(dest);
fs.readdirSync(src).forEach(file => {
const srcPath = path.join(src, file);
const destPath = path.join(dest, file);
if (fs.statSync(srcPath).isDirectory()) {
copyDir(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
});
}
console.log(`🚀 Creating your Node.js Auth backend in "${projectName}"...`);
copyDir(templateDir, targetDir);
// Install dependencies
console.log("📦 Installing dependencies...");
try {
execSync("npm install", { stdio: "inherit", cwd: targetDir });
console.log("✅ All dependencies installed successfully!");
} catch (err) {
console.error("❌ Failed to install dependencies. Please run `npm install` manually inside the project folder.");
}
console.log("✅ Done!");
console.log(`👉 Next steps:
cd ${projectName}
npm start
`);