package-json-scripts-portability 0.0.2 → 0.0.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.
Files changed (71) hide show
  1. package/README.md +60 -19
  2. package/dist/analyze-script.d.mts +10 -3
  3. package/dist/analyze-script.mjs +24 -15
  4. package/dist/command-checker.mjs +331 -99
  5. package/dist/command-usage.mjs +85 -36
  6. package/dist/commands.mjs +124 -5
  7. package/dist/context.mjs +8 -1
  8. package/dist/fixes.d.mts +1 -1
  9. package/dist/fixes.mjs +193 -49
  10. package/dist/line-break-checker.mjs +40 -7
  11. package/dist/messages.mjs +9 -1
  12. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/sorted-array-insertion-index-with.mjs +33 -0
  13. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/sorted-array-insertion-index.mjs +10 -0
  14. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/semver/is-semver-in-range.mjs +5 -0
  15. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/semver/parse-semver.mjs +26 -0
  16. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-after.mjs +24 -0
  17. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/isUnsafeProperty.mjs +18 -0
  18. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/setOwnValue.mjs +23 -0
  19. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/toPropertyKey.mjs +16 -0
  20. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/baseToString.mjs +26 -0
  21. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/hasOwn.mjs +13 -0
  22. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/isDeepKey.mjs +39 -0
  23. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/isIndex.mjs +10 -0
  24. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/tags.mjs +2 -1
  25. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/toKey.mjs +15 -0
  26. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedIndex.mjs +36 -0
  27. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedIndexBy.mjs +54 -0
  28. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedLastIndex.mjs +34 -0
  29. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedLastIndexBy.mjs +21 -0
  30. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/function/identity.mjs +5 -0
  31. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/cloneDeep.mjs +52 -0
  32. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/cloneDeepWith.mjs +179 -0
  33. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/get.mjs +67 -0
  34. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/has.mjs +53 -0
  35. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/property.mjs +26 -0
  36. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isArguments.mjs +26 -0
  37. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isBuffer.mjs +24 -0
  38. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isMatch.mjs +34 -0
  39. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isMatchWith.mjs +163 -0
  40. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isNaN.mjs +18 -0
  41. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isNil.mjs +24 -0
  42. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isNumber.mjs +24 -0
  43. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isObjectLike.mjs +28 -0
  44. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isSymbol.mjs +19 -0
  45. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isTypedArray.mjs +21 -0
  46. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/matches.mjs +36 -0
  47. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/matchesProperty.mjs +47 -0
  48. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/iteratee.mjs +46 -0
  49. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/toPath.mjs +75 -0
  50. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/toString.mjs +23 -0
  51. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/function/identity.mjs +24 -0
  52. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isEqualWith.mjs +1 -2
  53. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isNil.mjs +24 -0
  54. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isNull.mjs +25 -0
  55. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isPrimitive.mjs +26 -0
  56. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isSymbol.mjs +28 -0
  57. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isTypedArray.mjs +20 -0
  58. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isUndefined.mjs +25 -0
  59. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/remeda/dist/binarySearchCutoffIndex-Bu-OMJ0Z.mjs +26 -0
  60. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/remeda/dist/sortedIndexWith.mjs +7 -0
  61. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/verkit/dist/comparison-CmVirWIW.mjs +108 -0
  62. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/verkit/dist/range-C5wjdo9a.mjs +164 -0
  63. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/verkit/dist/set-BGFWKKE8.mjs +38 -0
  64. package/dist/redirection-checker.mjs +76 -24
  65. package/dist/reporter.mjs +63 -18
  66. package/dist/runners.d.mts +2 -5
  67. package/dist/runners.mjs +103 -22
  68. package/dist/script-analyzer.mjs +457 -155
  69. package/dist/word-checker.mjs +232 -103
  70. package/dist/words.mjs +34 -7
  71. package/package.json +27 -23
@@ -1,23 +1,73 @@
1
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";
2
+ import { CMD_BUILTINS, COMMANDS, EXACT_NAME_BUILTINS, EXEC_SUBCOMMANDS, EXEC_VALUE_OPTIONS, NON_PORTABLE_BUILTINS, NPM_EXEC_VALUE_OPTIONS, NPM_VALUE_OPTIONS, OS_COMMANDS, PACKAGE_MANAGER_VALUE_OPTIONS, PORTABLE_SHELL_BUILTINS, POSIX_SHELL_BUILTINS, SHELL_STATE_COMMANDS, TIMEOUT_VALUE_OPTIONS, WRAPPER_VALUE_OPTIONS } from "./commands.mjs";
3
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";
4
+ import { DEPENDENCY_BINARY, commandFix, crossEnvEdits, pathCommandFix, replacement } from "./fixes.mjs";
5
+ import { NODE_SCRIPT_SUGGESTION, SHELL_SCRIPT_SUGGESTION, describeReplacement, truncate } from "./messages.mjs";
6
+ import { LINUX_PLATFORMS, NOT_LINUX_OR_WINDOWS, PLATFORMS, POSIX_PLATFORMS, WINDOWS_PLATFORMS } from "./platforms.mjs";
7
7
  import { arrayDifference } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-difference.mjs";
