Skip to content

Commit

Permalink
Replace validate-npm-package-name with a regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
askoufis committed Apr 19, 2024
1 parent 6f33953 commit e7e0066
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 58 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-llamas-chew.md
@@ -0,0 +1,5 @@
---
'sku': patch
---

Replace `validate-npm-package-name` dependency with a regexp
1 change: 0 additions & 1 deletion packages/sku/package.json
Expand Up @@ -118,7 +118,6 @@
"terser-webpack-plugin": "^5.1.4",
"tree-kill": "^1.2.1",
"typescript": "~5.3.0",
"validate-npm-package-name": "^5.0.0",
"webpack": "^5.52.0",
"webpack-bundle-analyzer": "^4.6.1",
"webpack-dev-server": "^5.0.2",
Expand Down
25 changes: 11 additions & 14 deletions packages/sku/scripts/init.js
Expand Up @@ -11,7 +11,6 @@ const chalk = require('chalk');
const fs = require('node:fs/promises');
const { posix: path } = require('node:path');
const { isEmptyDir } = require('../lib/isEmptyDir');
const validatePackageName = require('validate-npm-package-name');
const dedent = require('dedent');
const { setCwd } = require('../lib/cwd');
const prettierWrite = require('../lib/runPrettier').write;
Expand Down Expand Up @@ -48,6 +47,11 @@ const getTemplateFileDestinationFromRoot =
return path.join(projectRoot, filePathRelativeToTemplate);
};

// Copied from `package-name-regex@4.0.0`
// See https://github.com/dword-design/package-name-regex/blob/acae7d482b1d03379003899df4d484238625364d/src/index.js#L1-L2
const packageNameRegex =
/^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;

(async () => {
const projectName = args.argv[0];

Expand Down Expand Up @@ -89,22 +93,15 @@ const getTemplateFileDestinationFromRoot =
'braid-design-system',
].sort();

const {
validForNewPackages,
errors = [],
warnings = [],
} = validatePackageName(appName);
const isValidPackageName = packageNameRegex.test(appName);

if (!validForNewPackages) {
if (!isValidPackageName) {
console.error(dedent`
Could not create a project called ${chalk.red(`"${appName}"`)} \
because of npm naming restrictions:
Could not create a project called ${chalk.red(
`"${appName}"`,
)} because of npm naming restrictions. \
Please see https://docs.npmjs.com/cli/configuring-npm/package-json for package name rules.
`);

const results = [...errors, ...warnings];
results.forEach((result) => console.error(chalk.red(` * ${result}`)));

process.exit(1);
}

if (reservedNames.indexOf(appName) >= 0) {
Expand Down
44 changes: 1 addition & 43 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e7e0066

Please sign in to comment.