package-json-scripts-portability 0.0.2 → 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 (71) hide show
  1. package/README.md +58 -19
  2. package/dist/analyze-script.d.mts +3 -3
  3. package/dist/analyze-script.mjs +19 -14
  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 +25 -21
package/README.md CHANGED
@@ -1,15 +1,15 @@
1
1
  # package-json-scripts-portability [![npm](https://img.shields.io/npm/v/package-json-scripts-portability)](https://npmx.dev/package-json-scripts-portability)
2
2
 
3
- Finds package.json scripts that don't work the same on Windows, macOS and Linux, and suggests fixes.
4
- Package managers run scripts with cmd.exe on Windows and with `sh` elsewhere, and `sh` differs too: it's dash on Debian/Ubuntu and Bash 3.2 on macOS.
3
+ Finds `package.json` scripts that don't work the same on Windows, macOS and Linux, and suggests fixes.
4
+ Package managers run scripts with cmd.exe on Windows and with `sh` elsewhere, and `sh` differs too, as it's dash on Debian/Ubuntu and Bash 3.2 on macOS.
5
5
 
6
6
  - 🖥️ **Every platform**: cmd.exe on Windows, Bash 3.2 and BSD tools on macOS, dash and GNU tools on Linux.
7
7
  - 🔍 **Commands, syntax and options**: `rm`, `open`, `FOO=1 cmd`, `$VAR`, single quotes, `;`, `&`, `[[ ... ]]`, `sed -i`, `mkdir -p`, `%VAR%`, backslash paths and many more.
8
- - 🪆 **Nested scripts**: also checks what `sh -c`, [cross-env], [concurrently], `npx` and the like run.
8
+ - 🪆 **Nested scripts**: also checks what `sh -c`, [`cross-env`][cross-env], [`concurrently`][concurrently], `npx` and the like run.
9
9
  - 🔀 **Fallbacks**: `rm -rf dist || rmdir /s /q dist` works everywhere, so it's not reported.
10
10
  - 🔧 **Safe fixes**: only when the script keeps working the same way, e.g. `rimraf dist` for `rm -rf dist`.
11
11
  - 📦 **Package managers**: npm, pnpm, Yarn, aube, upm and `node --run`, including the quirks of Yarn v1 and the portable shell of Yarn v2+.
12
- - 🔒 **Static**: parses the scripts with [unbash] and never runs them.
12
+ - 🔒 **Static**: parses the scripts with [`unbash`][unbash] and never runs them.
13
13
 
14
14
  ## Examples
15
15
 
@@ -28,7 +28,7 @@ Package managers run scripts with cmd.exe on Windows and with `sh` elsewhere, an
28
28
  | `node scripts\build.js` | macOS, Linux | POSIX shells treat backslashes as escape characters | `node scripts/build.js` |
29
29
  | `if exist dist rd /s /q dist` | macOS, Linux | It's cmd.exe syntax | |
30
30
 
31
- The scripts are shown as they are after JSON decoding: in package.json, `node scripts\build.js` is written as `"node scripts\\build.js"`.
31
+ The scripts are shown as they are after JSON decoding, so `node scripts\build.js` is written as `"node scripts\\build.js"` in `package.json`.
32
32
 
33
33
  ## Usage
34
34
 
@@ -65,7 +65,7 @@ analyzeScript(script).forEach((issue) => {
65
65
  });