8
8
  import { arrayIncludes } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-includes.mjs";
9
+ import { arrayIntersection } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-intersection.mjs";
9
10
  import { setHas } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/set/set-has.mjs";
10
11
  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*$/;
12
+ import { substringBefore } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-before.mjs";
13
+ import { ASSIGNMENT, WHITESPACE, doesMentionVariables, isPlainAssignment, isShortOptionCluster, staticValue } from "./words.mjs";
14
+ /**
15
+ * Checks whether Yarn v1 replaces `/` with `\` in a command's name: at the start of the script and
16
+ * after an operator, i.e. `&&`, `&`, `||` or `|`.
17
+ * @param source The source that the name is in
18
+ * @param position Where the name starts in the source
19
+ * @returns Whether it does
20
+ */
21
+ const isYarnCommandStart = (source, position) => {
22
+ let index = position;
23
+ while (index > 0 && WHITESPACE.test(source[index - 1] || "")) index--;
24
+ const previous = source[index - 1];
25
+ return index === 0 || previous === "&" || previous === "|";
26
+ };
27
+ /**
28
+ * Checks whether a command's name is in the wrong case, e.g. `MKDIR` for `mkdir`.
29
+ * @param name Name that the command was invoked with
30
+ * @returns Whether only its lower case is a known OS command
31
+ */
32
+ const isWrongCase = (name) => {
33
+ const lowerCaseName = name.toLowerCase();
34
+ return name !== lowerCaseName && !COMMANDS.has(name) && OS_COMMANDS.has(lowerCaseName);
35
+ };
36
+ /**
37
+ * Gets the platforms where a command in the wrong case is missing.
38
+ * @param lowerCaseName The command's name in lower case
39
+ * @param platforms Where the command in lower case is missing
40
+ * @param context Where the command is
41
+ * @returns The platforms
42
+ */
43
+ const wrongCasePlatforms = (lowerCaseName, platforms, context) => PLATFORMS.filter((platform) => platform === "linux" || platforms.includes(platform) || platform === "darwin" && EXACT_NAME_BUILTINS.has(lowerCaseName) || platform === "win32" && context.portable && PORTABLE_SHELL_BUILTINS.has(lowerCaseName));
44
+ /**
45
+ * Gets the text of a `cross-env` argument to tell whether it sets a variable, e.g. `B=$A`.
46
+ * @param word The argument
47
+ * @param value Its static value
48
+ * @returns The value, or the text if the value isn't static
49
+ */
50
+ const crossEnvArgumentText = (word, value) => value ?? word.text;
51
+ /**
52
+ * Finds the command that `cross-env` runs, after the variables that it sets.
53
+ * @param argumentWords Its arguments
54
+ * @param values Their static values
55
+ * @returns Index of the command's name, or -1 if there is none
56
+ */
57
+ const crossEnvCommandIndex = (argumentWords, values) => argumentWords.findIndex((word, index) => !ASSIGNMENT.test(crossEnvArgumentText(word, values[index])));
16
58
  /**
17
59
  * Checks whether a name is a package spec rather than a path, e.g. `@scope/name` or
18
60
  * `github:user/repo` (for `npx`)
19
61
  */
20
62
  const isPackageSpec = (name) => name.startsWith("@") || name.includes(":");
63
+ /** Commands that run a package's binary, whose first argument is a package spec, e.g. `npx` */
64
+ const PACKAGE_SPEC_RUNNERS = /* @__PURE__ */ new Set([
65
+ "aubx",
66
+ "bunx",
67
+ "npx",
68
+ "pnpx",
69
+ "upx"
70
+ ]);
21
71
  const SHELL_SCRIPT = /\.(?:bash|command|sh|zsh)$/;
22
72
  const WINDOWS_SCRIPT = /\.(?:bat|cmd|exe|ps1|vbs)$/i;
23
73
  /** Exit statuses of commands that `exit` can replace at the end of a script */
@@ -26,10 +76,58 @@ const EXIT_STATUSES = /* @__PURE__ */ new Map([
26
76
  ["false", "1"],
27
77
  ["true", "0"]
28
78
  ]);
