xpress-backend
Version:
Package that helps developers to generate files and folder in structure way, making project organization easier.
68 lines (52 loc) ⢠2.22 kB
JavaScript
import fs from "fs-extra";
import path from "path";
import inquirer from "inquirer";
import chalk from "chalk";
import ora from "ora";
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const TEMPLATE_DIR = path.join(__dirname, "templates");
const TARGET_DIR = process.cwd();
const QUESTIONS = [
{
type: "input",
name: "projectName",
message: "Enter the project name:",
default: "express-api",
},
];
async function createProject() {
console.log(chalk.blue("š Welcome to Node Express CLI Generator!"));
const answers = await inquirer.prompt(QUESTIONS);
const projectPath = path.join(TARGET_DIR, answers.projectName);
if (fs.existsSync(projectPath)) {
console.log(chalk.red("ā Project already exists. Choose a different name."));
return;
}
const spinner = ora({
text: chalk.yellow(`š Creating project: ${answers.projectName}...`),
color: 'cyan',
}).start();
try {
// Simulate processing time
await new Promise(resolve => setTimeout(resolve, 1000));
// Copy template files
fs.copySync(TEMPLATE_DIR, projectPath);
// Replace placeholders in package.json
const packageJsonPath = path.join(projectPath, "package.json");
const packageJson = fs.readJsonSync(packageJsonPath);
packageJson.name = answers.projectName;
fs.writeJsonSync(packageJsonPath, packageJson, { spaces: 2 });
spinner.succeed(chalk.green("ā
Project created successfully!"));
console.log(chalk.blue(`š Navigate to your project: cd ${answers.projectName}`));
console.log(chalk.blue("š” Run 'npm install' or 'yarn install' to install dependencies."));
console.log(chalk.blue("š” Run 'npm dev' or 'yarn dev' to start the development server."));
console.log(chalk.magentaBright("\nš Happy Coding!\n"));
} catch (error) {
spinner.fail(chalk.red("ā Error creating project!"));
console.error(error);
}
}
createProject();