66
66
  ```
67
67
 
68
- ### Checking a package.json
68
+ ### Checking a `package.json`
69
69
 
70
70
  Pass the dependencies and tell what runs the scripts:
71
71
 
@@ -97,7 +97,8 @@ Object.entries<string>(packageJson.scripts || {}).forEach(([scriptName, script])
97
97
 
98
98
  ### Messages
99
99
 
100
- Issues don't contain ready-made messages: `messageId` tells the kind of the issue, and `data` has the parts of the message.
100
+ Issues don't contain ready-made messages.
101
+ Instead, `messageId` tells the kind of the issue, and `data` has the parts of the message.
101
102
  `platforms` and `runners` aren't in `data`, so that you can format them yourself, for example with `formatPlatforms` and `formatRunners`.
102
103
 
103
104
  | `messageId` | `data` | Example message |
@@ -157,6 +158,7 @@ Whatever runs the scripts, the commands that package managers run are checked to
157
158
 
158
159
  Pass several runners to report what breaks with any of them, e.g. `runner: ['npm', 'pnpm']`.
159
160
  Each issue lists the runners that have it in `runners`, and is on the platforms where any of them has it.
161
+ Unknown runners are ignored, and without known ones the scripts are checked as npm runs them.
160
162
 
161
163
  ### Runner-agnostic scripts
162
164
 
@@ -182,14 +184,14 @@ Runner-agnostic scripts aren't checked with `node --run`, since it doesn't insta
182
184
 
183
185
  ## Options
184
186
 
185
- | Option | Default | Description |
186
- | ----------------------- | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
187
- | `dependencies` | `[]` | Names of the package's dependencies. With [del-cli], `del` is reported as cmd.exe's builtin, which takes precedence on Windows. |
188
- | `ignoreCommands` | `[]` | Commands not to report, e.g. ones that you install on every platform. The commands that they run are still checked. |
189
- | `runner` | `'npm'` | What runs the scripts, or a list of runners, see [Package managers](#package-managers). |
190
- | `runnerAgnosticScripts` | Install scripts and `prepare` | Scripts that are checked with all package managers, see [Runner-agnostic scripts](#runner-agnostic-scripts). |
191
- | `scriptName` | None | The script's name in package.json, e.g. `postinstall`, to tell whether it's runner-agnostic. |
192
- | `shellEmulator` | `false` | Whether pnpm's `shell-emulator` setting is on, so that it runs the scripts with Yarn's shell. |
187
+ | Option | Default | Description |
188
+ | ----------------------- | ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
189
+ | `dependencies` | `[]` | Names of the package's dependencies. With [`del-cli`][del-cli], `del` is reported as cmd.exe's builtin, which takes precedence on Windows outside Yarn's shell. |
190
+ | `ignoreCommands` | `[]` | Commands not to report, e.g. ones that you install on every platform. The commands that they run are still checked. |
191
+ | `runner` | `'npm'` | What runs the scripts, or a list of runners, see [Package managers](#package-managers). |
192
+ | `runnerAgnosticScripts` | Install scripts and `prepare` | Scripts that are checked with all package managers, see [Runner-agnostic scripts](#runner-agnostic-scripts). |
193
+ | `scriptName` | None | The script's name in `package.json`, e.g. `postinstall`, to tell whether it's runner-agnostic. |
194
+ | `shellEmulator` | `false` | Whether pnpm's `shell-emulator` setting is on, so that it runs the scripts with Yarn's shell. |
193
195
 
194
196
  ## API
195
197
 
@@ -204,8 +206,8 @@ Runner-agnostic scripts aren't checked with `node --run`, since it doesn't insta
204
206
  - `formatPlatforms(platforms)` formats platforms for people, e.g. `'macOS, Linux, and Windows'`.
205
207
  - `formatRunners(runners)` formats runners for people, e.g. `'pnpm and Yarn v1'`.
206
208
  - `getRunner(name, version?)` gets the `runner` value for a package manager, e.g. from the `packageManager` or `devEngines.packageManager` field: `getRunner('yarn', '1.22.22')` is `'yarn-classic'`.
207
- It returns `undefined` for other package managers, like Bun, and for Yarn without a version, since Yarn v1 and v2+ run scripts differently.
208
- For pnpm, it returns `'pnpm-10'` for v10 and older, and `'pnpm'` otherwise, including for no version and for ranges like `>=10`, which allow v11+.
209
+ It returns `undefined` for other package managers, like Bun, and for Yarn without a version or with a range that allows both v1 and v2+, like `>=1`, since they run scripts differently.
210
+ For pnpm, it returns `'pnpm-10'` for v10 and older, and `'pnpm'` otherwise, including for no version and for ranges like `>=10` or `^9 || ^11`, which allow v11+.
209
211
  - `PLATFORMS` is the list of all platforms: `['darwin', 'linux', 'win32']`.
210
212
  - `RUNNER_AGNOSTIC_SCRIPTS` is the set of the scripts that are runner-agnostic by default: `preinstall`, `install`, `postinstall` and `prepare`.
211
213
  - `RUNNERS` is the list of all `runner` values: `['aube', 'node', 'npm', 'pnpm', 'pnpm-10', 'upm', 'yarn', 'yarn-classic']`.
@@ -216,13 +218,50 @@ The types `AnalyzeScriptOptions`, `Platform`, `Runner`, `ScriptEdit`, `ScriptFix
216
218
 
217
219
  - Scripts are only parsed, so it doesn't know which commands are installed or which files exist.
218
220
  Commands from npm packages, like `rimraf`, are assumed to work everywhere.
219
- - The scripts of shells (`sh -c`), cross-env, concurrently, `npx`, `npm exec`, `pnpm dlx` and the like, and of commands like `env` and `xargs` are checked.
221
+ - The scripts of shells (`sh -c`), `cross-env`, `concurrently`, `npx`, `npm exec`, `pnpm dlx` and the like, and of commands like `env` and `xargs` are checked.
220
222
  Other tools that run commands, like `nodemon --exec`, are not, since each of them takes different options.