29
- /** Options of npx and `npm exec` that take a shell string */
79
+ /** Commands that change the directory of the commands that follow */
80
+ const DIRECTORY_COMMANDS = /* @__PURE__ */ new Set(["cd", "pushd"]);
81
+ /** Options of `npx` and `npm exec` that take a shell string */
30
82
  const CALL_OPTIONS = /* @__PURE__ */ new Set(["--call", "-c"]);
83
+ /** The shell string's option of `npx` and `npm exec` with an attached value */
84
+ const CALL_PREFIX = "--call=";
85
+ /**
86
+ * Gets the rest of a word after a prefix of plain text, e.g. the script in `--call='rm -rf x'`.
87
+ * @param word The word, whose text starts with the prefix
88
+ * @param length Length of the prefix
89
+ * @returns A word with the rest of the text and parts
90
+ */
91
+ const wordAfter = (word, length) => {
92
+ const pos = word.pos + length;
93
+ const [first, ...rest] = word.parts || [];
94
+ return {
95
+ end: word.end,
96
+ parts: first?.type === "Literal" && first.end > pos ? [{
97
+ ...first,
98
+ pos,
99
+ text: first.text.slice(length),
100
+ value: first.value.slice(length)
101
+ }, ...rest] : first && rest,
102
+ pos,
103
+ text: word.text.slice(length),
104
+ type: "Word",
105
+ value: word.value.slice(length)
106
+ };
107
+ };
31
108
  /** Options of pnpm and aube that make the command and its arguments a shell line */
32
109
  const SHELL_MODE_OPTIONS = /* @__PURE__ */ new Set(["--shell-mode", "-c"]);
110
+ const SHELL_OPTION = /^[+-]./;
111
+ /** A shell option that takes a value, also at the end of a cluster, e.g. `-o` in `-euo pipefail` */
112
+ const SHELL_VALUE_OPTION = /^(?:[+-][a-z]*o|--init-file|--rcfile)$/i;
113
+ /**
114
+ * Finds the script of a shell's `-c` option, e.g. in `sh -c "..."` or `sh -c -e "..."`.
115
+ * The shell's options end at the script file, whose arguments are its own, e.g. `-c` in
116
+ * `sh build.sh -c x`.
117
+ * @param values The static values of the shell's arguments
118
+ * @returns Index of the script, or -1 if there is none
119
+ */
120
+ const findCommandString = (values) => {
121
+ let index = 0;
122
+ let hasCommandOption = false;
123
+ while (index < values.length) {
124
+ const value = values[index];
125
+ if (value == null || !SHELL_OPTION.test(value)) return hasCommandOption ? index : -1;
126
+ hasCommandOption ||= isShortOptionCluster(value, "c");
127
+ index += SHELL_VALUE_OPTION.test(value) ? 2 : 1;
128
+ }
129
+ return -1;
130
+ };
33
131
  /**
34
132
  * Finds the command that a command runs, e.g. `rm` in `cross-env A=1 rm -rf dist`.
35
133
  * @param values The static values of the outer command's arguments
@@ -51,25 +149,54 @@ const findCommandIndex = (values, start, valueOptions, shouldSkipAssignments = f
51
149
  }
52
150
  return -1;
53
151
  };
152
+ /**
153
+ * Finds the issues with a command's options and arguments, e.g. `sed -i`.
154
+ * @param name Name that the command was invoked with
155
+ * @param argumentWords The words that follow it
156
+ * @param values Their static values
157
+ * @param context Where the command is
158
+ * @returns The issues, which aren't reported yet
159
+ */
160
+ const findUsageIssues = (name, argumentWords, values, context) => {
161
+ const wrapperValueOptions = WRAPPER_VALUE_OPTIONS.get(name);
162
+ const commandIndex = wrapperValueOptions ? findCommandIndex(values, 0, wrapperValueOptions) : -1;
163
+ const separator = values.indexOf("--");
164
+ const optionsEnd = Math.min(separator === -1 ? values.length : separator, commandIndex === -1 ? values.length : commandIndex);
165
+ const issues = checkCommandUsage(name, values, argumentWords, {
166
+ bash: context.shell === "bash",
167
+ native: context.shell === "native",
168
+ optionsEnd,
169
+ portable: context.portable
170
+ });
171
+ return isWrongCase(name) ? issues.map((issue) => issue.messageId === "unsupportedCommand" ? {
172
+ ...issue,
173
+ platforms: arrayIntersection(PLATFORMS, [...issue.platforms, ...LINUX_PLATFORMS])
174
+ } : issue) : issues;
175
+ };
54
176
  /** Checks commands, e.g. whether they're available everywhere, and the commands they run */
