validate-npm-package-name-cli 1.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc CHANGED
@@ -4,6 +4,7 @@
4
4
  "extends": "@ljharb/eslint-config/node/latest",
5
5
 
6
6
  "rules": {
7
+ "func-style": "off",
7
8
  "no-extra-parens": "off",
8
9
  },
9
10
  }
package/.nycrc ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "all": true,
3
+ "reporter": [
4
+ "html",
5
+ "text",
6
+ "lcov"
7
+ ],
8
+ "exclude": [
9
+ "coverage",
10
+ "eslint.config.mjs",
11
+ "test"
12
+ ]
13
+ }
package/CHANGELOG.md CHANGED
@@ -5,6 +5,25 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [v2.1.0](https://github.com/ljharb/validate-npm-package-name-cli/compare/v2.0.0...v2.1.0) - 2025-12-26
9
+
10
+ ### Commits
11
+
12
+ - [New] pass for invalid but preexisting packages [`66652e7`](https://github.com/ljharb/validate-npm-package-name-cli/commit/66652e7f4aeabf9a81c66b68569950e220c21d99)
13
+ - [Dev Deps] update `eslint`, `@ljharb/eslint-config` [`d28c6f2`](https://github.com/ljharb/validate-npm-package-name-cli/commit/d28c6f26c94531e8e34e434fff8c6d60b4ea1836)
14
+ - [Deps] update `pargs`, `validate-npm-package-name` [`cea9494`](https://github.com/ljharb/validate-npm-package-name-cli/commit/cea9494dbe6323730d2f46ce144166f18a085094)
15
+ - [Dev Deps] update `npmignore` [`416fbc7`](https://github.com/ljharb/validate-npm-package-name-cli/commit/416fbc7c204d07a74dd778bcba30bf73297bfb71)
16
+
17
+ ## [v2.0.0](https://github.com/ljharb/validate-npm-package-name-cli/compare/v1.0.0...v2.0.0) - 2025-10-31
18
+
19
+ ### Commits
20
+
21
+ - [Refactor] use `pargs` package [`3a8ba69`](https://github.com/ljharb/validate-npm-package-name-cli/commit/3a8ba69027be1e45fafccb67367183d7341c6f8f)
22
+ - [Tests] extract coverage config to file [`9930ba4`](https://github.com/ljharb/validate-npm-package-name-cli/commit/9930ba4d1b2f79009d73e08acda5dc9a1f021a62)
23
+ - [Dev Deps] update `@arethetypeswrong/cli`, `@ljharb/eslint-config`, `@ljharb/tsconfig`, `@types/node`, `undici-types` [`82aa998`](https://github.com/ljharb/validate-npm-package-name-cli/commit/82aa99819d87b78c0ec1a28b40ff3c971451ef5a)
24
+ - [Breaking] update `engines.node` [`bcaddc4`](https://github.com/ljharb/validate-npm-package-name-cli/commit/bcaddc45999755d95026b473c900c495830ee418)
25
+ - [Deps] update `validate-npm-package-name` [`5557a25`](https://github.com/ljharb/validate-npm-package-name-cli/commit/5557a25fc1e046efe00569c90de4282fee79449e)
26
+
8
27
  ## v1.0.0 - 2025-02-26
9
28
 
10
29
  ### Commits
package/bin.mjs CHANGED
@@ -1,11 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import { styleText } from 'util';
4
- import pargs from './pargs.mjs';
4
+ import pargs from 'pargs';
5
5
 
6
6
  const {
7
- positionals: [packageName],
8
7
  help,
8
+ positionals: [packageName],
9
9
  } = await pargs(import.meta.filename, {
10
10
  allowPositionals: 1,
11
11
  minPositionals: 1,
@@ -17,15 +17,26 @@ import validate from 'validate-npm-package-name';
17
17
 
18
18
  const {
19
19
  validForNewPackages,
20
+ validForOldPackages,
20
21
  warnings,
21
- errors,
22
+ errors: validateErrors,
22
23
  } = validate(packageName);
23
24
 
25
+ /** @type {boolean | null} */
26
+ let existsOnNpm = null;
27
+ if (!validForNewPackages && validForOldPackages) {
28
+ const res = await fetch(`https://registry.npmjs.org/${encodeURIComponent(packageName)}`);
29
+ existsOnNpm = res.ok;
30
+ }
31
+
24
32
  if (validForNewPackages) {
25
33
  console.log(styleText('green', packageName));
34
+ } else if (validForOldPackages && existsOnNpm) {
35
+ warnings?.forEach((warning) => console.warn(styleText('yellow', warning)));
36
+ console.log(styleText(['dim', 'green'], packageName));
26
37
  } else {
27
38
  warnings?.forEach((warning) => console.warn(styleText('yellow', warning)));
28
- errors?.forEach((error) => console.error(styleText('red', error)));
39
+ validateErrors?.forEach((error) => console.error(styleText('red', error)));
29
40
 
30
41
  process.exitCode = 1;
31
42
  }
@@ -0,0 +1,11 @@
1
+ import config from '@ljharb/eslint-config/flat/node/latest';
2
+
3
+ export default [
4
+ ...config,
5
+ {
6
+ rules: {
7
+ 'func-style': 'off',
8
+ 'no-extra-parens': 'off',
9
+ },
10
+ },
11
+ ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "validate-npm-package-name-cli",
3
- "version": "1.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "CLI for npmjs.com/validate-npm-package-name - give me a string and I'll tell you if it's a valid npm package name",
5
5
  "bin": "./bin.mjs",
6
6
  "main": false,
@@ -11,8 +11,8 @@
11
11
  "prepack": "npmignore --auto --commentLines=autogenerated",
12
12
  "prepublish": "not-in-publish || npm run prepublishOnly",
13
13
  "prepublishOnly": "safe-publish-latest",
14
- "lint": "eslint --ext=js,mjs .",
15
- "postlint": "tsc && attw -P",
14
+ "lint": "eslint .",
15
+ "postlint": "tsc",
16
16
  "pretest": "npm run lint",
17
17
  "tests-only": "c8 tape 'test/**/*.mjs'",
18
18
  "test": "npm run tests-only",
@@ -40,40 +40,29 @@
40
40
  },
41
41
  "homepage": "https://github.com/ljharb/validate-npm-package-name-cli#readme",
42
42
  "dependencies": {
43
- "validate-npm-package-name": "^6.0.0"
43
+ "pargs": "^1.2.1",
44
+ "validate-npm-package-name": "^7.0.1"
44
45
  },
45
46
  "devDependencies": {
46
- "@arethetypeswrong/cli": "^0.17.4",
47
- "@ljharb/eslint-config": "^21.1.1",
48
- "@ljharb/tsconfig": "^0.2.3",
49
- "@types/node": "^22.13.5",
47
+ "@ljharb/eslint-config": "^22.1.3",
48
+ "@ljharb/tsconfig": "^0.3.2",
49
+ "@types/is-core-module": "^2.2.2",
50
+ "@types/node": "^22.18.13",
50
51
  "@types/tape": "^5.8.1",
51
52
  "@types/validate-npm-package-name": "^4.0.2",
52
53
  "auto-changelog": "^2.5.0",
53
54
  "c8": "^10.1.3",
54
- "eslint": "=8.8.0",
55
+ "eslint": "^9.39.2",
55
56
  "in-publish": "^2.0.1",
56
- "npmignore": "^0.3.1",
57
+ "is-core-module": "^2.16.1",
58
+ "npmignore": "^0.3.5",
57
59
  "safe-publish-latest": "^2.0.0",
58
60
  "tape": "^5.9.0",
59
61
  "typescript": "next",
60
- "undici-types": "^7.3.0"
62
+ "undici-types": "^7.16.0"
61
63
  },
62
64
  "engines": {
63
- "node": "^22.14 || >= 23.9"
64
- },
65
- "c8": {
66
- "all": true,
67
- "reporter": [
68
- "html",
69
- "text",
70
- "lcov"
71
- ],
72
- "exclude": [
73
- "coverage",
74
- "test",
75
- "pargs.mjs"
76
- ]
65
+ "node": "^22.20 || ^24.10 || >= 25"
77
66
  },
78
67
  "auto-changelog": {
79
68
  "output": "CHANGELOG.md",
package/test/bin.mjs CHANGED
@@ -59,15 +59,31 @@ test('cli', async (t) => {
59
59
  const y = /** @type {const} */ ([
60
60
  'es-abstract',
61
61
  'jsonstream',
62
+ 'json',
62
63
  ...Object.keys(pkgJSON.dependencies),
63
64
  ...Object.keys(pkgJSON.devDependencies),
64
65
  ]);
65
66
 
66
67
  await Promise.all(y.map((pkg) => run(t, pkg, { out: pkg, outC: styleText('green', pkg) })));
67
68
 
69
+ // packages that exist on npm but are only validForOldPackages (not validForNewPackages)
70
+ const yOld = /** @type {const} */ ([
71
+ ['JSONStream', 'name can no longer contain capital letters'],
72
+ ['JSON', 'name can no longer contain capital letters'],
73
+ ['path', 'path is a core module name'],
74
+ ]);
75
+
76
+ await Promise.all(yOld.map(([pkg, warning]) => run(t, pkg, {
77
+ out: pkg,
78
+ outC: styleText(['dim', 'green'], pkg),
79
+ err: warning,
80
+ errC: styleText('yellow', warning),
81
+ })));
82
+
68
83
  const n = /** @type {const} */ ([
69
84
  ['foo bar', 'name can only contain URL-friendly characters'],
70
- ['JSONStream', '', 'name can no longer contain capital letters'],
85
+ ['worker_threads', '', 'worker_threads is a core module name'],
86
+ ['ThisPackageDoesNotExistOnNpm', '', 'name can no longer contain capital letters'],
71
87
  ]);
72
88
 
73
89
  await Promise.all(n.map(([
package/pargs.d.mts DELETED
@@ -1,65 +0,0 @@
1
- export type ExtractIterable<T> = T extends Iterable<infer U> ? U : never;
2
-
3
- // export type GroupedByString<T> = { [k: string]: null | T[] };
4
-
5
- // declare function groupByString<T>(iterable: Iterable<T>, cb: (item: T) => string): GroupedByString<T>;
6
-
7
- import { parseArgs, type ParseArgsConfig as PAC } from "util";
8
-
9
- export type ParseArgsConfig = PAC;
10
-
11
- type ParseArgsOptionsConfig = NonNullable<ParseArgsConfig['options']>;
12
-
13
- type ParseArgsOptionConfig = ParseArgsOptionsConfig[keyof ParseArgsOptionsConfig];
14
-
15
- export type PargsConfig = Omit<ParseArgsConfig, 'args' | 'strict' | 'allowPositionals' | 'options'> & {
16
- options?: {
17
- [longOption: string]: ParseArgsOptionConfig | (Omit<ParseArgsOptionConfig, 'type'> & {
18
- type: 'enum';
19
- choices: string[];
20
- });
21
- };
22
- allowPositionals?: boolean | number;
23
- minPositionals?: number;
24
- };
25
-
26
- export type PargsRootConfig = PargsConfig & {
27
- subcommands: Record<string, PargsConfig>
28
- };
29
-
30
- export type ParseArgsError = (Error | TypeError) & {
31
- code:
32
- | 'ERR_PARSE_ARGS_UNKNOWN_OPTION'
33
- | 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
34
- | 'ERR_INVALID_ARG_TYPE'
35
- | 'ERR_INVALID_ARG_VALUE'
36
- | 'ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL'
37
- };
38
-
39
- export type OptionToken = Extract<
40
- NonNullable<
41
- ReturnType<typeof parseArgs>['tokens']
42
- >[number],
43
- { kind: 'option' }
44
- >;
45
-
46
- export type PargsParsed<T extends (PargsConfig | PargsRootConfig)> = (
47
- T extends PargsRootConfig ? {
48
- command: { name: keyof T['subcommands'] } & PargsParsed<PargsConfig>,
49
- } : {}
50
- ) & {
51
- errors: string[],
52
- help(): Promise<void>,
53
- } & ReturnType<typeof parseArgs>;
54
-
55
- // declare function pargs<C extends PargsRootConfig>(
56
- // entrypointPath: ImportMeta['filename'],
57
- // obj: C,
58
- // ): Promise<PargsParsed<C>>;
59
-
60
- declare function pargs<C extends PargsRootConfig | PargsConfig>(
61
- entrypointPath: ImportMeta['filename'],
62
- obj: C,
63
- ): Promise<PargsParsed<C>>;
64
-
65
- export default pargs;
package/pargs.mjs DELETED
@@ -1,217 +0,0 @@
1
- import { parseArgs } from 'util';
2
- import { dirname, join } from 'path';
3
- import { realpathSync } from 'fs';
4
- import { readFile } from 'fs/promises';
5
-
6
- const { hasOwn } = Object;
7
-
8
- /** @type {(e: unknown) => e is import('./pargs.mjs').ParseArgsError} */
9
- function isParseArgsError(e) {
10
- return !!e
11
- && typeof e === 'object'
12
- && 'code' in e
13
- && (
14
- e.code === 'ERR_PARSE_ARGS_UNKNOWN_OPTION'
15
- || e.code === 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
16
- || e.code === 'ERR_INVALID_ARG_TYPE'
17
- || e.code === 'ERR_INVALID_ARG_VALUE'
18
- || e.code === 'ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL'
19
- );
20
- }
21
-
22
- /* eslint max-lines-per-function: 0, max-statements: 0, complexity: 0, sort-keys: 0, no-magic-numbers: 0 */
23
-
24
- /** @type {import('./pargs.mjs').default} */
25
- export default async function pargs(entrypointPath, obj) {
26
- const argv = process.argv.flatMap((arg) => {
27
- try {
28
- const realpathedArg = realpathSync(arg);
29
- if (
30
- realpathedArg === process.execPath
31
- || realpathedArg === entrypointPath
32
- ) {
33
- return [];
34
- }
35
- } catch (e) { /**/ }
36
- return arg;
37
- });
38
-
39
- if ('help' in obj || (obj.options && 'help' in obj.options)) {
40
- throw new TypeError('The "help" option is reserved');
41
- }
42
-
43
- /** @type {string[]} */
44
- const errors = [];
45
-
46
- if ('subcommands' in obj && (!obj.subcommands || typeof obj.subcommands !== 'object')) {
47
- throw new TypeError('Error: `subcommands` must be an object');
48
- }
49
-
50
- const { subcommands, ...passedConfig } = obj;
51
-
52
- if ('subcommands' in obj && Object.keys(obj.subcommands).length === 0) {
53
- throw new TypeError('Error: `subcommands` must be an object with at least one key');
54
- }
55
-
56
- if ('subcommands' in obj && 'allowPositionals' in passedConfig) {
57
- throw new TypeError('Error: `allowPositionals` is not allowed when `subcommands` is defined');
58
- }
59
-
60
- const enums = { __proto__: null };
61
-
62
- /** @type {{ options: import('./pargs.mjs').ParseArgsConfig['options'] & { help: { default: false, type: 'boolean' } } }} */
63
- const normalizedOptions = Object.fromEntries(Object.entries(passedConfig.options ?? {}).flatMap(([key, value]) => {
64
- if (value.type !== 'enum') {
65
- return [[key, value]];
66
- }
67
-
68
- if (!Array.isArray(value.choices) || !value.choices.every((x) => typeof x === 'string')) {
69
- throw new TypeError(`Error: enum choices must be an array of strings; \`${key}\` is invalid`);
70
- }
71
-
72
- enums[key] = value;
73
- return [[key, { ...value, type: 'string' }]];
74
- }).concat([
75
- [
76
- 'help',
77
- {
78
- default: false,
79
- type: 'boolean',
80
- },
81
- ],
82
- ]));
83
-
84
- /** @type {import('./pargs.mjs').ParseArgsConfig & { tokens: true, allowNegative: true, strict: true, options: typeof normalizedOptions }} */
85
- const newObj = {
86
- args: subcommands ? argv.slice(0, 1) : argv,
87
- ...passedConfig,
88
- options: normalizedOptions,
89
- tokens: true,
90
- allowNegative: true,
91
- allowPositionals: !!subcommands || typeof passedConfig.allowPositionals !== 'undefined',
92
- minPositionals: 0,
93
- strict: true,
94
- };
95
-
96
- // console.log(newObj.options);
97
-
98
- try {
99
- const { tokens, ...results } = parseArgs(newObj);
100
-
101
- const enumEntries = Object.entries(enums);
102
- if (enumEntries.length > 0) {
103
- enumEntries.forEach(([key, config]) => {
104
- const value = results.values[key];
105
- // console.log(key, config, value);
106
-
107
- if (!config.choices.includes(value)) {
108
- errors.push(`Error: Invalid value for option "${key}"`);
109
- }
110
- });
111
- }
112
-
113
- // eslint-disable-next-line no-inner-declarations
114
- async function help() {
115
- if (('help' in results.values && results.values.help) || errors.length > 0) {
116
- const helpText = await `${await readFile(join(dirname(entrypointPath), './help.txt'), 'utf-8')}`;
117
- if (errors.length === 0) {
118
- console.log(helpText);
119
- } else {
120
- console.error(`${helpText}${errors.length === 0 ? '' : '\n'}`);
121
-
122
- process.exitCode ||= parseInt('1'.repeat(errors.length), 2);
123
- errors.forEach((error) => console.error(error));
124
- }
125
-
126
- process.exit();
127
- }
128
- }
129
-
130
- const { allowPositionals, minPositionals } = passedConfig;
131
-
132
- const posCount = typeof allowPositionals === 'number' ? allowPositionals : allowPositionals || subcommands ? Infinity : 0;
133
- if (results.positionals.length > posCount) {
134
- errors.push(`Only ${posCount} positional arguments allowed; got ${results.positionals.length}`);
135
- }
136
- if (!results.values.help && typeof minPositionals === 'number' && results.positionals.length < minPositionals) {
137
- errors.push(`At least ${posCount} positional arguments are required; got ${results.positionals.length}`);
138
- }
139
-
140
- const optionTokens = tokens.filter(/** @type {(token: typeof tokens[number]) => token is import('./pargs.mjs').OptionToken} */ (token) => token.kind === 'option');
141
-
142
- const bools = obj.options ? Object.entries(obj.options).filter(([, { type }]) => type === 'boolean') : [];
143
- const boolMap = new Map(bools.concat([['help', newObj.options.help]]));
144
- for (let i = 0; i < optionTokens.length; i += 1) {
145
- const { name, value } = optionTokens[i];
146
- if (boolMap.has(name) && typeof value !== 'boolean' && typeof value !== 'undefined') {
147
- errors.push(`Error: Argument --${name} must be a boolean`);
148
- }
149
- }
150
-
151
- const passedArgs = new Set(optionTokens.map(({ name, rawName }) => (rawName === '--no-help' ? rawName : name)));
152
-
153
- const groups = Object.groupBy(passedArgs, (x) => x.replace(/^no-/, ''));
154
- for (let i = 0; i < bools.length; i++) {
155
- const [key] = bools[i];
156
- if ((groups[key]?.length ?? 0) > 1) {
157
- errors.push(`Error: Arguments \`--${key}\` and \`--no-${key}\` are mutually exclusive`);
158
- }
159
- if (passedArgs.has(`no-${key}`)) {
160
- results.values[key] = !results.values[`no-${key}`];
161
- }
162
- delete results.values[`no-${key}`];
163
- }
164
-
165
- const knownOptions = Object.keys(newObj.options);
166
- const unknownArgs = knownOptions.length > 0 ? passedArgs.difference(new Set(knownOptions)) : passedArgs;
167
- if (unknownArgs.size > 0) {
168
- errors.push(`Error: Unknown option(s): ${Array.from(unknownArgs, (x) => `\`${x}\``).join(', ')}`);
169
- }
170
-
171
- /** @type {undefined | import('./pargs.mjs').PargsParsed<import('./pargs.mjs').PargsConfig>} */
172
- let command;
173
- if (subcommands) {
174
- const subcommand = argv[0];
175
- // eslint-disable-next-line no-extra-parens
176
- if (hasOwn(/** @type {object} */ (subcommands), subcommand)) {
177
- process.argv.splice(process.argv.indexOf(subcommand), 1);
178
- command = await pargs(entrypointPath, subcommands[subcommand]);
179
- } else {
180
- errors.push(`Error: unknown command "${command}"`);
181
- }
182
- }
183
-
184
- return {
185
- help,
186
- errors,
187
- ...results,
188
- ...command && {
189
- help: ('help' in command && command.help) || help,
190
- command: {
191
- name: argv[0],
192
- ...command,
193
- },
194
- },
195
- ...obj.tokens && { tokens },
196
- };
197
- } catch (e) {
198
- const fakeErrors = [`Error: ${!!e && typeof e === 'object' && 'message' in e && e.message}`];
199
- if (isParseArgsError(e)) {
200
- return {
201
- async help() {
202
- const helpText = await `${await readFile(join(dirname(entrypointPath), './help.txt'), 'utf-8')}`;
203
- console.error(`${helpText}'\n`);
204
-
205
- process.exitCode ||= parseInt('1', 2);
206
- console.error(fakeErrors[0]);
207
-
208
- process.exit();
209
- },
210
- values: {},
211
- positionals: [],
212
- errors: fakeErrors,
213
- };
214
- }
215
- throw e;
216
- }
217
- }