package-json-scripts-portability 0.0.1 → 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/README.md +1 -1
- 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 -118
- package/dist/index.mjs +4 -3787
- 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 +22 -20
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { bashLabel } from "./context.mjs";
|
|
2
|
+
import { removal, replacement } from "./fixes.mjs";
|
|
3
|
+
import { BASH_SUGGESTION, NODE_SCRIPT_SUGGESTION, describeReplacement, truncate } from "./messages.mjs";
|
|
4
|
+
import { POSIX_PLATFORMS } from "./platforms.mjs";
|
|
5
|
+
import { mapGetOrInsert } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert.mjs";
|
|
6
|
+
import { setHas } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/set/set-has.mjs";
|
|
7
|
+
import { POSIX_PATH, WHITESPACE, staticValue } from "./words.mjs";
|
|
8
|
+
// src/redirection-checker.ts
|
|
9
|
+
/** The target of a redirection that duplicates or closes a file descriptor, e.g. `1` or `-` */
|
|
10
|
+
const DESCRIPTOR = /^(?:\d+|-)$/;
|
|
11
|
+
/** Device names, which cmd.exe treats as the device in any directory */
|
|
12
|
+
const WINDOWS_DEVICES = /* @__PURE__ */ new Set([
|
|
13
|
+
"aux",
|
|
14
|
+
"con",
|
|
15
|
+
"nul",
|
|
16
|
+
"prn"
|
|
17
|
+
]);
|
|
18
|
+
/** Checks redirections, e.g. for Bash-specific operators and device files */
|
|
19
|
+
var RedirectionChecker = class {
|
|
20
|
+
/** End offsets of here-documents, by the source that they're in */
|
|
21
|
+
hereDocumentEnds = /* @__PURE__ */ new Map();
|
|
22
|
+
#reporter;
|
|
23
|
+
#words;
|
|
24
|
+
constructor(reporter, words) {
|
|
25
|
+
this.#reporter = reporter;
|
|
26
|
+
this.#words = words;
|
|
27
|
+
}
|
|
28
|
+
checkEchoToFile(node, context) {
|
|
29
|
+
const doesWriteFile = node.redirects.some((redirect) => redirect.type === "Redirect" && (redirect.operator === ">" || redirect.operator === ">>") && redirect.descriptor == null && !POSIX_PATH.test(redirect.target?.value || "") && !setHas(WINDOWS_DEVICES, redirect.target?.value.toLowerCase()));
|
|
30
|
+
const quoted = node.args.find((argument) => argument.type === "Word" && argument.parts?.some((part) => part.type === "DoubleQuoted"));
|
|
31
|
+
if (doesWriteFile && quoted) this.#reporter.reportSyntax(context, quoted, "posix", true, "inconsistentBehavior", {
|
|
32
|
+
reason: "cmd.exe's `echo` writes the quotes into the file",
|
|
33
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
34
|
+
syntax: "Writing a double-quoted string with `echo`"
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Checks a redirection and its target.
|
|
39
|
+
* @param redirection Redirection to check, e.g. `> out.log`
|
|
40
|
+
* @param context Where the redirection is
|
|
41
|
+
* @param isWholeCommand Whether the redirection is the whole command, e.g. `>log` in `tsc | >log`
|
|
42
|
+
*/
|
|
43
|
+
checkRedirection(redirection, context, isWholeCommand = false) {
|
|
44
|
+
const text = context.source.slice(redirection.pos, redirection.end);
|
|
45
|
+
switch (redirection.type) {
|
|
46
|
+
case "HereDoc":
|
|
47
|
+
this.#reporter.reportSyntax(context, redirection, "posix", false, "unsupportedSyntax", {
|
|
48
|
+
suggestion: NODE_SCRIPT_SUGGESTION,
|
|
49
|
+
syntax: `The here-document \`${redirection.operator}\``
|
|
50
|
+
});
|
|
51
|
+
if (redirection.closing) mapGetOrInsert(this.hereDocumentEnds, context.source, []).push(redirection.closing.end);
|
|
52
|
+
return;
|
|
53
|
+
case "HereString":
|
|
54
|
+
this.#reporter.reportSyntax(context, redirection, "bash", true, "unsupportedSyntax", {
|
|
55
|
+
suggestion: "Pipe the value into the command instead.",
|
|
56
|
+
syntax: "The Bash-specific here-string `<<<`"
|
|
57
|
+
});
|
|
58
|
+
break;
|
|
59
|
+
case "Redirect": {
|
|
60
|
+
const { descriptor, operator } = redirection;
|
|
61
|
+
if (descriptor?.type === "FileDescriptorVariable") this.#reporter.reportSyntax(context, descriptor, "bash", false, "unsupportedSyntax", {
|
|
62
|
+
suggestion: BASH_SUGGESTION,
|
|
63
|
+
syntax: `The Bash-specific file descriptor variable \`{${descriptor.name}}\``
|
|
64
|
+
});
|
|
65
|
+
const separator = redirection.pos > 0 && !WHITESPACE.test(context.source.charAt(redirection.pos - 1)) ? " " : "";
|
|
66
|
+
const redirectionFix = (fixed) => ({
|
|
67
|
+
description: describeReplacement(text, fixed),
|
|
68
|
+
edits: [{
|
|
69
|
+
end: redirection.end,
|
|
70
|
+
pos: redirection.pos,
|
|
71
|
+
text: `${separator}${fixed}`
|
|
72
|
+
}]
|
|
73
|
+
});
|
|
74
|
+
if (operator === "&>" || operator === "&>>") {
|
|
75
|
+
const kind = operator === "&>" ? "bash" : "bash4";
|
|
76
|
+
this.#reporter.reportSyntax(context, redirection, kind, false, "unsupportedSyntax", {
|
|
77
|
+
suggestion: `Use \`${operator.slice(1)} file 2>&1\` instead.`,
|
|
78
|
+
syntax: `The ${bashLabel(kind)} \`${operator}\` redirection`
|
|
79
|
+
}, redirection.target && redirectionFix(`${operator.slice(1)} ${redirection.target.text} 2>&1`));
|
|
80
|
+
} else if (operator === ">|" || operator === "<>") {
|
|
81
|
+
const position = context.source.indexOf(operator, descriptor?.end || redirection.pos);
|
|
82
|
+
this.#reporter.reportSyntax(context, redirection, "posix", false, "unsupportedSyntax", {
|
|
83
|
+
suggestion: operator === ">|" ? "Use `>` instead." : NODE_SCRIPT_SUGGESTION,
|
|
84
|
+
syntax: `The \`${operator}\` redirection`
|
|
85
|
+
}, operator === ">|" ? replacement({
|
|
86
|
+
end: position + operator.length,
|
|
87
|
+
pos: position
|
|
88
|
+
}, operator, ">") : void 0);
|
|
89
|
+
}
|
|
90
|
+
if (redirection.target && (operator === ">&" || operator === "<&")) {
|
|
91
|
+
const target = staticValue(redirection.target);
|
|
92
|
+
if (target != null && DESCRIPTOR.test(target)) return;
|
|
93
|
+
if (operator === ">&" && target != null) this.#reporter.reportSyntax(context, redirection, "bash", false, "unsupportedSyntax", {
|
|
94
|
+
suggestion: `Use \`> ${truncate(target)} 2>&1\` instead.`,
|
|
95
|
+
syntax: "The Bash-specific `>&` redirection to a file"
|
|
96
|
+
}, descriptor ? void 0 : redirectionFix(`> ${redirection.target.text} 2>&1`));
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!redirection.target) return;
|
|
102
|
+
const target = staticValue(redirection.target);
|
|
103
|
+
if (target != null && WINDOWS_DEVICES.has(target.toLowerCase())) this.#reporter.report(context, redirection.target, "inconsistentBehavior", {
|
|
104
|
+
reason: `POSIX shells create a file named \`${target}\` instead`,
|
|
105
|
+
suggestion: "Remove the redirection or use a Node.js script instead.",
|
|
106
|
+
syntax: `The redirection to \`${target}\``
|
|
107
|
+
}, POSIX_PLATFORMS);
|
|
108
|
+
this.#words.checkWord(redirection.target, context, { redirectTarget: true });
|
|
109
|
+
if (target == null || target === "/dev/null" && context.portable) return;
|
|
110
|
+
const doesDiscardOutput = !isWholeCommand && redirection.type === "Redirect" && (redirection.operator === ">" || redirection.operator === ">>") && target === "/dev/null";
|
|
111
|
+
this.#words.checkPosixPath(redirection.target, target, context, doesDiscardOutput ? {
|
|
112
|
+
description: `Remove \`${truncate(text)}\`.`,
|
|
113
|
+
edits: [removal(context.source, redirection.pos, redirection.end)]
|
|
114
|
+
} : void 0);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
export { RedirectionChecker };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Platform } from "./platforms.mjs";
|
|
2
|
+
import { Runner } from "./runners.mjs";
|
|
3
|
+
export type ScriptIssueMessageId = 'inconsistentBehavior' | 'unsupportedCommand' | 'unsupportedSyntax' | 'unsupportedUsage';
|
|
4
|
+
export interface ScriptEdit {
|
|
5
|
+
/** End offset (exclusive) of the replaced text in the script */
|
|
6
|
+
end: number;
|
|
7
|
+
/** Start offset of the replaced text in the script */
|
|
8
|
+
pos: number;
|
|
9
|
+
/** The replacement */
|
|
10
|
+
text: string;
|
|
11
|
+
}
|
|
12
|
+
export interface ScriptFix {
|
|
13
|
+
/** Describes the fix for people, e.g. "Prefix the command with `cross-env`." */
|
|
14
|
+
description: string;
|
|
15
|
+
/** Replacements that don't overlap */
|
|
16
|
+
edits: ScriptEdit[];
|
|
17
|
+
}
|
|
18
|
+
export interface ScriptIssue {
|
|
19
|
+
/** Message data, apart from `platforms` */
|
|
20
|
+
data: Record<string, string>;
|
|
21
|
+
/** End offset (exclusive) of the issue in the script */
|
|
22
|
+
end: number;
|
|
23
|
+
/** A fix for the issue, if it has an obvious one */
|
|
24
|
+
fix?: ScriptFix;
|
|
25
|
+
messageId: ScriptIssueMessageId;
|
|
26
|
+
/** Platforms on which the script doesn't work as intended */
|
|
27
|
+
platforms: Platform[];
|
|
28
|
+
/** Start offset of the issue in the script */
|
|
29
|
+
pos: number;
|
|
30
|
+
/** Runners with which the script has the issue */
|
|
31
|
+
runners: Runner[];
|
|
32
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { rangeInScript } from "./context.mjs";
|
|
2
|
+
import { MACOS_PLATFORMS, NOT_LINUX_OR_WINDOWS, PLATFORMS, WINDOWS_PLATFORMS } from "./platforms.mjs";
|
|
3
|
+
import { clamp } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/math/clamp.mjs";
|
|
4
|
+
// src/reporter.ts
|
|
5
|
+
/**
|
|
6
|
+
* Gets what identifies an issue, apart from the platforms and the runners that it's found for.
|
|
7
|
+
* @param issue The issue
|
|
8
|
+
* @returns A string that is the same for the same issues
|
|
9
|
+
*/
|
|
10
|
+
const issueKey = (issue) => JSON.stringify([
|
|
11
|
+
issue.messageId,
|
|
12
|
+
issue.pos,
|
|
13
|
+
issue.end,
|
|
14
|
+
issue.data
|
|
15
|
+
]);
|
|
16
|
+
/** Platforms on which each kind of syntax doesn't work, by the shell that runs it */
|
|
17
|
+
const SYNTAX_PLATFORMS = {
|
|
18
|
+
bash: {
|
|
19
|
+
bash: [],
|
|
20
|
+
native: NOT_LINUX_OR_WINDOWS,
|
|
21
|
+
sh: NOT_LINUX_OR_WINDOWS
|
|
22
|
+
},
|
|
23
|
+
bash4: {
|
|
24
|
+
bash: MACOS_PLATFORMS,
|
|
25
|
+
native: PLATFORMS,
|
|
26
|
+
sh: PLATFORMS
|
|
27
|
+
},
|
|
28
|
+
bashNonPosix: {
|
|
29
|
+
bash: [],
|
|
30
|
+
native: PLATFORMS,
|
|
31
|
+
sh: PLATFORMS
|
|
32
|
+
},
|
|
33
|
+
posix: {
|
|
34
|
+
bash: [],
|
|
35
|
+
native: WINDOWS_PLATFORMS,
|
|
36
|
+
sh: []
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var Reporter = class {
|
|
40
|
+
issues = [];
|
|
41
|
+
/**
|
|
42
|
+
* Length of the analyzed script, which unbash's positions can exceed, e.g. after a trailing `\`
|
|
43
|
+
*/
|
|
44
|
+
#length;
|
|
45
|
+
#seen = /* @__PURE__ */ new Set();
|
|
46
|
+
constructor(length) {
|
|
47
|
+
this.#length = length;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Maps a fix's positions to the analyzed script.
|
|
51
|
+
* @param context Where the fix is
|
|
52
|
+
* @param fix The fix, with positions relative to the context's source
|
|
53
|
+
* @returns The mapped fix, or `undefined` if it can't be applied in the context
|
|
54
|
+
*/
|
|
55
|
+
#mapFix(context, fix) {
|
|
56
|
+
if (!fix || context.fixedRange) return;
|
|
57
|
+
const edits = fix.edits.map(({ end, pos, text }) => ({
|
|
58
|
+
end: context.offset + end,
|
|
59
|
+
pos: context.offset + pos,
|
|
60
|
+
text
|
|
61
|
+
}));
|
|
62
|
+
return edits.every(({ end, pos, text }) => pos >= 0 && pos <= end && end <= this.#length && context.fixExclusions.every((exclusion) => !exclusion.test(text))) ? {
|
|
63
|
+
description: fix.description,
|
|
64
|
+
edits
|
|
65
|
+
} : void 0;
|
|
66
|
+
}
|
|
67
|
+
report(context, node, messageId, data, platforms, fix) {
|
|
68
|
+
const remaining = platforms.filter((platform) => !context.covered.has(platform));
|
|
69
|
+
if (remaining.length > 0) {
|
|
70
|
+
const range = rangeInScript(context, node);
|
|
71
|
+
const pos = clamp(range.pos, 0, this.#length);
|
|
72
|
+
const end = clamp(range.end, pos, this.#length);
|
|
73
|
+
const key = issueKey({
|
|
74
|
+
data,
|
|
75
|
+
end,
|
|
76
|
+
messageId,
|
|
77
|
+
pos
|
|
78
|
+
});
|
|
79
|
+
if (!this.#seen.has(key)) {
|
|
80
|
+
this.#seen.add(key);
|
|
81
|
+
this.issues.push({
|
|
82
|
+
data,
|
|
83
|
+
end,
|
|
84
|
+
fix: this.#mapFix(context, fix),
|
|
85
|
+
messageId,
|
|
86
|
+
platforms: remaining,
|
|
87
|
+
pos
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return remaining;
|
|
92
|
+
}
|
|
93
|
+
reportSyntax(context, node, kind, isPortableSupported, messageId, data, fix) {
|
|
94
|
+
const platforms = context.portable ? isPortableSupported ? [] : PLATFORMS : SYNTAX_PLATFORMS[kind][context.shell];
|
|
95
|
+
return this.report(context, node, messageId, data, platforms, fix);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
export { Reporter, issueKey };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** What can run the scripts: a package manager, or `node` for `node --run` */
|
|
2
|
+
export type Runner = 'aube' | 'node' | 'npm' | 'pnpm' | 'pnpm-10' | 'upm' | 'yarn' | 'yarn-classic';
|
|
3
|
+
export declare const RUNNERS: readonly Runner[];
|
|
4
|
+
/**
|
|
5
|
+
* Scripts that run with any package manager by default, as they run on the machine of whoever
|
|
6
|
+
* installs the package: install scripts, and `prepare`, which runs when it's installed from git
|
|
7
|
+
*/
|
|
8
|
+
export declare const RUNNER_AGNOSTIC_SCRIPTS: ReadonlySet<string>;
|
|
9
|
+
/**
|
|
10
|
+
* Gets the runner for a package manager, e.g. the one in the `packageManager` or
|
|
11
|
+
* `devEngines.packageManager` field of package.json.
|
|
12
|
+
* @param name Name of the package manager, e.g. `yarn`
|
|
13
|
+
* @param version Its version or version range, which tells Yarn v1 from Yarn v2+, and pnpm v10
|
|
14
|
+
* from later versions, e.g. `1.22.22`
|
|
15
|
+
* @returns The runner, or `undefined` if it's an unknown package manager or Yarn of an unknown
|
|
16
|
+
* version
|
|
17
|
+
*/
|
|
18
|
+
export declare const getRunner: (name: string, version?: string) => Runner | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Formats runners as a list for people, e.g. "pnpm and Yarn v1".
|
|
21
|
+
* @param runners The runners to format
|
|
22
|
+
* @returns The formatted list
|
|
23
|
+
*/
|
|
24
|
+
export declare const formatRunners: (runners: readonly Runner[]) => string;
|
package/dist/runners.mjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { listFormat } from "./messages.mjs";
|
|
2
|
+
import { allUnionMembers } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/ts/all-union-members.mjs";
|
|
3
|
+
import { arrayDifference } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-difference.mjs";
|
|
4
|
+
import { arrayIntersection } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-intersection.mjs";
|
|
5
|
+
import { setHas } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/set/set-has.mjs";
|
|
6
|
+
// src/runners.ts
|
|
7
|
+
const RUNNERS = allUnionMembers()([
|
|
8
|
+
"aube",
|
|
9
|
+
"node",
|
|
10
|
+
"npm",
|
|
11
|
+
"pnpm",
|
|
12
|
+
"pnpm-10",
|
|
13
|
+
"upm",
|
|
14
|
+
"yarn",
|
|
15
|
+
"yarn-classic"
|
|
16
|
+
]);
|
|
17
|
+
/** Runners that are package managers: all of them but `node`, which doesn't install packages */
|
|
18
|
+
const PACKAGE_MANAGER_RUNNERS = arrayDifference(RUNNERS, ["node"]);
|
|
19
|
+
/**
|
|
20
|
+
* Scripts that run with any package manager by default, as they run on the machine of whoever
|
|
21
|
+
* installs the package: install scripts, and `prepare`, which runs when it's installed from git
|
|
22
|
+
*/
|
|
23
|
+
const RUNNER_AGNOSTIC_SCRIPTS = /* @__PURE__ */ new Set([
|
|
24
|
+
"install",
|
|
25
|
+
"postinstall",
|
|
26
|
+
"preinstall",
|
|
27
|
+
"prepare"
|
|
28
|
+
]);
|
|
29
|
+
/**
|
|
30
|
+
* Checks whether any package manager may run a script.
|
|
31
|
+
* @param scriptName The script's name in package.json
|
|
32
|
+
* @param scripts `true` for all scripts, `false` for none, or whether each script is
|
|
33
|
+
* runner-agnostic, on top of the defaults
|
|
34
|
+
* @returns Whether the script is runner-agnostic
|
|
35
|
+
*/
|
|
36
|
+
const isRunnerAgnosticScript = (scriptName, scripts = {}) => {
|
|
37
|
+
if (typeof scripts === "boolean") return scripts;
|
|
38
|
+
if (scriptName == null) return false;
|
|
39
|
+
return Object.hasOwn(scripts, scriptName) ? scripts[scriptName] === true : RUNNER_AGNOSTIC_SCRIPTS.has(scriptName);
|
|
40
|
+
};
|
|
41
|
+
const RUNNER_TRAITS = {
|
|
42
|
+
aube: { label: "aube" },
|
|
43
|
+
node: {
|
|
44
|
+
label: "`node --run`",
|
|
45
|
+
packageManagerVariables: /* @__PURE__ */ new Set()
|
|
46
|
+
},
|
|
47
|
+
npm: {
|
|
48
|
+
label: "npm",
|
|
49
|
+
setsHome: true
|
|
50
|
+
},
|
|
51
|
+
pnpm: { label: "pnpm" },
|
|
52
|
+
"pnpm-10": {
|
|
53
|
+
label: "pnpm v10",
|
|
54
|
+
setsHome: true
|
|
55
|
+
},
|
|
56
|
+
upm: {
|
|
57
|
+
label: "upm",
|
|
58
|
+
packageManagerVariables: /* @__PURE__ */ new Set([
|
|
59
|
+
"INIT_CWD",
|
|
60
|
+
"npm_lifecycle_event",
|
|
61
|
+
"npm_lifecycle_script",
|
|
62
|
+
"npm_package_json",
|
|
63
|
+
"npm_package_name",
|
|
64
|
+
"npm_package_version"
|
|
65
|
+
])
|
|
66
|
+
},
|
|
67
|
+
yarn: {
|
|
68
|
+
label: "Yarn",
|
|
69
|
+
portableShell: true
|
|
70
|
+
},
|
|
71
|
+
"yarn-classic": {
|
|
72
|
+
convertsSlashes: true,
|
|
73
|
+
label: "Yarn v1"
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
/** Package managers whose runner always has the same name, i.e. all of them but pnpm and Yarn */
|
|
77
|
+
const SAME_NAME_RUNNERS = /* @__PURE__ */ new Set([
|
|
78
|
+
"aube",
|
|
79
|
+
"npm",
|
|
80
|
+
"upm"
|
|
81
|
+
]);
|
|
82
|
+
/** The first number of a version or a version range, e.g. `1` in `^1.22.0` */
|
|
83
|
+
const MAJOR_VERSION = /\d+/;
|
|
84
|
+
/** A version range without an upper bound, e.g. `>=10`, which includes later major versions */
|
|
85
|
+
const UNBOUNDED_RANGE = /^\s*>/;
|
|
86
|
+
/**
|
|
87
|
+
* Gets the runner for a package manager, e.g. the one in the `packageManager` or
|
|
88
|
+
* `devEngines.packageManager` field of package.json.
|
|
89
|
+
* @param name Name of the package manager, e.g. `yarn`
|
|
90
|
+
* @param version Its version or version range, which tells Yarn v1 from Yarn v2+, and pnpm v10
|
|
91
|
+
* from later versions, e.g. `1.22.22`
|
|
92
|
+
* @returns The runner, or `undefined` if it's an unknown package manager or Yarn of an unknown
|
|
93
|
+
* version
|
|
94
|
+
*/
|
|
95
|
+
const getRunner = (name, version = "") => {
|
|
96
|
+
const major = Number(MAJOR_VERSION.exec(version)?.[0]);
|
|
97
|
+
if (name === "pnpm") return major <= 10 && !UNBOUNDED_RANGE.test(version) ? "pnpm-10" : "pnpm";
|
|
98
|
+
if (name !== "yarn") return setHas(SAME_NAME_RUNNERS, name) ? name : void 0;
|
|
99
|
+
if (major === 1) return "yarn-classic";
|
|
100
|
+
return major >= 2 ? "yarn" : void 0;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Gets how a runner runs the scripts.
|
|
104
|
+
* @param runner What runs the scripts
|
|
105
|
+
* @param isShellEmulated Whether pnpm's `shell-emulator` setting is on
|
|
106
|
+
* @returns The runner's traits
|
|
107
|
+
*/
|
|
108
|
+
const runnerTraits = (runner, isShellEmulated) => isShellEmulated && (runner === "pnpm" || runner === "pnpm-10") ? {
|
|
109
|
+
...RUNNER_TRAITS[runner],
|
|
110
|
+
portableShell: true
|
|
111
|
+
} : RUNNER_TRAITS[runner];
|
|
112
|
+
/**
|
|
113
|
+
* Formats runners as a list for people, e.g. "pnpm and Yarn v1".
|
|
114
|
+
* @param runners The runners to format
|
|
115
|
+
* @returns The formatted list
|
|
116
|
+
*/
|
|
117
|
+
const formatRunners = (runners) => listFormat.format(arrayIntersection(RUNNERS, runners).map((runner) => RUNNER_TRAITS[runner].label));
|
|
118
|
+
export { PACKAGE_MANAGER_RUNNERS, RUNNERS, RUNNER_AGNOSTIC_SCRIPTS, formatRunners, getRunner, isRunnerAgnosticScript, runnerTraits };
|