55
177
  var CommandChecker = class {
56
178
  #analyzeNestedWords;
57
- #dependencies;
58
- #ignoredCommands;
179
+ /** Sources in which a command changed the directory, e.g. with `cd`, by the walk so far */
180
+ #directoryChangeSources = /* @__PURE__ */ new Set();
181
+ /** Whether a script that the walked one is nested in changed the directory before it */
182
+ #isEnclosingDirectoryChanged = false;
183
+ #options;
59
184
  /** Fixes that depend on where a command is, by the command's name, e.g. `exit 0` for `true` */
60
185
  #positionalFixes = /* @__PURE__ */ new Map();
61
186
  #reporter;
62
- constructor(reporter, { dependencies = [], ignoreCommands = [] }, analyzeNestedWords) {
187
+ #runnerTraits;
188
+ constructor(reporter, options, analyzeNestedWords, runnerTraits) {
63
189
  this.#reporter = reporter;
64
- this.#dependencies = new Set(dependencies);
65
- this.#ignoredCommands = new Set(ignoreCommands);
190
+ this.#options = options;
66
191
  this.#analyzeNestedWords = analyzeNestedWords;
192
+ this.#runnerTraits = runnerTraits;
67
193
  }
68
194
  /**
69
195
  * Adds fixes for commands that end the script, which can change what follows them.
70
196
  * @param statements Top-level statements of a script
197
+ * @param source The script's source
71
198
  */
72
- addPositionalFixes(statements) {
199
+ addPositionalFixes(statements, source) {
73
200
  const last = statements.at(-1);
74
201
  if (!last || last.background) return;
75
202
  const list = last.command;
@@ -77,43 +204,100 @@ var CommandChecker = class {
77
204
  const final = commands.at(-1);
78
205
  if (final?.type === "Command" && final.name && final.prefix.length === 0 && final.suffix.length === 0) {
79
206
  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}`));
207
+ if (status != null) {
208
+ const text = `exit ${status}`;
209
+ this.#positionalFixes.set(final.name, {
210
+ description: status === "0" ? `Replace \`${truncate(final.name.text)}\` with \`${text}\`, which Bash rejects if the script gets extra arguments.` : describeReplacement(final.name.text, text),
211
+ edits: [{
212
+ end: final.name.end,
213
+ pos: final.name.pos,
214
+ text
215
+ }]
216
+ });
217
+ }
81
218
  }
82
219
  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
- });
220
+ const commandName = command?.type === "Command" && command.name && staticValue(command.name) || "";
221
+ 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 && !SHELL_STATE_COMMANDS.has(commandName) && !EXIT_STATUSES.has(commandName) && !doesMentionVariables(source.slice(command.pos, command.end), new Set(exporting.suffix.filter(isPlainAssignment).map(({ name }) => name)))) {
222
+ const edits = crossEnvEdits([...exporting.suffix, ...command.prefix.filter((item) => item.type === "Assignment")], [command.name, ...command.suffix.filter((item) => item.type === "Word")]);
223
+ if (edits) {
224
+ const closingQuote = edits.find((edit) => edit.pos === exporting.end);
225
+ this.#positionalFixes.set(exporting.name, {
226
+ description: "Replace `export ... &&` with `cross-env`.",
227
+ edits: [
228
+ {
229
+ end: exporting.name.end,
230
+ pos: exporting.name.pos,
231
+ text: "cross-env"
232
+ },
233
+ {
234
+ end: command.pos,
235
+ pos: exporting.end,
236
+ text: `${closingQuote?.text || ""} `
237
+ },
238
+ ...edits.filter((edit) => edit !== closingQuote)
239
+ ]
240
+ });
241
+ }
242
+ }
95
243
  }
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 [];
244
+ /**
245
+ * Reports a command that isn't available on all platforms.
246
+ * @param name Name that the command was invoked with
247
+ * @param nameWord Word that the name comes from
248
+ * @param argumentWords The words that follow it
249
+ * @param context Where the command is
250
+ * @param usageIssues The issues with the command's usage, e.g. on Linux for `TIMEOUT /T 5`
251
+ * @returns The platforms of the report
252
+ */
253
+ #checkCommandAvailability(name, nameWord, argumentWords, context, usageIssues) {
103
254
  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;
