create-nodejs-app-template
Version:
[](https://www.npmjs.com/package/create-nodejs-app-template) [](https://github.com/umairabbas786/create-nodej
78 lines (59 loc) • 2.73 kB
JavaScript
const download = require("download-git-repo");
const path = require("path");
const fs = require("fs-extra");
const chalk = require("chalk");
const repoUrl = "direct:https://github.com/umairabbas786/create-nodejs-app-template.git#main";
// Get the project name from command-line arguments
let projectName = process.argv[2];
// Regular expression to validate camelCase format (no spaces, special characters, or underscores)
const isValidCamelCase = (name) => /^[a-z][a-zA-Z0-9]*$/.test(name);
// Ensure a project name is provided
if (!projectName) {
console.error(chalk.red("\nError: Please provide a project name in camelCase format."));
console.log(chalk.yellow("\nExample usage:"));
console.log(chalk.green(" npx create-nodejs-app-template myProjectName\n"));
process.exit(1);
}
// Validate project name format
if (!isValidCamelCase(projectName)) {
console.error(chalk.red("\nError: Project name must be in camelCase format (e.g., myProjectName)."));
console.log(chalk.yellow("Allowed: letters (a-z), capital letters (A-Z), and numbers (0-9)."));
process.exit(1);
}
// Convert project name to lowercase
projectName = projectName.toLowerCase();
console.log(chalk.blue(`\nCreating a new Node.js app in "${projectName}"...`));
// Define target path
const targetPath = path.join(process.cwd(), projectName);
// Remove existing directory if it exists
if (fs.existsSync(targetPath)) {
console.log(chalk.yellow(`\nDirectory ${projectName} already exists. Removing...`));
fs.removeSync(targetPath);
}
// Download the template from the GitHub repository
download(repoUrl, targetPath, { clone: true }, (err) => {
if (err) {
console.error(chalk.red("Error downloading the template:"), err);
process.exit(1);
}
console.log(chalk.green("\nProject setup complete!"));
// Update the package.json with the correct project name and remove GitHub-related URLs
const packageJsonPath = path.join(targetPath, "package.json");
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
// Set the project name to lowercase
packageJson.name = projectName;
// Remove GitHub related fields
delete packageJson.homepage;
delete packageJson.bugs;
delete packageJson.repository;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log(chalk.green("Updated project name and removed GitHub URLs in package.json."));
}
console.log(chalk.cyan(`\nNext steps:`));
console.log(chalk.yellow(`\n cd ${projectName}`));
console.log(chalk.yellow(` npm install`));
console.log(chalk.yellow(` npm run dev`));
console.log(chalk.cyan("\nHappy coding! 🚀\n"));
});