221
- - Unquoted globs are only reported if they contain `**/`: many tools expand simpler globs themselves when the shell doesn't.
223
+ - Unquoted globs are only reported if they contain `**/`, because many tools expand simpler globs themselves when the shell doesn't.
224
+ - A tool's own `%NAME%` placeholder, like in `--bundle-structure "%LANG_ISO%.json"`, looks the same as a cmd.exe variable, so it's reported as one.
225
+ In single quotes it isn't, but then the quotes are reported, since cmd.exe passes them on.
222
226
  - A script that isn't valid shell syntax is only checked for cmd.exe syntax like `if exist`.
223
227
  - `node --run` and upm don't run `pre` and `post` scripts, which isn't checked.
224
228
  - Whether the package is private isn't known, so its install scripts are runner-agnostic, unless you tell otherwise.
225
229
 
230
+ ## Contributors
231
+
232
+ <!-- eslint-disable markdown-preferences/padding-line-between-blocks, markdown/require-alt-text -- The list below is generated -->
233
+ <!-- cspell:disable -->
234
+
235
+ <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
236
+ <!-- prettier-ignore-start -->
237
+ <!-- markdownlint-disable -->
238
+ <table>
239
+ <tbody>
240
+ <tr>
241
+ <td align="center" valign="top" width="14.28%"><a href="https://github.com/andreww2012"><img src="https://avatars.githubusercontent.com/u/6554045?v=4&size=70" width="70px" height="70px" alt="Andrew Kazakov"/><br /><sub><b>Andrew Kazakov</b></sub></a><br /><a href="https://github.com/andreww2012/package-json-scripts-portability/commits?author=andreww2012" title="Code">💻</a> <a href="https://github.com/andreww2012/package-json-scripts-portability/issues?q=author%3Aandreww2012" title="Bug reports">🐛</a> <a href="https://github.com/andreww2012/package-json-scripts-portability/commits?author=andreww2012" title="Documentation">📖</a> <a href="#ideas-andreww2012" title="Ideas, Planning, & Feedback">🤔</a> <a href="#maintenance-andreww2012" title="Maintenance">🚧</a> <a href="#research-andreww2012" title="Research">🔬</a></td>
242
+ <td align="center" valign="top" width="14.28%"><a href="https://github.com/michaelfaith"><img src="https://avatars.githubusercontent.com/u/8071845?v=4&size=70" width="70px" height="70px" alt="michael faith"/><br /><sub><b>michael faith</b></sub></a><br /><a href="#ideas-michaelfaith" title="Ideas, Planning, & Feedback">🤔</a></td>
243
+ </tr>
244
+ </tbody>
245
+ <tfoot>
246
+ <tr>
247
+ <td align="center" size="13px" colspan="7">
248
+ <img src="https://raw.githubusercontent.com/all-contributors/all-contributors-cli/1b8533af435da9854653492b1327a23a4dbd0a10/assets/logo-small.svg">
249
+ <a href="https://all-contributors.js.org/docs/en/bot/usage">Add your contributions</a>
250
+ </img>
251
+ </td>
252
+ </tr>
253
+ </tfoot>
254
+ </table>
255
+
256
+ <!-- markdownlint-restore -->
257
+ <!-- prettier-ignore-end -->
258
+
259
+ <!-- ALL-CONTRIBUTORS-LIST:END -->
260
+
261
+ <!-- eslint-enable markdown-preferences/padding-line-between-blocks, markdown/require-alt-text -- End of the generated list -->
262
+
263
+ <!-- cspell:enable -->
264
+
226
265
  ## License
227
266
 
228
267
  [MIT](./LICENSE.md)
@@ -2,7 +2,7 @@ import { ScriptIssue } from "./reporter.mjs";
2
2
  import { Runner } from "./runners.mjs";
3
3
  import { MaybeReadonlyArray } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/types/maybe-readonly-array.mjs";