255
+ if (lowerCaseName === "del" && this.#options.dependencies.includes("del-cli")) {
256
+ const suggestion = "Use `del-cli` instead.";
257
+ const fix = replacement(nameWord, nameWord.text, "del-cli");
258
+ return [...context.portable ? [] : this.#reporter.report(context, nameWord, "inconsistentBehavior", {
259
+ reason: "cmd.exe's builtin `del` takes precedence over del-cli's `del` binary",
260
+ suggestion,
261
+ syntax: "`del`"
262
+ }, WINDOWS_PLATFORMS, fix), ...name === lowerCaseName ? [] : this.#reporter.report(context, nameWord, "unsupportedCommand", {
263
+ command: name,
264
+ suggestion
265
+ }, LINUX_PLATFORMS, fix)];
108
266
  }
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;
267
+ if (lowerCaseName === "type" && context.shell === "native" && !context.portable) {
268
+ const firstIssue = this.#reporter.issues.length;
269
+ const covered = this.#reporter.report(context, nameWord, "inconsistentBehavior", {
270
+ reason: "cmd.exe's `type` prints a file, and the `type` of POSIX shells tells what a command is",
271
+ suggestion: NODE_SCRIPT_SUGGESTION,
272
+ syntax: "`type`"
273
+ }, PLATFORMS);
274
+ this.#reporter.markFailures(firstIssue, POSIX_PLATFORMS);
275
+ return covered;
276
+ }
277
+ if (lowerCaseName === "type" && context.portable) return this.#reporter.report(context, nameWord, "unsupportedCommand", {
278
+ command: name,
279
+ suggestion: NODE_SCRIPT_SUGGESTION
280
+ }, WINDOWS_PLATFORMS);
281
+ if (context.portable && PORTABLE_SHELL_BUILTINS.has(name)) return [];
282
+ if (context.portable && NON_PORTABLE_BUILTINS.has(name)) {
283
+ const info = COMMANDS.get(name);
284
+ return this.#reporter.report(context, nameWord, "unsupportedCommand", {
285
+ command: name,
286
+ suggestion: !info?.bashBuiltin && info?.suggestion || "Use a Node.js script instead."
287
+ }, PLATFORMS, this.#positionalFixes.get(nameWord));
288
+ }
289
+ const isNameWrongCase = isWrongCase(name);
290
+ const info = COMMANDS.get(isNameWrongCase ? lowerCaseName : name);
291
+ if (!info && !isNameWrongCase) return [];
292
+ let platforms = info?.platforms || [];
293
+ if (isNameWrongCase) platforms = wrongCasePlatforms(lowerCaseName, platforms, context).filter((platform) => usageIssues.every((issue) => !issue.platforms.includes(platform)));
294
+ else if (info?.bashBuiltin && context.shell === "bash") platforms = arrayDifference(platforms, NOT_LINUX_OR_WINDOWS);
295
+ else if (context.shell !== "native" && POSIX_SHELL_BUILTINS.has(name)) platforms = arrayDifference(platforms, WINDOWS_PLATFORMS);
296
+ if (context.portable && CMD_BUILTINS.has(lowerCaseName)) platforms = arrayIntersection(PLATFORMS, [...platforms, ...WINDOWS_PLATFORMS]);
113
297
  return this.#reporter.report(context, nameWord, "unsupportedCommand", {
114
298
  command: name,
115
- suggestion: info.suggestion
116
- }, platforms, this.#positionalFixes.get(nameWord) || commandFix(name, nameWord, argumentWords, context.source));
299
+ suggestion: info?.suggestion || `Use \`${lowerCaseName}\` in lower case instead.`
300
+ }, platforms, this.#positionalFixes.get(nameWord) || commandFix(lowerCaseName, nameWord, argumentWords, context.source) || (info || lowerCaseName === "echo" ? void 0 : replacement(nameWord, nameWord.text, lowerCaseName)));
117
301
  }
118
302
  /**
119
303
  * Checks a command and the commands that it runs.
@@ -121,22 +305,52 @@ var CommandChecker = class {
121
305
  * @param argumentWords The words that follow it
122
306
  * @param context Where the command is
123
307
  * @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
308
+ * @param isCrossSpawned Whether `cross-spawn` runs the command (in `cross-env`), which works with
125
309
  * paths and shebangs on Windows
310
+ * @param isPackageSpecName Whether the name is a package spec that a package manager reads, e.g.
311
+ * `./packages/cli` in `npx ./packages/cli`, rather than a command that a shell runs
126
312
  * @returns What the command is, and what it runs
127
313
  */
