@pnpm/cli.parse-cli-args 1000.1.4
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/LICENSE +22 -0
- package/README.md +15 -0
- package/lib/index.d.ts +23 -0
- package/lib/index.js +189 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015-2016 Rico Sta. Cruz and other contributors
|
|
4
|
+
Copyright (c) 2016-2026 Zoltan Kochan and other contributors
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @pnpm/parse-cli-args
|
|
2
|
+
|
|
3
|
+
> Parses the CLI args passed to pnpm
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@pnpm/parse-cli-args)
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @pnpm/parse-cli-args
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## License
|
|
14
|
+
|
|
15
|
+
MIT
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface ParsedCliArgs {
|
|
2
|
+
argv: {
|
|
3
|
+
remain: string[];
|
|
4
|
+
cooked: string[];
|
|
5
|
+
original: string[];
|
|
6
|
+
};
|
|
7
|
+
params: string[];
|
|
8
|
+
options: Record<string, any>;
|
|
9
|
+
cmd: string | null;
|
|
10
|
+
unknownOptions: Map<string, string[]>;
|
|
11
|
+
fallbackCommandUsed: boolean;
|
|
12
|
+
workspaceDir: string | undefined;
|
|
13
|
+
}
|
|
14
|
+
export declare function parseCliArgs(opts: {
|
|
15
|
+
escapeArgs?: string[];
|
|
16
|
+
fallbackCommand?: string;
|
|
17
|
+
getCommandLongName: (commandName: string) => string | null;
|
|
18
|
+
getTypesByCommandName: (commandName: string) => object;
|
|
19
|
+
renamedOptions?: Record<string, string>;
|
|
20
|
+
shorthandsByCommandName: Record<string, Record<string, string | string[]>>;
|
|
21
|
+
universalOptionsTypes: Record<string, unknown>;
|
|
22
|
+
universalShorthands: Record<string, string | string[]>;
|
|
23
|
+
}, inputArgv: string[]): Promise<ParsedCliArgs>;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { PnpmError } from '@pnpm/error';
|
|
2
|
+
import nopt from '@pnpm/nopt';
|
|
3
|
+
import { findWorkspaceDir } from '@pnpm/workspace.root-finder';
|
|
4
|
+
import didYouMean, { ReturnTypeEnums } from 'didyoumean2';
|
|
5
|
+
const RECURSIVE_CMDS = new Set(['recursive', 'multi', 'm']);
|
|
6
|
+
const SPECIALLY_ESCAPED_CMDS = new Set(['run', 'dlx']);
|
|
7
|
+
export async function parseCliArgs(opts, inputArgv) {
|
|
8
|
+
const noptExploratoryResults = nopt({
|
|
9
|
+
filter: [String],
|
|
10
|
+
help: Boolean,
|
|
11
|
+
recursive: Boolean,
|
|
12
|
+
...opts.universalOptionsTypes,
|
|
13
|
+
...opts.getTypesByCommandName('add'),
|
|
14
|
+
...opts.getTypesByCommandName('install'),
|
|
15
|
+
}, {
|
|
16
|
+
r: '--recursive',
|
|
17
|
+
...opts.universalShorthands,
|
|
18
|
+
}, inputArgv, 0, { escapeArgs: opts.escapeArgs });
|
|
19
|
+
const recursiveCommandUsed = RECURSIVE_CMDS.has(noptExploratoryResults.argv.remain[0]);
|
|
20
|
+
let commandName = getCommandName(noptExploratoryResults.argv.remain);
|
|
21
|
+
let cmd = commandName ? opts.getCommandLongName(commandName) : null;
|
|
22
|
+
const fallbackCommandUsed = Boolean(commandName && !cmd && opts.fallbackCommand);
|
|
23
|
+
if (fallbackCommandUsed) {
|
|
24
|
+
cmd = opts.fallbackCommand;
|
|
25
|
+
commandName = opts.fallbackCommand;
|
|
26
|
+
inputArgv.unshift(opts.fallbackCommand);
|
|
27
|
+
// The run command has special casing for --help and is handled further below.
|
|
28
|
+
}
|
|
29
|
+
else if (!SPECIALLY_ESCAPED_CMDS.has(cmd)) {
|
|
30
|
+
if (noptExploratoryResults['help']) {
|
|
31
|
+
return {
|
|
32
|
+
...getParsedArgsForHelp(),
|
|
33
|
+
workspaceDir: await getWorkspaceDir(noptExploratoryResults),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
if (noptExploratoryResults['version'] || noptExploratoryResults['v']) {
|
|
37
|
+
return {
|
|
38
|
+
argv: noptExploratoryResults.argv,
|
|
39
|
+
cmd: null,
|
|
40
|
+
options: {
|
|
41
|
+
version: true,
|
|
42
|
+
},
|
|
43
|
+
params: noptExploratoryResults.argv.remain,
|
|
44
|
+
unknownOptions: new Map(),
|
|
45
|
+
fallbackCommandUsed: false,
|
|
46
|
+
workspaceDir: await getWorkspaceDir(noptExploratoryResults),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function getParsedArgsForHelp() {
|
|
51
|
+
return {
|
|
52
|
+
argv: noptExploratoryResults.argv,
|
|
53
|
+
cmd: 'help',
|
|
54
|
+
options: {},
|
|
55
|
+
params: noptExploratoryResults.argv.remain,
|
|
56
|
+
unknownOptions: new Map(),
|
|
57
|
+
fallbackCommandUsed: false,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
const types = {
|
|
61
|
+
...opts.universalOptionsTypes,
|
|
62
|
+
...opts.getTypesByCommandName(commandName),
|
|
63
|
+
}; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
64
|
+
function getCommandName(args) {
|
|
65
|
+
if (recursiveCommandUsed) {
|
|
66
|
+
args = args.slice(1);
|
|
67
|
+
}
|
|
68
|
+
if (opts.getCommandLongName(args[0]) !== 'install' || args.length === 1) {
|
|
69
|
+
return args[0];
|
|
70
|
+
}
|
|
71
|
+
return 'add';
|
|
72
|
+
}
|
|
73
|
+
function getEscapeArgsWithSpecialCases() {
|
|
74
|
+
if (!SPECIALLY_ESCAPED_CMDS.has(cmd)) {
|
|
75
|
+
return opts.escapeArgs;
|
|
76
|
+
}
|
|
77
|
+
// We'd like everything after the run script's name to be passed to the
|
|
78
|
+
// script's argv itself. For example, "pnpm run echo --test" should pass
|
|
79
|
+
// "--test" to the "echo" script. This requires determining the script's
|
|
80
|
+
// name and declaring it as the "escape arg".
|
|
81
|
+
//
|
|
82
|
+
// The name of the run script is normally the second argument (ex: pnpm
|
|
83
|
+
// run foo), but can be pushed back by recursive commands (ex: pnpm
|
|
84
|
+
// recursive run foo) or becomes the first argument when the fallback
|
|
85
|
+
// command (ex: pnpm foo) is set to 'run'.
|
|
86
|
+
const indexOfRunScriptName = 1 +
|
|
87
|
+
(recursiveCommandUsed ? 1 : 0) +
|
|
88
|
+
(fallbackCommandUsed && opts.fallbackCommand === 'run' ? -1 : 0);
|
|
89
|
+
return [noptExploratoryResults.argv.remain[indexOfRunScriptName]];
|
|
90
|
+
}
|
|
91
|
+
const { argv, ...options } = nopt({
|
|
92
|
+
recursive: Boolean,
|
|
93
|
+
...types,
|
|
94
|
+
}, {
|
|
95
|
+
...opts.universalShorthands,
|
|
96
|
+
...opts.shorthandsByCommandName[commandName],
|
|
97
|
+
}, inputArgv, 0, { escapeArgs: getEscapeArgsWithSpecialCases() });
|
|
98
|
+
const workspaceDir = await getWorkspaceDir(options);
|
|
99
|
+
// For the run command, it's not clear whether --help should be passed to the
|
|
100
|
+
// underlying script or invoke pnpm's help text until an additional nopt call.
|
|
101
|
+
if (SPECIALLY_ESCAPED_CMDS.has(cmd) && options['help']) {
|
|
102
|
+
return {
|
|
103
|
+
...getParsedArgsForHelp(),
|
|
104
|
+
workspaceDir,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
if (opts.renamedOptions != null) {
|
|
108
|
+
for (const [cliOption, optionValue] of Object.entries(options)) {
|
|
109
|
+
if (opts.renamedOptions[cliOption]) {
|
|
110
|
+
options[opts.renamedOptions[cliOption]] = optionValue;
|
|
111
|
+
delete options[cliOption];
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const params = argv.remain.slice(1);
|
|
116
|
+
if (options['recursive'] !== true && (options['filter'] || options['filter-prod'] || recursiveCommandUsed)) {
|
|
117
|
+
options['recursive'] = true;
|
|
118
|
+
const subCmd = argv.remain[1] && opts.getCommandLongName(argv.remain[1]);
|
|
119
|
+
if (subCmd && recursiveCommandUsed) {
|
|
120
|
+
params.shift();
|
|
121
|
+
argv.remain.shift();
|
|
122
|
+
cmd = subCmd;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (options['workspace-root']) {
|
|
126
|
+
if (options['global']) {
|
|
127
|
+
throw new PnpmError('OPTIONS_CONFLICT', '--workspace-root may not be used with --global');
|
|
128
|
+
}
|
|
129
|
+
if (!workspaceDir) {
|
|
130
|
+
throw new PnpmError('NOT_IN_WORKSPACE', '--workspace-root may only be used inside a workspace');
|
|
131
|
+
}
|
|
132
|
+
options['dir'] = workspaceDir;
|
|
133
|
+
}
|
|
134
|
+
if (cmd === 'install' && params.length > 0) {
|
|
135
|
+
cmd = 'add';
|
|
136
|
+
}
|
|
137
|
+
else if (!cmd && options['recursive']) {
|
|
138
|
+
cmd = 'recursive';
|
|
139
|
+
}
|
|
140
|
+
const knownOptions = new Set(Object.keys(types));
|
|
141
|
+
return {
|
|
142
|
+
argv,
|
|
143
|
+
cmd,
|
|
144
|
+
params,
|
|
145
|
+
workspaceDir,
|
|
146
|
+
fallbackCommandUsed,
|
|
147
|
+
...normalizeOptions(options, knownOptions),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
const CUSTOM_OPTION_PREFIX = 'config.';
|
|
151
|
+
function normalizeOptions(options, knownOptions) {
|
|
152
|
+
const standardOptionNames = [];
|
|
153
|
+
const normalizedOptions = {};
|
|
154
|
+
for (const [optionName, optionValue] of Object.entries(options)) {
|
|
155
|
+
if (optionName.startsWith(CUSTOM_OPTION_PREFIX)) {
|
|
156
|
+
normalizedOptions[optionName.substring(CUSTOM_OPTION_PREFIX.length)] = optionValue;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
normalizedOptions[optionName] = optionValue;
|
|
160
|
+
standardOptionNames.push(optionName);
|
|
161
|
+
}
|
|
162
|
+
const unknownOptions = getUnknownOptions(standardOptionNames, knownOptions);
|
|
163
|
+
return { options: normalizedOptions, unknownOptions };
|
|
164
|
+
}
|
|
165
|
+
function getUnknownOptions(usedOptions, knownOptions) {
|
|
166
|
+
const unknownOptions = new Map();
|
|
167
|
+
const closestMatches = getClosestOptionMatches.bind(null, Array.from(knownOptions));
|
|
168
|
+
for (const usedOption of usedOptions) {
|
|
169
|
+
if (knownOptions.has(usedOption) || usedOption.startsWith('//') || isScopeRegistryOption(usedOption))
|
|
170
|
+
continue;
|
|
171
|
+
unknownOptions.set(usedOption, closestMatches(usedOption));
|
|
172
|
+
}
|
|
173
|
+
return unknownOptions;
|
|
174
|
+
}
|
|
175
|
+
function isScopeRegistryOption(optionName) {
|
|
176
|
+
return /^@[a-z0-9][\w.-]*:registry$/.test(optionName);
|
|
177
|
+
}
|
|
178
|
+
function getClosestOptionMatches(knownOptions, option) {
|
|
179
|
+
return didYouMean(option, knownOptions, {
|
|
180
|
+
returnType: ReturnTypeEnums.ALL_CLOSEST_MATCHES,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
async function getWorkspaceDir(parsedOpts) {
|
|
184
|
+
if (parsedOpts['global'] || parsedOpts['ignore-workspace'])
|
|
185
|
+
return undefined;
|
|
186
|
+
const dir = parsedOpts['dir'] ?? process.cwd();
|
|
187
|
+
return findWorkspaceDir(dir);
|
|
188
|
+
}
|
|
189
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pnpm/cli.parse-cli-args",
|
|
3
|
+
"version": "1000.1.4",
|
|
4
|
+
"description": "Parses the CLI args passed to pnpm",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pnpm",
|
|
7
|
+
"pnpm11"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"funding": "https://opencollective.com/pnpm",
|
|
11
|
+
"repository": "https://github.com/pnpm/pnpm/tree/main/cli/parse-cli-args",
|
|
12
|
+
"homepage": "https://github.com/pnpm/pnpm/tree/main/cli/parse-cli-args#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/pnpm/pnpm/issues"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "lib/index.js",
|
|
18
|
+
"types": "lib/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": "./lib/index.js"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"lib",
|
|
24
|
+
"!*.map"
|
|
25
|
+
],
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@pnpm/nopt": "^0.3.1",
|
|
28
|
+
"didyoumean2": "^7.0.4",
|
|
29
|
+
"@pnpm/workspace.root-finder": "1000.1.3",
|
|
30
|
+
"@pnpm/error": "1000.0.5"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"tempy": "3.0.0",
|
|
34
|
+
"@pnpm/cli.parse-cli-args": "1000.1.4"
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=22.13"
|
|
38
|
+
},
|
|
39
|
+
"jest": {
|
|
40
|
+
"preset": "@pnpm/jest-config"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
|
|
44
|
+
"_test": "cross-env NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules --disable-warning=ExperimentalWarning --disable-warning=DEP0169\" jest",
|
|
45
|
+
"test": "pnpm run compile && pnpm run _test",
|
|
46
|
+
"start": "tsgo --watch",
|
|
47
|
+
"compile": "tsgo --build && pnpm run lint --fix"
|
|
48
|
+
}
|
|
49
|
+
}
|