package-json-scripts-portability 0.0.1 → 0.0.3

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 (115) hide show
  1. package/README.md +59 -20
  2. package/dist/analyze-script.d.mts +34 -0
  3. package/dist/analyze-script.mjs +53 -0
  4. package/dist/command-checker.mjs +527 -0
  5. package/dist/command-usage.mjs +413 -0
  6. package/dist/commands.mjs +639 -0
  7. package/dist/context.mjs +25 -0
  8. package/dist/fixes.d.mts +9 -0
  9. package/dist/fixes.mjs +354 -0
  10. package/dist/index.d.mts +6 -118
  11. package/dist/index.mjs +4 -3787
  12. package/dist/line-break-checker.mjs +104 -0
  13. package/dist/messages.mjs +38 -0
  14. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-difference.mjs +11 -0
  15. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-includes.mjs +8 -0
  16. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-intersection.mjs +13 -0
  17. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-is-subset.mjs +16 -0
  18. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/arrayify.mjs +7 -0
  19. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/sorted-array-insertion-index-with.mjs +33 -0
  20. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/sorted-array-insertion-index.mjs +10 -0
  21. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/iterable/sliding-window.mjs +30 -0
  22. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert-computed.mjs +42 -0
  23. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert.mjs +40 -0
  24. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/math/clamp.mjs +7 -0
  25. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/path/to-forward-slashes.mjs +8 -0
  26. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/predicate/is-array.mjs +39 -0
  27. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/semver/is-semver-in-range.mjs +5 -0
  28. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/semver/parse-semver.mjs +26 -0
  29. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/set/set-has.mjs +8 -0
  30. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-after-last.mjs +24 -0
  31. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-after.mjs +24 -0
  32. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-before.mjs +24 -0
  33. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/truncate.mjs +46 -0
  34. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/ts/all-union-members.mjs +45 -0
  35. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/types/maybe-readonly-array.d.mts +17 -0
  36. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/value/is-equal.mjs +37 -0
  37. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/@antfu/utils/dist/index.mjs +5 -0
  38. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/globalThis.mjs +5 -0
  39. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/isUnsafeProperty.mjs +18 -0
  40. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/setOwnValue.mjs +23 -0
  41. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/toPropertyKey.mjs +16 -0
  42. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/differenceBy.mjs +40 -0
  43. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/differenceWith.mjs +37 -0
  44. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/intersectionWith.mjs +43 -0
  45. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/isSubsetWith.mjs +36 -0
  46. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/bigint/clamp.mjs +15 -0
  47. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/baseToString.mjs +26 -0
  48. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/getSymbols.mjs +5 -0
  49. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/getTag.mjs +13 -0
  50. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/hasOwn.mjs +13 -0
  51. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/isDeepKey.mjs +39 -0
  52. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/isIndex.mjs +10 -0
  53. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/regexMultiByte.mjs +10 -0
  54. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/tags.mjs +28 -0
  55. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/toKey.mjs +15 -0
  56. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedIndex.mjs +36 -0
  57. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedIndexBy.mjs +54 -0
  58. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedLastIndex.mjs +34 -0
  59. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedLastIndexBy.mjs +21 -0
  60. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/function/identity.mjs +5 -0
  61. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/cloneDeep.mjs +52 -0
  62. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/cloneDeepWith.mjs +179 -0
  63. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/get.mjs +67 -0
  64. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/has.mjs +53 -0
  65. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/property.mjs +26 -0
  66. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isArguments.mjs +26 -0
  67. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isBuffer.mjs +24 -0
  68. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isMatch.mjs +34 -0
  69. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isMatchWith.mjs +163 -0
  70. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isNaN.mjs +18 -0
  71. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isNil.mjs +24 -0
  72. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isNumber.mjs +24 -0
  73. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isObject.mjs +28 -0
  74. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isObjectLike.mjs +28 -0
  75. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isSymbol.mjs +19 -0
  76. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isTypedArray.mjs +21 -0
  77. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/matches.mjs +36 -0
  78. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/matchesProperty.mjs +47 -0
  79. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/string/truncate.mjs +78 -0
  80. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/eq.mjs +18 -0
  81. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/iteratee.mjs +46 -0
  82. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/toPath.mjs +75 -0
  83. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/toString.mjs +23 -0
  84. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/function/identity.mjs +24 -0
  85. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/function/noop.mjs +12 -0
  86. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/math/clamp.mjs +23 -0
  87. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isBuffer.mjs +24 -0
  88. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isEqual.mjs +21 -0
  89. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isEqualWith.mjs +150 -0
  90. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isNil.mjs +24 -0
  91. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isNull.mjs +25 -0
  92. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isPlainObject.mjs +50 -0
  93. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isPrimitive.mjs +26 -0
  94. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isSymbol.mjs +28 -0
  95. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isTypedArray.mjs +20 -0
  96. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isUndefined.mjs +25 -0
  97. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/remeda/dist/binarySearchCutoffIndex-Bu-OMJ0Z.mjs +26 -0
  98. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/remeda/dist/isArray.mjs +18 -0
  99. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/remeda/dist/sortedIndexWith.mjs +7 -0
  100. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/ts-extras/distribution/array-includes.mjs +5 -0
  101. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/ts-extras/distribution/set-has.mjs +5 -0
  102. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/verkit/dist/comparison-CmVirWIW.mjs +108 -0
  103. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/verkit/dist/range-C5wjdo9a.mjs +164 -0
  104. package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/verkit/dist/set-BGFWKKE8.mjs +38 -0
  105. package/dist/platforms.d.mts +8 -0
  106. package/dist/platforms.mjs +26 -0
  107. package/dist/redirection-checker.mjs +169 -0
  108. package/dist/reporter.d.mts +32 -0
  109. package/dist/reporter.mjs +143 -0
  110. package/dist/runners.d.mts +21 -0
  111. package/dist/runners.mjs +199 -0
  112. package/dist/script-analyzer.mjs +853 -0
  113. package/dist/word-checker.mjs +573 -0
  114. package/dist/words.mjs +70 -0
  115. package/package.json +36 -30