128
- checkInvocation(nameWord, argumentWords, context, depth, isCrossSpawned = false) {
314
+ checkInvocation(nameWord, argumentWords, context, depth, isCrossSpawned = false, isPackageSpecName = false) {
129
315
  const name = staticValue(nameWord);
130
316
  const result = {
131
- crossEnv: name === "cross-env" || name === "cross-env-shell",
317
+ crossEnv: void 0,
132
318
  name,
133
319
  target: name
134
320
  };
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;
321
+ if (name == null || depth > 10) return result;
322
+ if (DIRECTORY_COMMANDS.has(name.toLowerCase())) this.#directoryChangeSources.add(context.source);
323
+ const values = argumentWords.map(staticValue);
324
+ if (name === "cross-env" || name === "cross-env-shell") {
325
+ const commandIndex = crossEnvCommandIndex(argumentWords, values);
326
+ result.crossEnv = {
327
+ commandStart: argumentWords[commandIndex]?.pos ?? Number.POSITIVE_INFINITY,
328
+ shell: name === "cross-env-shell",
329
+ start: nameWord.end,
330
+ variables: new Set(argumentWords.slice(0, commandIndex === -1 ? void 0 : commandIndex).map((word, index) => substringBefore(crossEnvArgumentText(word, values[index]), "=")))
331
+ };
332
+ }
333
+ const follow = (wrapped) => {
334
+ result.target = wrapped?.target ?? name;
335
+ result.crossEnv ||= wrapped?.crossEnv;
336
+ };
337
+ if (this.#options.ignoredCommands.has(name)) {
338
+ follow(this.#checkWrapped(name, argumentWords, values, context, depth));
339
+ return result;
340
+ }
341
+ const lowerCaseName = name.toLowerCase();
342
+ if (this.#options.ignoredCommands.has(lowerCaseName) && isWrongCase(name)) {
343
+ const firstIssue = this.#reporter.issues.length;
344
+ const covered = this.#reporter.report(context, nameWord, "unsupportedCommand", {
345
+ command: name,
346
+ suggestion: `Use \`${lowerCaseName}\` in lower case instead.`
347
+ }, wrongCasePlatforms(lowerCaseName, [], context), lowerCaseName === "echo" ? void 0 : replacement(nameWord, nameWord.text, lowerCaseName));
348
+ this.#reporter.markFailures(firstIssue);
349
+ follow(this.#checkWrapped(name, argumentWords, values, withCovered(context, covered), depth));
138
350
  return result;
139
351
  }
352
+ const firstIssue = this.#reporter.issues.length;
353
+ const usageIssues = findUsageIssues(name, argumentWords, values, context);
140
354
  const covered = [];
141
355
  if (SHELL_SCRIPT.test(name)) covered.push(...this.#reporter.report(context, nameWord, "unsupportedCommand", {
142
356
  command: truncate(nameWord.text),
@@ -146,30 +360,32 @@ var CommandChecker = class {
146
360
  command: truncate(nameWord.text),
147
361
  suggestion: NODE_SCRIPT_SUGGESTION
148
362
  }, 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));
363
+ else if (!isPackageSpecName && name.includes("/") && !isPackageSpec(name)) {
364
+ const isConverted = context.runnerScript && depth === 0 && DEPENDENCY_BINARY.test(name) && isYarnCommandStart(context.source, nameWord.pos) && this.#runnerTraits.get("convertsSlashes");
151
365
  if (!isCrossSpawned && !isConverted) covered.push(...this.#reporter.reportSyntax(context, nameWord, "posix", true, "unsupportedSyntax", {
152
366
  suggestion: "cmd.exe treats `/` as an option prefix; run files with `node`, and binaries by their name instead.",
153
367
  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;
368
+ }, pathCommandFix(name, nameWord, this.#isEnclosingDirectoryChanged || this.#directoryChangeSources.has(context.source))));
369
+ } else covered.push(...this.#checkCommandAvailability(name, nameWord, argumentWords, context, usageIssues));
370
+ covered.push(...this.#reportUsage(usageIssues, nameWord, argumentWords, context));
371
+ this.#reporter.markFailures(firstIssue);
372
+ follow(this.#checkWrapped(name, argumentWords, values, withCovered(context, covered), depth));
158
373
  return result;
159
374
  }
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 }) => {
375
+ /**
376
+ * Analyzes words that a command runs as a script, e.g. in `sh -c "..."`.
377
+ * @param words The words
378
+ * @param context Where the command is
379
+ * @param options How the script runs
380
+ */
381
+ #analyzeNested(words, context, options) {
382
+ const wasDirectoryChanged = this.#isEnclosingDirectoryChanged;
383
+ this.#isEnclosingDirectoryChanged ||= this.#directoryChangeSources.has(context.source);
384
+ this.#analyzeNestedWords(words, context, options);
385
+ this.#isEnclosingDirectoryChanged = wasDirectoryChanged;
386
+ }
387
+ #reportUsage(issues, nameWord, argumentWords, context) {
388
+ return issues.flatMap(({ argumentIndex, data, fix, messageId, platforms }) => {
173
389
  const fixedWord = fix && argumentWords[fix.argumentIndex];
174
390
  return this.#reporter.report(context, argumentIndex == null ? nameWord : {
175
391
  end: argumentWords[argumentIndex]?.end || nameWord.end,
@@ -184,15 +400,18 @@ var CommandChecker = class {
184
400
  } : void 0);
185
401
  });
186
402
  }
