package-json-scripts-portability 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -19
- package/dist/analyze-script.d.mts +10 -3
- package/dist/analyze-script.mjs +24 -15
- package/dist/command-checker.mjs +331 -99
- package/dist/command-usage.mjs +85 -36
- package/dist/commands.mjs +124 -5
- package/dist/context.mjs +8 -1
- package/dist/fixes.d.mts +1 -1
- package/dist/fixes.mjs +193 -49
- package/dist/line-break-checker.mjs +40 -7
- package/dist/messages.mjs +9 -1
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/sorted-array-insertion-index-with.mjs +33 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/sorted-array-insertion-index.mjs +10 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/semver/is-semver-in-range.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/semver/parse-semver.mjs +26 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-after.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/isUnsafeProperty.mjs +18 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/setOwnValue.mjs +23 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/toPropertyKey.mjs +16 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/baseToString.mjs +26 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/hasOwn.mjs +13 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/isDeepKey.mjs +39 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/isIndex.mjs +10 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/tags.mjs +2 -1
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/toKey.mjs +15 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedIndex.mjs +36 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedIndexBy.mjs +54 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedLastIndex.mjs +34 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/array/sortedLastIndexBy.mjs +21 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/function/identity.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/cloneDeep.mjs +52 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/cloneDeepWith.mjs +179 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/get.mjs +67 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/has.mjs +53 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/object/property.mjs +26 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isArguments.mjs +26 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isBuffer.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isMatch.mjs +34 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isMatchWith.mjs +163 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isNaN.mjs +18 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isNil.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isNumber.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isObjectLike.mjs +28 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isSymbol.mjs +19 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isTypedArray.mjs +21 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/matches.mjs +36 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/matchesProperty.mjs +47 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/iteratee.mjs +46 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/toPath.mjs +75 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/toString.mjs +23 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/function/identity.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isEqualWith.mjs +1 -2
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isNil.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isNull.mjs +25 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isPrimitive.mjs +26 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isSymbol.mjs +28 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isTypedArray.mjs +20 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isUndefined.mjs +25 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/remeda/dist/binarySearchCutoffIndex-Bu-OMJ0Z.mjs +26 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/remeda/dist/sortedIndexWith.mjs +7 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/verkit/dist/comparison-CmVirWIW.mjs +108 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/verkit/dist/range-C5wjdo9a.mjs +164 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/verkit/dist/set-BGFWKKE8.mjs +38 -0
- package/dist/redirection-checker.mjs +76 -24
- package/dist/reporter.mjs +63 -18
- package/dist/runners.d.mts +2 -5
- package/dist/runners.mjs +103 -22
- package/dist/script-analyzer.mjs +457 -155
- package/dist/word-checker.mjs +232 -103
- package/dist/words.mjs +34 -7
- package/package.json +27 -23
package/README.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
# 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
|
|
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
|
|
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
|
|
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,8 @@ 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
|
+
With the `platforms` option, both only take those platforms into account.
|
|
162
|
+
Unknown runners are ignored, and without known ones the scripts are checked as npm runs them.
|
|
160
163
|
|
|
161
164
|
### Runner-agnostic scripts
|
|
162
165
|
|
|
@@ -182,14 +185,15 @@ Runner-agnostic scripts aren't checked with `node --run`, since it doesn't insta
|
|
|
182
185
|
|
|
183
186
|
## Options
|
|
184
187
|
|
|
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
|
-
| `
|
|
190
|
-
| `
|
|
191
|
-
| `
|
|
192
|
-
| `
|
|
188
|
+
| Option | Default | Description |
|
|
189
|
+
| ----------------------- | ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
190
|
+
| `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. |
|
|
191
|
+
| `ignoreCommands` | `[]` | Commands not to report, e.g. ones that you install on every platform. The commands that they run are still checked. |
|
|
192
|
+
| `platforms` | All platforms | Platforms that the scripts need to work on. Issues on other platforms aren't reported. |
|
|
193
|
+
| `runner` | `'npm'` | What runs the scripts, or a list of runners, see [Package managers](#package-managers). |
|
|
194
|
+
| `runnerAgnosticScripts` | Install scripts and `prepare` | Scripts that are checked with all package managers, see [Runner-agnostic scripts](#runner-agnostic-scripts). |
|
|
195
|
+
| `scriptName` | None | The script's name in `package.json`, e.g. `postinstall`, to tell whether it's runner-agnostic. |
|
|
196
|
+
| `shellEmulator` | `false` | Whether pnpm's `shell-emulator` setting is on, so that it runs the scripts with Yarn's shell. |
|
|
193
197
|
|
|
194
198
|
## API
|
|
195
199
|
|
|
@@ -204,8 +208,8 @@ Runner-agnostic scripts aren't checked with `node --run`, since it doesn't insta
|
|
|
204
208
|
- `formatPlatforms(platforms)` formats platforms for people, e.g. `'macOS, Linux, and Windows'`.
|
|
205
209
|
- `formatRunners(runners)` formats runners for people, e.g. `'pnpm and Yarn v1'`.
|
|
206
210
|
- `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
|
|
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+.
|
|
211
|
+
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.
|
|
212
|
+
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
213
|
- `PLATFORMS` is the list of all platforms: `['darwin', 'linux', 'win32']`.
|
|
210
214
|
- `RUNNER_AGNOSTIC_SCRIPTS` is the set of the scripts that are runner-agnostic by default: `preinstall`, `install`, `postinstall` and `prepare`.
|
|
211
215
|
- `RUNNERS` is the list of all `runner` values: `['aube', 'node', 'npm', 'pnpm', 'pnpm-10', 'upm', 'yarn', 'yarn-classic']`.
|
|
@@ -216,13 +220,50 @@ The types `AnalyzeScriptOptions`, `Platform`, `Runner`, `ScriptEdit`, `ScriptFix
|
|
|
216
220
|
|
|
217
221
|
- Scripts are only parsed, so it doesn't know which commands are installed or which files exist.
|
|
218
222
|
Commands from npm packages, like `rimraf`, are assumed to work everywhere.
|
|
219
|
-
- The scripts of shells (`sh -c`), cross-env
|
|
223
|
+
- 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
224
|
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
|
|
225
|
+
- Unquoted globs are only reported if they contain `**/`, because many tools expand simpler globs themselves when the shell doesn't.
|
|
226
|
+
- 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.
|
|
227
|
+
In single quotes it isn't, but then the quotes are reported, since cmd.exe passes them on.
|
|
222
228
|
- A script that isn't valid shell syntax is only checked for cmd.exe syntax like `if exist`.
|
|
223
229
|
- `node --run` and upm don't run `pre` and `post` scripts, which isn't checked.
|
|
224
230
|
- Whether the package is private isn't known, so its install scripts are runner-agnostic, unless you tell otherwise.
|
|
225
231
|
|
|
232
|
+
## Contributors
|
|
233
|
+
|
|
234
|
+
<!-- eslint-disable markdown-preferences/padding-line-between-blocks, markdown/require-alt-text -- The list below is generated -->
|
|
235
|
+
<!-- cspell:disable -->
|
|
236
|
+
|
|
237
|
+
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
238
|
+
<!-- prettier-ignore-start -->
|
|
239
|
+
<!-- markdownlint-disable -->
|
|
240
|
+
<table>
|
|
241
|
+
<tbody>
|
|
242
|
+
<tr>
|
|
243
|
+
<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>
|
|
244
|
+
<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>
|
|
245
|
+
</tr>
|
|
246
|
+
</tbody>
|
|
247
|
+
<tfoot>
|
|
248
|
+
<tr>
|
|
249
|
+
<td align="center" size="13px" colspan="7">
|
|
250
|
+
<img src="https://raw.githubusercontent.com/all-contributors/all-contributors-cli/1b8533af435da9854653492b1327a23a4dbd0a10/assets/logo-small.svg">
|
|
251
|
+
<a href="https://all-contributors.js.org/docs/en/bot/usage">Add your contributions</a>
|
|
252
|
+
</img>
|
|
253
|
+
</td>
|
|
254
|
+
</tr>
|
|
255
|
+
</tfoot>
|
|
256
|
+
</table>
|
|
257
|
+
|
|
258
|
+
<!-- markdownlint-restore -->
|
|
259
|
+
<!-- prettier-ignore-end -->
|
|
260
|
+
|
|
261
|
+
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
262
|
+
|
|
263
|
+
<!-- eslint-enable markdown-preferences/padding-line-between-blocks, markdown/require-alt-text -- End of the generated list -->
|
|
264
|
+
|
|
265
|
+
<!-- cspell:enable -->
|
|
266
|
+
|
|
226
267
|
## License
|
|
227
268
|
|
|
228
269
|
[MIT](./LICENSE.md)
|
|
@@ -1,11 +1,18 @@
|
|
|
1
|
+
import { Platform } from "./platforms.mjs";
|
|
1
2
|
import { ScriptIssue } from "./reporter.mjs";
|
|
2
3
|
import { Runner } from "./runners.mjs";
|
|
3
4
|
import { MaybeReadonlyArray } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/types/maybe-readonly-array.mjs";
|
|
4
5
|
export interface AnalyzeScriptOptions {
|
|
5
|
-
/** Names of the package's dependencies, e.g. to detect del-cli's `del` alias */
|
|
6
|
+
/** Names of the package's dependencies, e.g. to detect `del-cli`'s `del` alias */
|
|
6
7
|
dependencies?: readonly string[];
|
|
7
8
|
/** Commands that shouldn't be reported */
|
|
8
9
|
ignoreCommands?: readonly string[];
|
|
10
|
+
/**
|
|
11
|
+
* Platforms that the script needs to work on.
|
|
12
|
+
* Issues are only reported on them, with the runners that have the issue on them.
|
|
13
|
+
* Defaults to all platforms
|
|
14
|
+
*/
|
|
15
|
+
platforms?: readonly Platform[];
|
|
9
16
|
/**
|
|
10
17
|
* What runs the script: a package manager, or `node` for `node --run`.
|
|
11
18
|
* It decides e.g. which shell runs the script and which environment variables it gets.
|
|
@@ -20,13 +27,13 @@ export interface AnalyzeScriptOptions {
|
|
|
20
27
|
* on top of the defaults: `preinstall`, `install`, `postinstall` and `prepare`
|
|
21
28
|
*/
|
|
22
29
|
runnerAgnosticScripts?: boolean | Readonly<Record<string, boolean>>;
|
|
23
|
-
/** The script's name in package.json
|
|
30
|
+
/** The script's name in `package.json`, e.g. `postinstall`, to tell if it's runner-agnostic */
|
|
24
31
|
scriptName?: string;
|
|
25
32
|
/** Whether pnpm's `shell-emulator` setting is on, so that it runs the script with Yarn's shell */
|
|
26
33
|
shellEmulator?: boolean;
|
|
27
34
|
}
|
|
28
35
|
/**
|
|
29
|
-
* Analyzes a package.json script for constructs that don't work (the same) on all platforms.
|
|
36
|
+
* Analyzes a `package.json` script for constructs that don't work (the same) on all platforms.
|
|
30
37
|
* @param script The script's source
|
|
31
38
|
* @param options Analysis options
|
|
32
39
|
* @returns The issues found, with offsets relative to the script
|
package/dist/analyze-script.mjs
CHANGED
|
@@ -1,33 +1,41 @@
|
|
|
1
1
|
import { PLATFORMS } from "./platforms.mjs";
|
|
2
|
-
import {
|
|
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
|
-
|
|
11
|
-
|
|
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
|
+
* @param platforms The platforms to report the issues on
|
|
17
18
|
* @returns The merged issues
|
|
18
19
|
*/
|
|
19
|
-
const mergeIssues = (issuesByRunner) => {
|
|
20
|
+
const mergeIssues = (issuesByRunner, platforms) => {
|
|
20
21
|
const merged = /* @__PURE__ */ new Map();
|
|
22
|
+
const mergedIssues = /* @__PURE__ */ new Set();
|
|
21
23
|
issuesByRunner.forEach((issues, runner) => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
const isMerged = mergedIssues.has(issues);
|
|
25
|
+
mergedIssues.add(issues);
|
|
26
|
+
issues.forEach((issue, key) => {
|
|
27
|
+
const issuePlatforms = arrayIntersection(issue.platforms, platforms);
|
|
28
|
+
if (issuePlatforms.length === 0) return;
|
|
24
29
|
const existing = merged.get(key);
|
|
25
30
|
if (existing) {
|
|
26
|
-
existing.platforms = arrayIntersection(PLATFORMS, [...existing.platforms, ...issue.platforms]);
|
|
27
31
|
existing.runners.push(runner);
|
|
28
|
-
if (!
|
|
32
|
+
if (!isMerged) {
|
|
33
|
+
existing.platforms = arrayIntersection(PLATFORMS, [...existing.platforms, ...issuePlatforms]);
|
|
34
|
+
if (!isEqual(existing.fix, issue.fix)) existing.fix = void 0;
|
|
35
|
+
}
|
|
29
36
|
} else merged.set(key, {
|
|
30
37
|
...issue,
|
|
38
|
+
platforms: issuePlatforms,
|
|
31
39
|
runners: [runner]
|
|
32
40
|
});
|
|
33
41
|
});
|
|
@@ -35,14 +43,15 @@ const mergeIssues = (issuesByRunner) => {
|
|
|
35
43
|
return [...merged.values()];
|
|
36
44
|
};
|
|
37
45
|
/**
|
|
38
|
-
* Analyzes a package.json script for constructs that don't work (the same) on all platforms.
|
|
46
|
+
* Analyzes a `package.json` script for constructs that don't work (the same) on all platforms.
|
|
39
47
|
* @param script The script's source
|
|
40
48
|
* @param options Analysis options
|
|
41
49
|
* @returns The issues found, with offsets relative to the script
|
|
42
50
|
*/
|
|
43
|
-
const analyzeScript = (script, options
|
|
44
|
-
if (
|
|
45
|
-
const
|
|
46
|
-
|
|
51
|
+
const analyzeScript = (script, options) => {
|
|
52
|
+
if (COMMENT_LINES.test(script.trimEnd())) return [];
|
|
53
|
+
const analysisOptions = options || {};
|
|
54
|
+
const runners = isRunnerAgnosticScript(analysisOptions.scriptName, analysisOptions.runnerAgnosticScripts) ? PACKAGE_MANAGER_RUNNERS : arrayify(analysisOptions.runner).filter((runner) => KNOWN_RUNNERS.has(runner));
|
|
55
|
+
return mergeIssues(analyzeWithRunners(script, analysisOptions, runners.length > 0 ? runners : DEFAULT_RUNNERS), analysisOptions.platforms || PLATFORMS);
|
|
47
56
|
};
|
|
48
57
|
export { analyzeScript };
|