package-json-scripts-portability 0.0.1 → 0.0.2
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/README.md +1 -1
- package/dist/analyze-script.d.mts +34 -0
- package/dist/analyze-script.mjs +48 -0
- package/dist/command-checker.mjs +295 -0
- package/dist/command-usage.mjs +364 -0
- package/dist/commands.mjs +520 -0
- package/dist/context.mjs +18 -0
- package/dist/fixes.d.mts +9 -0
- package/dist/fixes.mjs +210 -0
- package/dist/index.d.mts +6 -118
- package/dist/index.mjs +4 -3787
- package/dist/line-break-checker.mjs +71 -0
- package/dist/messages.mjs +30 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-difference.mjs +11 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-includes.mjs +8 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-intersection.mjs +13 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-is-subset.mjs +16 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/arrayify.mjs +7 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/iterable/sliding-window.mjs +30 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert-computed.mjs +42 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert.mjs +40 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/math/clamp.mjs +7 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/path/to-forward-slashes.mjs +8 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/predicate/is-array.mjs +39 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/set/set-has.mjs +8 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-after-last.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-before.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/truncate.mjs +46 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/ts/all-union-members.mjs +45 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/types/maybe-readonly-array.d.mts +17 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/value/is-equal.mjs +37 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/@antfu/utils/dist/index.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/globalThis.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/differenceBy.mjs +40 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/differenceWith.mjs +37 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/intersectionWith.mjs +43 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/isSubsetWith.mjs +36 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/bigint/clamp.mjs +15 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/getSymbols.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/getTag.mjs +13 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/regexMultiByte.mjs +10 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/tags.mjs +27 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isObject.mjs +28 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/string/truncate.mjs +78 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/eq.mjs +18 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/function/noop.mjs +12 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/math/clamp.mjs +23 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isBuffer.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isEqual.mjs +21 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isEqualWith.mjs +151 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isPlainObject.mjs +50 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/remeda/dist/isArray.mjs +18 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/ts-extras/distribution/array-includes.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/ts-extras/distribution/set-has.mjs +5 -0
- package/dist/platforms.d.mts +8 -0
- package/dist/platforms.mjs +26 -0
- package/dist/redirection-checker.mjs +117 -0
- package/dist/reporter.d.mts +32 -0
- package/dist/reporter.mjs +98 -0
- package/dist/runners.d.mts +24 -0
- package/dist/runners.mjs +118 -0
- package/dist/script-analyzer.mjs +551 -0
- package/dist/word-checker.mjs +444 -0
- package/dist/words.mjs +43 -0
- package/package.json +22 -20
package/README.md
CHANGED
|
@@ -38,7 +38,7 @@ pnpm i package-json-scripts-portability
|
|
|
38
38
|
|
|
39
39
|
Replace `pnpm i` with the installation command of your package manager.
|
|
40
40
|
|
|
41
|
-
Node.js 22
|
|
41
|
+
Node.js 22+ is required.
|
|
42
42
|
|
|
43
43
|
```ts
|
|
44
44
|
import {analyzeScript, applyFix, formatPlatforms} from 'package-json-scripts-portability';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ScriptIssue } from "./reporter.mjs";
|
|
2
|
+
import { Runner } from "./runners.mjs";
|
|
3
|
+
import { MaybeReadonlyArray } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/types/maybe-readonly-array.mjs";
|
|
4
|
+
export interface AnalyzeScriptOptions {
|
|
5
|
+
/** Names of the package's dependencies, e.g. to detect del-cli's `del` alias */
|
|
6
|
+
dependencies?: readonly string[];
|
|
7
|
+
/** Commands that shouldn't be reported */
|
|
8
|
+
ignoreCommands?: readonly string[];
|
|
9
|
+
/**
|
|
10
|
+
* What runs the script: a package manager, or `node` for `node --run`.
|
|
11
|
+
* It decides e.g. which shell runs the script and which environment variables it gets.
|
|
12
|
+
* Pass several runners to report what breaks with any of them.
|
|
13
|
+
* Defaults to `npm`
|
|
14
|
+
*/
|
|
15
|
+
runner?: MaybeReadonlyArray<Runner>;
|
|
16
|
+
/**
|
|
17
|
+
* Scripts that any package manager may run, which are checked with all of them instead of
|
|
18
|
+
* `runner`.
|
|
19
|
+
* `true` makes all scripts runner-agnostic, `false` none, and an object sets it for each script
|
|
20
|
+
* on top of the defaults: `preinstall`, `install`, `postinstall` and `prepare`
|
|
21
|
+
*/
|
|
22
|
+
runnerAgnosticScripts?: boolean | Readonly<Record<string, boolean>>;
|
|
23
|
+
/** The script's name in package.json, e.g. `postinstall`, to tell if it's runner-agnostic */
|
|
24
|
+
scriptName?: string;
|
|
25
|
+
/** Whether pnpm's `shell-emulator` setting is on, so that it runs the script with Yarn's shell */
|
|
26
|
+
shellEmulator?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Analyzes a package.json script for constructs that don't work (the same) on all platforms.
|
|
30
|
+
* @param script The script's source
|
|
31
|
+
* @param options Analysis options
|
|
32
|
+
* @returns The issues found, with offsets relative to the script
|
|
33
|
+
*/
|
|
34
|
+
export declare const analyzeScript: (script: string, options?: AnalyzeScriptOptions) => ScriptIssue[];
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { PLATFORMS } from "./platforms.mjs";
|
|
2
|
+
import { issueKey } from "./reporter.mjs";
|
|
3
|
+
import { PACKAGE_MANAGER_RUNNERS, isRunnerAgnosticScript } from "./runners.mjs";
|
|
4
|
+
import { analyzeWithRunners } from "./script-analyzer.mjs";
|
|
5
|
+
import { arrayIntersection } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-intersection.mjs";
|
|
6
|
+
import { arrayify } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/arrayify.mjs";
|
|
7
|
+
import { isEqual } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/value/is-equal.mjs";
|
|
8
|
+
// src/analyze-script.ts
|
|
9
|
+
const DEFAULT_RUNNERS = ["npm"];
|
|
10
|
+
/** A script that only consists of a comment line, i.e. documentation rather than a script */
|
|
11
|
+
const COMMENT_LINE = /^\s*#[^\n]*$/;
|
|
12
|
+
/**
|
|
13
|
+
* Merges the issues found with each runner, e.g. `rm` with both npm and pnpm.
|
|
14
|
+
* A merged issue is on the platforms where any of the runners has it, and keeps its fix only if
|
|
15
|
+
* all of them suggest the same one.
|
|
16
|
+
* @param issuesByRunner The issues found with each runner
|
|
17
|
+
* @returns The merged issues
|
|
18
|
+
*/
|
|
19
|
+
const mergeIssues = (issuesByRunner) => {
|
|
20
|
+
const merged = /* @__PURE__ */ new Map();
|
|
21
|
+
issuesByRunner.forEach((issues, runner) => {
|
|
22
|
+
issues.forEach((issue) => {
|
|
23
|
+
const key = issueKey(issue);
|
|
24
|
+
const existing = merged.get(key);
|
|
25
|
+
if (existing) {
|
|
26
|
+
existing.platforms = arrayIntersection(PLATFORMS, [...existing.platforms, ...issue.platforms]);
|
|
27
|
+
existing.runners.push(runner);
|
|
28
|
+
if (!isEqual(existing.fix, issue.fix)) existing.fix = void 0;
|
|
29
|
+
} else merged.set(key, {
|
|
30
|
+
...issue,
|
|
31
|
+
runners: [runner]
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
return [...merged.values()];
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Analyzes a package.json script for constructs that don't work (the same) on all platforms.
|
|
39
|
+
* @param script The script's source
|
|
40
|
+
* @param options Analysis options
|
|
41
|
+
* @returns The issues found, with offsets relative to the script
|
|
42
|
+
*/
|
|
43
|
+
const analyzeScript = (script, options = {}) => {
|
|
44
|
+
if (COMMENT_LINE.test(script)) return [];
|
|
45
|
+
const runners = isRunnerAgnosticScript(options.scriptName, options.runnerAgnosticScripts) ? PACKAGE_MANAGER_RUNNERS : arrayify(options.runner);
|
|
46
|
+
return mergeIssues(analyzeWithRunners(script, options, runners.length > 0 ? runners : DEFAULT_RUNNERS));
|
|
47
|
+
};
|
|
48
|
+
export { analyzeScript };
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { checkCommandUsage } from "./command-usage.mjs";
|
|
2
|
+
import { CMD_BUILTINS, COMMANDS, EXEC_SUBCOMMANDS, EXEC_VALUE_OPTIONS, PACKAGE_MANAGER_VALUE_OPTIONS, PORTABLE_SHELL_BUILTINS, WRAPPER_VALUE_OPTIONS } from "./commands.mjs";
|
|
3
|
+
import { withCovered } from "./context.mjs";
|
|
4
|
+
import { DEPENDENCY_BINARY, commandFix, pathCommandFix, replacement } from "./fixes.mjs";
|
|
5
|
+
import { NODE_SCRIPT_SUGGESTION, SHELL_SCRIPT_SUGGESTION, truncate } from "./messages.mjs";
|
|
6
|
+
import { LINUX_PLATFORMS, PLATFORMS, POSIX_PLATFORMS, WINDOWS_PLATFORMS } from "./platforms.mjs";
|
|
7
|
+
import { arrayDifference } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-difference.mjs";
|
|
8
|
+
import { arrayIncludes } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-includes.mjs";
|
|
9
|
+
import { setHas } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/set/set-has.mjs";
|
|
10
|
+
import { slidingWindow } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/iterable/sliding-window.mjs";
|
|
11
|
+
import { ASSIGNMENT, WHITESPACE, isPlainAssignment, isShortOptionCluster, staticValue } from "./words.mjs";
|
|
12
|
+
// src/command-checker.ts
|
|
13
|
+
const MAX_DEPTH = 10;
|
|
14
|
+
/** Where Yarn v1 replaces `/` with `\` in a command's name: at the start and after an operator */
|
|
15
|
+
const YARN_COMMAND_START = /(?:^|&&|&|\|\||\|)\s*$/;
|
|
16
|
+
/**
|
|
17
|
+
* Checks whether a name is a package spec rather than a path, e.g. `@scope/name` or
|
|
18
|
+
* `github:user/repo` (for `npx`)
|
|
19
|
+
*/
|
|
20
|
+
const isPackageSpec = (name) => name.startsWith("@") || name.includes(":");
|
|
21
|
+
const SHELL_SCRIPT = /\.(?:bash|command|sh|zsh)$/;
|
|
22
|
+
const WINDOWS_SCRIPT = /\.(?:bat|cmd|exe|ps1|vbs)$/i;
|
|
23
|
+
/** Exit statuses of commands that `exit` can replace at the end of a script */
|
|
24
|
+
const EXIT_STATUSES = /* @__PURE__ */ new Map([
|
|
25
|
+
[":", "0"],
|
|
26
|
+
["false", "1"],
|
|
27
|
+
["true", "0"]
|
|
28
|
+
]);
|
|
29
|
+
/** Options of npx and `npm exec` that take a shell string */
|
|
30
|
+
const CALL_OPTIONS = /* @__PURE__ */ new Set(["--call", "-c"]);
|
|
31
|
+
/** Options of pnpm and aube that make the command and its arguments a shell line */
|
|
32
|
+
const SHELL_MODE_OPTIONS = /* @__PURE__ */ new Set(["--shell-mode", "-c"]);
|
|
33
|
+
/**
|
|
34
|
+
* Finds the command that a command runs, e.g. `rm` in `cross-env A=1 rm -rf dist`.
|
|
35
|
+
* @param values The static values of the outer command's arguments
|
|
36
|
+
* @param start Index of the first argument to consider
|
|
37
|
+
* @param valueOptions Options of the outer command that take a value
|
|
38
|
+
* @param shouldSkipAssignments Whether to skip `NAME=value` arguments
|
|
39
|
+
* @returns Index of the nested command's name, or -1 if there is none
|
|
40
|
+
*/
|
|
41
|
+
const findCommandIndex = (values, start, valueOptions, shouldSkipAssignments = false) => {
|
|
42
|
+
for (let index = start; index < values.length; index++) {
|
|
43
|
+
const value = values[index];
|
|
44
|
+
if (value == null) return index;
|
|
45
|
+
if (value === "--") return index + 1;
|
|
46
|
+
if (value.startsWith("-") && value.length > 1) {
|
|
47
|
+
if (valueOptions.includes(value)) index++;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (!(shouldSkipAssignments && ASSIGNMENT.test(value))) return index;
|
|
51
|
+
}
|
|
52
|
+
return -1;
|
|
53
|
+
};
|
|
54
|
+
/** Checks commands, e.g. whether they're available everywhere, and the commands they run */
|
|
55
|
+
var CommandChecker = class {
|
|
56
|
+
#analyzeNestedWords;
|
|
57
|
+
#dependencies;
|
|
58
|
+
#ignoredCommands;
|
|
59
|
+
/** Fixes that depend on where a command is, by the command's name, e.g. `exit 0` for `true` */
|
|
60
|
+
#positionalFixes = /* @__PURE__ */ new Map();
|
|
61
|
+
#reporter;
|
|
62
|
+
constructor(reporter, { dependencies = [], ignoreCommands = [] }, analyzeNestedWords) {
|
|
63
|
+
this.#reporter = reporter;
|
|
64
|
+
this.#dependencies = new Set(dependencies);
|
|
65
|
+
this.#ignoredCommands = new Set(ignoreCommands);
|
|
66
|
+
this.#analyzeNestedWords = analyzeNestedWords;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Adds fixes for commands that end the script, which can change what follows them.
|
|
70
|
+
* @param statements Top-level statements of a script
|
|
71
|
+
*/
|
|
72
|
+
addPositionalFixes(statements) {
|
|
73
|
+
const last = statements.at(-1);
|
|
74
|
+
if (!last || last.background) return;
|
|
75
|
+
const list = last.command;
|
|
76
|
+
const commands = list.type === "AndOr" ? list.commands : [list];
|
|
77
|
+
const final = commands.at(-1);
|
|
78
|
+
if (final?.type === "Command" && final.name && final.prefix.length === 0 && final.suffix.length === 0) {
|
|
79
|
+
const status = EXIT_STATUSES.get(staticValue(final.name) || "");
|
|
80
|
+
if (status != null) this.#positionalFixes.set(final.name, replacement(final.name, final.name.text, `exit ${status}`));
|
|
81
|
+
}
|
|
82
|
+
const [exporting, command] = commands;
|
|
83
|
+
if (list.type === "AndOr" && list.operators.length === 1 && list.operators[0] === "&&" && exporting?.type === "Command" && exporting.name && staticValue(exporting.name) === "export" && exporting.prefix.length === 0 && exporting.suffix.length > 0 && exporting.suffix.every(isPlainAssignment) && command?.type === "Command" && command.name) this.#positionalFixes.set(exporting.name, {
|
|
84
|
+
description: "Replace `export ... &&` with `cross-env`.",
|
|
85
|
+
edits: [{
|
|
86
|
+
end: exporting.name.end,
|
|
87
|
+
pos: exporting.name.pos,
|
|
88
|
+
text: "cross-env"
|
|
89
|
+
}, {
|
|
90
|
+
end: command.pos,
|
|
91
|
+
pos: exporting.end,
|
|
92
|
+
text: " "
|
|
93
|
+
}]
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
#checkCommandAvailability(name, nameWord, argumentWords, context) {
|
|
97
|
+
if (name === "del" && this.#dependencies.has("del-cli")) return this.#reporter.report(context, nameWord, "inconsistentBehavior", {
|
|
98
|
+
reason: "cmd.exe's builtin `del` takes precedence over del-cli's `del` binary",
|
|
99
|
+
suggestion: "Use `del-cli` instead.",
|
|
100
|
+
syntax: "`del`"
|
|
101
|
+
}, WINDOWS_PLATFORMS, replacement(nameWord, nameWord.text, "del-cli"));
|
|
102
|
+
if (context.portable && PORTABLE_SHELL_BUILTINS.has(name)) return [];
|
|
103
|
+
const lowerCaseName = name.toLowerCase();
|
|
104
|
+
let info = COMMANDS.get(name);
|
|
105
|
+
if (!info) {
|
|
106
|
+
const windowsInfo = COMMANDS.get(lowerCaseName);
|
|
107
|
+
if (windowsInfo && !windowsInfo.platforms.includes("win32")) info = windowsInfo;
|
|
108
|
+
}
|
|
109
|
+
if (!info) return [];
|
|
110
|
+
let { platforms } = info;
|
|
111
|
+
if (info.bashBuiltin && context.shell === "bash") platforms = arrayDifference(platforms, LINUX_PLATFORMS);
|
|
112
|
+
if (context.portable && CMD_BUILTINS.has(lowerCaseName)) platforms = PLATFORMS;
|
|
113
|
+
return this.#reporter.report(context, nameWord, "unsupportedCommand", {
|
|
114
|
+
command: name,
|
|
115
|
+
suggestion: info.suggestion
|
|
116
|
+
}, platforms, this.#positionalFixes.get(nameWord) || commandFix(name, nameWord, argumentWords, context.source));
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Checks a command and the commands that it runs.
|
|
120
|
+
* @param nameWord The command's name, e.g. `rm`
|
|
121
|
+
* @param argumentWords The words that follow it
|
|
122
|
+
* @param context Where the command is
|
|
123
|
+
* @param depth How many commands run this command, e.g. 1 for `rm` in `cross-env A=1 rm`
|
|
124
|
+
* @param isCrossSpawned Whether cross-spawn runs the command (in `cross-env`), which works with
|
|
125
|
+
* paths and shebangs on Windows
|
|
126
|
+
* @returns What the command is, and what it runs
|
|
127
|
+
*/
|
|
128
|
+
checkInvocation(nameWord, argumentWords, context, depth, isCrossSpawned = false) {
|
|
129
|
+
const name = staticValue(nameWord);
|
|
130
|
+
const result = {
|
|
131
|
+
crossEnv: name === "cross-env" || name === "cross-env-shell",
|
|
132
|
+
name,
|
|
133
|
+
target: name
|
|
134
|
+
};
|
|
135
|
+
if (name == null || depth > MAX_DEPTH) return result;
|
|
136
|
+
if (this.#ignoredCommands.has(name)) {
|
|
137
|
+
result.target = this.#checkWrapped(name, argumentWords, context, depth)?.target ?? name;
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
const covered = [];
|
|
141
|
+
if (SHELL_SCRIPT.test(name)) covered.push(...this.#reporter.report(context, nameWord, "unsupportedCommand", {
|
|
142
|
+
command: truncate(nameWord.text),
|
|
143
|
+
suggestion: SHELL_SCRIPT_SUGGESTION
|
|
144
|
+
}, WINDOWS_PLATFORMS));
|
|
145
|
+
else if (WINDOWS_SCRIPT.test(name)) covered.push(...this.#reporter.report(context, nameWord, "unsupportedCommand", {
|
|
146
|
+
command: truncate(nameWord.text),
|
|
147
|
+
suggestion: NODE_SCRIPT_SUGGESTION
|
|
148
|
+
}, POSIX_PLATFORMS));
|
|
149
|
+
else if (name.includes("/") && !isPackageSpec(name)) {
|
|
150
|
+
const isConverted = context.convertsSlashes && depth === 0 && DEPENDENCY_BINARY.test(name) && YARN_COMMAND_START.test(context.source.slice(0, nameWord.pos));
|
|
151
|
+
if (!isCrossSpawned && !isConverted) covered.push(...this.#reporter.reportSyntax(context, nameWord, "posix", true, "unsupportedSyntax", {
|
|
152
|
+
suggestion: "cmd.exe treats `/` as an option prefix; run files with `node`, and binaries by their name instead.",
|
|
153
|
+
syntax: `Running a command by its path (\`${truncate(nameWord.text)}\`)`
|
|
154
|
+
}, pathCommandFix(name, nameWord)));
|
|
155
|
+
} else covered.push(...this.#checkCommandAvailability(name, nameWord, argumentWords, context));
|
|
156
|
+
covered.push(...this.#checkUsage(name, nameWord, argumentWords, context));
|
|
157
|
+
result.target = this.#checkWrapped(name, argumentWords, withCovered(context, covered), depth)?.target ?? name;
|
|
158
|
+
return result;
|
|
159
|
+
}
|
|
160
|
+
#checkUsage(name, nameWord, argumentWords, context) {
|
|
161
|
+
const values = argumentWords.map(staticValue);
|
|
162
|
+
const wrapperValueOptions = WRAPPER_VALUE_OPTIONS.get(name);
|
|
163
|
+
const commandIndex = wrapperValueOptions ? findCommandIndex(values, 0, wrapperValueOptions) : -1;
|
|
164
|
+
const optionsEnd = Math.min(values.includes("--") ? values.indexOf("--") : values.length, commandIndex === -1 ? values.length : commandIndex);
|
|
165
|
+
return checkCommandUsage(name, argumentWords.map((argument, index) => ({
|
|
166
|
+
text: argument.text,
|
|
167
|
+
value: values[index]
|
|
168
|
+
})), {
|
|
169
|
+
bash: context.shell === "bash",
|
|
170
|
+
optionsEnd,
|
|
171
|
+
portable: context.portable
|
|
172
|
+
}).flatMap(({ argumentIndex, data, fix, messageId, platforms }) => {
|
|
173
|
+
const fixedWord = fix && argumentWords[fix.argumentIndex];
|
|
174
|
+
return this.#reporter.report(context, argumentIndex == null ? nameWord : {
|
|
175
|
+
end: argumentWords[argumentIndex]?.end || nameWord.end,
|
|
176
|
+
pos: nameWord.pos
|
|
177
|
+
}, messageId, data, platforms, fix && fixedWord ? {
|
|
178
|
+
description: fix.description,
|
|
179
|
+
edits: [{
|
|
180
|
+
end: fixedWord.end,
|
|
181
|
+
pos: fixedWord.pos,
|
|
182
|
+
text: fix.text
|
|
183
|
+
}]
|
|
184
|
+
} : void 0);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
#checkWrapped(name, argumentWords, context, depth) {
|
|
188
|
+
const values = argumentWords.map(staticValue);
|
|
189
|
+
const invoke = (index, nestedContext = context, isCrossSpawned = false) => {
|
|
190
|
+
const word = argumentWords[index];
|
|
191
|
+
return !word || values[index] === "." ? void 0 : this.checkInvocation(word, argumentWords.slice(index + 1), nestedContext, depth + 1, isCrossSpawned);
|
|
192
|
+
};
|
|
193
|
+
const nestedShellString = (index, shell = "native", bashOptions) => {
|
|
194
|
+
const word = argumentWords[index];
|
|
195
|
+
if (word && depth < MAX_DEPTH) this.#analyzeNestedWords([word], context, {
|
|
196
|
+
bashOptions,
|
|
197
|
+
crossEnv: false,
|
|
198
|
+
maskScriptGlobs: name === "concurrently",
|
|
199
|
+
shell
|
|
200
|
+
});
|
|
201
|
+
};
|
|
202
|
+
const nestedShellLine = (index, isCrossEnv = false) => {
|
|
203
|
+
if (index !== -1 && depth < MAX_DEPTH) this.#analyzeNestedWords(argumentWords.slice(index), context, {
|
|
204
|
+
crossEnv: isCrossEnv,
|
|
205
|
+
shell: "native"
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
const commandStringIndex = () => {
|
|
209
|
+
const index = values.findIndex((value) => isShortOptionCluster(value, "c"));
|
|
210
|
+
return index === -1 ? -1 : index + 1;
|
|
211
|
+
};
|
|
212
|
+
const callIndex = (start, valueOptions) => {
|
|
213
|
+
const commandIndex = findCommandIndex(values, start, [...valueOptions, ...CALL_OPTIONS]);
|
|
214
|
+
const end = commandIndex === -1 ? values.length : commandIndex;
|
|
215
|
+
const index = values.findIndex((value, i) => i >= start && i < end && setHas(CALL_OPTIONS, value));
|
|
216
|
+
return index === -1 ? -1 : index + 1;
|
|
217
|
+
};
|
|
218
|
+
const shellModeIndex = (start, valueOptions) => {
|
|
219
|
+
const commandIndex = findCommandIndex(values, start, valueOptions);
|
|
220
|
+
return values.slice(start, commandIndex === -1 ? values.length : commandIndex).some((value) => setHas(SHELL_MODE_OPTIONS, value)) ? commandIndex : -1;
|
|
221
|
+
};
|
|
222
|
+
const valueOptions = WRAPPER_VALUE_OPTIONS.get(name);
|
|
223
|
+
switch (name) {
|
|
224
|
+
case "bash":
|
|
225
|
+
case "ksh":
|
|
226
|
+
case "zsh":
|
|
227
|
+
nestedShellString(commandStringIndex(), "bash", new Set(slidingWindow(values, 2).flatMap(([option, value]) => option === "-O" && value ? [value] : [])));
|
|
228
|
+
return;
|
|
229
|
+
case "concurrently":
|
|
230
|
+
argumentWords.forEach((argument, index) => {
|
|
231
|
+
if (argument.parts?.some((part) => part.type === "DoubleQuoted" || part.type === "SingleQuoted") && WHITESPACE.test(argument.value)) nestedShellString(index);
|
|
232
|
+
});
|
|
233
|
+
return;
|
|
234
|
+
case "cross-env":
|
|
235
|
+
case "cross-env-shell": {
|
|
236
|
+
const index = values.findIndex((value) => value == null || !ASSIGNMENT.test(value));
|
|
237
|
+
if (name === "cross-env") return invoke(index, {
|
|
238
|
+
...context,
|
|
239
|
+
crossEnv: true
|
|
240
|
+
}, true);
|
|
241
|
+
nestedShellLine(index, true);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
case "dash":
|
|
245
|
+
case "sh":
|
|
246
|
+
nestedShellString(commandStringIndex(), "sh");
|
|
247
|
+
return;
|
|
248
|
+
case "aube":
|
|
249
|
+
case "bun":
|
|
250
|
+
case "npm":
|
|
251
|
+
case "pnpm":
|
|
252
|
+
case "upm":
|
|
253
|
+
case "yarn": {
|
|
254
|
+
const subcommand = findCommandIndex(values, 0, PACKAGE_MANAGER_VALUE_OPTIONS);
|
|
255
|
+
if (!arrayIncludes(EXEC_SUBCOMMANDS[name], values[subcommand])) return;
|
|
256
|
+
if (name === "npm" || name === "upm") {
|
|
257
|
+
const call = callIndex(subcommand + 1, EXEC_VALUE_OPTIONS);
|
|
258
|
+
if (call !== -1) {
|
|
259
|
+
nestedShellString(call);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
} else if (name === "aube" || name === "pnpm") {
|
|
263
|
+
const shellLine = shellModeIndex(subcommand + 1, EXEC_VALUE_OPTIONS);
|
|
264
|
+
if (shellLine !== -1) {
|
|
265
|
+
nestedShellLine(shellLine);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return invoke(findCommandIndex(values, subcommand + 1, EXEC_VALUE_OPTIONS));
|
|
270
|
+
}
|
|
271
|
+
case "npx":
|
|
272
|
+
case "upx": {
|
|
273
|
+
const call = callIndex(0, valueOptions || []);
|
|
274
|
+
if (call !== -1) {
|
|
275
|
+
nestedShellString(call);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
case "aubx":
|
|
281
|
+
case "pnpx": {
|
|
282
|
+
const shellLine = shellModeIndex(0, valueOptions || []);
|
|
283
|
+
if (shellLine !== -1) {
|
|
284
|
+
nestedShellLine(shellLine);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (!valueOptions) return;
|
|
291
|
+
const commandIndex = findCommandIndex(values, 0, valueOptions, name === "env");
|
|
292
|
+
return name === "command" && values.slice(0, commandIndex).some((value) => value === "-v" || value === "-V") ? void 0 : invoke(commandIndex);
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
export { CommandChecker };
|