187
- #checkWrapped(name, argumentWords, context, depth) {
188
- const values = argumentWords.map(staticValue);
189
- const invoke = (index, nestedContext = context, isCrossSpawned = false) => {
403
+ #checkWrapped(name, argumentWords, values, context, depth) {
404
+ const invoke = (index, nestedContext = context, isCrossSpawned = false, isPackageSpecName = false) => {
190
405
  const word = argumentWords[index];
191
- return !word || values[index] === "." ? void 0 : this.checkInvocation(word, argumentWords.slice(index + 1), nestedContext, depth + 1, isCrossSpawned);
406
+ return !word || values[index] === "." ? void 0 : this.checkInvocation(word, argumentWords.slice(index + 1), nestedContext.portable ? {
407
+ ...nestedContext,
408
+ portable: false
409
+ } : nestedContext, depth + 1, isCrossSpawned, isPackageSpecName);
192
410
  };
193
- const nestedShellString = (index, shell = "native", bashOptions) => {
411
+ const isPackageSpecAt = (start, index) => values.slice(start, index).every((value) => value !== "-p" && value !== "--package" && !value?.startsWith("--package="));
412
+ const nestedShellString = (index, { bashOptions, shell = "native" } = {}) => {
194
413
  const word = argumentWords[index];
195
- if (word && depth < MAX_DEPTH) this.#analyzeNestedWords([word], context, {
414
+ if (word && depth < 10) this.#analyzeNested([word], context, {
196
415
  bashOptions,
197
416
  crossEnv: false,
198
417
  maskScriptGlobs: name === "concurrently",
@@ -200,31 +419,38 @@ var CommandChecker = class {
200
419
  });
201
420
  };
202
421
  const nestedShellLine = (index, isCrossEnv = false) => {
203
- if (index !== -1 && depth < MAX_DEPTH) this.#analyzeNestedWords(argumentWords.slice(index), context, {
204
- crossEnv: isCrossEnv,
422
+ if (index !== -1 && depth < 10) this.#analyzeNested(argumentWords.slice(index), context, {
423
+ crossEnv: isCrossEnv && "script",
205
424
  shell: "native"
206
425
  });
207
426
  };
208
- const commandStringIndex = () => {
209
- const index = values.findIndex((value) => isShortOptionCluster(value, "c"));
210
- return index === -1 ? -1 : index + 1;
211
- };
212
427
  const callIndex = (start, valueOptions) => {
213
428
  const commandIndex = findCommandIndex(values, start, [...valueOptions, ...CALL_OPTIONS]);
214
429
  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;
430
+ return values.findIndex((value, i) => i >= start && i < end && (setHas(CALL_OPTIONS, value) || argumentWords[i]?.text.startsWith(CALL_PREFIX) === true));
217
431
  };
218
- const shellModeIndex = (start, valueOptions) => {
432
+ const analyzeCall = (index) => {
433
+ const option = argumentWords[index];
434
+ if (setHas(CALL_OPTIONS, values[index])) nestedShellString(index + 1);
435
+ else if (option && depth < 10) this.#analyzeNested([wordAfter(option, 7)], context, {
436
+ crossEnv: false,
437
+ outerWord: option,
438
+ shell: "native"
439
+ });
440
+ };
441
+ const shellModeIndex = (optionsStart, start, valueOptions) => {
219
442
  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;
443
+ return values.slice(optionsStart, commandIndex === -1 ? values.length : commandIndex).some((value) => setHas(SHELL_MODE_OPTIONS, value) || name === "pnpm" && isShortOptionCluster(value, "c")) ? commandIndex : -1;
221
444
  };
222
445
  const valueOptions = WRAPPER_VALUE_OPTIONS.get(name);