@@ -0,0 +1,527 @@
1
+ import { checkCommandUsage } from "./command-usage.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
+ import { withCovered } from "./context.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
+ 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 { arrayIntersection } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-intersection.mjs";
10
+ import { setHas } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/set/set-has.mjs";
11
+ import { slidingWindow } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/iterable/sliding-window.mjs";
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])));
58
+ /**
59
+ * Checks whether a name is a package spec rather than a path, e.g. `@scope/name` or
60
+ * `github:user/repo` (for `npx`)
61
+ */
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
+ ]);
71
+ const SHELL_SCRIPT = /\.(?:bash|command|sh|zsh)$/;
72
+ const WINDOWS_SCRIPT = /\.(?:bat|cmd|exe|ps1|vbs)$/i;
73
+ /** Exit statuses of commands that `exit` can replace at the end of a script */
74
+ const EXIT_STATUSES = /* @__PURE__ */ new Map([
75
+ [":", "0"],
76
+ ["false", "1"],
77
+ ["true", "0"]
78
+ ]);
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 */
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
+ };
108
+ /** Options of pnpm and aube that make the command and its arguments a shell line */
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
+ };
131
+ /**
132
+ * Finds the command that a command runs, e.g. `rm` in `cross-env A=1 rm -rf dist`.
133
+ * @param values The static values of the outer command's arguments
134
+ * @param start Index of the first argument to consider
135
+ * @param valueOptions Options of the outer command that take a value
136
+ * @param shouldSkipAssignments Whether to skip `NAME=value` arguments
137
+ * @returns Index of the nested command's name, or -1 if there is none
138
+ */
139
+ const findCommandIndex = (values, start, valueOptions, shouldSkipAssignments = false) => {
140
+ for (let index = start; index < values.length; index++) {
141
+ const value = values[index];
142
+ if (value == null) return index;
143
+ if (value === "--") return index + 1;
144
+ if (value.startsWith("-") && value.length > 1) {
145
+ if (valueOptions.includes(value)) index++;
146
+ continue;
147
+ }
148
+ if (!(shouldSkipAssignments && ASSIGNMENT.test(value))) return index;
149
+ }
150
+ return -1;
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
+ };
176
+ /** Checks commands, e.g. whether they're available everywhere, and the commands they run */
177
+ var CommandChecker = class {
178
+ #analyzeNestedWords;
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;
184
+ /** Fixes that depend on where a command is, by the command's name, e.g. `exit 0` for `true` */
185
+ #positionalFixes = /* @__PURE__ */ new Map();
186
+ #reporter;
187
+ #runnerTraits;
188
+ constructor(reporter, options, analyzeNestedWords, runnerTraits) {
189
+ this.#reporter = reporter;
190
+ this.#options = options;
191
+ this.#analyzeNestedWords = analyzeNestedWords;
192
+ this.#runnerTraits = runnerTraits;
193
+ }
194
+ /**
195
+ * Adds fixes for commands that end the script, which can change what follows them.
196
+ * @param statements Top-level statements of a script
197
+ * @param source The script's source
198
+ */
199
+ addPositionalFixes(statements, source) {
200
+ const last = statements.at(-1);
201
+ if (!last || last.background) return;
202
+ const list = last.command;
203
+ const commands = list.type === "AndOr" ? list.commands : [list];
204
+ const final = commands.at(-1);
205
+ if (final?.type === "Command" && final.name && final.prefix.length === 0 && final.suffix.length === 0) {
206
+ const status = EXIT_STATUSES.get(staticValue(final.name) || "");
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
+ }
218
+ }
219
+ const [exporting, command] = commands;
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
+ }
243
+ }
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) {
254
+ const lowerCaseName = name.toLowerCase();
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)];
266
+ }
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]);
297
+ return this.#reporter.report(context, nameWord, "unsupportedCommand", {
298
+ command: name,
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)));
301
+ }
302
+ /**
303
+ * Checks a command and the commands that it runs.
304
+ * @param nameWord The command's name, e.g. `rm`
305
+ * @param argumentWords The words that follow it
306
+ * @param context Where the command is
307
+ * @param depth How many commands run this command, e.g. 1 for `rm` in `cross-env A=1 rm`
308
+ * @param isCrossSpawned Whether `cross-spawn` runs the command (in `cross-env`), which works with
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
312
+ * @returns What the command is, and what it runs
313
+ */
314
+ checkInvocation(nameWord, argumentWords, context, depth, isCrossSpawned = false, isPackageSpecName = false) {
315
+ const name = staticValue(nameWord);
316
+ const result = {
317
+ crossEnv: void 0,
318
+ name,
319
+ target: name
320
+ };
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));
350
+ return result;
351
+ }
352
+ const firstIssue = this.#reporter.issues.length;
353
+ const usageIssues = findUsageIssues(name, argumentWords, values, context);
354
+ const covered = [];
355
+ if (SHELL_SCRIPT.test(name)) covered.push(...this.#reporter.report(context, nameWord, "unsupportedCommand", {
356
+ command: truncate(nameWord.text),
357
+ suggestion: SHELL_SCRIPT_SUGGESTION
358
+ }, WINDOWS_PLATFORMS));
359
+ else if (WINDOWS_SCRIPT.test(name)) covered.push(...this.#reporter.report(context, nameWord, "unsupportedCommand", {
360
+ command: truncate(nameWord.text),
361
+ suggestion: NODE_SCRIPT_SUGGESTION
362
+ }, POSIX_PLATFORMS));
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");
365
+ if (!isCrossSpawned && !isConverted) covered.push(...this.#reporter.reportSyntax(context, nameWord, "posix", true, "unsupportedSyntax", {
366
+ suggestion: "cmd.exe treats `/` as an option prefix; run files with `node`, and binaries by their name instead.",
367
+ syntax: `Running a command by its path (\`${truncate(nameWord.text)}\`)`
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));
373
+ return result;
374
+ }
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 }) => {
389
+ const fixedWord = fix && argumentWords[fix.argumentIndex];
390
+ return this.#reporter.report(context, argumentIndex == null ? nameWord : {
391
+ end: argumentWords[argumentIndex]?.end || nameWord.end,
392
+ pos: nameWord.pos
393
+ }, messageId, data, platforms, fix && fixedWord ? {
394
+ description: fix.description,
395
+ edits: [{
396
+ end: fixedWord.end,
397
+ pos: fixedWord.pos,
398
+ text: fix.text
399
+ }]
400
+ } : void 0);
401
+ });
402
+ }
403
+ #checkWrapped(name, argumentWords, values, context, depth) {
404
+ const invoke = (index, nestedContext = context, isCrossSpawned = false, isPackageSpecName = false) => {
405
+ const word = argumentWords[index];
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);
410
+ };
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" } = {}) => {
413
+ const word = argumentWords[index];
414
+ if (word && depth < 10) this.#analyzeNested([word], context, {
415
+ bashOptions,
416
+ crossEnv: false,
417
+ maskScriptGlobs: name === "concurrently",
418
+ shell
419
+ });
420
+ };
421
+ const nestedShellLine = (index, isCrossEnv = false) => {
422
+ if (index !== -1 && depth < 10) this.#analyzeNested(argumentWords.slice(index), context, {
423
+ crossEnv: isCrossEnv && "script",
424
+ shell: "native"
425
+ });
426
+ };
427
+ const callIndex = (start, valueOptions) => {
428
+ const commandIndex = findCommandIndex(values, start, [...valueOptions, ...CALL_OPTIONS]);
429
+ const end = commandIndex === -1 ? values.length : commandIndex;
430
+ return values.findIndex((value, i) => i >= start && i < end && (setHas(CALL_OPTIONS, value) || argumentWords[i]?.text.startsWith(CALL_PREFIX) === true));
431
+ };
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) => {
442
+ const commandIndex = findCommandIndex(values, start, valueOptions);
443
+ return values.slice(optionsStart, commandIndex === -1 ? values.length : commandIndex).some((value) => setHas(SHELL_MODE_OPTIONS, value) || name === "pnpm" && isShortOptionCluster(value, "c")) ? commandIndex : -1;
444
+ };
445
+ const valueOptions = WRAPPER_VALUE_OPTIONS.get(name);
446
+ switch (name) {
447
+ case "bash":
448
+ case "ksh":
449
+ case "zsh":
450
+ nestedShellString(findCommandString(values), {
451
+ bashOptions: new Set(slidingWindow(values, 2).flatMap(([option, value]) => option === "-O" && value ? [value] : [])),
452
+ shell: "bash"
453
+ });
454
+ return;
455
+ case "concurrently":
456
+ argumentWords.forEach((argument, index) => {
457
+ if (argument.parts?.some((part) => part.type === "DoubleQuoted" || part.type === "SingleQuoted") && WHITESPACE.test(argument.value)) nestedShellString(index);
458
+ });
459
+ return;
460
+ case "cross-env":
461
+ case "cross-env-shell": {
462
+ const index = crossEnvCommandIndex(argumentWords, values);
463
+ if (name === "cross-env") return invoke(index, {
464
+ ...context,
465
+ crossEnv: "command"
466
+ }, true);
467
+ nestedShellLine(index, true);
468
+ return;
469
+ }
470
+ case "dash":
471
+ case "sh":
472
+ nestedShellString(findCommandString(values), { shell: "sh" });
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
+ }
478
+ case "aube":
479
+ case "bun":
480
+ case "npm":
481
+ case "pnpm":
482
+ case "upm":
483
+ case "yarn": {
484
+ const subcommand = findCommandIndex(values, 0, name === "npm" ? NPM_VALUE_OPTIONS : PACKAGE_MANAGER_VALUE_OPTIONS);
485
+ if (!arrayIncludes(EXEC_SUBCOMMANDS[name], values[subcommand])) return;
486
+ const execValueOptions = name === "npm" ? NPM_EXEC_VALUE_OPTIONS : EXEC_VALUE_OPTIONS;
487
+ if (name === "npm" || name === "upm") {
488
+ const call = callIndex(subcommand + 1, execValueOptions);
489
+ if (call !== -1) {
490
+ analyzeCall(call);
491
+ return;
492
+ }
493
+ } else if (name === "aube" || name === "pnpm") {
494
+ const shellLine = shellModeIndex(name === "pnpm" ? 0 : subcommand + 1, subcommand + 1, EXEC_VALUE_OPTIONS);
495
+ if (shellLine !== -1) {
496
+ nestedShellLine(shellLine);
497
+ return;
498
+ }
499
+ }
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));
502
+ }
503
+ case "npx":
504
+ case "upx": {
505
+ const call = callIndex(0, valueOptions || []);
506
+ if (call !== -1) {
507
+ analyzeCall(call);
508
+ return;
509
+ }
510
+ break;
511
+ }
512
+ case "aubx":
513
+ case "pnpx": {
514
+ const shellLine = shellModeIndex(0, 0, valueOptions || []);
515
+ if (shellLine !== -1) {
516
+ nestedShellLine(shellLine);
517
+ return;
518
+ }
519
+ break;
520
+ }
521
+ }
522
+ if (!valueOptions) return;
523
+ const commandIndex = findCommandIndex(values, 0, valueOptions, name === "env");
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));
525
+ }
526
+ };
527
+ export { CommandChecker };