validate-npm-package-name-cli 1.0.0 → 2.0.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/.nycrc +12 -0
- package/CHANGELOG.md +10 -0
- package/bin.mjs +4 -4
- package/package.json +9 -21
- package/pargs.d.mts +0 -65
- package/pargs.mjs +0 -217
package/.nycrc
ADDED
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,16 @@ 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.0.0](https://github.com/ljharb/validate-npm-package-name-cli/compare/v1.0.0...v2.0.0) - 2025-10-31
|
|
9
|
+
|
|
10
|
+
### Commits
|
|
11
|
+
|
|
12
|
+
- [Refactor] use `pargs` package [`3a8ba69`](https://github.com/ljharb/validate-npm-package-name-cli/commit/3a8ba69027be1e45fafccb67367183d7341c6f8f)
|
|
13
|
+
- [Tests] extract coverage config to file [`9930ba4`](https://github.com/ljharb/validate-npm-package-name-cli/commit/9930ba4d1b2f79009d73e08acda5dc9a1f021a62)
|
|
14
|
+
- [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)
|
|
15
|
+
- [Breaking] update `engines.node` [`bcaddc4`](https://github.com/ljharb/validate-npm-package-name-cli/commit/bcaddc45999755d95026b473c900c495830ee418)
|
|
16
|
+
- [Deps] update `validate-npm-package-name` [`5557a25`](https://github.com/ljharb/validate-npm-package-name-cli/commit/5557a25fc1e046efe00569c90de4282fee79449e)
|
|
17
|
+
|
|
8
18
|
## v1.0.0 - 2025-02-26
|
|
9
19
|
|
|
10
20
|
### 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 '
|
|
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,
|
|
@@ -18,14 +18,14 @@ import validate from 'validate-npm-package-name';
|
|
|
18
18
|
const {
|
|
19
19
|
validForNewPackages,
|
|
20
20
|
warnings,
|
|
21
|
-
errors,
|
|
21
|
+
errors: validateErrors,
|
|
22
22
|
} = validate(packageName);
|
|
23
23
|
|
|
24
24
|
if (validForNewPackages) {
|
|
25
25
|
console.log(styleText('green', packageName));
|
|
26
26
|
} else {
|
|
27
27
|
warnings?.forEach((warning) => console.warn(styleText('yellow', warning)));
|
|
28
|
-
|
|
28
|
+
validateErrors?.forEach((error) => console.error(styleText('red', error)));
|
|
29
29
|
|
|
30
30
|
process.exitCode = 1;
|
|
31
31
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "validate-npm-package-name-cli",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.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,
|
|
@@ -40,13 +40,14 @@
|
|
|
40
40
|
},
|
|
41
41
|
"homepage": "https://github.com/ljharb/validate-npm-package-name-cli#readme",
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"
|
|
43
|
+
"pargs": "1.1",
|
|
44
|
+
"validate-npm-package-name": "^6.0.2"
|
|
44
45
|
},
|
|
45
46
|
"devDependencies": {
|
|
46
|
-
"@arethetypeswrong/cli": "^0.
|
|
47
|
-
"@ljharb/eslint-config": "^21.
|
|
48
|
-
"@ljharb/tsconfig": "^0.2
|
|
49
|
-
"@types/node": "^22.13
|
|
47
|
+
"@arethetypeswrong/cli": "^0.18.2",
|
|
48
|
+
"@ljharb/eslint-config": "^21.2.0",
|
|
49
|
+
"@ljharb/tsconfig": "^0.3.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",
|
|
@@ -57,23 +58,10 @@
|
|
|
57
58
|
"safe-publish-latest": "^2.0.0",
|
|
58
59
|
"tape": "^5.9.0",
|
|
59
60
|
"typescript": "next",
|
|
60
|
-
"undici-types": "^7.
|
|
61
|
+
"undici-types": "^7.16.0"
|
|
61
62
|
},
|
|
62
63
|
"engines": {
|
|
63
|
-
"node": "^22.
|
|
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
|
-
]
|
|
64
|
+
"node": "^22.20 || ^24.10 || >= 25"
|
|
77
65
|
},
|
|
78
66
|
"auto-changelog": {
|
|
79
67
|
"output": "CHANGELOG.md",
|
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
|
-
}
|