223
446
  switch (name) {
224
447
  case "bash":
225
448
  case "ksh":
226
449
  case "zsh":
227
- nestedShellString(commandStringIndex(), "bash", new Set(slidingWindow(values, 2).flatMap(([option, value]) => option === "-O" && value ? [value] : [])));
450
+ nestedShellString(findCommandString(values), {
451
+ bashOptions: new Set(slidingWindow(values, 2).flatMap(([option, value]) => option === "-O" && value ? [value] : [])),
452
+ shell: "bash"
453
+ });
228
454
  return;
229
455
  case "concurrently":
230
456
  argumentWords.forEach((argument, index) => {
@@ -233,53 +459,59 @@ var CommandChecker = class {
233
459
  return;
234
460
  case "cross-env":
235
461
  case "cross-env-shell": {
236
- const index = values.findIndex((value) => value == null || !ASSIGNMENT.test(value));
462
+ const index = crossEnvCommandIndex(argumentWords, values);
237
463
  if (name === "cross-env") return invoke(index, {
238
464
  ...context,
239
- crossEnv: true
465
+ crossEnv: "command"
240
466
  }, true);
241
467
  nestedShellLine(index, true);
242
468
  return;
243
469
  }
244
470
  case "dash":
245
471
  case "sh":
246
- nestedShellString(commandStringIndex(), "sh");
472
+ nestedShellString(findCommandString(values), { shell: "sh" });
247
473
  return;
474
+ case "timeout": {
475
+ const duration = findCommandIndex(values, 0, TIMEOUT_VALUE_OPTIONS);
476
+ return duration === -1 || values[duration]?.startsWith("/") ? void 0 : invoke(duration + 1);
477
+ }
248
478
  case "aube":
249
479
  case "bun":
250
480
  case "npm":
251
481
  case "pnpm":
252
482
  case "upm":
253
483
  case "yarn": {
254
- const subcommand = findCommandIndex(values, 0, PACKAGE_MANAGER_VALUE_OPTIONS);
484
+ const subcommand = findCommandIndex(values, 0, name === "npm" ? NPM_VALUE_OPTIONS : PACKAGE_MANAGER_VALUE_OPTIONS);
255
485
  if (!arrayIncludes(EXEC_SUBCOMMANDS[name], values[subcommand])) return;
486
+ const execValueOptions = name === "npm" ? NPM_EXEC_VALUE_OPTIONS : EXEC_VALUE_OPTIONS;
256
487
  if (name === "npm" || name === "upm") {
257
- const call = callIndex(subcommand + 1, EXEC_VALUE_OPTIONS);
488
+ const call = callIndex(subcommand + 1, execValueOptions);
258
489
  if (call !== -1) {
259
- nestedShellString(call);
490
+ analyzeCall(call);
260
491
  return;
261
492
  }
262
493
  } else if (name === "aube" || name === "pnpm") {
263
- const shellLine = shellModeIndex(subcommand + 1, EXEC_VALUE_OPTIONS);
494
+ const shellLine = shellModeIndex(name === "pnpm" ? 0 : subcommand + 1, subcommand + 1, EXEC_VALUE_OPTIONS);
264
495
  if (shellLine !== -1) {
265
496
  nestedShellLine(shellLine);
266
497
  return;
267
498
  }
268
499
  }
269
- return invoke(findCommandIndex(values, subcommand + 1, EXEC_VALUE_OPTIONS));
500
+ const commandIndex = findCommandIndex(values, subcommand + 1, execValueOptions);
501
+ return invoke(commandIndex, context, false, (values[subcommand] !== "exec" || name === "npm" || name === "upm") && isPackageSpecAt(subcommand + 1, commandIndex));
270
502
  }
271
503
  case "npx":
272
504
  case "upx": {
273
505
  const call = callIndex(0, valueOptions || []);
274
506
  if (call !== -1) {
275
- nestedShellString(call);
507
+ analyzeCall(call);
276
508
  return;
277
509
  }
278
510
  break;
279
511
  }
280
512
  case "aubx":
281
513
  case "pnpx": {
282
- const shellLine = shellModeIndex(0, valueOptions || []);
514
+ const shellLine = shellModeIndex(0, 0, valueOptions || []);
283
515
  if (shellLine !== -1) {
284
516
  nestedShellLine(shellLine);
285
517
  return;
@@ -289,7 +521,7 @@ var CommandChecker = class {
289
521
  }
290
522
  if (!valueOptions) return;
291
523
  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);
524
+ return name === "command" && values.slice(0, commandIndex).some((value) => value === "-v" || value === "-V") ? void 0 : invoke(commandIndex, context, false, PACKAGE_SPEC_RUNNERS.has(name) && isPackageSpecAt(0, commandIndex));
293
525
  }
294
526
  };
295
527
  export { CommandChecker };