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/dist/fixes.mjs
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { describeReplacement, truncate } from "./messages.mjs";
|
|
2
|
+
import { ASSIGNMENT, GLOB_PART_TYPES, isPlainWord, staticValue } from "./words.mjs";
|
|
3
|
+
// src/fixes.ts
|
|
4
|
+
const LINE_WHITESPACE = /[\t ]/;
|
|
5
|
+
const LINE_BREAK = /^\r?\n/;
|
|
6
|
+
/** Options of `rm` that `rimraf` doesn't need, e.g. `-rf` */
|
|
7
|
+
const RM_OPTION = /^(?:-[Rfr]+|--force|--recursive)$/;
|
|
8
|
+
/** A binary of a dependency, which package managers add to `PATH` */
|
|
9
|
+
const DEPENDENCY_BINARY = /^(?:\.\/)?node_modules\/\.bin\/([^/]+)$/;
|
|
10
|
+
const JAVASCRIPT_FILE = /\.[cm]?js$/;
|
|
11
|
+
/** A brace expansion of plain alternatives, e.g. `{a,b}` */
|
|
12
|
+
const SIMPLE_BRACE_EXPANSION = /^\{([\w+\-./@]*(?:,[\w+\-./@]*)+)\}$/;
|
|
13
|
+
const PLAIN_TEXT = /^[\w+\-./:=@]*$/;
|
|
14
|
+
/** Creates a fix that replaces a piece of the script */
|
|
15
|
+
const replacement = (node, original, text) => ({
|
|
16
|
+
description: describeReplacement(original, text),
|
|
17
|
+
edits: [{
|
|
18
|
+
end: node.end,
|
|
19
|
+
pos: node.pos,
|
|
20
|
+
text
|
|
21
|
+
}]
|
|
22
|
+
});
|
|
23
|
+
/** Creates a fix that wraps a word in double quotes */
|
|
24
|
+
const quoting = (word, description) => ({
|
|
25
|
+
description,
|
|
26
|
+
edits: [{
|
|
27
|
+
end: word.pos,
|
|
28
|
+
pos: word.pos,
|
|
29
|
+
text: "\""
|
|
30
|
+
}, {
|
|
31
|
+
end: word.end,
|
|
32
|
+
pos: word.end,
|
|
33
|
+
text: "\""
|
|
34
|
+
}]
|
|
35
|
+
});
|
|
36
|
+
/**
|
|
37
|
+
* Creates an edit that removes a piece of a script, along with the whitespace that separates it.
|
|
38
|
+
* @param source The script
|
|
39
|
+
* @param pos Start offset of the piece
|
|
40
|
+
* @param end End offset (exclusive) of the piece
|
|
41
|
+
* @returns The edit
|
|
42
|
+
*/
|
|
43
|
+
const removal = (source, pos, end) => {
|
|
44
|
+
let start = pos;
|
|
45
|
+
let stop = end;
|
|
46
|
+
while (start > 0 && LINE_WHITESPACE.test(source.charAt(start - 1))) start--;
|
|
47
|
+
if (start === 0 || source[start - 1] === "\n") {
|
|
48
|
+
while (stop < source.length && LINE_WHITESPACE.test(source.charAt(stop))) stop++;
|
|
49
|
+
const lineBreak = LINE_BREAK.exec(source.slice(stop))?.[0];
|
|
50
|
+
if (lineBreak) stop += lineBreak.length;
|
|
51
|
+
else if (stop === source.length && start > 0) start -= source[start - 2] === "\r" ? 2 : 1;
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
end: stop,
|
|
55
|
+
pos: start,
|
|
56
|
+
text: ""
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Gets a fix for a command that isn't available on all platforms, e.g. `rimraf` for `rm -rf`.
|
|
61
|
+
* @param name Name that the command was invoked with, e.g. `rm`
|
|
62
|
+
* @param nameWord Word that the name comes from
|
|
63
|
+
* @param argumentWords The words that follow it
|
|
64
|
+
* @param source The source that node positions refer to
|
|
65
|
+
* @returns The fix, if there's a replacement that works the same
|
|
66
|
+
*/
|
|
67
|
+
const commandFix = (name, nameWord, argumentWords, source) => {
|
|
68
|
+
const first = argumentWords.at(0);
|
|
69
|
+
switch (name) {
|
|
70
|
+
case "env": return first && ASSIGNMENT.test(first.text) && argumentWords.some((word) => !ASSIGNMENT.test(word.text)) ? replacement(nameWord, nameWord.text, "cross-env") : void 0;
|
|
71
|
+
case "rm": {
|
|
72
|
+
const optionCount = argumentWords.findIndex((word) => !RM_OPTION.test(staticValue(word) || ""));
|
|
73
|
+
const paths = optionCount === -1 ? [] : argumentWords.slice(optionCount);
|
|
74
|
+
const globs = paths.filter((word) => word.parts?.some((part) => GLOB_PART_TYPES.includes(part.type)));
|
|
75
|
+
if (paths.length === 0 || paths.some((word) => staticValue(word)?.startsWith("-") && word.text !== "--") || globs.some((word) => !isPlainWord(word, GLOB_PART_TYPES))) return;
|
|
76
|
+
const end = argumentWords[optionCount - 1]?.end || nameWord.end;
|
|
77
|
+
const original = source.slice(nameWord.pos, end);
|
|
78
|
+
return globs.length === 0 ? replacement({
|
|
79
|
+
end,
|
|
80
|
+
pos: nameWord.pos
|
|
81
|
+
}, original, "rimraf") : {
|
|
82
|
+
description: `Replace \`${truncate(original)}\` with \`rimraf --glob\`, and quote the globs.`,
|
|
83
|
+
edits: [{
|
|
84
|
+
end,
|
|
85
|
+
pos: nameWord.pos,
|
|
86
|
+
text: "rimraf --glob"
|
|
87
|
+
}, ...globs.flatMap((word) => quoting(word, "").edits)]
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
case "source": {
|
|
91
|
+
const file = first && staticValue(first);
|
|
92
|
+
if (!first || file == null) return;
|
|
93
|
+
const range = {
|
|
94
|
+
end: first.end,
|
|
95
|
+
pos: nameWord.pos
|
|
96
|
+
};
|
|
97
|
+
return replacement(range, source.slice(range.pos, range.end), `. ${file.includes("/") ? "" : "./"}${first.text}`);
|
|
98
|
+
}
|
|
99
|
+
default: return;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Gets a fix for running a command by its path, e.g. `node ./build.js` for `./build.js`.
|
|
104
|
+
* @param name Path that the command was invoked with
|
|
105
|
+
* @param nameWord Word that the path comes from
|
|
106
|
+
* @returns The fix, if the path is a dependency's binary or a JavaScript file
|
|
107
|
+
*/
|
|
108
|
+
const pathCommandFix = (name, nameWord) => {
|
|
109
|
+
const binary = DEPENDENCY_BINARY.exec(name)?.[1];
|
|
110
|
+
if (binary) return replacement(nameWord, nameWord.text, binary);
|
|
111
|
+
return JAVASCRIPT_FILE.test(name) ? {
|
|
112
|
+
description: "Run the file with `node`.",
|
|
113
|
+
edits: [{
|
|
114
|
+
end: nameWord.pos,
|
|
115
|
+
pos: nameWord.pos,
|
|
116
|
+
text: "node "
|
|
117
|
+
}]
|
|
118
|
+
} : void 0;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Gets a fix that expands a brace expansion, e.g. `dist/a dist/b` for `dist/{a,b}`.
|
|
122
|
+
* @param word The word that contains the brace expansion
|
|
123
|
+
* @param part Brace expansion in the word, e.g. `{a,b}`
|
|
124
|
+
* @returns The fix, if the word only consists of plain text and the brace expansion
|
|
125
|
+
*/
|
|
126
|
+
const braceExpansionFix = (word, part) => {
|
|
127
|
+
const alternatives = SIMPLE_BRACE_EXPANSION.exec(part.text)?.[1]?.split(",");
|
|
128
|
+
const isPlain = (word.parts || []).every((other) => other === part || other.type === "Literal" && PLAIN_TEXT.test(other.text));
|
|
129
|
+
if (!alternatives || !isPlain) return;
|
|
130
|
+
const prefix = word.text.slice(0, part.pos - word.pos);
|
|
131
|
+
const suffix = word.text.slice(part.end - word.pos);
|
|
132
|
+
return replacement(word, word.text, alternatives.map((alternative) => `${prefix}${alternative}${suffix}`).join(" "));
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Checks whether joining a statement to the previous one with `&&` keeps the logic the same,
|
|
136
|
+
* apart from stopping after a failure.
|
|
137
|
+
* @param next The statement that follows
|
|
138
|
+
* @returns Whether the rest of the logic stays the same, e.g. not for `a; b || c`, which would run
|
|
139
|
+
* `c` when `a` fails
|
|
140
|
+
*/
|
|
141
|
+
const canChain = (next) => !next.background && !(next.command.type === "AndOr" && next.command.operators.includes("||"));
|
|
142
|
+
/**
|
|
143
|
+
* Gets a fix for a line break that separates commands.
|
|
144
|
+
* @param gapStart Where the text between the commands starts
|
|
145
|
+
* @param gap The text between the commands, including the line break
|
|
146
|
+
* @param next The statement that follows it
|
|
147
|
+
* @returns The fix, if the text is only whitespace and the rest of the logic stays the same
|
|
148
|
+
*/
|
|
149
|
+
const lineBreakFix = (gapStart, gap, next) => gap.trim().length === 0 && canChain(next) ? {
|
|
150
|
+
description: "Replace the line break with `&&`, which stops at the first failing command.",
|
|
151
|
+
edits: [{
|
|
152
|
+
end: next.pos,
|
|
153
|
+
pos: gapStart,
|
|
154
|
+
text: " && "
|
|
155
|
+
}]
|
|
156
|
+
} : void 0;
|
|
157
|
+
/**
|
|
158
|
+
* Gets a fix that joins the lines of a command, e.g. after `||` or a `\` line continuation.
|
|
159
|
+
* @param source The source that the command is in
|
|
160
|
+
* @param position Where the line break is
|
|
161
|
+
* @param end Where the whitespace after the line break ends
|
|
162
|
+
* @returns The fix, which keeps the command the same in POSIX shells
|
|
163
|
+
*/
|
|
164
|
+
const joinLinesFix = (source, position, end) => {
|
|
165
|
+
return {
|
|
166
|
+
description: "Join the lines.",
|
|
167
|
+
edits: [source[position - 1] === "\\" ? {
|
|
168
|
+
end: position + 1,
|
|
169
|
+
pos: position - 1,
|
|
170
|
+
text: ""
|
|
171
|
+
} : {
|
|
172
|
+
end,
|
|
173
|
+
pos: position,
|
|
174
|
+
text: " "
|
|
175
|
+
}]
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Gets a fix for a `;` that separates commands.
|
|
180
|
+
* @param range Where the `;` is
|
|
181
|
+
* @param gap The text between the commands, including the `;`
|
|
182
|
+
* @param next The statement that follows it, if any
|
|
183
|
+
* @returns The fix, unless the statement that follows has alternatives (`||`) or runs in the
|
|
184
|
+
* background
|
|
185
|
+
*/
|
|
186
|
+
const semicolonFix = (range, gap, next) => {
|
|
187
|
+
if (!next) return {
|
|
188
|
+
description: "Remove the `;`.",
|
|
189
|
+
edits: [{
|
|
190
|
+
...range,
|
|
191
|
+
text: ""
|
|
192
|
+
}]
|
|
193
|
+
};
|
|
194
|
+
return gap.trim() === ";" && canChain(next) ? {
|
|
195
|
+
description: "Replace `;` with `&&`, which stops at the first failing command.",
|
|
196
|
+
edits: [{
|
|
197
|
+
end: next.pos,
|
|
198
|
+
pos: range.pos - gap.indexOf(";"),
|
|
199
|
+
text: " && "
|
|
200
|
+
}]
|
|
201
|
+
} : void 0;
|
|
202
|
+
};
|
|
203
|
+
/**
|
|
204
|
+
* Applies a fix to the script that it was found in.
|
|
205
|
+
* @param script Text of the script to change
|
|
206
|
+
* @param fix A fix from `analyzeScript`
|
|
207
|
+
* @returns The fixed script
|
|
208
|
+
*/
|
|
209
|
+
const applyFix = (script, fix) => fix.edits.toSorted((left, right) => right.pos - left.pos).reduce((result, { end, pos, text }) => result.slice(0, pos) + text + result.slice(end), script);
|
|
210
|
+
export { DEPENDENCY_BINARY, applyFix, braceExpansionFix, commandFix, joinLinesFix, lineBreakFix, pathCommandFix, quoting, removal, replacement, semicolonFix };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AnalyzeScriptOptions, analyzeScript } from "./analyze-script.mjs";
|
|
2
|
+
import { applyFix } from "./fixes.mjs";
|
|
3
|
+
import { PLATFORMS, Platform, formatPlatforms } from "./platforms.mjs";
|
|
4
|
+
import { ScriptEdit, ScriptFix, ScriptIssue, ScriptIssueMessageId } from "./reporter.mjs";
|
|
5
|
+
import { RUNNERS, RUNNER_AGNOSTIC_SCRIPTS, Runner, formatRunners, getRunner } from "./runners.mjs";
|
|
6
|
+
export { type AnalyzeScriptOptions, PLATFORMS, type Platform, RUNNERS, RUNNER_AGNOSTIC_SCRIPTS, type Runner, type ScriptEdit, type ScriptFix, type ScriptIssue, type ScriptIssueMessageId, analyzeScript, applyFix, formatPlatforms, formatRunners, getRunner };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { analyzeScript } from "./analyze-script.mjs";
|
|
2
|
+
import { applyFix } from "./fixes.mjs";
|
|
3
|
+
import { PLATFORMS, formatPlatforms } from "./platforms.mjs";
|
|
4
|
+
import { RUNNERS, RUNNER_AGNOSTIC_SCRIPTS, formatRunners, getRunner } from "./runners.mjs";
|
|
5
|
+
export { PLATFORMS, RUNNERS, RUNNER_AGNOSTIC_SCRIPTS, analyzeScript, applyFix, formatPlatforms, formatRunners, getRunner };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { rangeInScript } from "./context.mjs";
|
|
2
|
+
import { joinLinesFix } from "./fixes.mjs";
|
|
3
|
+
import { slidingWindow } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/iterable/sliding-window.mjs";
|
|
4
|
+
// src/line-break-checker.ts
|
|
5
|
+
const ONE_LINE_SUGGESTION = "Put the command on one line instead.";
|
|
6
|
+
/** Checks line breaks, after which cmd.exe ignores the rest of the script */
|
|
7
|
+
var LineBreakChecker = class {
|
|
8
|
+
/** Ranges of reported line breaks in the analyzed script, e.g. to skip them in nested scripts */
|
|
9
|
+
#lineBreaks = [];
|
|
10
|
+
#multilineSources = /* @__PURE__ */ new Set();
|
|
11
|
+
#reporter;
|
|
12
|
+
constructor(reporter) {
|
|
13
|
+
this.#reporter = reporter;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Reports a line break in a command, e.g. after a `\` line continuation or in a quoted string.
|
|
17
|
+
* @param node The command
|
|
18
|
+
* @param context Where the command is
|
|
19
|
+
*/
|
|
20
|
+
checkCommandLineBreak(node, context) {
|
|
21
|
+
const newline = context.source.slice(node.pos, node.end).indexOf("\n");
|
|
22
|
+
if (newline === -1) return;
|
|
23
|
+
const position = node.pos + newline;
|
|
24
|
+
const isInWord = [
|
|
25
|
+
node.name,
|
|
26
|
+
...node.prefix,
|
|
27
|
+
...node.suffix
|
|
28
|
+
].some((item) => item && item.pos <= position && position < item.end);
|
|
29
|
+
const isQuoted = isInWord && context.source[position - 1] !== "\\";
|
|
30
|
+
this.reportLineBreak(context, position, ONE_LINE_SUGGESTION, isInWord ? void 0 : joinLinesFix(context.source, position, position + 1), isQuoted);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Reports a line break after an operator of a list or pipeline, e.g. after `||`.
|
|
34
|
+
* @param commands The commands that the operators join
|
|
35
|
+
* @param context Where the commands are
|
|
36
|
+
*/
|
|
37
|
+
checkOperatorLineBreaks(commands, context) {
|
|
38
|
+
for (const [command, next] of slidingWindow(commands, 2)) {
|
|
39
|
+
const between = context.source.slice(command.end, next.pos);
|
|
40
|
+
const newline = between.indexOf("\n");
|
|
41
|
+
if (newline !== -1) {
|
|
42
|
+
const position = command.end + newline;
|
|
43
|
+
this.reportLineBreak(context, position, ONE_LINE_SUGGESTION, between.includes("#") ? void 0 : joinLinesFix(context.source, position, next.pos));
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Reports a line break, after which cmd.exe ignores the rest of the script, once per source.
|
|
50
|
+
* @param context Where the line break is
|
|
51
|
+
* @param position Where the line break is in the source
|
|
52
|
+
* @param suggestion What to do instead
|
|
53
|
+
* @param fix A fix for the report
|
|
54
|
+
* @param isPortableSupported Whether Yarn's shell supports it, e.g. in quotes
|
|
55
|
+
*/
|
|
56
|
+
reportLineBreak(context, position, suggestion, fix, isPortableSupported = false) {
|
|
57
|
+
const lineBreak = {
|
|
58
|
+
end: position + 1,
|
|
59
|
+
pos: position
|
|
60
|
+
};
|
|
61
|
+
const range = rangeInScript(context, lineBreak);
|
|
62
|
+
if (this.#lineBreaks.some((reported) => reported.pos < range.end && range.pos < reported.end) || this.#multilineSources.has(context.source)) return;
|
|
63
|
+
if (this.#reporter.reportSyntax(context, lineBreak, "posix", isPortableSupported, "unsupportedSyntax", {
|
|
64
|
+
suggestion,
|
|
65
|
+
syntax: "A multi-line script"
|
|
66
|
+
}, fix).length === 0) return;
|
|
67
|
+
this.#multilineSources.add(context.source);
|
|
68
|
+
this.#lineBreaks.push(range);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
export { LineBreakChecker };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { truncate as truncate$2 } from "./node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/truncate.mjs";
|
|
2
|
+
// src/messages.ts
|
|
3
|
+
const BASH_4_SUGGESTION = "It needs Bash 4 or later, which macOS doesn't ship; use a Node.js script instead.";
|
|
4
|
+
const BASH_SUGGESTION = "Scripts run with `sh` (which isn't Bash on e.g. Debian/Ubuntu) or cmd.exe (on Windows).";
|
|
5
|
+
const CROSS_ENV_SUGGESTION = "Use `cross-env` instead.";
|
|
6
|
+
const CROSS_ENV_VARIABLE_SUGGESTION = "`cross-env` only replaces `$VAR` and `${VAR}` in all versions; use a Node.js script instead.";
|
|
7
|
+
const DEFAULT_SUGGESTION = "Use a cross-platform package or a Node.js script instead.";
|
|
8
|
+
const NODE_SCRIPT_SUGGESTION = "Use a Node.js script instead.";
|
|
9
|
+
const RIMRAF_SUGGESTION = "Use `rimraf` instead.";
|
|
10
|
+
const SHELL_SCRIPT_SUGGESTION = "Shell scripts require a POSIX shell; use a Node.js script instead.";
|
|
11
|
+
const VARIABLE_SUGGESTION = "Use `cross-env` or a Node.js script instead.";
|
|
12
|
+
/** Joins names for people, e.g. "macOS, Linux, and Windows" */
|
|
13
|
+
const listFormat = new Intl.ListFormat("en");
|
|
14
|
+
/** Maximum length of script text in a report message */
|
|
15
|
+
const MAX_SNIPPET_CHARACTERS = 40;
|
|
16
|
+
const WHITESPACE = /\s+/g;
|
|
17
|
+
/**
|
|
18
|
+
* Shortens script text for a report message.
|
|
19
|
+
* @param text Text from the script
|
|
20
|
+
* @returns The text on a single line, cut off at the maximum length
|
|
21
|
+
*/
|
|
22
|
+
const truncate = (text) => truncate$2(text.replaceAll(WHITESPACE, " "), { length: MAX_SNIPPET_CHARACTERS });
|
|
23
|
+
/**
|
|
24
|
+
* Describes a fix that replaces script text, e.g. "Replace `rm -rf` with `rimraf`.".
|
|
25
|
+
* @param original The replaced text
|
|
26
|
+
* @param replacement The text that replaces it
|
|
27
|
+
* @returns The description
|
|
28
|
+
*/
|
|
29
|
+
const describeReplacement = (original, replacement) => `Replace \`${truncate(original)}\` with \`${truncate(replacement)}\`.`;
|
|
30
|
+
export { BASH_4_SUGGESTION, BASH_SUGGESTION, CROSS_ENV_SUGGESTION, CROSS_ENV_VARIABLE_SUGGESTION, DEFAULT_SUGGESTION, NODE_SCRIPT_SUGGESTION, RIMRAF_SUGGESTION, SHELL_SCRIPT_SUGGESTION, VARIABLE_SUGGESTION, describeReplacement, listFormat, truncate };
|
package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-difference.mjs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { differenceBy } from "../vendor/es-toolkit/dist/array/differenceBy.mjs";
|
|
2
|
+
import { differenceWith } from "../vendor/es-toolkit/dist/array/differenceWith.mjs";
|
|
3
|
+
// node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-difference.mjs
|
|
4
|
+
function arrayDifference(firstArray, secondArray, fn) {
|
|
5
|
+
if (!fn) {
|
|
6
|
+
const secondSet = new Set(secondArray);
|
|
7
|
+
return firstArray.filter((item) => !secondSet.has(item));
|
|
8
|
+
}
|
|
9
|
+
return fn.length === 2 ? differenceWith(firstArray, secondArray, fn) : differenceBy(firstArray, secondArray, fn);
|
|
10
|
+
}
|
|
11
|
+
export { arrayDifference };
|
package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-includes.mjs
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { arrayIncludes as arrayIncludes$1 } from "../vendor/ts-extras/distribution/array-includes.mjs";
|
|
2
|
+
// node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-includes.mjs
|
|
3
|
+
/**
|
|
4
|
+
* Checks whether an array has a value, like `Array.prototype.includes`, but as a type guard: when
|
|
5
|
+
* it returns `true`, the value is narrowed to the element type of the array
|
|
6
|
+
*/
|
|
7
|
+
const arrayIncludes = arrayIncludes$1;
|
|
8
|
+
export { arrayIncludes };
|
package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-intersection.mjs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { intersectionWith } from "../vendor/es-toolkit/dist/array/intersectionWith.mjs";
|
|
2
|
+
// node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-intersection.mjs
|
|
3
|
+
function arrayIntersection(firstArray, secondArray, fn) {
|
|
4
|
+
if (!fn) {
|
|
5
|
+
const secondSet = new Set(secondArray);
|
|
6
|
+
return firstArray.filter((item) => secondSet.has(item));
|
|
7
|
+
}
|
|
8
|
+
if (fn.length === 2) return intersectionWith(firstArray, secondArray, fn);
|
|
9
|
+
const mapper = fn;
|
|
10
|
+
const secondKeys = new Set(secondArray.map((item) => mapper(item)));
|
|
11
|
+
return firstArray.filter((item) => secondKeys.has(mapper(item)));
|
|
12
|
+
}
|
|
13
|
+
export { arrayIntersection };
|
package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-is-subset.mjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { isSubsetWith } from "../vendor/es-toolkit/dist/array/isSubsetWith.mjs";
|
|
2
|
+
// node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/array-is-subset.mjs
|
|
3
|
+
function arrayIsSubset(superset, subset, fn) {
|
|
4
|
+
if (!fn) {
|
|
5
|
+
const supersetSet = new Set(superset);
|
|
6
|
+
return subset.every((item) => supersetSet.has(item));
|
|
7
|
+
}
|
|
8
|
+
if (fn.length === 2) {
|
|
9
|
+
const areItemsEqual = fn;
|
|
10
|
+
return isSubsetWith(superset, subset, (subsetItem, supersetItem) => areItemsEqual(supersetItem, subsetItem));
|
|
11
|
+
}
|
|
12
|
+
const mapper = fn;
|
|
13
|
+
const mappedSuperset = new Set(superset.map((item) => mapper(item)));
|
|
14
|
+
return subset.every((item) => mappedSuperset.has(mapper(item)));
|
|
15
|
+
}
|
|
16
|
+
export { arrayIsSubset };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { isArray } from "../predicate/is-array.mjs";
|
|
2
|
+
// node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/array/arrayify.mjs
|
|
3
|
+
function arrayify(value, shouldWrapNullish = false) {
|
|
4
|
+
if (value == null) return shouldWrapNullish ? [value] : [];
|
|
5
|
+
return isArray(value) ? value : [value];
|
|
6
|
+
}
|
|
7
|
+
export { arrayify };
|
package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/iterable/sliding-window.mjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/iterable/sliding-window.mjs
|
|
2
|
+
function* generateWindows(iterable, size, step, shouldYieldPartialWindows) {
|
|
3
|
+
const buffer = [];
|
|
4
|
+
let skip = 0;
|
|
5
|
+
for (const item of iterable) {
|
|
6
|
+
if (skip > 0) {
|
|
7
|
+
skip--;
|
|
8
|
+
continue;
|
|
9
|
+
}
|
|
10
|
+
buffer.push(item);
|
|
11
|
+
if (buffer.length !== size) continue;
|
|
12
|
+
yield buffer.slice();
|
|
13
|
+
if (step >= size) {
|
|
14
|
+
buffer.length = 0;
|
|
15
|
+
skip = step - size;
|
|
16
|
+
} else buffer.splice(0, step);
|
|
17
|
+
}
|
|
18
|
+
if (!shouldYieldPartialWindows || buffer.length === 0) return;
|
|
19
|
+
yield buffer.slice();
|
|
20
|
+
while (buffer.length > step) {
|
|
21
|
+
buffer.splice(0, step);
|
|
22
|
+
yield buffer.slice();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function slidingWindow(iterable, size, step = 1, { partialWindows = false } = {}) {
|
|
26
|
+
if (!Number.isSafeInteger(size) || size < 1) throw new Error("`size` must be a positive integer.");
|
|
27
|
+
if (!Number.isSafeInteger(step) || step < 1) throw new Error("`step` must be a positive integer.");
|
|
28
|
+
return generateWindows(iterable, size, step, partialWindows);
|
|
29
|
+
}
|
|
30
|
+
export { slidingWindow };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert-computed.mjs
|
|
2
|
+
/**
|
|
3
|
+
* Returns the value stored under `key`, first inserting the result of `computeDefaultValue` when
|
|
4
|
+
* the key is not present.
|
|
5
|
+
*
|
|
6
|
+
* The lazy counterpart of `mapGetOrInsert`: `computeDefaultValue` runs only on a miss, so it suits
|
|
7
|
+
* defaults that are expensive to build, must not be shared between keys, or have side effects.
|
|
8
|
+
* An already present value is never overwritten, and a stored `undefined` counts as present.
|
|
9
|
+
*
|
|
10
|
+
* Calls the native `Map#getOrInsertComputed` when the map has it, which doesn't call the `has`,
|
|
11
|
+
* `get` and `set` methods of the map: a `Map` subclass that overrides them should override
|
|
12
|
+
* `getOrInsertComputed` too.
|
|
13
|
+
* @param map - The map to read from.
|
|
14
|
+
* Mutated only when `key` is not present.
|
|
15
|
+
* @param key - The key to look up.
|
|
16
|
+
* @param computeDefaultValue - Produces the value to insert.
|
|
17
|
+
* Called with `key`, and only when `key` is not present.
|
|
18
|
+
* @returns The value already stored under `key`, or the computed value when it has just been
|
|
19
|
+
* inserted.
|
|
20
|
+
* @example
|
|
21
|
+
* // Grouping entries by key: every miss gets its own array
|
|
22
|
+
* const entriesByPackageId = new Map<string, Entry[]>();
|
|
23
|
+
* mapGetOrInsertComputed(entriesByPackageId, packageId, () => []).push(newEntry);
|
|
24
|
+
* @example
|
|
25
|
+
* // The looked-up key is passed to the factory
|
|
26
|
+
* const messagesByChannel = new Map<string, string[]>();
|
|
27
|
+
* mapGetOrInsertComputed(messagesByChannel, 'general', (channel) => [`joined ${channel}`]);
|
|
28
|
+
* // ['joined general']
|
|
29
|
+
* @example
|
|
30
|
+
* // Nothing is computed for a key that is already there
|
|
31
|
+
* mapGetOrInsertComputed(new Map([['alice', 10]]), 'alice', () => expensiveDefault());
|
|
32
|
+
* // 10, `expensiveDefault` never runs
|
|
33
|
+
*/
|
|
34
|
+
const mapGetOrInsertComputed = (map, key, computeDefaultValue) => {
|
|
35
|
+
const nativeMap = map;
|
|
36
|
+
if (nativeMap.getOrInsertComputed) return nativeMap.getOrInsertComputed(key, computeDefaultValue);
|
|
37
|
+
if (map.has(key)) return map.get(key);
|
|
38
|
+
const defaultValue = computeDefaultValue(key);
|
|
39
|
+
map.set(key, defaultValue);
|
|
40
|
+
return defaultValue;
|
|
41
|
+
};
|
|
42
|
+
export { mapGetOrInsertComputed };
|
package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert.mjs
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/map/map-get-or-insert.mjs
|
|
2
|
+
/**
|
|
3
|
+
* Returns the value stored under `key`, first inserting `defaultValue` when the key is not present.
|
|
4
|
+
*
|
|
5
|
+
* The return type is not optional, unlike `Map#get`, so the result is directly usable, which
|
|
6
|
+
* collapses the usual "look up, create when missing, then use" dance into a single expression.
|
|
7
|
+
* An already present value is never overwritten, and a stored `undefined` counts as present.
|
|
8
|
+
*
|
|
9
|
+
* `defaultValue` is evaluated by the caller on every call, even when `key` is already present.
|
|
10
|
+
* Use `mapGetOrInsertComputed` when producing it is expensive or has side effects.
|
|
11
|
+
*
|
|
12
|
+
* Calls the native `Map#getOrInsert` when the map has it, which doesn't call the `has`, `get` and
|
|
13
|
+
* `set` methods of the map: a `Map` subclass that overrides them should override `getOrInsert` too.
|
|
14
|
+
* @param map - The map to read from.
|
|
15
|
+
* Mutated only when `key` is not present.
|
|
16
|
+
* @param key - The key to look up.
|
|
17
|
+
* @param defaultValue - The value to insert and return when `key` is not present.
|
|
18
|
+
* @returns The value already stored under `key`, or `defaultValue` when it has just been inserted.
|
|
19
|
+
* @example
|
|
20
|
+
* // Appending to an array-valued map without a separate existence check
|
|
21
|
+
* const tagsByPostId = new Map<string, string[]>();
|
|
22
|
+
* mapGetOrInsert(tagsByPostId, 'post-1', []).push('typescript');
|
|
23
|
+
* // Map { 'post-1' => ['typescript'] }
|
|
24
|
+
* @example
|
|
25
|
+
* // An existing value is returned as-is
|
|
26
|
+
* mapGetOrInsert(new Map([['alice', 10]]), 'alice', 0);
|
|
27
|
+
* // 10
|
|
28
|
+
* @example
|
|
29
|
+
* // A stored `undefined` is a value, not a miss
|
|
30
|
+
* mapGetOrInsert(new Map<string, number | undefined>([['a', undefined]]), 'a', 42);
|
|
31
|
+
* // undefined
|
|
32
|
+
*/
|
|
33
|
+
const mapGetOrInsert = (map, key, defaultValue) => {
|
|
34
|
+
const nativeMap = map;
|
|
35
|
+
if (nativeMap.getOrInsert) return nativeMap.getOrInsert(key, defaultValue);
|
|
36
|
+
if (map.has(key)) return map.get(key);
|
|
37
|
+
map.set(key, defaultValue);
|
|
38
|
+
return defaultValue;
|
|
39
|
+
};
|
|
40
|
+
export { mapGetOrInsert };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { clamp as clamp$1 } from "../vendor/es-toolkit/dist/bigint/clamp.mjs";
|
|
2
|
+
import { clamp as clamp$2 } from "../vendor/es-toolkit/dist/math/clamp.mjs";
|
|
3
|
+
// node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/math/clamp.mjs
|
|
4
|
+
function clamp(value, bound1, bound2) {
|
|
5
|
+
return typeof value === "bigint" ? clamp$1(value, bound1, bound2) : clamp$2(value, bound1, bound2);
|
|
6
|
+
}
|
|
7
|
+
export { clamp };
|
package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/path/to-forward-slashes.mjs
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { slash } from "../vendor/@antfu/utils/dist/index.mjs";
|
|
2
|
+
// node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/path/to-forward-slashes.mjs
|
|
3
|
+
/**
|
|
4
|
+
* Replaces every backslash in a path with a forward slash, turning a Windows path into the form
|
|
5
|
+
* that URLs, glob patterns and POSIX tools expect
|
|
6
|
+
*/
|
|
7
|
+
const toForwardSlashes = slash;
|
|
8
|
+
export { toForwardSlashes };
|
package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/predicate/is-array.mjs
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { isArray as isArray$1 } from "../vendor/remeda/dist/isArray.mjs";
|
|
2
|
+
// node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/predicate/is-array.mjs
|
|
3
|
+
/**
|
|
4
|
+
* Checks whether a value is an array, like `Array.isArray`, but also narrows readonly arrays.
|
|
5
|
+
*
|
|
6
|
+
* `Array.isArray` narrows to `any[]`, and a readonly array is not assignable to it.
|
|
7
|
+
* So for a `T | readonly T[]` value, it gives `any[]` when `true` and keeps `readonly T[]` when
|
|
8
|
+
* `false`.
|
|
9
|
+
* This guard narrows to the array members of the value's type (or to `readonly unknown[]` if
|
|
10
|
+
* there are none) when `true`, and removes them when `false`.
|
|
11
|
+
* @param data - The value to check.
|
|
12
|
+
* @returns `true` if `data` is an array, `false` otherwise.
|
|
13
|
+
* @example
|
|
14
|
+
* declare const paths: string | readonly string[];
|
|
15
|
+
*
|
|
16
|
+
* if (isArray(paths)) {
|
|
17
|
+
* paths; // readonly string[]
|
|
18
|
+
* } else {
|
|
19
|
+
* paths; // string
|
|
20
|
+
* }
|
|
21
|
+
* @example
|
|
22
|
+
* // Mutable arrays and tuples keep their types
|
|
23
|
+
* declare const size: number | [width: number, height: number];
|
|
24
|
+
*
|
|
25
|
+
* const [width, height] = isArray(size) ? size : [size, size];
|
|
26
|
+
* @example
|
|
27
|
+
* // Narrows `unknown` to a readonly array, so its elements can't be modified by mistake
|
|
28
|
+
* declare const value: unknown;
|
|
29
|
+
*
|
|
30
|
+
* if (isArray(value)) {
|
|
31
|
+
* value; // readonly unknown[]
|
|
32
|
+
* }
|
|
33
|
+
* @example
|
|
34
|
+
* isArray([1, 2, 3]); // true
|
|
35
|
+
* isArray(new Set([1, 2, 3])); // false
|
|
36
|
+
* isArray({length: 0}); // false
|
|
37
|
+
*/
|
|
38
|
+
const isArray = isArray$1;
|
|
39
|
+
export { isArray };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { setHas as setHas$1 } from "../vendor/ts-extras/distribution/set-has.mjs";
|
|
2
|
+
// node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/set/set-has.mjs
|
|
3
|
+
/**
|
|
4
|
+
* Checks whether a set has a value, like `Set.prototype.has`, but as a type guard: when it returns
|
|
5
|
+
* `true`, the value is narrowed to the element type of the set
|
|
6
|
+
*/
|
|
7
|
+
const setHas = setHas$1;
|
|
8
|
+
export { setHas };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-after-last.mjs
|
|
2
|
+
/**
|
|
3
|
+
* Gets the part of a string after the last occurrence of a delimiter.
|
|
4
|
+
* When the delimiter is missing, returns `fallback`, which defaults to the
|
|
5
|
+
* whole string.
|
|
6
|
+
* @param text - The string to search.
|
|
7
|
+
* @param delimiter - The substring to search for.
|
|
8
|
+
* @param fallback - What to return when the delimiter is missing (defaults to `text`).
|
|
9
|
+
* @returns The part after the last delimiter, or `fallback`.
|
|
10
|
+
* @example
|
|
11
|
+
* substringAfterLast('src/utils/index.ts', '/');
|
|
12
|
+
* // 'index.ts'
|
|
13
|
+
* @example
|
|
14
|
+
* // File extension, empty when there is none
|
|
15
|
+
* substringAfterLast('index.test.ts', '.', '');
|
|
16
|
+
* // 'ts'
|
|
17
|
+
* substringAfterLast('Makefile', '.', '');
|
|
18
|
+
* // ''
|
|
19
|
+
*/
|
|
20
|
+
const substringAfterLast = (text, delimiter, fallback = text) => {
|
|
21
|
+
const index = text.lastIndexOf(delimiter);
|
|
22
|
+
return index === -1 ? fallback : text.slice(index + delimiter.length);
|
|
23
|
+
};
|
|
24
|
+
export { substringAfterLast };
|
package/dist/node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-before.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// node_modules/.pnpm/utilery@0.4.0/node_modules/utilery/dist/string/substring-before.mjs
|
|
2
|
+
/**
|
|
3
|
+
* Gets the part of a string before the first occurrence of a delimiter.
|
|
4
|
+
* When the delimiter is missing, returns `fallback`, which defaults to the
|
|
5
|
+
* whole string.
|
|
6
|
+
* @param text - The string to search.
|
|
7
|
+
* @param delimiter - The substring to search for.
|
|
8
|
+
* @param fallback - What to return when the delimiter is missing (defaults to `text`).
|
|
9
|
+
* @returns The part before the first delimiter, or `fallback`.
|
|
10
|
+
* @example
|
|
11
|
+
* substringBefore('index.test.ts', '.');
|
|
12
|
+
* // 'index'
|
|
13
|
+
* substringBefore('README', '.');
|
|
14
|
+
* // 'README'
|
|
15
|
+
* @example
|
|
16
|
+
* // A different result for a missing delimiter
|
|
17
|
+
* substringBefore('no-scheme', '://', '');
|
|
18
|
+
* // ''
|
|
19
|
+
*/
|
|
20
|
+
const substringBefore = (text, delimiter, fallback = text) => {
|
|
21
|
+
const index = text.indexOf(delimiter);
|
|
22
|
+
return index === -1 ? fallback : text.slice(0, index);
|
|
23
|
+
};
|
|
24
|
+
export { substringBefore };
|