Skip to content

Commit

Permalink
feat: add package name validation using validate-npm-package-name
Browse files Browse the repository at this point in the history
  • Loading branch information
KiranRajeev-KV committed Dec 26, 2024
1 parent c631901 commit da7802a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
27 changes: 25 additions & 2 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import figlet from "figlet";
import chalk from "chalk";
import { createSpinner } from "nanospinner";
import { metadata, commands, templates } from "./configs.js";
import validate from "validate-npm-package-name";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand Down Expand Up @@ -91,10 +92,32 @@ function initCommand(options) {
const selectedTemplate = options.template || "basic"; // Default to 'basic' if no template is specified
const packageName = options.name || "quick-start-express-server"; // Default to 'quick-start-express-server' if no name is specified

if (packageName) {
const validateResult = validate(packageName);
if (validateResult.validForNewPackages === false) {
if (validateResult.errors) {
console.error(
chalk.red.bold(
`Invalid package name: ${validateResult.errors}. Please provide a valid package name.`
)
);
} else if (validateResult.warnings) {
console.error(
chalk.red.bold(
`Invalid package name: ${validateResult.warnings}. Please provide a valid package name.`
)
);
}
return;
}
}

if (!templates[selectedTemplate]) {
console.error(
chalk.bgRed.white(
`Template ${selectedTemplate} does not exist. To see available templates use "qse list".`
chalk.red(
`Template ${selectedTemplate} does not exist. To see available templates use ${chalk.yellow(
'"qse list"'
)}.`
)
);
return;
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
"commander": "^12.1.0",
"figlet": "^1.7.0",
"fs-extra": "^11.2.0",
"nanospinner": "^1.1.0"
"nanospinner": "^1.1.0",
"validate-npm-package-name": "^6.0.0"
},
"devDependencies": {
"jest": "^29.7.0"
}
}
}

0 comments on commit da7802a

Please sign in to comment.