UNPKG

setup-mern

Version:

A CLI tool to generate a MERN backend boilerplate in seconds!

67 lines (56 loc) 2.09 kB
#!/usr/bin/env node import fs from "fs-extra"; import path from "path"; import chalk from "chalk"; import { fileURLToPath } from "url"; import inquirer from "inquirer"; import ora from "ora"; // Get __dirname in ES Modules const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // Paths const templatesDir = path.join(__dirname, "../templates"); const targetDir = process.argv[2] || "my-new-project"; // Check if target directory already exists if (fs.existsSync(targetDir)) { console.error(chalk.red(`Error: Directory "${targetDir}" already exists.`)); process.exit(1); } // Prompt user to confirm const questions = [ { type: "confirm", name: "proceed", message: `Do you want to create a MERN backend boilerplate in "./${targetDir}"?`, default: true, }, ]; inquirer.prompt(questions).then(async (answers) => { if (!answers.proceed) { console.log(chalk.yellow("Setup canceled.")); process.exit(0); } // Start the loader const spinner = ora("Creating boilerplate...").start(); try { // Copy templates to target directory await fs.copy(templatesDir, targetDir); // Update package.json name const packageJsonPath = path.join(targetDir, "package.json"); const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")); packageJson.name = path.basename(targetDir); await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)); // Stop the loader and show success message spinner.succeed(chalk.green("Boilerplate created successfully!")); console.log(chalk.blue(`\nNext steps:`)); console.log(`1. cd ${targetDir}`); console.log(`2. npm install`); console.log(`3. npm start (or npm run dev for development)`); console.log(chalk.yellow("\nHappy coding! 🚀\n")); } catch (err) { // Stop the loader and show error message spinner.fail(chalk.red("Failed to create boilerplate.")); console.error(chalk.red(err.message)); process.exit(1); } });