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
package/dist/fixes.mjs ADDED
@@ -0,0 +1,354 @@
1
+ import { CMD_BUILTINS, OS_COMMANDS, SHELL_STATE_COMMANDS } from "./commands.mjs";
2
+ import { describeReplacement, truncate } from "./messages.mjs";
3
+ import { substringAfter } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-after.mjs";
4
+ import { toForwardSlashes } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/path/to-forward-slashes.mjs";
5
+ import { ASSIGNMENT, GLOB_PART_TYPES, isPlainAssignment, isPlainWord, nestedSource, staticValue } from "./words.mjs";
6
+ // src/fixes.ts
7
+ const LINE_WHITESPACE = /[\t ]/;
8
+ const LINE_BREAK = /^\r?\n/;
9
+ const BACKSLASH_CODE_POINT = 92;
10
+ /** Options of `rm` that `rimraf` doesn't need, e.g. `-rf` */
11
+ const RM_OPTION = /^(?:-[Rfr]+|--force|--recursive)$/;
12
+ /** One of these options that makes `rm` delete directories, e.g. `-rf` */
13
+ const RM_RECURSIVE_OPTION = /^(?:-f*[Rr]|--recursive)/;
14
+ /** One of these options that makes `rm` ignore missing paths, e.g. `-rf` */
15
+ const RM_FORCE_OPTION = /^(?:-[Rr]*f|--force)/;
16
+ /** A binary of a dependency, which package managers add to `PATH` */
17
+ const DEPENDENCY_BINARY = /^(?:\.\/)?node_modules\/\.bin\/([^/]+)$/;
18
+ /** Commands that run instead of a dependency's binary with the same name, e.g. cmd.exe's `del` */
19
+ const SHADOWING_COMMANDS = OS_COMMANDS.union(CMD_BUILTINS).union(SHELL_STATE_COMMANDS);
20
+ /** Characters that `rimraf --glob` treats as glob syntax, e.g. `[` */
21
+ const GLOB_SYNTAX = /[(*?[\\\]{}]|^!/;
22
+ /** A brace expansion of plain alternatives, e.g. `{a,b}` */
23
+ const SIMPLE_BRACE_EXPANSION = /^\{([\w+\-./@]*(?:,[\w+\-./@]*)+)\}$/;
24
+ const PLAIN_TEXT = /^[\w+\-./:=@]*$/;
25
+ const JOIN_LINES_DESCRIPTION = "Join the lines.";
26
+ /**
27
+ * Text that `cross-env` changes in the words of its command: it removes `'`, and `\` before `$`,
28
+ * `"`, `\` or `'`
29
+ */
30
+ const CROSS_ENV_CHANGED_TEXT = /'|\\["$'\\]/;
31
+ /**
32
+ * Values that `cross-env` changes, e.g. by removing their quotes or replacing variables, or only
33
+ * reads up to a line break, or with `~` at the start or after `:`, which shells expand in
34
+ * assignments, but not in arguments
35
+ */
36
+ const CROSS_ENV_CHANGED_VALUE = /[\n\r$\\]|:~|^["'~]/;
37
+ /** Parts of a value that shells don't split or expand as globs in an argument either */
38
+ const UNSPLIT_PART_TYPES = /* @__PURE__ */ new Set([
39
+ "DoubleQuoted",
40
+ "Literal",
41
+ "SingleQuoted"
42
+ ]);
43
+ const QUOTES_OR_BACKSLASH = /["'\\]/;
44
+ /**
45
+ * Checks whether a file that `.` runs is a dotfile without a slash, e.g. `.env`, which shells look
46
+ * for in `PATH`, but where it's unlikely to be, unlike e.g. conda's `activate`
47
+ */
48
+ const isDotfileName = (file) => file.startsWith(".") && !file.includes("/");
49
+ /** Creates a fix that replaces a piece of the script */
50
+ const replacement = (node, original, text) => ({
51
+ description: describeReplacement(original, text),
52
+ edits: [{
53
+ end: node.end,
54
+ pos: node.pos,
55
+ text
56
+ }]
57
+ });
58
+ /** Creates a fix that wraps a word in double quotes */
59
+ const quoting = (word, description) => ({
60
+ description,
61
+ edits: [{
62
+ end: word.pos,
63
+ pos: word.pos,
64
+ text: "\""
65
+ }, {
66
+ end: word.end,
67
+ pos: word.end,
68
+ text: "\""
69
+ }]
70
+ });
71
+ /** Gets the value of a word, with placeholders for its expansions */
72
+ const maskedValue = (word) => word.parts ? nestedSource(word.parts) : word.value;
73
+ /**
74
+ * Gets the edits that keep the value of an assignment the same in an argument of `cross-env`.
75
+ * @param value The value
76
+ * @returns The edits, e.g. ones that quote `./bin:$PATH`, which shells split in arguments, or
77
+ * `undefined` if no edits can keep it
78
+ */
79
+ const crossEnvValueEdits = (value) => {
80
+ const parts = value.parts || [];
81
+ if (CROSS_ENV_CHANGED_VALUE.test(maskedValue(value)) || parts.some((part) => part.type === "BraceExpansion")) return;
82
+ if (parts.every((part) => UNSPLIT_PART_TYPES.has(part.type))) return [];
83
+ return QUOTES_OR_BACKSLASH.test(value.text) ? void 0 : quoting(value, "").edits;
84
+ };
85
+ /**
86
+ * Checks whether `cross-env` runs a command with the same words as the shell, e.g. not with
87
+ * `console.log('a')`, whose quotes it removes, or with `""`, which it drops.
88
+ * @param words The command's name and arguments
89
+ * @returns Whether it does
90
+ */
91
+ const isCrossEnvCommandSafe = (words) => words.every((word) => {
92
+ const value = maskedValue(word);
93
+ return value.length > 0 && !CROSS_ENV_CHANGED_TEXT.test(value);
94
+ });
95
+ /**
96
+ * Gets the edits that keep a command the same when `cross-env` sets its variables.
97
+ * @param assignments The items that become the arguments of `cross-env` before the command
98
+ * @param commandWords The command's name and arguments
99
+ * @returns The edits, or `undefined` if `cross-env` would run something else
100
+ */
101
+ const crossEnvEdits = (assignments, commandWords) => {
102
+ const edits = [];
103
+ return isCrossEnvCommandSafe(commandWords) && assignments.every((item) => {
104
+ const valueEdits = isPlainAssignment(item) ? crossEnvValueEdits(item.value) : void 0;
105
+ edits.push(...valueEdits || []);
106
+ return valueEdits;
107
+ }) ? edits : void 0;
108
+ };
109
+ /**
110
+ * Finds the `\` line continuation before a line, e.g. in `a \` + line break + `b`.
111
+ * @param source The script
112
+ * @param lineStart Where the line starts
113
+ * @returns Where the `\` is, or -1 if the line doesn't continue the previous one
114
+ */
115
+ const lineContinuationStart = (source, lineStart) => {
116
+ if (source[lineStart - 1] !== "\n") return -1;
117
+ const lineEnd = lineStart - (source[lineStart - 2] === "\r" ? 2 : 1);
118
+ let backslashes = 0;
119
+ while (source.codePointAt(lineEnd - backslashes - 1) === BACKSLASH_CODE_POINT) backslashes++;
120
+ return backslashes % 2 === 1 ? lineEnd - 1 : -1;
121
+ };
122
+ /**
123
+ * Creates an edit that removes a piece of a script, along with the whitespace that separates it.
124
+ * @param source The script
125
+ * @param pos Start offset of the piece
126
+ * @param end End offset (exclusive) of the piece
127
+ * @returns The edit
128
+ */
129
+ const removal = (source, pos, end) => {
130
+ let start = pos;
131
+ let stop = end;
132
+ while (start > 0 && LINE_WHITESPACE.test(source.charAt(start - 1))) start--;
133
+ const continuation = lineContinuationStart(source, start);
134
+ if (continuation !== -1) {
135
+ start = continuation;
136
+ while (start > 0 && LINE_WHITESPACE.test(source.charAt(start - 1))) start--;
137
+ } else if (start === 0 || source[start - 1] === "\n") {
138
+ while (stop < source.length && LINE_WHITESPACE.test(source.charAt(stop))) stop++;
139
+ const lineBreak = LINE_BREAK.exec(source.slice(stop))?.[0];
140
+ if (lineBreak) stop += lineBreak.length;
141
+ else if (stop === source.length && start > 0) start -= source[start - 2] === "\r" ? 2 : 1;
142
+ }
143
+ return {
144
+ end: stop,
145
+ pos: start,
146
+ text: ""
147
+ };
148
+ };
149
+ /**
150
+ * Gets a fix for a command that isn't available on all platforms, e.g. `rimraf` for `rm -rf`.
151
+ * @param name Name that the command was invoked with, e.g. `rm`
152
+ * @param nameWord Word that the name comes from
153
+ * @param argumentWords The words that follow it
154
+ * @param source The source that node positions refer to
155
+ * @returns The fix, if there's a replacement that works the same
156
+ */
157
+ const commandFix = (name, nameWord, argumentWords, source) => {
158
+ const first = argumentWords.at(0);
159
+ switch (name) {
160
+ case "env": {
161
+ const commandIndex = argumentWords.findIndex((word) => !ASSIGNMENT.test(word.text));
162
+ return commandIndex > 0 && argumentWords.slice(0, commandIndex).every((word) => !CROSS_ENV_CHANGED_VALUE.test(substringAfter(maskedValue(word), "="))) && isCrossEnvCommandSafe(argumentWords.slice(commandIndex)) ? replacement(nameWord, nameWord.text, "cross-env") : void 0;
163
+ }
164
+ case "rm": {
165
+ const optionCount = argumentWords.findIndex((word) => !RM_OPTION.test(staticValue(word) || ""));
166
+ const paths = optionCount === -1 ? [] : argumentWords.slice(optionCount);
167
+ const options = argumentWords.slice(0, optionCount).map((word) => staticValue(word) || "");
168
+ const globs = paths.filter((word) => word.parts?.some((part) => GLOB_PART_TYPES.includes(part.type)));
169
+ if (paths.length === 0 || options.every((option) => !RM_RECURSIVE_OPTION.test(option)) || options.every((option) => !RM_FORCE_OPTION.test(option)) || paths.some((word) => staticValue(word)?.startsWith("-") && word.text !== "--") || globs.some((word) => !isPlainWord(word, GLOB_PART_TYPES)) || globs.some((word) => word.text.includes("**")) || globs.length > 0 && paths.some((word) => !globs.includes(word) && GLOB_SYNTAX.test(staticValue(word) ?? "*"))) return;
170
+ const end = argumentWords[optionCount - 1]?.end || nameWord.end;
171
+ const original = source.slice(nameWord.pos, end);
172
+ return globs.length === 0 ? replacement({
173
+ end,
174
+ pos: nameWord.pos
175
+ }, original, "rimraf") : {
176
+ description: `Replace \`${truncate(original)}\` with \`rimraf --glob\`, and quote the globs.`,
177
+ edits: [{
178
+ end,
179
+ pos: nameWord.pos,
180
+ text: "rimraf --glob"
181
+ }, ...globs.flatMap((word) => quoting(word, "").edits)]
182
+ };
183
+ }
184
+ case "source": {
185
+ const file = first && staticValue(first);
186
+ if (!first || file == null) return;
187
+ const range = {
188
+ end: first.end,
189
+ pos: nameWord.pos
190
+ };
191
+ return replacement(range, source.slice(range.pos, range.end), `. ${isDotfileName(file) ? "./" : ""}${first.text}`);
192
+ }
193
+ default: return;
194
+ }
195
+ };
196
+ /**
197
+ * Gets a fix for running a command by its path, e.g. `tsc` for `./node_modules/.bin/tsc`.
198
+ * @param name Path that the command was invoked with
199
+ * @param nameWord Word that the path comes from
200
+ * @param isDirectoryChanged Whether a command before it changed the directory, e.g. `cd`
201
+ * @returns The fix, if the path is a dependency's binary
202
+ */
203
+ const pathCommandFix = (name, nameWord, isDirectoryChanged) => {
204
+ const binary = DEPENDENCY_BINARY.exec(name)?.[1];
205
+ return binary && !isDirectoryChanged && !SHADOWING_COMMANDS.has(binary.toLowerCase()) ? replacement(nameWord, nameWord.text, binary) : void 0;
206
+ };
207
+ /**
208
+ * Gets a fix that replaces the backslashes of a Windows path with slashes.
209
+ * @param word The word with the path, e.g. `--config=configs\app.json`
210
+ * @param original The word's text as the script has it
211
+ * @param shouldQuote Whether to also quote the path, e.g. `src\**\*.js`
212
+ * @returns The fix, which only replaces the path
213
+ */
214
+ const backslashPathFix = (word, original, shouldQuote) => {
215
+ const start = word.text.lastIndexOf("=") + 1;
216
+ const path = toForwardSlashes(word.text.slice(start));
217
+ const text = shouldQuote ? `"${path}"` : path;
218
+ return {
219
+ description: describeReplacement(original, `${original.slice(0, start)}${text}`),
220
+ edits: [{
221
+ end: word.end,
222
+ pos: word.pos + start,
223
+ text
224
+ }]
225
+ };
226
+ };
227
+ /**
228
+ * Gets a fix that expands a brace expansion, e.g. `dist/a dist/b` for `dist/{a,b}`.
229
+ * @param word The word that contains the brace expansion
230
+ * @param part Brace expansion in the word, e.g. `{a,b}`
231
+ * @returns The fix, if the word only consists of plain text and the brace expansion
232
+ */
233
+ const braceExpansionFix = (word, part) => {
234
+ const alternatives = SIMPLE_BRACE_EXPANSION.exec(part.text)?.[1]?.split(",");
235
+ const isPlain = (word.parts || []).every((other) => other === part || other.type === "Literal" && PLAIN_TEXT.test(other.text));
236
+ if (!alternatives || !isPlain) return;
237
+ const prefix = word.text.slice(0, part.pos - word.pos);
238
+ const suffix = word.text.slice(part.end - word.pos);
239
+ return replacement(word, word.text, alternatives.map((alternative) => `${prefix}${alternative}${suffix}`).join(" "));
240
+ };
241
+ const hasAlternatives = ({ command }) => command.type === "AndOr" && command.operators.includes("||");
242
+ /** Checks whether a command is only a word with the given text, e.g. `\` */
243
+ const isLoneWord = (node, text) => node?.type === "Command" && node.prefix.length === 0 && node.name?.text === text && node.suffix.length === 0;
244
+ /** Checks whether a statement is a lone `\` at the end of the script, which can't follow `&&` */
245
+ const isTrailingBackslash = ({ command }) => isLoneWord(command, "\\");
246
+ /**
247
+ * Checks whether a statement ends with a command that is only a carriage return, e.g. after `&&`
248
+ * and before a line feed, whose fix removes it
249
+ */
250
+ const doesEndWithCarriageReturn = ({ command }) => {
251
+ const last = command.type === "AndOr" ? command.commands.at(-1) : command;
252
+ return isLoneWord(last?.type === "Pipeline" ? last.commands.at(-1) : last, "\r");
253
+ };
254
+ /**
255
+ * Checks whether joining two statements with `&&` keeps the logic the same, apart from stopping
256
+ * after a failure.
257
+ * @param statement The first statement
258
+ * @param next The statement that follows
259
+ * @returns Whether the rest of the logic stays the same, e.g. not for `a; b || c`, which would run
260
+ * `c` when `a` fails, or for `a || b; c`, which cmd.exe would read as `a || (b && c)`
261
+ */
262
+ const canChain = (statement, next) => !next.background && !hasAlternatives(statement) && !hasAlternatives(next) && !isTrailingBackslash(next);
263
+ /**
264
+ * Gets a fix for a line break that separates statements.
265
+ * @param source The source that the statements are in
266
+ * @param gapStart Where the text between the statements starts, after the bodies of the first
267
+ * statement's here-documents
268
+ * @param statement The statement before the line break
269
+ * @param next The statement that follows it
270
+ * @returns The fix, if the text is only whitespace and the rest of the logic stays the same
271
+ */
272
+ const lineBreakFix = (source, gapStart, statement, next) => {
273
+ const gap = source.slice(gapStart, next.pos);
274
+ if (gap.trim().length > 0 || gapStart !== statement.end || lineContinuationStart(source, gapStart + gap.indexOf("\n") + 1) !== -1 || doesEndWithCarriageReturn(statement)) return;
275
+ if (statement.background) return {
276
+ description: JOIN_LINES_DESCRIPTION,
277
+ edits: [{
278
+ end: next.pos,
279
+ pos: gapStart,
280
+ text: " "
281
+ }]
282
+ };
283
+ return canChain(statement, next) ? {
284
+ description: "Replace the line break with `&&`, which stops at the first failing command.",
285
+ edits: [{
286
+ end: next.pos,
287
+ pos: gapStart,
288
+ text: " && "
289
+ }]
290
+ } : void 0;
291
+ };
292
+ /**
293
+ * Gets a fix that joins the lines of a command, e.g. after `||` or a `\` line continuation.
294
+ * @param source The source that the command is in
295
+ * @param position Where the line break is
296
+ * @param end Where the text after the line break ends, e.g. the indentation of the next line
297
+ * @returns The fix, which keeps the command the same in POSIX shells, if it only removes whitespace
298
+ * or a `\` line continuation
299
+ */
300
+ const joinLinesFix = (source, position, end) => {
301
+ if (source[position - 1] === "\\") return {
302
+ description: JOIN_LINES_DESCRIPTION,
303
+ edits: [{
304
+ end: position + 1,
305
+ pos: position - 1,
306
+ text: ""
307
+ }]
308
+ };
309
+ return source.slice(position, end).trim().length === 0 ? {
310
+ description: JOIN_LINES_DESCRIPTION,
311
+ edits: [{
312
+ end,
313
+ pos: position,
314
+ text: " "
315
+ }]
316
+ } : void 0;
317
+ };
318
+ /**
319
+ * Gets a fix for a `;` that separates commands.
320
+ * @param range Where the `;` is
321
+ * @param gap The text between the commands, including the `;`
322
+ * @param statement The statement before it
323
+ * @param next The statement that follows it, if any
324
+ * @returns The fix, unless a statement has alternatives (`||`) or the next one runs in the
325
+ * background
326
+ */
327
+ const semicolonFix = (range, gap, statement, next) => {
328
+ if (!next) {
329
+ const text = gap[gap.indexOf(";") + 1] === "#" ? " " : "";
330
+ return {
331
+ description: "Remove the `;`.",
332
+ edits: [{
333
+ ...range,
334
+ text
335
+ }]
336
+ };
337
+ }
338
+ return gap.trim() === ";" && canChain(statement, next) ? {
339
+ description: "Replace `;` with `&&`, which stops at the first failing command.",
340
+ edits: [{
341
+ end: next.pos,
342
+ pos: range.pos - gap.indexOf(";"),
343
+ text: " && "
344
+ }]
345
+ } : void 0;
346
+ };
347
+ /**
348
+ * Applies a fix to the script that it was found in.
349
+ * @param script Text of the script to change
350
+ * @param fix A fix from `analyzeScript`
351
+ * @returns The fixed script
352
+ */
353
+ const applyFix = (script, fix) => fix.edits.toSorted((left, right) => right.pos - left.pos).reduce((result, { end, pos, text }) => result.slice(0, pos) + text + result.slice(end), script);
354
+ export { DEPENDENCY_BINARY, applyFix, backslashPathFix, braceExpansionFix, commandFix, crossEnvEdits, isDotfileName, joinLinesFix, lineBreakFix, pathCommandFix, quoting, removal, replacement, semicolonFix };
package/dist/index.d.mts CHANGED
@@ -1,118 +1,6 @@
1
- import "unbash";
2
- type Platform = 'darwin' | 'linux' | 'win32';
3
- export declare const PLATFORMS: readonly Platform[];
4
- /**
5
- * Formats platforms as a list for people, e.g. "macOS, Linux, and Windows".
6
- * @param platforms The platforms to format
7
- * @returns The formatted list
8
- */
9
- export declare const formatPlatforms: (platforms: readonly Platform[]) => string;
10
- /** What can run the scripts: a package manager, or `node` for `node --run` */
11
- type Runner = 'aube' | 'node' | 'npm' | 'pnpm' | 'pnpm-10' | 'upm' | 'yarn' | 'yarn-classic';
12
- export declare const RUNNERS: readonly Runner[];
13
- /**
14
- * Scripts that run with any package manager by default, as they run on the machine of whoever
15
- * installs the package: install scripts, and `prepare`, which runs when it's installed from git
16
- */
17
- export declare const RUNNER_AGNOSTIC_SCRIPTS: ReadonlySet<string>;
18
- /**
19
- * Gets the runner for a package manager, e.g. the one in the `packageManager` or
20
- * `devEngines.packageManager` field of package.json.
21
- * @param name Name of the package manager, e.g. `yarn`
22
- * @param version Its version or version range, which tells Yarn v1 from Yarn v2+, and pnpm v10
23
- * from later versions, e.g. `1.22.22`
24
- * @returns The runner, or `undefined` if it's an unknown package manager or Yarn of an unknown
25
- * version
26
- */
27
- export declare const getRunner: (name: string, version?: string) => "aube" | "node" | "npm" | "pnpm" | "pnpm-10" | "upm" | "yarn" | "yarn-classic" | undefined;
28
- /**
29
- * Formats runners as a list for people, e.g. "pnpm and Yarn v1".
30
- * @param runners The runners to format
31
- * @returns The formatted list
32
- */
33
- export declare const formatRunners: (runners: readonly Runner[]) => string;
34
- /**
35
- * A value or a readonly array of such values: `T | readonly T[]`.
36
- *
37
- * Unlike `MaybeArray`, it also accepts readonly arrays and tuples (like `as const` literals), so
38
- * it fits parameters and options that only read the array.
39
- * To tell the two cases apart, use `isArray`: unlike `Array.isArray`, it narrows this type in
40
- * both branches.
41
- * @example
42
- * const watch = (paths: MaybeReadonlyArray<string>) => {
43
- * const list = isArray(paths) ? paths : [paths]; // readonly string[]
44
- * // ...
45
- * };
46
- *
47
- * watch('src');
48
- * watch(['src', 'test'] as const); // an error with `MaybeArray<string>`
49
- */
50
- type MaybeReadonlyArray<T> = T | readonly T[];
51
- type ScriptIssueMessageId = 'inconsistentBehavior' | 'unsupportedCommand' | 'unsupportedSyntax' | 'unsupportedUsage';
52
- interface ScriptEdit {
53
- /** End offset (exclusive) of the replaced text in the script */
54
- end: number;
55
- /** Start offset of the replaced text in the script */
56
- pos: number;
57
- /** The replacement */
58
- text: string;
59
- }
60
- interface ScriptFix {
61
- /** Describes the fix for people, e.g. "Prefix the command with `cross-env`." */
62
- description: string;
63
- /** Replacements that don't overlap */
64
- edits: ScriptEdit[];
65
- }
66
- interface ScriptIssue {
67
- /** Message data, apart from `platforms` */
68
- data: Record<string, string>;
69
- /** End offset (exclusive) of the issue in the script */
70
- end: number;
71
- /** A fix that the rule suggests, if the issue has an obvious one */
72
- fix?: ScriptFix;
73
- messageId: ScriptIssueMessageId;
74
- /** Platforms on which the script doesn't work as intended */
75
- platforms: Platform[];
76
- /** Start offset of the issue in the script */
77
- pos: number;
78
- /** Runners with which the script has the issue */
79
- runners: Runner[];
80
- }
81
- interface AnalyzeScriptOptions {
82
- /** Names of the package's dependencies, e.g. to detect del-cli's `del` alias */
83
- dependencies?: readonly string[];
84
- /** Commands that shouldn't be reported */
85
- ignoreCommands?: readonly string[];
86
- /**
87
- * What runs the script: a package manager, or `node` for `node --run`.
88
- * It decides e.g. which shell runs the script and which environment variables it gets.
89
- * Several runners report what breaks with any of them.
90
- * Defaults to `npm`
91
- */
92
- runner?: MaybeReadonlyArray<Runner>;
93
- /**
94
- * Scripts that any package manager may run, which are checked with all of them instead of
95
- * `runner`: `true` for all scripts, `false` for none, or whether each script is, by name, on
96
- * top of the defaults (`preinstall`, `install`, `postinstall` and `prepare`)
97
- */
98
- runnerAgnosticScripts?: boolean | Readonly<Record<string, boolean>>;
99
- /** The script's name in package.json, e.g. `postinstall`, to tell if it's runner-agnostic */
100
- scriptName?: string;
101
- /** Whether pnpm's `shell-emulator` setting is on, so that it runs the script with Yarn's shell */
102
- shellEmulator?: boolean;
103
- }
104
- /**
105
- * Analyzes a package.json script for constructs that don't work (the same) on all platforms.
106
- * @param script The script's source
107
- * @param options Analysis options
108
- * @returns The issues found, with offsets relative to the script
109
- */
110
- export declare const analyzeScript: (script: string, options?: AnalyzeScriptOptions) => ScriptIssue[];
111
- /**
112
- * Applies a fix to the script that it was found in.
113
- * @param script Text of the script to change
114
- * @param fix A fix from `analyzeScript`
115
- * @returns The fixed script
116
- */
117
- export declare const applyFix: (script: string, fix: ScriptFix) => string;
118
- export type { AnalyzeScriptOptions, Platform, Runner, ScriptEdit, ScriptFix, ScriptIssue, ScriptIssueMessageId };
1
+ import { AnalyzeScriptOptions, analyzeScript } from "./analyze-script.mjs";
2
+ import { applyFix } from "./fixes.mjs";
3
+ import { PLATFORMS, Platform, formatPlatforms } from "./platforms.mjs";
4
+ import { ScriptEdit, ScriptFix, ScriptIssue, ScriptIssueMessageId } from "./reporter.mjs";
5
+ import { RUNNERS, RUNNER_AGNOSTIC_SCRIPTS, Runner, formatRunners, getRunner } from "./runners.mjs";
6
+ export { type AnalyzeScriptOptions, PLATFORMS, type Platform, RUNNERS, RUNNER_AGNOSTIC_SCRIPTS, type Runner, type ScriptEdit, type ScriptFix, type ScriptIssue, type ScriptIssueMessageId, analyzeScript, applyFix, formatPlatforms, formatRunners, getRunner };