package-json-scripts-portability 0.0.0 → 0.0.2
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/LICENSE.md +21 -0
- package/README.md +235 -45
- package/dist/analyze-script.d.mts +34 -0
- package/dist/analyze-script.mjs +48 -0
- package/dist/command-checker.mjs +295 -0
- package/dist/command-usage.mjs +364 -0
- package/dist/commands.mjs +520 -0
- package/dist/context.mjs +18 -0
- package/dist/fixes.d.mts +9 -0
- package/dist/fixes.mjs +210 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.mjs +5 -0
- package/dist/line-break-checker.mjs +71 -0
- package/dist/messages.mjs +30 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-difference.mjs +11 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-includes.mjs +8 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-intersection.mjs +13 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-is-subset.mjs +16 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/arrayify.mjs +7 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/iterable/sliding-window.mjs +30 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert-computed.mjs +42 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert.mjs +40 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/math/clamp.mjs +7 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/path/to-forward-slashes.mjs +8 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/predicate/is-array.mjs +39 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/set/set-has.mjs +8 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-after-last.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-before.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/truncate.mjs +46 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/ts/all-union-members.mjs +45 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/types/maybe-readonly-array.d.mts +17 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/value/is-equal.mjs +37 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/@antfu/utils/dist/index.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/_internal/globalThis.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/differenceBy.mjs +40 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/differenceWith.mjs +37 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/intersectionWith.mjs +43 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/array/isSubsetWith.mjs +36 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/bigint/clamp.mjs +15 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/getSymbols.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/getTag.mjs +13 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/regexMultiByte.mjs +10 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/_internal/tags.mjs +27 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/predicate/isObject.mjs +28 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/string/truncate.mjs +78 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/compat/util/eq.mjs +18 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/function/noop.mjs +12 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/math/clamp.mjs +23 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isBuffer.mjs +24 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isEqual.mjs +21 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isEqualWith.mjs +151 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/es-toolkit/dist/predicate/isPlainObject.mjs +50 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/remeda/dist/isArray.mjs +18 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/ts-extras/distribution/array-includes.mjs +5 -0
- package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/vendor/ts-extras/distribution/set-has.mjs +5 -0
- package/dist/platforms.d.mts +8 -0
- package/dist/platforms.mjs +26 -0
- package/dist/redirection-checker.mjs +117 -0
- package/dist/reporter.d.mts +32 -0
- package/dist/reporter.mjs +98 -0
- package/dist/runners.d.mts +24 -0
- package/dist/runners.mjs +118 -0
- package/dist/script-analyzer.mjs +551 -0
- package/dist/word-checker.mjs +444 -0
- package/dist/words.mjs +43 -0
- package/package.json +118 -6
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 andreww2012
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,45 +1,235 @@
|
|
|
1
|
-
# package-json-scripts-portability
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
1
|
+
# package-json-scripts-portability [](https://npmx.dev/package-json-scripts-portability)
|
|
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.
|
|
5
|
+
|
|
6
|
+
- 🖥️ **Every platform**: cmd.exe on Windows, Bash 3.2 and BSD tools on macOS, dash and GNU tools on Linux.
|
|
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.
|
|
9
|
+
- 🔀 **Fallbacks**: `rm -rf dist || rmdir /s /q dist` works everywhere, so it's not reported.
|
|
10
|
+
- 🔧 **Safe fixes**: only when the script keeps working the same way, e.g. `rimraf dist` for `rm -rf dist`.
|
|
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.
|
|
13
|
+
|
|
14
|
+
## Examples
|
|
15
|
+
|
|
16
|
+
| Script | Breaks on | Why | Fix |
|
|
17
|
+
| ------------------------------ | -------------- | ----------------------------------------------------------- | --------------------------------------- |
|
|
18
|
+
| `rm -rf dist` | Windows | There's no `rm` | `rimraf dist` |
|
|
19
|
+
| `NODE_ENV=production webpack` | Windows | cmd.exe doesn't support inline environment variables | `cross-env NODE_ENV=production webpack` |
|
|
20
|
+
| `eslint 'src/**/*.ts'` | Windows | cmd.exe passes the single quotes on to the command | `eslint "src/**/*.ts"` |
|
|
21
|
+
| `tsc; vite build` | Windows | cmd.exe doesn't support `;` | `tsc && vite build` |
|
|
22
|
+
| `npm run a & npm run b` | Windows | cmd.exe runs the commands one by one | |
|
|
23
|
+
| `node x.js > /dev/null` | Windows | There's no `/dev/null` | `node x.js` |
|
|
24
|
+
| `mkdir -p dist/assets` | Windows | cmd.exe's `mkdir` doesn't support `-p` | |
|
|
25
|
+
| `open http://localhost:3000` | Linux, Windows | `open` only exists on macOS | |
|
|
26
|
+
| `sed -i 's/a/b/' file.txt` | macOS, Windows | BSD `sed -i` requires a backup suffix, Windows has no `sed` | |
|
|
27
|
+
| `sh -c "[[ -f x ]] && node x"` | Linux, Windows | dash doesn't support `[[ ... ]]`, Windows has no `sh` | |
|
|
28
|
+
| `node scripts\build.js` | macOS, Linux | POSIX shells treat backslashes as escape characters | `node scripts/build.js` |
|
|
29
|
+
| `if exist dist rd /s /q dist` | macOS, Linux | It's cmd.exe syntax | |
|
|
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"`.
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
pnpm i package-json-scripts-portability
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Replace `pnpm i` with the installation command of your package manager.
|
|
40
|
+
|
|
41
|
+
Node.js 22+ is required.
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import {analyzeScript, applyFix, formatPlatforms} from 'package-json-scripts-portability';
|
|
45
|
+
|
|
46
|
+
const script = 'rm -rf dist';
|
|
47
|
+
|
|
48
|
+
analyzeScript(script).forEach((issue) => {
|
|
49
|
+
// {
|
|
50
|
+
// messageId: 'unsupportedCommand',
|
|
51
|
+
// data: {command: 'rm', suggestion: 'Use `rimraf` instead.'},
|
|
52
|
+
// platforms: ['win32'],
|
|
53
|
+
// pos: 0,
|
|
54
|
+
// end: 2,
|
|
55
|
+
// fix: {description: 'Replace `rm -rf` with `rimraf`.', edits: [{pos: 0, end: 6, text: 'rimraf'}]},
|
|
56
|
+
// runners: ['npm'],
|
|
57
|
+
// }
|
|
58
|
+
|
|
59
|
+
script.slice(issue.pos, issue.end); // 'rm'
|
|
60
|
+
formatPlatforms(issue.platforms); // 'Windows'
|
|
61
|
+
|
|
62
|
+
if (issue.fix) {
|
|
63
|
+
applyFix(script, issue.fix); // 'rimraf dist'
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Checking a package.json
|
|
69
|
+
|
|
70
|
+
Pass the dependencies and tell what runs the scripts:
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import {readFile} from 'node:fs/promises';
|
|
74
|
+
import {
|
|
75
|
+
type AnalyzeScriptOptions,
|
|
76
|
+
analyzeScript,
|
|
77
|
+
formatPlatforms,
|
|
78
|
+
getRunner,
|
|
79
|
+
} from 'package-json-scripts-portability';
|
|
80
|
+
|
|
81
|
+
const packageJson = JSON.parse(await readFile('package.json', 'utf8'));
|
|
82
|
+
// e.g. `pnpm@10.0.0`, or `yarn@1.22.22` for Yarn v1
|
|
83
|
+
const packageManager: string = packageJson.packageManager || '';
|
|
84
|
+
const [name = '', version] = packageManager.split('@', 2);
|
|
85
|
+
const options: AnalyzeScriptOptions = {
|
|
86
|
+
dependencies: Object.keys({...packageJson.dependencies, ...packageJson.devDependencies}),
|
|
87
|
+
runner: getRunner(name, version),
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
Object.entries<string>(packageJson.scripts || {}).forEach(([scriptName, script]) => {
|
|
91
|
+
analyzeScript(script, options).forEach((issue) => {
|
|
92
|
+
const text = script.slice(issue.pos, issue.end);
|
|
93
|
+
console.log(`${scriptName}: \`${text}\` breaks on ${formatPlatforms(issue.platforms)}`);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Messages
|
|
99
|
+
|
|
100
|
+
Issues don't contain ready-made messages: `messageId` tells the kind of the issue, and `data` has the parts of the message.
|
|
101
|
+
`platforms` and `runners` aren't in `data`, so that you can format them yourself, for example with `formatPlatforms` and `formatRunners`.
|
|
102
|
+
|
|
103
|
+
| `messageId` | `data` | Example message |
|
|
104
|
+
| ---------------------- | -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
105
|
+
| `unsupportedCommand` | `command`, `suggestion` | `rm` is not available on Windows. Use `rimraf` instead. |
|
|
106
|
+
| `unsupportedSyntax` | `syntax`, `suggestion` | The `;` command separator is not supported on Windows. Use `&&` instead. |
|
|
107
|
+
| `unsupportedUsage` | `usage`, `reason`, `suggestion` | `mkdir -p` is not supported on Windows: cmd.exe's `mkdir` doesn't support POSIX options. Use a Node.js script instead. |
|
|
108
|
+
| `inconsistentBehavior` | `syntax`, `reason`, `suggestion` | The `&` operator works differently on Windows: cmd.exe runs the commands sequentially instead of in the background. Use `concurrently` instead. |
|
|
109
|
+
|
|
110
|
+
These messages come from the following templates.
|
|
111
|
+
They use ESLint's placeholder syntax, so in an ESLint rule you can pass them as `meta.messages` and the `data` below to `context.report()`.
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
import {
|
|
115
|
+
type ScriptIssue,
|
|
116
|
+
type ScriptIssueMessageId,
|
|
117
|
+
formatPlatforms,
|
|
118
|
+
} from 'package-json-scripts-portability';
|
|
119
|
+
|
|
120
|
+
const MESSAGES: Readonly<Record<ScriptIssueMessageId, string>> = {
|
|
121
|
+
inconsistentBehavior: '{{syntax}} works differently on {{platforms}}: {{reason}}. {{suggestion}}',
|
|
122
|
+
unsupportedCommand: '`{{command}}` is not available on {{platforms}}. {{suggestion}}',
|
|
123
|
+
unsupportedSyntax: '{{syntax}} is not supported on {{platforms}}. {{suggestion}}',
|
|
124
|
+
unsupportedUsage: '`{{usage}}` is not supported on {{platforms}}: {{reason}}. {{suggestion}}',
|
|
125
|
+
};
|
|
126
|
+
const PLACEHOLDER = /\{\{(\w+)\}\}/g;
|
|
127
|
+
|
|
128
|
+
const formatIssue = (issue: ScriptIssue) => {
|
|
129
|
+
const data: Record<string, string> = {...issue.data, platforms: formatPlatforms(issue.platforms)};
|
|
130
|
+
return MESSAGES[issue.messageId].replaceAll(PLACEHOLDER, (_, key: string) => data[key] || '');
|
|
131
|
+
};
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Package managers
|
|
135
|
+
|
|
136
|
+
By default, the scripts are checked as npm runs them: with `sh` on macOS and Linux, and with cmd.exe on Windows.
|
|
137
|
+
If something else runs them, set `runner`, e.g. with [`getRunner`](#api) from the `packageManager` field.
|
|
138
|
+
It decides which shell runs the scripts and which environment variables they get.
|
|
139
|
+
For example, unlike npm and pnpm v10, pnpm v11+ doesn't set `HOME`, which Windows doesn't set either, and `node --run` doesn't set `npm_*` variables.
|
|
140
|
+
|
|
141
|
+
| Scripts are run by | Options |
|
|
142
|
+
| ------------------ | ------------------------------------------------------------------------------------ |
|
|
143
|
+
| npm | None, or `runner: 'npm'` |
|
|
144
|
+
| pnpm v11+ | `runner: 'pnpm'`, and `shellEmulator: true` if its `shell-emulator` setting is on |
|
|
145
|
+
| pnpm v10 and older | `runner: 'pnpm-10'`, and `shellEmulator: true` if its `shell-emulator` setting is on |
|
|
146
|
+
| [aube] | `runner: 'aube'` |
|
|
147
|
+
| [upm] | `runner: 'upm'` |
|
|
148
|
+
| Yarn v2+ | `runner: 'yarn'` |
|
|
149
|
+
| Yarn v1 | `runner: 'yarn-classic'` |
|
|
150
|
+
| `node --run` | `runner: 'node'` |
|
|
151
|
+
| Bun | None, but the results for Windows only apply to `bun run --shell=system` |
|
|
152
|
+
| Deno or Nub | Not supported yet, as they run the scripts with their own shells |
|
|
153
|
+
|
|
154
|
+
Whatever runs the scripts, the commands that package managers run are checked too, e.g. in `pnpm exec`, `aubx` or `upx`.
|
|
155
|
+
|
|
156
|
+
### Several runners
|
|
157
|
+
|
|
158
|
+
Pass several runners to report what breaks with any of them, e.g. `runner: ['npm', 'pnpm']`.
|
|
159
|
+
Each issue lists the runners that have it in `runners`, and is on the platforms where any of them has it.
|
|
160
|
+
|
|
161
|
+
### Runner-agnostic scripts
|
|
162
|
+
|
|
163
|
+
Some scripts can run with any package manager.
|
|
164
|
+
For example, the install scripts of a published package run with the package manager of whoever installs it.
|
|
165
|
+
Pass the script's name in `scriptName`, and such scripts are checked with all package managers instead of `runner`:
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
import {analyzeScript, formatRunners} from 'package-json-scripts-portability';
|
|
169
|
+
|
|
170
|
+
const script = 'cross-env CONFIG=${HOME}/.config node install.js';
|
|
171
|
+
|
|
172
|
+
analyzeScript(script, {runner: 'npm', scriptName: 'postinstall'}).forEach((issue) => {
|
|
173
|
+
formatRunners(issue.runners); // 'aube, pnpm, upm, and Yarn v1', since only npm and pnpm v10 set `HOME`
|
|
174
|
+
});
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
By default, `preinstall`, `install`, `postinstall` and `prepare` (which runs when the package is installed from git) are runner-agnostic.
|
|
178
|
+
`runnerAgnosticScripts` changes that: `true` makes all scripts runner-agnostic, `false` none, and an object sets it for each script on top of the defaults, e.g. `{prepare: false, release: true}`.
|
|
179
|
+
Install scripts of a private package only run with its own package manager, so pass `runnerAgnosticScripts: false` for one.
|
|
180
|
+
|
|
181
|
+
Runner-agnostic scripts aren't checked with `node --run`, since it doesn't install packages.
|
|
182
|
+
|
|
183
|
+
## Options
|
|
184
|
+
|
|
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. |
|
|
193
|
+
|
|
194
|
+
## API
|
|
195
|
+
|
|
196
|
+
- `analyzeScript(script, options?)` returns the issues of a script, which are not necessarily sorted by position.
|
|
197
|
+
Each issue has:
|
|
198
|
+
- `messageId` and `data`: see [Messages](#messages).
|
|
199
|
+
- `platforms`: where the script doesn't work as intended, as `process.platform` values: `'darwin'`, `'linux'` or `'win32'`.
|
|
200
|
+
- `pos` and `end`: the offsets of the reported text in the script, `end` excluded.
|
|
201
|
+
- `fix`: a `description` and non-overlapping `edits` (`pos`, `end`, `text`), if the issue has a fix that keeps the script working the same way.
|
|
202
|
+
- `runners`: the runners that have the issue, e.g. `['pnpm', 'yarn-classic']`.
|
|
203
|
+
- `applyFix(script, fix)` returns the script with all edits of a fix applied.
|
|
204
|
+
- `formatPlatforms(platforms)` formats platforms for people, e.g. `'macOS, Linux, and Windows'`.
|
|
205
|
+
- `formatRunners(runners)` formats runners for people, e.g. `'pnpm and Yarn v1'`.
|
|
206
|
+
- `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
|
+
- `PLATFORMS` is the list of all platforms: `['darwin', 'linux', 'win32']`.
|
|
210
|
+
- `RUNNER_AGNOSTIC_SCRIPTS` is the set of the scripts that are runner-agnostic by default: `preinstall`, `install`, `postinstall` and `prepare`.
|
|
211
|
+
- `RUNNERS` is the list of all `runner` values: `['aube', 'node', 'npm', 'pnpm', 'pnpm-10', 'upm', 'yarn', 'yarn-classic']`.
|
|
212
|
+
|
|
213
|
+
The types `AnalyzeScriptOptions`, `Platform`, `Runner`, `ScriptEdit`, `ScriptFix`, `ScriptIssue` and `ScriptIssueMessageId` are exported too.
|
|
214
|
+
|
|
215
|
+
## Limitations
|
|
216
|
+
|
|
217
|
+
- Scripts are only parsed, so it doesn't know which commands are installed or which files exist.
|
|
218
|
+
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.
|
|
220
|
+
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.
|
|
222
|
+
- A script that isn't valid shell syntax is only checked for cmd.exe syntax like `if exist`.
|
|
223
|
+
- `node --run` and upm don't run `pre` and `post` scripts, which isn't checked.
|
|
224
|
+
- Whether the package is private isn't known, so its install scripts are runner-agnostic, unless you tell otherwise.
|
|
225
|
+
|
|
226
|
+
## License
|
|
227
|
+
|
|
228
|
+
[MIT](./LICENSE.md)
|
|
229
|
+
|
|
230
|
+
[aube]: https://aube.sh
|
|
231
|
+
[concurrently]: https://npmx.dev/concurrently
|
|
232
|
+
[cross-env]: https://npmx.dev/cross-env
|
|
233
|
+
[del-cli]: https://npmx.dev/del-cli
|
|
234
|
+
[unbash]: https://npmx.dev/unbash
|
|
235
|
+
[upm]: https://npmx.dev/upm
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ScriptIssue } from "./reporter.mjs";
|
|
2
|
+
import { Runner } from "./runners.mjs";
|
|
3
|
+
import { MaybeReadonlyArray } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/types/maybe-readonly-array.mjs";
|
|
4
|
+
export interface AnalyzeScriptOptions {
|
|
5
|
+
/** Names of the package's dependencies, e.g. to detect del-cli's `del` alias */
|
|
6
|
+
dependencies?: readonly string[];
|
|
7
|
+
/** Commands that shouldn't be reported */
|
|
8
|
+
ignoreCommands?: readonly string[];
|
|
9
|
+
/**
|
|
10
|
+
* What runs the script: a package manager, or `node` for `node --run`.
|
|
11
|
+
* It decides e.g. which shell runs the script and which environment variables it gets.
|
|
12
|
+
* Pass several runners to report what breaks with any of them.
|
|
13
|
+
* Defaults to `npm`
|
|
14
|
+
*/
|
|
15
|
+
runner?: MaybeReadonlyArray<Runner>;
|
|
16
|
+
/**
|
|
17
|
+
* Scripts that any package manager may run, which are checked with all of them instead of
|
|
18
|
+
* `runner`.
|
|
19
|
+
* `true` makes all scripts runner-agnostic, `false` none, and an object sets it for each script
|
|
20
|
+
* on top of the defaults: `preinstall`, `install`, `postinstall` and `prepare`
|
|
21
|
+
*/
|
|
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 */
|
|
24
|
+
scriptName?: string;
|
|
25
|
+
/** Whether pnpm's `shell-emulator` setting is on, so that it runs the script with Yarn's shell */
|
|
26
|
+
shellEmulator?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Analyzes a package.json script for constructs that don't work (the same) on all platforms.
|
|
30
|
+
* @param script The script's source
|
|
31
|
+
* @param options Analysis options
|
|
32
|
+
* @returns The issues found, with offsets relative to the script
|
|
33
|
+
*/
|
|
34
|
+
export declare const analyzeScript: (script: string, options?: AnalyzeScriptOptions) => ScriptIssue[];
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { PLATFORMS } from "./platforms.mjs";
|
|
2
|
+
import { issueKey } from "./reporter.mjs";
|
|
3
|
+
import { PACKAGE_MANAGER_RUNNERS, isRunnerAgnosticScript } from "./runners.mjs";
|
|
4
|
+
import { analyzeWithRunners } from "./script-analyzer.mjs";
|
|
5
|
+
import { arrayIntersection } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-intersection.mjs";
|
|
6
|
+
import { arrayify } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/arrayify.mjs";
|
|
7
|
+
import { isEqual } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/value/is-equal.mjs";
|
|
8
|
+
// src/analyze-script.ts
|
|
9
|
+
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]*$/;
|
|
12
|
+
/**
|
|
13
|
+
* Merges the issues found with each runner, e.g. `rm` with both npm and pnpm.
|
|
14
|
+
* A merged issue is on the platforms where any of the runners has it, and keeps its fix only if
|
|
15
|
+
* all of them suggest the same one.
|
|
16
|
+
* @param issuesByRunner The issues found with each runner
|
|
17
|
+
* @returns The merged issues
|
|
18
|
+
*/
|
|
19
|
+
const mergeIssues = (issuesByRunner) => {
|
|
20
|
+
const merged = /* @__PURE__ */ new Map();
|
|
21
|
+
issuesByRunner.forEach((issues, runner) => {
|
|
22
|
+
issues.forEach((issue) => {
|
|
23
|
+
const key = issueKey(issue);
|
|
24
|
+
const existing = merged.get(key);
|
|
25
|
+
if (existing) {
|
|
26
|
+
existing.platforms = arrayIntersection(PLATFORMS, [...existing.platforms, ...issue.platforms]);
|
|
27
|
+
existing.runners.push(runner);
|
|
28
|
+
if (!isEqual(existing.fix, issue.fix)) existing.fix = void 0;
|
|
29
|
+
} else merged.set(key, {
|
|
30
|
+
...issue,
|
|
31
|
+
runners: [runner]
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
return [...merged.values()];
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Analyzes a package.json script for constructs that don't work (the same) on all platforms.
|
|
39
|
+
* @param script The script's source
|
|
40
|
+
* @param options Analysis options
|
|
41
|
+
* @returns The issues found, with offsets relative to the script
|
|
42
|
+
*/
|
|
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
|
+
};
|
|
48
|
+
export { analyzeScript };
|