1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.replaceEnvVariables = exports.buildIgnorePattern = exports.sortMap = exports.dynamicRequireNoCache = exports.dynamicRequire = exports.DefaultStream = exports.BufferStream = exports.bufferStream = exports.prettifySyncErrors = exports.prettifyAsyncErrors = exports.releaseAfterUseAsync = exports.getMapWithDefault = exports.getSetWithDefault = exports.getArrayWithDefault = exports.getFactoryWithDefault = exports.isIndexableObject = exports.mapAndFind = exports.mapAndFilter = exports.validateEnum = exports.assertNever = exports.overrideType = exports.escapeRegExp = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const fslib_1 = require("@yarnpkg/fslib");
|
6 | const clipanion_1 = require("clipanion");
|
7 | const micromatch_1 = tslib_1.__importDefault(require("micromatch"));
|
8 | const stream_1 = require("stream");
|
9 | function escapeRegExp(str) {
|
10 | return str.replace(/[.*+?^${}()|[\]\\]/g, `\\$&`);
|
11 | }
|
12 | exports.escapeRegExp = escapeRegExp;
|
13 | function overrideType(val) {
|
14 | }
|
15 | exports.overrideType = overrideType;
|
16 | function assertNever(arg) {
|
17 | throw new Error(`Assertion failed: Unexpected object '${arg}'`);
|
18 | }
|
19 | exports.assertNever = assertNever;
|
20 | function validateEnum(def, value) {
|
21 | if (!Object.values(def).includes(value))
|
22 | throw new Error(`Assertion failed: Invalid value for enumeration`);
|
23 | return value;
|
24 | }
|
25 | exports.validateEnum = validateEnum;
|
26 | function mapAndFilter(iterable, cb) {
|
27 | const output = [];
|
28 | for (const value of iterable) {
|
29 | const out = cb(value);
|
30 | if (out !== mapAndFilterSkip) {
|
31 | output.push(out);
|
32 | }
|
33 | }
|
34 | return output;
|
35 | }
|
36 | exports.mapAndFilter = mapAndFilter;
|
37 | const mapAndFilterSkip = Symbol();
|
38 | mapAndFilter.skip = mapAndFilterSkip;
|
39 | function mapAndFind(iterable, cb) {
|
40 | for (const value of iterable) {
|
41 | const out = cb(value);
|
42 | if (out !== mapAndFindSkip) {
|
43 | return out;
|
44 | }
|
45 | }
|
46 | return undefined;
|
47 | }
|
48 | exports.mapAndFind = mapAndFind;
|
49 | const mapAndFindSkip = Symbol();
|
50 | mapAndFind.skip = mapAndFindSkip;
|
51 | function isIndexableObject(value) {
|
52 | return typeof value === `object` && value !== null;
|
53 | }
|
54 | exports.isIndexableObject = isIndexableObject;
|
55 | function getFactoryWithDefault(map, key, factory) {
|
56 | let value = map.get(key);
|
57 | if (typeof value === `undefined`)
|
58 | map.set(key, value = factory());
|
59 | return value;
|
60 | }
|
61 | exports.getFactoryWithDefault = getFactoryWithDefault;
|
62 | function getArrayWithDefault(map, key) {
|
63 | let value = map.get(key);
|
64 | if (typeof value === `undefined`)
|
65 | map.set(key, value = []);
|
66 | return value;
|
67 | }
|
68 | exports.getArrayWithDefault = getArrayWithDefault;
|
69 | function getSetWithDefault(map, key) {
|
70 | let value = map.get(key);
|
71 | if (typeof value === `undefined`)
|
72 | map.set(key, value = new Set());
|
73 | return value;
|
74 | }
|
75 | exports.getSetWithDefault = getSetWithDefault;
|
76 | function getMapWithDefault(map, key) {
|
77 | let value = map.get(key);
|
78 | if (typeof value === `undefined`)
|
79 | map.set(key, value = new Map());
|
80 | return value;
|
81 | }
|
82 | exports.getMapWithDefault = getMapWithDefault;
|
83 |
|
84 |
|
85 | async function releaseAfterUseAsync(fn, cleanup) {
|
86 | if (cleanup == null)
|
87 | return await fn();
|
88 | try {
|
89 | return await fn();
|
90 | }
|
91 | finally {
|
92 | await cleanup();
|
93 | }
|
94 | }
|
95 | exports.releaseAfterUseAsync = releaseAfterUseAsync;
|
96 |
|
97 |
|
98 | async function prettifyAsyncErrors(fn, update) {
|
99 | try {
|
100 | return await fn();
|
101 | }
|
102 | catch (error) {
|
103 | error.message = update(error.message);
|
104 | throw error;
|
105 | }
|
106 | }
|
107 | exports.prettifyAsyncErrors = prettifyAsyncErrors;
|
108 |
|
109 | function prettifySyncErrors(fn, update) {
|
110 | try {
|
111 | return fn();
|
112 | }
|
113 | catch (error) {
|
114 | error.message = update(error.message);
|
115 | throw error;
|
116 | }
|
117 | }
|
118 | exports.prettifySyncErrors = prettifySyncErrors;
|
119 |
|
120 | async function bufferStream(stream) {
|
121 | return await new Promise((resolve, reject) => {
|
122 | const chunks = [];
|
123 | stream.on(`error`, error => {
|
124 | reject(error);
|
125 | });
|
126 | stream.on(`data`, chunk => {
|
127 | chunks.push(chunk);
|
128 | });
|
129 | stream.on(`end`, () => {
|
130 | resolve(Buffer.concat(chunks));
|
131 | });
|
132 | });
|
133 | }
|
134 | exports.bufferStream = bufferStream;
|
135 |
|
136 | class BufferStream extends stream_1.Transform {
|
137 | constructor() {
|
138 | super(...arguments);
|
139 | this.chunks = [];
|
140 | }
|
141 | _transform(chunk, encoding, cb) {
|
142 | if (encoding !== `buffer` || !Buffer.isBuffer(chunk))
|
143 | throw new Error(`Assertion failed: BufferStream only accept buffers`);
|
144 | this.chunks.push(chunk);
|
145 | cb(null, null);
|
146 | }
|
147 | _flush(cb) {
|
148 | cb(null, Buffer.concat(this.chunks));
|
149 | }
|
150 | }
|
151 | exports.BufferStream = BufferStream;
|
152 |
|
153 | class DefaultStream extends stream_1.Transform {
|
154 | constructor(ifEmpty = Buffer.alloc(0)) {
|
155 | super();
|
156 | this.active = true;
|
157 | this.ifEmpty = ifEmpty;
|
158 | }
|
159 | _transform(chunk, encoding, cb) {
|
160 | if (encoding !== `buffer` || !Buffer.isBuffer(chunk))
|
161 | throw new Error(`Assertion failed: DefaultStream only accept buffers`);
|
162 | this.active = false;
|
163 | cb(null, chunk);
|
164 | }
|
165 | _flush(cb) {
|
166 | if (this.active && this.ifEmpty.length > 0) {
|
167 | cb(null, this.ifEmpty);
|
168 | }
|
169 | }
|
170 | }
|
171 | exports.DefaultStream = DefaultStream;
|
172 |
|
173 |
|
174 |
|
175 | function dynamicRequire(path) {
|
176 |
|
177 | if (typeof __non_webpack_require__ !== `undefined`) {
|
178 |
|
179 | return __non_webpack_require__(path);
|
180 | }
|
181 | else {
|
182 | return require(path);
|
183 | }
|
184 | }
|
185 | exports.dynamicRequire = dynamicRequire;
|
186 | function dynamicRequireNoCache(path) {
|
187 | const physicalPath = fslib_1.npath.fromPortablePath(path);
|
188 | const currentCacheEntry = require.cache[physicalPath];
|
189 | delete require.cache[physicalPath];
|
190 | let result;
|
191 | try {
|
192 | result = dynamicRequire(physicalPath);
|
193 | const freshCacheEntry = require.cache[physicalPath];
|
194 | const freshCacheIndex = module.children.indexOf(freshCacheEntry);
|
195 | if (freshCacheIndex !== -1) {
|
196 | module.children.splice(freshCacheIndex, 1);
|
197 | }
|
198 | }
|
199 | finally {
|
200 | require.cache[physicalPath] = currentCacheEntry;
|
201 | }
|
202 | return result;
|
203 | }
|
204 | exports.dynamicRequireNoCache = dynamicRequireNoCache;
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | function sortMap(values, mappers) {
|
214 | const asArray = Array.from(values);
|
215 | if (!Array.isArray(mappers))
|
216 | mappers = [mappers];
|
217 | const stringified = [];
|
218 | for (const mapper of mappers)
|
219 | stringified.push(asArray.map(value => mapper(value)));
|
220 | const indices = asArray.map((_, index) => index);
|
221 | indices.sort((a, b) => {
|
222 | for (const layer of stringified) {
|
223 | const comparison = layer[a] < layer[b] ? -1 : layer[a] > layer[b] ? +1 : 0;
|
224 | if (comparison !== 0) {
|
225 | return comparison;
|
226 | }
|
227 | }
|
228 | return 0;
|
229 | });
|
230 | return indices.map(index => {
|
231 | return asArray[index];
|
232 | });
|
233 | }
|
234 | exports.sortMap = sortMap;
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | function buildIgnorePattern(ignorePatterns) {
|
243 | if (ignorePatterns.length === 0)
|
244 | return null;
|
245 | return ignorePatterns.map(pattern => {
|
246 | return `(${micromatch_1.default.makeRe(pattern, {
|
247 | windows: false,
|
248 | }).source})`;
|
249 | }).join(`|`);
|
250 | }
|
251 | exports.buildIgnorePattern = buildIgnorePattern;
|
252 | function replaceEnvVariables(value, { env }) {
|
253 | const regex = /\${(?<variableName>[\d\w_]+)(?<colon>:)?-?(?<fallback>[^}]+)?}/g;
|
254 | return value.replace(regex, (...args) => {
|
255 | const { variableName, colon, fallback } = args[args.length - 1];
|
256 | const variableExist = Object.prototype.hasOwnProperty.call(env, variableName);
|
257 | const variableValue = env[variableName];
|
258 | if (variableValue)
|
259 | return variableValue;
|
260 | if (variableExist && !variableValue && colon)
|
261 | return fallback;
|
262 | if (variableExist)
|
263 | return variableValue;
|
264 | if (fallback)
|
265 | return fallback;
|
266 | throw new clipanion_1.UsageError(`Environment variable not found (${variableName})`);
|
267 | });
|
268 | }
|
269 | exports.replaceEnvVariables = replaceEnvVariables;
|