4
4
  export interface AnalyzeScriptOptions {
5
- /** Names of the package's dependencies, e.g. to detect del-cli's `del` alias */
5
+ /** Names of the package's dependencies, e.g. to detect `del-cli`'s `del` alias */
6
6
  dependencies?: readonly string[];
7
7
  /** Commands that shouldn't be reported */
8
8
  ignoreCommands?: readonly string[];
@@ -20,13 +20,13 @@ export interface AnalyzeScriptOptions {
20
20
  * on top of the defaults: `preinstall`, `install`, `postinstall` and `prepare`
21
21
  */
22
22
  runnerAgnosticScripts?: boolean | Readonly<Record<string, boolean>>;
23
- /** The script's name in package.json, e.g. `postinstall`, to tell if it's runner-agnostic */
23
+ /** The script's name in `package.json`, e.g. `postinstall`, to tell if it's runner-agnostic */
24
24
  scriptName?: string;
25
25
  /** Whether pnpm's `shell-emulator` setting is on, so that it runs the script with Yarn's shell */
26
26
  shellEmulator?: boolean;
27
27
  }
28
28
  /**
29
- * Analyzes a package.json script for constructs that don't work (the same) on all platforms.
29
+ * Analyzes a `package.json` script for constructs that don't work (the same) on all platforms.
30
30
  * @param script The script's source
31
31
  * @param options Analysis options
32
32
  * @returns The issues found, with offsets relative to the script
@@ -1,31 +1,35 @@
1
1
  import { PLATFORMS } from "./platforms.mjs";
2
- import { issueKey } from "./reporter.mjs";
3
- import { PACKAGE_MANAGER_RUNNERS, isRunnerAgnosticScript } from "./runners.mjs";
2
+ import { PACKAGE_MANAGER_RUNNERS, RUNNERS, isRunnerAgnosticScript } from "./runners.mjs";
4
3
  import { analyzeWithRunners } from "./script-analyzer.mjs";
5
4
  import { arrayIntersection } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-intersection.mjs";
6
5
  import { arrayify } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/arrayify.mjs";
7
6
  import { isEqual } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/value/is-equal.mjs";
8
7
  // src/analyze-script.ts
9
8
  const DEFAULT_RUNNERS = ["npm"];
10
- /** A script that only consists of a comment line, i.e. documentation rather than a script */
11
- const COMMENT_LINE = /^\s*#[^\n]*$/;
9
+ const KNOWN_RUNNERS = new Set(RUNNERS);
10
+ /** A script that only consists of comment lines, i.e. documentation rather than a script */
11
+ const COMMENT_LINES = /^\s*#[^\n]*(?:\n\s*#[^\n]*)*$/;
12
12
  /**
13
13
  * Merges the issues found with each runner, e.g. `rm` with both npm and pnpm.
14
14
  * A merged issue is on the platforms where any of the runners has it, and keeps its fix only if
15
15
  * all of them suggest the same one.
16
- * @param issuesByRunner The issues found with each runner
16
+ * @param issuesByRunner The issues found with each runner, by their keys
17
17
  * @returns The merged issues
18
18
  */
19
19
  const mergeIssues = (issuesByRunner) => {
20
20
  const merged = /* @__PURE__ */ new Map();
21
+ const mergedIssues = /* @__PURE__ */ new Set();
21
22
  issuesByRunner.forEach((issues, runner) => {
22
- issues.forEach((issue) => {
23
- const key = issueKey(issue);
23
+ const isMerged = mergedIssues.has(issues);
24
+ mergedIssues.add(issues);
25
+ issues.forEach((issue, key) => {
24
26
  const existing = merged.get(key);
25
27
  if (existing) {
26
- existing.platforms = arrayIntersection(PLATFORMS, [...existing.platforms, ...issue.platforms]);
27
28
  existing.runners.push(runner);
28
- if (!isEqual(existing.fix, issue.fix)) existing.fix = void 0;
29
+ if (!isMerged) {
30
+ existing.platforms = arrayIntersection(PLATFORMS, [...existing.platforms, ...issue.platforms]);
31
+ if (!isEqual(existing.fix, issue.fix)) existing.fix = void 0;
32
+ }
29
33
  } else merged.set(key, {
30
34
  ...issue,
31
35
  runners: [runner]
@@ -35,14 +39,15 @@ const mergeIssues = (issuesByRunner) => {
35
39
  return [...merged.values()];
36
40
  };
37
41
  /**
38
- * Analyzes a package.json script for constructs that don't work (the same) on all platforms.
42
+ * Analyzes a `package.json` script for constructs that don't work (the same) on all platforms.
39
43
  * @param script The script's source
40
44
  * @param options Analysis options
41
45
  * @returns The issues found, with offsets relative to the script
42
46
  */
43
- const analyzeScript = (script, options = {}) => {
44
- if (COMMENT_LINE.test(script)) return [];
45
- const runners = isRunnerAgnosticScript(options.scriptName, options.runnerAgnosticScripts) ? PACKAGE_MANAGER_RUNNERS : arrayify(options.runner);
46
- return mergeIssues(analyzeWithRunners(script, options, runners.length > 0 ? runners : DEFAULT_RUNNERS));
47
+ const analyzeScript = (script, options) => {
48
+ if (COMMENT_LINES.test(script.trimEnd())) return [];
49
+ const analysisOptions = options || {};
50
+ const runners = isRunnerAgnosticScript(analysisOptions.scriptName, analysisOptions.runnerAgnosticScripts) ? PACKAGE_MANAGER_RUNNERS : arrayify(analysisOptions.runner).filter((runner) => KNOWN_RUNNERS.has(runner));
51
+ return mergeIssues(analyzeWithRunners(script, analysisOptions, runners.length > 0 ? runners : DEFAULT_RUNNERS));
47
52
  };
48
53
  export { analyzeScript };