validate-npm-package-name-cli 1.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/.eslintrc +9 -0
- package/.github/FUNDING.yml +12 -0
- package/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +50 -0
- package/bin.mjs +31 -0
- package/help.txt +2 -0
- package/package.json +91 -0
- package/pargs.d.mts +65 -0
- package/pargs.mjs +217 -0
- package/test/bin.mjs +82 -0
- package/tsconfig.json +12 -0
package/.eslintrc
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# These are supported funding model platforms
|
|
2
|
+
|
|
3
|
+
github: [ljharb]
|
|
4
|
+
patreon: # Replace with a single Patreon username
|
|
5
|
+
open_collective: # Replace with a single Open Collective username
|
|
6
|
+
ko_fi: # Replace with a single Ko-fi username
|
|
7
|
+
tidelift: npm/validate-npm-package-name-cli
|
|
8
|
+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
|
9
|
+
liberapay: # Replace with a single Liberapay username
|
|
10
|
+
issuehunt: # Replace with a single IssueHunt username
|
|
11
|
+
otechie: # Replace with a single Otechie username
|
|
12
|
+
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## v1.0.0 - 2025-02-26
|
|
9
|
+
|
|
10
|
+
### Commits
|
|
11
|
+
|
|
12
|
+
- Initial implementation, tests, readme, types [`f124d4f`](https://github.com/ljharb/validate-npm-package-name-cli/commit/f124d4f6395fb7e9730150f86efdee7192a0d846)
|
|
13
|
+
- Initial commit [`d54f61b`](https://github.com/ljharb/validate-npm-package-name-cli/commit/d54f61b4a62960b2bb2fa1058928fe2a081a4899)
|
|
14
|
+
- npm init [`bb7b57e`](https://github.com/ljharb/validate-npm-package-name-cli/commit/bb7b57e5572d392aec1924eef5865670cab11d85)
|
|
15
|
+
- Only apps should have lockfiles [`19b86ee`](https://github.com/ljharb/validate-npm-package-name-cli/commit/19b86eecc5061fe286bab03bb8fee78c286de13d)
|
|
16
|
+
- [Tests] rename `require` due to TS bug [`4be4bf9`](https://github.com/ljharb/validate-npm-package-name-cli/commit/4be4bf91f1f7197b57058e84cc6a49941dc7f8a2)
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jordan Harband
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# validate-npm-package-name-cli <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
|
|
2
|
+
|
|
3
|
+
[![github actions][actions-image]][actions-url]
|
|
4
|
+
[![coverage][codecov-image]][codecov-url]
|
|
5
|
+
[![License][license-image]][license-url]
|
|
6
|
+
[![Downloads][downloads-image]][downloads-url]
|
|
7
|
+
|
|
8
|
+
[![npm badge][npm-badge-png]][package-url]
|
|
9
|
+
|
|
10
|
+
CLI for https://npmjs.com/validate-npm-package-name - give me a string and I'll tell you if it's a valid npm package name.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npx validate-npm-package-name-cli npm # if not installed
|
|
16
|
+
|
|
17
|
+
validate-npm-package-name-cli npm # if installed and in the PATH
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
$ validate-npm-package-name-cli --help
|
|
22
|
+
Usage:
|
|
23
|
+
validate-npm-package-name-cli <npm package specifier>
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
npm install --save-dev validate-npm-package-name-cli
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## License
|
|
33
|
+
|
|
34
|
+
MIT
|
|
35
|
+
|
|
36
|
+
[package-url]: https://npmjs.org/package/validate-npm-package-name-cli
|
|
37
|
+
[npm-version-svg]: https://versionbadg.es/ljharb/validate-npm-package-name-cli.svg
|
|
38
|
+
[deps-svg]: https://david-dm.org/ljharb/validate-npm-package-name-cli.svg
|
|
39
|
+
[deps-url]: https://david-dm.org/ljharb/validate-npm-package-name-cli
|
|
40
|
+
[dev-deps-svg]: https://david-dm.org/ljharb/validate-npm-package-name-cli/dev-status.svg
|
|
41
|
+
[dev-deps-url]: https://david-dm.org/ljharb/validate-npm-package-name-cli#info=devDependencies
|
|
42
|
+
[npm-badge-png]: https://nodei.co/npm/validate-npm-package-name-cli.png?downloads=true&stars=true
|
|
43
|
+
[license-image]: https://img.shields.io/npm/l/validate-npm-package-name-cli.svg
|
|
44
|
+
[license-url]: LICENSE
|
|
45
|
+
[downloads-image]: https://img.shields.io/npm/dm/validate-npm-package-name-cli.svg
|
|
46
|
+
[downloads-url]: https://npm-stat.com/charts.html?package=validate-npm-package-name-cli
|
|
47
|
+
[codecov-image]: https://codecov.io/gh/ljharb/validate-npm-package-name-cli/branch/main/graphs/badge.svg
|
|
48
|
+
[codecov-url]: https://app.codecov.io/gh/ljharb/validate-npm-package-name-cli/
|
|
49
|
+
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/validate-npm-package-name-cli
|
|
50
|
+
[actions-url]: https://github.com/ljharb/validate-npm-package-name-cli/actions
|
package/bin.mjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { styleText } from 'util';
|
|
4
|
+
import pargs from './pargs.mjs';
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
positionals: [packageName],
|
|
8
|
+
help,
|
|
9
|
+
} = await pargs(import.meta.filename, {
|
|
10
|
+
allowPositionals: 1,
|
|
11
|
+
minPositionals: 1,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
await help();
|
|
15
|
+
|
|
16
|
+
import validate from 'validate-npm-package-name';
|
|
17
|
+
|
|
18
|
+
const {
|
|
19
|
+
validForNewPackages,
|
|
20
|
+
warnings,
|
|
21
|
+
errors,
|
|
22
|
+
} = validate(packageName);
|
|
23
|
+
|
|
24
|
+
if (validForNewPackages) {
|
|
25
|
+
console.log(styleText('green', packageName));
|
|
26
|
+
} else {
|
|
27
|
+
warnings?.forEach((warning) => console.warn(styleText('yellow', warning)));
|
|
28
|
+
errors?.forEach((error) => console.error(styleText('red', error)));
|
|
29
|
+
|
|
30
|
+
process.exitCode = 1;
|
|
31
|
+
}
|
package/help.txt
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "validate-npm-package-name-cli",
|
|
3
|
+
"version": "1.0.0",
|
|
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
|
+
"bin": "./bin.mjs",
|
|
6
|
+
"main": false,
|
|
7
|
+
"exports": {
|
|
8
|
+
"./package.json": "./package.json"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"prepack": "npmignore --auto --commentLines=autogenerated",
|
|
12
|
+
"prepublish": "not-in-publish || npm run prepublishOnly",
|
|
13
|
+
"prepublishOnly": "safe-publish-latest",
|
|
14
|
+
"lint": "eslint --ext=js,mjs .",
|
|
15
|
+
"postlint": "tsc && attw -P",
|
|
16
|
+
"pretest": "npm run lint",
|
|
17
|
+
"tests-only": "c8 tape 'test/**/*.mjs'",
|
|
18
|
+
"test": "npm run tests-only",
|
|
19
|
+
"posttest": "npx npm@'>= 10.2' audit --production",
|
|
20
|
+
"version": "auto-changelog && git add CHANGELOG.md",
|
|
21
|
+
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/ljharb/validate-npm-package-name-cli.git"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"npm",
|
|
29
|
+
"package",
|
|
30
|
+
"name",
|
|
31
|
+
"names",
|
|
32
|
+
"valid",
|
|
33
|
+
"validate",
|
|
34
|
+
"validation"
|
|
35
|
+
],
|
|
36
|
+
"author": "Jordan Harband <ljharb@gmail.com>",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/ljharb/validate-npm-package-name-cli/issues"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/ljharb/validate-npm-package-name-cli#readme",
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"validate-npm-package-name": "^6.0.0"
|
|
44
|
+
},
|
|
45
|
+
"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",
|
|
50
|
+
"@types/tape": "^5.8.1",
|
|
51
|
+
"@types/validate-npm-package-name": "^4.0.2",
|
|
52
|
+
"auto-changelog": "^2.5.0",
|
|
53
|
+
"c8": "^10.1.3",
|
|
54
|
+
"eslint": "=8.8.0",
|
|
55
|
+
"in-publish": "^2.0.1",
|
|
56
|
+
"npmignore": "^0.3.1",
|
|
57
|
+
"safe-publish-latest": "^2.0.0",
|
|
58
|
+
"tape": "^5.9.0",
|
|
59
|
+
"typescript": "next",
|
|
60
|
+
"undici-types": "^7.3.0"
|
|
61
|
+
},
|
|
62
|
+
"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
|
+
]
|
|
77
|
+
},
|
|
78
|
+
"auto-changelog": {
|
|
79
|
+
"output": "CHANGELOG.md",
|
|
80
|
+
"template": "keepachangelog",
|
|
81
|
+
"unreleased": false,
|
|
82
|
+
"commitLimit": false,
|
|
83
|
+
"backfillLimit": false,
|
|
84
|
+
"hideCredit": true
|
|
85
|
+
},
|
|
86
|
+
"publishConfig": {
|
|
87
|
+
"ignore": [
|
|
88
|
+
".github/workflows"
|
|
89
|
+
]
|
|
90
|
+
}
|
|
91
|
+
}
|
package/pargs.d.mts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
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
|
+
}
|
package/test/bin.mjs
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import test from 'tape';
|
|
2
|
+
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { promisify, styleText } from 'util';
|
|
5
|
+
import { execFile as execFileC } from 'child_process';
|
|
6
|
+
import { createRequire } from 'module';
|
|
7
|
+
|
|
8
|
+
const requireX = createRequire(import.meta.url);
|
|
9
|
+
const pkgJSON = requireX('../package.json');
|
|
10
|
+
|
|
11
|
+
const execFile = promisify(execFileC);
|
|
12
|
+
|
|
13
|
+
const { PATH, DEBUG } = process.env;
|
|
14
|
+
|
|
15
|
+
const bin = join(import.meta.dirname, '../bin.mjs');
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {test.Test} t
|
|
19
|
+
* @param {string} pkg
|
|
20
|
+
* @param {{ code?: number, out?: string, err?: string, outC?: string, errC?: string }} expected
|
|
21
|
+
*/
|
|
22
|
+
async function run(t, pkg, expected) {
|
|
23
|
+
t.test(`package: ${pkg}`, async (st) => {
|
|
24
|
+
const [
|
|
25
|
+
{ stdout: outB, stderr: errB, code = 0 },
|
|
26
|
+
{ stdout: stdoutColorB, stderr: stderrColorB },
|
|
27
|
+
] = await Promise.all([
|
|
28
|
+
execFile(bin, [pkg], { env: { PATH, NO_COLORS: '1' } }),
|
|
29
|
+
execFile(bin, [pkg], { env: { PATH, FORCE_COLOR: '1' } }),
|
|
30
|
+
].map((x) => x.catch((e) => e)));
|
|
31
|
+
|
|
32
|
+
const stdout = `${outB}`.trim();
|
|
33
|
+
const stderr = `${errB}`.trim();
|
|
34
|
+
const stdoutColor = `${stdoutColorB}`.trim();
|
|
35
|
+
const stderrColor = `${stderrColorB}`.trim();
|
|
36
|
+
|
|
37
|
+
st.equal(code, Number(expected.code ?? 0), 'should have expected exit code');
|
|
38
|
+
|
|
39
|
+
const { out = '', err = '', outC = '', errC = '' } = expected;
|
|
40
|
+
if (DEBUG) {
|
|
41
|
+
`${stdout}`.split('\n').forEach((line, lineIndex) => {
|
|
42
|
+
st.equal(line, out.split('\n')[lineIndex], `should have expected output ${lineIndex}`);
|
|
43
|
+
});
|
|
44
|
+
`${stdoutColor}`.split('\n').forEach((line, lineIndex) => {
|
|
45
|
+
st.equal(line, outC.split('\n')[lineIndex], `should have expected output ${lineIndex}`);
|
|
46
|
+
});
|
|
47
|
+
} else {
|
|
48
|
+
st.equal(`${stdout}`, out, 'should have expected output');
|
|
49
|
+
st.equal(`${stderr}`, err, 'should have expected error output');
|
|
50
|
+
st.equal(`${stdoutColor}`, outC, 'should have expected color output');
|
|
51
|
+
st.equal(`${stderrColor}`, errC, 'should have expected color error output');
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
test('cli', async (t) => {
|
|
57
|
+
process.env.FORCE_COLOR = '1'; // for faucet etc
|
|
58
|
+
|
|
59
|
+
const y = /** @type {const} */ ([
|
|
60
|
+
'es-abstract',
|
|
61
|
+
'jsonstream',
|
|
62
|
+
...Object.keys(pkgJSON.dependencies),
|
|
63
|
+
...Object.keys(pkgJSON.devDependencies),
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
await Promise.all(y.map((pkg) => run(t, pkg, { out: pkg, outC: styleText('green', pkg) })));
|
|
67
|
+
|
|
68
|
+
const n = /** @type {const} */ ([
|
|
69
|
+
['foo bar', 'name can only contain URL-friendly characters'],
|
|
70
|
+
['JSONStream', '', 'name can no longer contain capital letters'],
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
await Promise.all(n.map(([
|
|
74
|
+
pkg,
|
|
75
|
+
errors,
|
|
76
|
+
warnings = '',
|
|
77
|
+
]) => run(t, pkg, {
|
|
78
|
+
code: 1,
|
|
79
|
+
err: `${warnings}\n${errors}`.split('\n').filter(Boolean).join('\n'),
|
|
80
|
+
errC: `${warnings && styleText('yellow', warnings)}\n${errors && styleText('red', errors)}`.split('\n').filter(Boolean).join('\n'),
|
|
81
|
+
})));
|
|
82
|
+
});
|