UNPKG

cl-generate

Version:

A cross-platform CLI tool to generate NestJS clean architecture modules

143 lines (122 loc) 3.84 kB
const path = require("path"); const { execSync } = require("child_process"); const { promisify } = require("util"); const exec = promisify(require("child_process").exec); const scriptDir = path.join(__dirname); const setupCleanScript = path.join(scriptDir, "setup.js"); const { withLoading } = require("../shares/loadingSpinner"); // Colors for output (ANSI escape codes) const COLORS = { GREEN: "\x1b[32m", YELLOW: "\x1b[33m", RED: "\x1b[31m", CYAN: "\x1b[36m", NC: "\x1b[0m", }; module.exports = async function createModule(projectName, packageManager) { if (!projectName) { console.error(`${COLORS.RED}❌ Project name is required${COLORS.NC}`); process.exit(1); } try { console.log( `${COLORS.CYAN}🚀 Setting up new NestJS project with clean architecture...${COLORS.NC}` ); // Check for NestJS CLI await checkNestCli(); // Create new NestJS project await createNestProject(projectName, packageManager); // Change to project directory process.chdir(projectName); // Install additional dependencies await installDependencies(packageManager); // Remove unnecessary files await removeFiles(); // Run setup script await runSetupScript(); displaySuccessMessage(projectName); } catch (error) { handleError(error); } }; async function checkNestCli() { console.log(`${COLORS.YELLOW}🔍 Checking for NestJS CLI...${COLORS.NC}`); try { await exec("nest --version"); console.log(`${COLORS.GREEN}✓ NestJS CLI already installed${COLORS.NC}`); } catch (error) { await withLoading( `${COLORS.YELLOW}📦 Installing NestJS CLI: ${COLORS.NC}`, async () => { await exec("npm install -g @nestjs/cli", { stdio: "inherit" }); console.log( `${COLORS.GREEN}✓ NestJS CLI installed successfully${COLORS.NC}` ); } ); } } async function createNestProject(projectName, packageManager) { await withLoading( `${COLORS.YELLOW}🏗️ Creating NestJS project: ${COLORS.NC}`, async () => { await exec(`nest new ${projectName} -p ${packageManager} --skip-git`, { stdio: "inherit", }); } ); } async function installDependencies(packageManager) { await withLoading( `${COLORS.YELLOW}📦 Installing additional dependencies: ${COLORS.NC}`, async () => { const dependencies = "@nestjs/config @nestjs/microservices class-validator class-transformer dotenv @grpc/grpc-js @nestjs/swagger fastify"; await exec( `${packageManager} ${ packageManager === "yarn" ? "add" : "install" } ${dependencies}`, { stdio: "inherit", } ); } ); } async function runSetupScript() { try { await require(setupCleanScript)(); } catch (error) { throw new Error(`Setup script failed: ${error.message}`); } } async function removeFiles() { const filesToRemove = ["src"]; for (const file of filesToRemove) { try { await exec(`rm -rf ${file}`, { stdio: "inherit" }); // console.log(`${COLORS.YELLOW}✔ Removed file: ${COLORS.NC}${file}`); } catch (error) { console.error( `${COLORS.RED}❌ Failed to remove file: ${COLORS.NC}${file} - ${error.message}` ); } } console.log(`${COLORS.GREEN}❇️ Setup NestJS project completed`); } function displaySuccessMessage(projectName) { console.log( `${COLORS.GREEN}✅ Project setup completed successfully! 🎉${COLORS.NC}` ); } function handleError(error) { console.error( `${COLORS.RED}❌ Error creating NestJS project:${COLORS.NC} ${error.message}` ); if (error.message.includes("nest")) { console.error( `${COLORS.YELLOW}Try installing @nestjs/cli manually: npm install -g @nestjs/cli${COLORS.NC}` ); } process.exit(1); }