Skip to content

Commit

Permalink
validate project name inline with npm name rules (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
rin-st authored Jan 6, 2025
1 parent f983396 commit 4db867d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-pigs-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-eth": patch
---

validate project name inline with npm name rules
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@types/inquirer": "9.0.3",
"@types/ncp": "2.0.5",
"@types/node": "18.16.0",
"@types/validate-npm-package-name": "4.0.2",
"eslint": "^9.15.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
Expand All @@ -58,7 +59,8 @@
"inquirer": "9.2.0",
"listr2": "^8.2.1",
"merge-packages": "^0.1.6",
"ncp": "2.0.0"
"ncp": "2.0.0",
"validate-npm-package-name": "6.0.0"
},
"packageManager": "[email protected]"
}
17 changes: 15 additions & 2 deletions src/utils/parse-arguments-into-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { validateFoundryUp } from "./system-validation";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { validateNpmName } from "./validate-name";

const validateExternalExtension = async (
extensionName: string,
Expand Down Expand Up @@ -74,7 +75,7 @@ export async function parseArgumentsIntoOptions(
"-h": "--help",
},
{
argv: rawArgs.slice(2).map(a => a.toLowerCase()),
argv: rawArgs.slice(2),
},
);

Expand All @@ -84,7 +85,7 @@ export async function parseArgumentsIntoOptions(

const help = args["--help"] ?? false;

const project = args._[0] ?? null;
let project: string | null = args._[0] ?? null;

// use the original extension arg
const extensionName = args["--extension"] && rawArgs.slice(2).find(a => a.toLowerCase() === args["--extension"]);
Expand All @@ -101,6 +102,18 @@ export async function parseArgumentsIntoOptions(
);
}

if (project) {
const validation = validateNpmName(project);
if (!validation.valid) {
console.error(
`Could not create a project called ${chalk.yellow(`"${project}"`)} because of naming restrictions:`,
);

validation.problems.forEach(p => console.error(`${chalk.red(">>")} Project ${p}`));
project = null;
}
}

let solidityFrameworkChoices = [
SOLIDITY_FRAMEWORKS.HARDHAT,
SOLIDITY_FRAMEWORKS.FOUNDRY,
Expand Down
10 changes: 9 additions & 1 deletion src/utils/prompt-for-missing-options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Options, RawOptions, SolidityFrameworkChoices } from "../types";
import inquirer from "inquirer";
import { SOLIDITY_FRAMEWORKS } from "./consts";
import { validateNpmName } from "./validate-name";
import { basename, resolve } from "path";

// default values for unspecified args
const defaultOptions: RawOptions = {
Expand All @@ -23,7 +25,13 @@ export async function promptForMissingOptions(
name: "project",
message: "Your project name:",
default: defaultOptions.project,
validate: (value: string) => value.length > 0,
validate: (name: string) => {
const validation = validateNpmName(basename(resolve(name)));
if (validation.valid) {
return true;
}
return "Project " + validation.problems[0];
},
},
{
type: "list",
Expand Down
22 changes: 22 additions & 0 deletions src/utils/validate-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import validateProjectName from "validate-npm-package-name";

type ValidateNpmNameResult =
| {
valid: true;
}
| {
valid: false;
problems: string[];
};

export function validateNpmName(name: string): ValidateNpmNameResult {
const nameValidation = validateProjectName(name);
if (nameValidation.validForNewPackages) {
return { valid: true };
}

return {
valid: false,
problems: [...(nameValidation.errors || []), ...(nameValidation.warnings || [])],
};
}
16 changes: 16 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,13 @@ __metadata:
languageName: node
linkType: hard

"@types/validate-npm-package-name@npm:4.0.2":
version: 4.0.2
resolution: "@types/validate-npm-package-name@npm:4.0.2"
checksum: 3f35a3cc8ddd919b456843f36d55a4f1df5f03d5d9b6494b4d8f5f3b24e3f24a11c922772d9970a67f1249214da18c157776e9c6d2e72227799459849dfd9c76
languageName: node
linkType: hard

"@typescript-eslint/eslint-plugin@npm:8.15.0":
version: 8.15.0
resolution: "@typescript-eslint/eslint-plugin@npm:8.15.0"
Expand Down Expand Up @@ -1389,6 +1396,7 @@ __metadata:
"@types/inquirer": 9.0.3
"@types/ncp": 2.0.5
"@types/node": 18.16.0
"@types/validate-npm-package-name": 4.0.2
arg: 5.0.2
chalk: 5.2.0
eslint: ^9.15.0
Expand All @@ -1406,6 +1414,7 @@ __metadata:
tslib: 2.5.0
typescript: ^5.6.3
typescript-eslint: ^8.15.0
validate-npm-package-name: 6.0.0
bin:
create-eth: bin/create-dapp-se2.js
languageName: unknown
Expand Down Expand Up @@ -5026,6 +5035,13 @@ __metadata:
languageName: node
linkType: hard

"validate-npm-package-name@npm:6.0.0":
version: 6.0.0
resolution: "validate-npm-package-name@npm:6.0.0"
checksum: 4d018c4fa07f95534a5fea667adc653b1ef52f08bf56aff066c28394499d0a6949c0b00edbd7077c4dc1e041da9220af7c742ced67d7d2d6a1b07d10cbe91b29
languageName: node
linkType: hard

"wcwidth@npm:^1.0.1":
version: 1.0.1
resolution: "wcwidth@npm:1.0.1"
Expand Down

0 comments on commit 4db867d

Please sign in to comment.