UNPKG

clean-gen

Version:

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

108 lines (91 loc) 2.93 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"); // 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) { 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); // Change to project directory process.chdir(projectName); // Install additional dependencies await installDependencies(); // 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) { console.log(`${COLORS.YELLOW}📦 Installing NestJS CLI...${COLORS.NC}`); await exec("npm install -g @nestjs/cli", { stdio: "inherit" }); console.log( `${COLORS.GREEN}✓ NestJS CLI installed successfully${COLORS.NC}` ); } } async function createNestProject(projectName) { console.log(`${COLORS.YELLOW}🏗️ Creating NestJS project...${COLORS.NC}`); await exec(`nest new ${projectName} -p npm --skip-git`, { stdio: "inherit", }); } async function installDependencies() { console.log( `${COLORS.YELLOW}📦 Installing additional dependencies...${COLORS.NC}` ); const dependencies = "@nestjs/config class-validator class-transformer dotenv"; await exec(`npm install ${dependencies}`, { stdio: "inherit" }); } async function runSetupScript() { try { require(setupCleanScript)(); } catch (error) { throw new Error(`Setup script failed: ${error.message}`); } } function displaySuccessMessage(projectName) { console.log( `${COLORS.GREEN}✅ Project setup completed successfully! 🎉${COLORS.NC}` ); console.log(`\nTo get started:`); console.log(` cd ${projectName}`); console.log(` npm run start:dev`); } 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); }