UNPKG

create-vault

Version:

A CLI tool to instantly set up a Full Stack (MERN) or Backend project with a structured folder system and pre-installed dependencies.

102 lines (85 loc) 3.02 kB
import fs from "fs"; import path from "path"; import { execSync } from "child_process"; import chalk from "chalk"; import inquirer from "inquirer"; import { FOLDERS, FILES, DEFAULT_ENV, GITIGNORE_CONTENT, } from "../constants/constant.js"; const createPackageJson = (dir, dependencies) => { const packageJsonPath = path.join(dir, "package.json"); if (!fs.existsSync(packageJsonPath)) { console.log(chalk.green("Initializing package.json...")); execSync("npm init -y", { cwd: dir, stdio: "inherit" }); const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")); packageJson.type = "module"; fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); console.log(chalk.blue('Updated package.json with "type": "module".')); } if (dependencies.length === 0) { console.log(chalk.red("No dependencies selected. Skipping installation.")); return; } console.log( chalk.green(`Installing dependencies: ${dependencies.join(", ")}...`) ); try { execSync(`npm install ${dependencies.join(" ")}`, { cwd: dir, stdio: "inherit", }); console.log(chalk.yellow("Dependencies installed successfully.")); } catch (error) { console.log(chalk.red("Error installing dependencies:", error.message)); } }; const setupBackend = async (baseDir) => { const createDir = (dir) => { if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); console.log(chalk.green(`Created directory: ${dir}`)); } }; const createFile = (filePath, content = "") => { if (!fs.existsSync(filePath)) { fs.writeFileSync(filePath, content); } }; FOLDERS.forEach((folder) => createDir(path.join(baseDir, folder))); FILES.forEach(({ path: filePath, content }) => createFile(path.join(baseDir, filePath), content) ); const { dependencies } = await inquirer.prompt([ { type: "checkbox", name: "dependencies", message: chalk.yellow("Select dependencies to install:"), choices: [ { name: "Express (Server framework)", value: "express", checked: true }, { name: "Mongoose (MongoDB ODM)", value: "mongoose", checked: true }, { name: "Cors (Cross-Origin Resource Sharing)", value: "cors", checked: true, }, { name: "Dotenv (Environment variables)", value: "dotenv" }, { name: "JWT (JSON Web Tokens)", value: "jsonwebtoken" }, { name: "Nodemon (Auto-restart for development)", value: "nodemon", checked: true, }, ], }, ]); if (dependencies.length > 0) { createPackageJson(`${baseDir}/server`, dependencies); } else { console.log(chalk.red("No dependencies selected. Skipping installation.")); } console.log(chalk.green(`Backend setup created in ${baseDir}/server 🚀`)); }; export default setupBackend;