package-management 0.0.10 → 0.0.12
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/dist/index.cjs +1293 -0
- package/dist/index.d.cts +887 -0
- package/dist/index.d.mts +887 -0
- package/dist/index.d.ts +887 -0
- package/dist/index.mjs +1229 -0
- package/package.json +4 -1
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1229 @@
|
|
|
1
|
+
import ErrorStackParser from 'error-stack-parser';
|
|
2
|
+
import path$1, { normalize, basename, dirname, relative, join } from 'pathe';
|
|
3
|
+
import process from 'node:process';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { readFileSync, existsSync, writeFileSync } from 'node:fs';
|
|
7
|
+
import { asyncCacheFn } from 'async-cache-fn';
|
|
8
|
+
import { execaSync, execa } from 'execa';
|
|
9
|
+
import { findUp } from 'find-up';
|
|
10
|
+
import { readFile, rm } from 'node:fs/promises';
|
|
11
|
+
import { resolvePathSync } from 'mlly';
|
|
12
|
+
import { globbySync } from 'globby';
|
|
13
|
+
import gitignoreParser from 'parse-gitignore';
|
|
14
|
+
import * as WST from 'workspace-tools';
|
|
15
|
+
import { resolveAlias } from 'pathe/utils';
|
|
16
|
+
import os from 'node:os';
|
|
17
|
+
import { modify, applyEdits } from 'jsonc-parser';
|
|
18
|
+
import { morph } from '@arktype/util';
|
|
19
|
+
import { createStorage, restoreSnapshot, snapshot } from 'unstorage';
|
|
20
|
+
import defineMemoryDriver from 'unstorage/drivers/memory';
|
|
21
|
+
import defineFsLiteDriver from 'unstorage/drivers/fs-lite';
|
|
22
|
+
|
|
23
|
+
const toArray = (data) => Array.isArray(data) ? data : [data];
|
|
24
|
+
const entriesOf = (o) => Object.entries(o);
|
|
25
|
+
const fromEntries = (entries) => Object.fromEntries(entries);
|
|
26
|
+
const notFalsy = (value) => [false, null, void 0].every((v) => v !== value);
|
|
27
|
+
const select = (obj, selection, mode) => {
|
|
28
|
+
if (!selection) return obj;
|
|
29
|
+
const filtered = entriesOf(obj).filter(([key]) => {
|
|
30
|
+
return selection[key];
|
|
31
|
+
});
|
|
32
|
+
return fromEntries(filtered);
|
|
33
|
+
};
|
|
34
|
+
function normalizePath(path, option) {
|
|
35
|
+
if (typeof option === "function") {
|
|
36
|
+
return normalizePath(path);
|
|
37
|
+
}
|
|
38
|
+
if (option === false) {
|
|
39
|
+
return path;
|
|
40
|
+
}
|
|
41
|
+
return normalize(path);
|
|
42
|
+
}
|
|
43
|
+
const invariant = (predicate, message) => {
|
|
44
|
+
if (!predicate) {
|
|
45
|
+
throw new Error(message);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
function getArrayItemAtOffset(arr, index, offset = 0) {
|
|
49
|
+
if (arr === void 0 || index === void 0) return void 0;
|
|
50
|
+
return arr[index + offset];
|
|
51
|
+
}
|
|
52
|
+
function isMatching(a, b) {
|
|
53
|
+
if (!a || !b) return false;
|
|
54
|
+
return a === b;
|
|
55
|
+
}
|
|
56
|
+
function checkResult(cb, options) {
|
|
57
|
+
try {
|
|
58
|
+
return buildSingleProp({
|
|
59
|
+
key: options?.keys?.data ?? "data",
|
|
60
|
+
value: cb()
|
|
61
|
+
});
|
|
62
|
+
} catch (error) {
|
|
63
|
+
return buildSingleProp({
|
|
64
|
+
key: "error",
|
|
65
|
+
value: error
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function isPropertyKey(input) {
|
|
70
|
+
return typeof input === "string" || typeof input === "number" || typeof input === "symbol";
|
|
71
|
+
}
|
|
72
|
+
function buildSingleProp(entry) {
|
|
73
|
+
const [key, value] = Array.isArray(entry) ? entry : [entry.key, entry.value];
|
|
74
|
+
if (!isPropertyKey(key)) {
|
|
75
|
+
return {};
|
|
76
|
+
}
|
|
77
|
+
return { [key]: value };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function findCallerStackFrame(options) {
|
|
81
|
+
return findErrorStackFrame(options, (frame) => frame.isParentOfRootFunction);
|
|
82
|
+
}
|
|
83
|
+
function findErrorStackFrame(options, find) {
|
|
84
|
+
return getErrorStackFrames(options, find)[0];
|
|
85
|
+
}
|
|
86
|
+
function getErrorStackFrames(options, filter) {
|
|
87
|
+
const { error, rootFunctionName, startFrom = "top" } = options;
|
|
88
|
+
const frames = startFrom === "bottom" ? ErrorStackParser.parse(error).reverse() : ErrorStackParser.parse(error);
|
|
89
|
+
const { parsedFrames } = frames.reduce(
|
|
90
|
+
(acc, frame, index) => {
|
|
91
|
+
const parsed = parseFrame({
|
|
92
|
+
frame,
|
|
93
|
+
index,
|
|
94
|
+
rootFunctionName,
|
|
95
|
+
frames
|
|
96
|
+
});
|
|
97
|
+
const isValid = filter ? filter(parsed) : true;
|
|
98
|
+
if (isValid) {
|
|
99
|
+
acc.parsedFrames.push(parsed);
|
|
100
|
+
}
|
|
101
|
+
return acc;
|
|
102
|
+
},
|
|
103
|
+
{ parsedFrames: [] }
|
|
104
|
+
);
|
|
105
|
+
return parsedFrames ?? [];
|
|
106
|
+
}
|
|
107
|
+
function parseFrame(options) {
|
|
108
|
+
const { frame, frames, index, rootFunctionName, cwd, debug } = options ?? {};
|
|
109
|
+
const functionName = parseFunctionName(frame.functionName);
|
|
110
|
+
const isRootFunction = isMatching(functionName, rootFunctionName);
|
|
111
|
+
const beforeFrameFunctionName = parseFunctionName(
|
|
112
|
+
getArrayItemAtOffset(frames, index, -1)?.functionName
|
|
113
|
+
);
|
|
114
|
+
const isParentOfRootFunction = isMatching(
|
|
115
|
+
beforeFrameFunctionName,
|
|
116
|
+
rootFunctionName
|
|
117
|
+
);
|
|
118
|
+
const fileData = frame.fileName ? getFilePathData({ filepath: frame.fileName, cwd }) : void 0;
|
|
119
|
+
const { isFileInCwd: isFrameInScope, ...restFileData } = fileData ?? {};
|
|
120
|
+
const data = {
|
|
121
|
+
...restFileData,
|
|
122
|
+
functionName,
|
|
123
|
+
source: frame.source,
|
|
124
|
+
sourceFunctionName: frame.functionName,
|
|
125
|
+
isFrameInScope,
|
|
126
|
+
place: placeFormatter(index, frames?.length),
|
|
127
|
+
isRootFunction,
|
|
128
|
+
isParentOfRootFunction,
|
|
129
|
+
rootFunctionName
|
|
130
|
+
};
|
|
131
|
+
debug && console.log(data);
|
|
132
|
+
return data;
|
|
133
|
+
}
|
|
134
|
+
function getFilePathData({
|
|
135
|
+
filepath,
|
|
136
|
+
cwd
|
|
137
|
+
}) {
|
|
138
|
+
const workingDir = cwd ?? process.cwd();
|
|
139
|
+
const filePath = normalize(toFilePath(filepath));
|
|
140
|
+
const fileBasename = basename(filePath);
|
|
141
|
+
const dirPath = dirname(filePath);
|
|
142
|
+
const dirBasename = basename(dirPath);
|
|
143
|
+
const relativeFilePath = relative(workingDir, filePath);
|
|
144
|
+
const relativeDirPath = dirname(relativeFilePath);
|
|
145
|
+
const isFileInCwd = filePath.startsWith(workingDir);
|
|
146
|
+
return {
|
|
147
|
+
filePath,
|
|
148
|
+
dirPath,
|
|
149
|
+
relativeFilePath,
|
|
150
|
+
relativeDirPath,
|
|
151
|
+
fileBasename,
|
|
152
|
+
dirBasename,
|
|
153
|
+
isFileInCwd
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function toFilePath(fileName) {
|
|
157
|
+
return fileName.startsWith("file:") ? fileURLToPath(fileName) : fileName;
|
|
158
|
+
}
|
|
159
|
+
function parseFunctionName(functionName) {
|
|
160
|
+
if (!functionName) return void 0;
|
|
161
|
+
return removeStart(functionName, "Module.");
|
|
162
|
+
}
|
|
163
|
+
function removeStart(input, value) {
|
|
164
|
+
return input.startsWith(value) ? input.slice(value.length) : input;
|
|
165
|
+
}
|
|
166
|
+
function placeFormatter(index, total) {
|
|
167
|
+
if (index === void 0 || !total) return void 0;
|
|
168
|
+
return [index, total].join("/");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const _filename = (options) => {
|
|
172
|
+
const { filePath } = findCallerStackFrame({ error: new Error(), ...options }) ?? {};
|
|
173
|
+
return filePath;
|
|
174
|
+
};
|
|
175
|
+
const _dirname = (options) => {
|
|
176
|
+
const { dirPath } = findCallerStackFrame({ error: new Error(), ...options }) ?? {};
|
|
177
|
+
return dirPath;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
async function resolveModule(module) {
|
|
181
|
+
const resolved = await module;
|
|
182
|
+
return resolved.default || resolved;
|
|
183
|
+
}
|
|
184
|
+
function isPackageModuleFound(name, options) {
|
|
185
|
+
return Boolean(resolvePackageModulePath(name, options));
|
|
186
|
+
}
|
|
187
|
+
function resolvePackageModulePath(name, options) {
|
|
188
|
+
const resolvedPath = findResolvedModulePath(
|
|
189
|
+
[`${name}/package.json`, name],
|
|
190
|
+
options
|
|
191
|
+
);
|
|
192
|
+
if (resolvedPath === void 0) {
|
|
193
|
+
console.error(`Could not resolve package ${name}`);
|
|
194
|
+
return void 0;
|
|
195
|
+
}
|
|
196
|
+
return resolvedPath;
|
|
197
|
+
}
|
|
198
|
+
function findResolvedModulePath(paths, options) {
|
|
199
|
+
for (const path of paths) {
|
|
200
|
+
const resolvedPath = resolveModulePath(path, options);
|
|
201
|
+
if (resolvedPath === void 0) continue;
|
|
202
|
+
return resolvedPath;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
function resolveModulePath(modulePath, options) {
|
|
206
|
+
const { normalize = true, paths, ...rest } = options ?? {};
|
|
207
|
+
try {
|
|
208
|
+
const path = normalizePath(modulePath, normalize);
|
|
209
|
+
return resolvePathSync(path, {
|
|
210
|
+
url: paths,
|
|
211
|
+
...rest
|
|
212
|
+
});
|
|
213
|
+
} catch (e) {
|
|
214
|
+
return void 0;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const tsconfig = (tsconfigDir) => ({
|
|
219
|
+
get paths() {
|
|
220
|
+
return globbySync(["tsconfig.json", "tsconfig.*.json"], {
|
|
221
|
+
cwd: tsconfigDir
|
|
222
|
+
}) ?? [];
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
function parseGitignoreFile(gitignorePath, options) {
|
|
227
|
+
return gitignoreParser(gitignorePath, options);
|
|
228
|
+
}
|
|
229
|
+
function getGitignoreData(gitignorePath) {
|
|
230
|
+
const gitignoreFileContent = readFileSync(gitignorePath, "utf-8");
|
|
231
|
+
const gitignores = parseGitignoreFile(gitignoreFileContent);
|
|
232
|
+
return gitignores;
|
|
233
|
+
}
|
|
234
|
+
const gitignore = (gitignorePath) => ({
|
|
235
|
+
get data() {
|
|
236
|
+
return getGitignoreData(gitignorePath);
|
|
237
|
+
},
|
|
238
|
+
get patterns() {
|
|
239
|
+
return getGitignoreData(gitignorePath).patterns;
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
const dependencyTypeMap = {
|
|
244
|
+
dependency: "dependencies",
|
|
245
|
+
devDependency: "devDependencies",
|
|
246
|
+
peerDependency: "peerDependencies",
|
|
247
|
+
optionalDependency: "optionalDependencies"
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
function isDependencyInPackageJson(options, packageJson) {
|
|
251
|
+
return Boolean(findDependencyInPackageJson(options, packageJson));
|
|
252
|
+
}
|
|
253
|
+
function findDependencyInPackageJson(option, packageJson) {
|
|
254
|
+
const { name, type } = typeof option === "string" ? { name: option, type: void 0 } : option;
|
|
255
|
+
if (!packageJson) return void 0;
|
|
256
|
+
const allowedTypes = typeof type === "string" ? {
|
|
257
|
+
[type]: true
|
|
258
|
+
} : type ?? {
|
|
259
|
+
dependency: true,
|
|
260
|
+
devDependency: true
|
|
261
|
+
};
|
|
262
|
+
const allowedDependencyTypes = getAllowedDependencyTypeList(allowedTypes);
|
|
263
|
+
const matches = allowedDependencyTypes.map((type2) => {
|
|
264
|
+
return getPackageJsonDependencyItem({ name, type: type2 }, packageJson);
|
|
265
|
+
}).filter(notFalsy);
|
|
266
|
+
if (matches.length === 0) return void 0;
|
|
267
|
+
return {
|
|
268
|
+
firstMatch: matches[0],
|
|
269
|
+
matches
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
function getPackageJsonDependencyItem({
|
|
273
|
+
name,
|
|
274
|
+
type
|
|
275
|
+
}, packageJson) {
|
|
276
|
+
if (!packageJson) return void 0;
|
|
277
|
+
const dependencyMap = getPackageJsonDependencyMap(type, packageJson);
|
|
278
|
+
const version = dependencyMap?.[name];
|
|
279
|
+
if (version === void 0) return void 0;
|
|
280
|
+
return {
|
|
281
|
+
name,
|
|
282
|
+
version,
|
|
283
|
+
type
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
function getAllowedDependencyTypeList(selected) {
|
|
287
|
+
return Object.keys(dependencyTypeMap).filter(
|
|
288
|
+
(key) => selected[key]
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
function getPackageJsonDependencyMap(type, packageJson) {
|
|
292
|
+
return packageJson[dependencyTypeMap[type]];
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function getWorkspaceFolder(options) {
|
|
296
|
+
const {
|
|
297
|
+
cwd = process.cwd(),
|
|
298
|
+
fallbackToGitRoot = true,
|
|
299
|
+
throwIfNotFound = true
|
|
300
|
+
} = options ?? {};
|
|
301
|
+
const getFolder = fallbackToGitRoot ? WST.findProjectRoot : WST.getWorkspaceManagerRoot;
|
|
302
|
+
const folder = getFolder(cwd);
|
|
303
|
+
if (folder === void 0 && throwIfNotFound) {
|
|
304
|
+
throw new Error(`Could not find workspace folder`);
|
|
305
|
+
}
|
|
306
|
+
return folder;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function getPackageFolder(options) {
|
|
310
|
+
return WST.findPackageRoot(options?.cwd ?? process.cwd());
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function getPackageInfo(options) {
|
|
314
|
+
const packageDir = getPackageFolder(options);
|
|
315
|
+
return readPackageInfo({ packageDir });
|
|
316
|
+
}
|
|
317
|
+
function readPackageInfo({
|
|
318
|
+
packageDir
|
|
319
|
+
}) {
|
|
320
|
+
const packageJsonPath = path.join(packageDir, "package.json");
|
|
321
|
+
const packageJson = JSON.parse(
|
|
322
|
+
readFileSync(packageJsonPath, "utf-8")
|
|
323
|
+
);
|
|
324
|
+
return {
|
|
325
|
+
name: packageJson.name,
|
|
326
|
+
path: packageJsonPath,
|
|
327
|
+
dirpath: packageDir,
|
|
328
|
+
packageJson
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function getWorkspaceProjectInfo(options) {
|
|
333
|
+
try {
|
|
334
|
+
const workspaceDir = getWorkspaceFolder(options);
|
|
335
|
+
return readPackageInfo({ packageDir: workspaceDir });
|
|
336
|
+
} catch (e) {
|
|
337
|
+
console.error(e);
|
|
338
|
+
throw new Error("Root workspace not found");
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function getWorkspacePackageInfoList(options) {
|
|
343
|
+
const { workspaceDir, resolveWstList } = common(options);
|
|
344
|
+
const wstList = WST.getWorkspaceInfos(workspaceDir);
|
|
345
|
+
return resolveWstList(wstList);
|
|
346
|
+
}
|
|
347
|
+
function common(options) {
|
|
348
|
+
const { cwd, includeRoot: includeWorkspace } = options ?? {};
|
|
349
|
+
const workspaceDir = getWorkspaceFolder({ cwd });
|
|
350
|
+
function resolveWstList(list) {
|
|
351
|
+
if (!workspaceDir || !list) return [];
|
|
352
|
+
if (includeWorkspace) return [getWorkspaceProjectInfo(), ...list];
|
|
353
|
+
return list;
|
|
354
|
+
}
|
|
355
|
+
return {
|
|
356
|
+
workspaceDir,
|
|
357
|
+
includeWorkspace,
|
|
358
|
+
resolveWstList
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function getWorkspacePackageInfoMap(options) {
|
|
363
|
+
const workspaceInfo = getWorkspacePackageInfoList(options);
|
|
364
|
+
const entries = workspaceInfo.map((info) => [info.name, info]);
|
|
365
|
+
return Object.fromEntries(entries);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function getProjectInfoByName(name, options) {
|
|
369
|
+
const { cwd } = options ?? {};
|
|
370
|
+
return getWorkspacePackageInfoMap({ cwd })[name];
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function getGitRootFolder(options) {
|
|
374
|
+
const { cwd = process.cwd(), throwIfNotFound = true } = options ?? {};
|
|
375
|
+
const { stdout } = execaSync("git", ["rev-parse", "--show-toplevel"], {
|
|
376
|
+
cwd,
|
|
377
|
+
reject: false
|
|
378
|
+
});
|
|
379
|
+
if (!stdout) {
|
|
380
|
+
if (throwIfNotFound) {
|
|
381
|
+
throw new Error(`Directory "${cwd}" is not in a git repository`);
|
|
382
|
+
}
|
|
383
|
+
return void 0;
|
|
384
|
+
}
|
|
385
|
+
return path$1.normalize(stdout);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function getProjectInfo(folder, options) {
|
|
389
|
+
if (isWorkspaceFolderTypeOption(folder)) {
|
|
390
|
+
return getWorkspaceProjectInfo(
|
|
391
|
+
parseWorkspaceFolderTypeOptions(folder, options)
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
if (folder === "<package_folder>") {
|
|
395
|
+
return getPackageInfo(options);
|
|
396
|
+
}
|
|
397
|
+
if (folder === "<gitroot_folder>") {
|
|
398
|
+
return readPackageInfo({ packageDir: getGitRootFolder(options) });
|
|
399
|
+
}
|
|
400
|
+
return getProjectInfoByName(folder.packageName, options);
|
|
401
|
+
}
|
|
402
|
+
function isWorkspaceFolderTypeOption(option) {
|
|
403
|
+
return Boolean(
|
|
404
|
+
option === "<workspace_folder>" || typeof option === "object" && "<workspace_folder>" in option
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
function parseWorkspaceFolderTypeOptions(option, defaultOptions) {
|
|
408
|
+
if (typeof option === "string") {
|
|
409
|
+
return defaultOptions ?? {};
|
|
410
|
+
}
|
|
411
|
+
return { ...defaultOptions, ...option["<workspace_folder>"] };
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
const project = (...args) => {
|
|
415
|
+
const [source, projectOptions] = args;
|
|
416
|
+
const { packageJson, packageJsonPath, packageName, projectDir } = info();
|
|
417
|
+
const {
|
|
418
|
+
findPackageManager: findPackageManager2,
|
|
419
|
+
detectPackageManagers,
|
|
420
|
+
detectLockfilePackageManagers,
|
|
421
|
+
detectGlobalPackageManagers,
|
|
422
|
+
globalVersions,
|
|
423
|
+
filterPackageManagers,
|
|
424
|
+
mapPackageManagers
|
|
425
|
+
} = definePackageManagerClient({ cwd: projectDir });
|
|
426
|
+
return {
|
|
427
|
+
packageJson,
|
|
428
|
+
packageJsonPath,
|
|
429
|
+
packageName,
|
|
430
|
+
projectDir,
|
|
431
|
+
findPackageManager: findPackageManager2,
|
|
432
|
+
detectPackageManagers,
|
|
433
|
+
detectGlobalPackageManagers,
|
|
434
|
+
detectLockfilePackageManagers,
|
|
435
|
+
globalVersions,
|
|
436
|
+
mapPackageManagers,
|
|
437
|
+
tsconfig: tsconfig(projectDir ?? ""),
|
|
438
|
+
gitignore: gitignore(path$1.join(projectDir ?? "", ".gitignore")),
|
|
439
|
+
filterPackageManagers,
|
|
440
|
+
getPackageJson: () => get("packageJson"),
|
|
441
|
+
findDependencyInPackageJson: (options) => findDependencyInPackageJson(options, get("packageJson")),
|
|
442
|
+
isDependencyInPackageJson: (options) => isDependencyInPackageJson(options, get("packageJson"))
|
|
443
|
+
};
|
|
444
|
+
function info() {
|
|
445
|
+
const {
|
|
446
|
+
packageJson: packageJson2,
|
|
447
|
+
name: packageName2,
|
|
448
|
+
dirpath: projectDir2,
|
|
449
|
+
path: packageJsonPath2
|
|
450
|
+
} = getProjectInfo(source, projectOptions) ?? {};
|
|
451
|
+
return {
|
|
452
|
+
packageJson: packageJson2,
|
|
453
|
+
packageJsonPath: packageJsonPath2,
|
|
454
|
+
packageName: packageName2,
|
|
455
|
+
projectDir: projectDir2
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
function get(key) {
|
|
459
|
+
return info()[key];
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
function getWorkspacePackageNames(options) {
|
|
464
|
+
return getWorkspacePackageInfoList(options).map((e) => e.name);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
const workspace = {
|
|
468
|
+
packageNames: getWorkspacePackageNames,
|
|
469
|
+
packageGraph: getWorkspacePackageInfoMap,
|
|
470
|
+
packageList: getWorkspacePackageInfoList,
|
|
471
|
+
workspaceRootDir: getWorkspaceFolder,
|
|
472
|
+
getProject: project
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
function importer(imports, options) {
|
|
476
|
+
const { install: defaultInstall = true, installer } = options ?? {};
|
|
477
|
+
return Promise.all(
|
|
478
|
+
imports.map(async (e) => {
|
|
479
|
+
const importOpt = resolveImportOption(e);
|
|
480
|
+
const shouldInstall = importOpt.install ?? defaultInstall;
|
|
481
|
+
if (shouldInstall && importOpt.name) {
|
|
482
|
+
await installImport(importOpt.name, importOpt, installer);
|
|
483
|
+
}
|
|
484
|
+
const m = await importOpt.import();
|
|
485
|
+
return resolveModule(m);
|
|
486
|
+
})
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
const definePackage = (option) => {
|
|
490
|
+
const { name, ...rest } = typeof option === "string" ? { name: option } : option;
|
|
491
|
+
return {
|
|
492
|
+
name,
|
|
493
|
+
import: () => import(name),
|
|
494
|
+
...rest
|
|
495
|
+
};
|
|
496
|
+
};
|
|
497
|
+
function resolveImportOption(option) {
|
|
498
|
+
if (typeof option === "function") {
|
|
499
|
+
return {
|
|
500
|
+
import: option
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
if (option instanceof Promise) {
|
|
504
|
+
return {
|
|
505
|
+
import: () => option
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
return option;
|
|
509
|
+
}
|
|
510
|
+
async function installImport(packageName, options, installer) {
|
|
511
|
+
if (!packageName) return;
|
|
512
|
+
const { checkExists } = options ?? {};
|
|
513
|
+
if (checkExists && isPackageDependency(packageName)) return;
|
|
514
|
+
const installerFn = installer ?? (await workspace.getProject("<package_folder>").findPackageManager()).installPackage;
|
|
515
|
+
await installerFn(packageName, options);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
const importMap = async (importMap2, options) => {
|
|
519
|
+
const keys = Object.keys(importMap2);
|
|
520
|
+
const imported = await importer(
|
|
521
|
+
keys.map((key) => importMap2[key]),
|
|
522
|
+
options
|
|
523
|
+
);
|
|
524
|
+
return Object.fromEntries(
|
|
525
|
+
keys.map((key, index) => [key, imported[index]])
|
|
526
|
+
);
|
|
527
|
+
};
|
|
528
|
+
|
|
529
|
+
function definePackageManager(config, options) {
|
|
530
|
+
const { cwd: defaultCwd } = options ?? {};
|
|
531
|
+
const { command, args: agentArgs, options: agentOptions } = config;
|
|
532
|
+
const findLockfilePath = asyncCacheFn(async (options2) => {
|
|
533
|
+
const { cwd = defaultCwd } = options2 ?? {};
|
|
534
|
+
const lockfiles = toArray(config.meta.lockfile);
|
|
535
|
+
return await findUp(lockfiles, { cwd });
|
|
536
|
+
});
|
|
537
|
+
const installPackage = async (packageName, options2) => {
|
|
538
|
+
const install = agentArgs.install;
|
|
539
|
+
const { dev, preferOffline } = select(
|
|
540
|
+
install.options,
|
|
541
|
+
{
|
|
542
|
+
preferOffline: true,
|
|
543
|
+
cwd: defaultCwd,
|
|
544
|
+
...options2
|
|
545
|
+
});
|
|
546
|
+
const packageNames = toArray(packageName);
|
|
547
|
+
try {
|
|
548
|
+
await $$({
|
|
549
|
+
command,
|
|
550
|
+
args: [install.command, dev, preferOffline, ...packageNames],
|
|
551
|
+
cwd: defaultCwd,
|
|
552
|
+
...options2
|
|
553
|
+
});
|
|
554
|
+
} catch (error) {
|
|
555
|
+
throw new Error(`Failed to install: ${packageNames.join(", ")}`, {
|
|
556
|
+
cause: error
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
const uninstallPackage = async (packageName, options2) => {
|
|
561
|
+
const uninstall = agentArgs.uninstall;
|
|
562
|
+
const cwd = options2?.cwd ?? defaultCwd;
|
|
563
|
+
const { packageJson } = getPackageInfo({ cwd });
|
|
564
|
+
const installed = toArray(packageName).filter(
|
|
565
|
+
(name) => isDependencyInPackageJson(name, packageJson)
|
|
566
|
+
);
|
|
567
|
+
if (installed.length === 0) return;
|
|
568
|
+
try {
|
|
569
|
+
await $$({
|
|
570
|
+
command,
|
|
571
|
+
args: [uninstall.command, ...installed],
|
|
572
|
+
cwd,
|
|
573
|
+
...options2
|
|
574
|
+
});
|
|
575
|
+
} catch (error) {
|
|
576
|
+
throw new Error(`Failed to uninstall: ${installed.join(", ")}`, {
|
|
577
|
+
cause: error
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
return {
|
|
582
|
+
id: config.id,
|
|
583
|
+
config,
|
|
584
|
+
findLockfilePath,
|
|
585
|
+
hasLockfile: asyncCacheFn(async (...args) => {
|
|
586
|
+
const lockfilePath = await findLockfilePath.noCache(...args);
|
|
587
|
+
return Boolean(lockfilePath);
|
|
588
|
+
}),
|
|
589
|
+
readLockfile: asyncCacheFn(async (...args) => {
|
|
590
|
+
const lockfilePath = await findLockfilePath.noCache(...args);
|
|
591
|
+
if (!lockfilePath) return void 0;
|
|
592
|
+
return readFile(lockfilePath, "utf8");
|
|
593
|
+
}),
|
|
594
|
+
globalVersion: asyncCacheFn(async (...args) => {
|
|
595
|
+
const [options2] = args;
|
|
596
|
+
try {
|
|
597
|
+
const { stdout } = await $$({
|
|
598
|
+
command,
|
|
599
|
+
args: [agentOptions.version],
|
|
600
|
+
cwd: defaultCwd,
|
|
601
|
+
...options2
|
|
602
|
+
});
|
|
603
|
+
return `${stdout}`;
|
|
604
|
+
} catch (e) {
|
|
605
|
+
return void 0;
|
|
606
|
+
}
|
|
607
|
+
}),
|
|
608
|
+
definePackage,
|
|
609
|
+
installPackage,
|
|
610
|
+
uninstallPackage,
|
|
611
|
+
defineImportMap(imports, options2) {
|
|
612
|
+
const { install = true } = options2 ?? {};
|
|
613
|
+
return importMap(imports, {
|
|
614
|
+
install,
|
|
615
|
+
installer: installPackage
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
async function $$(options) {
|
|
621
|
+
const { command, args = [], silent = true, cwd, shellOptions } = options;
|
|
622
|
+
return execa(command, args.filter(notFalsy), {
|
|
623
|
+
cwd,
|
|
624
|
+
...shellOptions,
|
|
625
|
+
// `pipe` keeps output off the console while still capturing it: a failed
|
|
626
|
+
// command can report why it failed, and `globalVersion` can read stdout.
|
|
627
|
+
// `ignore` discards the reason along with the output.
|
|
628
|
+
stdio: silent ? "pipe" : "inherit"
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
function definePackageManagerConfig(config) {
|
|
633
|
+
return config;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
const bun = definePackageManagerConfig({
|
|
637
|
+
id: "bun",
|
|
638
|
+
name: "Bun",
|
|
639
|
+
command: "bun",
|
|
640
|
+
runner: "bunx",
|
|
641
|
+
meta: {
|
|
642
|
+
lockfile: "bun.lockb"
|
|
643
|
+
},
|
|
644
|
+
args: {
|
|
645
|
+
install: {
|
|
646
|
+
command: "install",
|
|
647
|
+
options: {
|
|
648
|
+
dev: "-D",
|
|
649
|
+
preferOffline: "--prefer-offline"
|
|
650
|
+
}
|
|
651
|
+
},
|
|
652
|
+
uninstall: {
|
|
653
|
+
command: "uninstall"
|
|
654
|
+
}
|
|
655
|
+
},
|
|
656
|
+
options: {
|
|
657
|
+
version: "--version"
|
|
658
|
+
}
|
|
659
|
+
});
|
|
660
|
+
|
|
661
|
+
const npm = definePackageManagerConfig({
|
|
662
|
+
id: "npm",
|
|
663
|
+
name: "NPM",
|
|
664
|
+
command: "npm",
|
|
665
|
+
runner: "npx",
|
|
666
|
+
meta: {
|
|
667
|
+
lockfile: ["package-lock.json", "npm-shrinkwrap.json"]
|
|
668
|
+
},
|
|
669
|
+
args: {
|
|
670
|
+
install: {
|
|
671
|
+
command: "install",
|
|
672
|
+
options: {
|
|
673
|
+
dev: "-D",
|
|
674
|
+
preferOffline: "--prefer-offline"
|
|
675
|
+
}
|
|
676
|
+
},
|
|
677
|
+
uninstall: {
|
|
678
|
+
command: "uninstall"
|
|
679
|
+
}
|
|
680
|
+
},
|
|
681
|
+
options: {
|
|
682
|
+
version: "--version"
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
|
|
686
|
+
const pnpm = definePackageManagerConfig({
|
|
687
|
+
id: "pnpm",
|
|
688
|
+
name: "PNPM",
|
|
689
|
+
command: "pnpm",
|
|
690
|
+
runner: "pnpx",
|
|
691
|
+
meta: {
|
|
692
|
+
lockfile: "pnpm-lock.yaml"
|
|
693
|
+
},
|
|
694
|
+
args: {
|
|
695
|
+
install: {
|
|
696
|
+
command: "install",
|
|
697
|
+
options: {
|
|
698
|
+
dev: "-D",
|
|
699
|
+
preferOffline: "--prefer-offline"
|
|
700
|
+
}
|
|
701
|
+
},
|
|
702
|
+
uninstall: {
|
|
703
|
+
command: "remove"
|
|
704
|
+
}
|
|
705
|
+
},
|
|
706
|
+
options: {
|
|
707
|
+
version: "--version"
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
|
|
711
|
+
const yarn = definePackageManagerConfig({
|
|
712
|
+
id: "yarn",
|
|
713
|
+
name: "Yarn",
|
|
714
|
+
command: "yarn",
|
|
715
|
+
runner: "yarn dlx",
|
|
716
|
+
meta: {
|
|
717
|
+
lockfile: "yarn.lock"
|
|
718
|
+
},
|
|
719
|
+
args: {
|
|
720
|
+
install: {
|
|
721
|
+
command: "add",
|
|
722
|
+
options: {
|
|
723
|
+
dev: "-D",
|
|
724
|
+
preferOffline: "--prefer-offline"
|
|
725
|
+
}
|
|
726
|
+
},
|
|
727
|
+
uninstall: {
|
|
728
|
+
command: "remove"
|
|
729
|
+
}
|
|
730
|
+
},
|
|
731
|
+
options: {
|
|
732
|
+
version: "--version"
|
|
733
|
+
}
|
|
734
|
+
});
|
|
735
|
+
|
|
736
|
+
async function findPackageManager(packageManagers, options) {
|
|
737
|
+
const packageManager = await findPackageManagerSafely(
|
|
738
|
+
packageManagers,
|
|
739
|
+
options
|
|
740
|
+
);
|
|
741
|
+
invariant(packageManager, "No package manager found");
|
|
742
|
+
return packageManager;
|
|
743
|
+
}
|
|
744
|
+
async function findPackageManagerSafely(packageManagers, options) {
|
|
745
|
+
const [fromLockfile] = await detectLockfilePackageManagers(
|
|
746
|
+
packageManagers,
|
|
747
|
+
options
|
|
748
|
+
);
|
|
749
|
+
if (fromLockfile) return fromLockfile;
|
|
750
|
+
const [fromGlobal] = await detectGlobalPackageManagers(
|
|
751
|
+
packageManagers,
|
|
752
|
+
options
|
|
753
|
+
);
|
|
754
|
+
return fromGlobal;
|
|
755
|
+
}
|
|
756
|
+
async function detectPackageManagers(packageManagers, options) {
|
|
757
|
+
return [
|
|
758
|
+
...await detectLockfilePackageManagers(packageManagers, options),
|
|
759
|
+
...await detectGlobalPackageManagers(packageManagers, options)
|
|
760
|
+
];
|
|
761
|
+
}
|
|
762
|
+
async function detectLockfilePackageManagers(packageManagers, options) {
|
|
763
|
+
return filterPackageManagers(
|
|
764
|
+
packageManagers,
|
|
765
|
+
(packageManager) => packageManager.hasLockfile(options),
|
|
766
|
+
options
|
|
767
|
+
);
|
|
768
|
+
}
|
|
769
|
+
async function detectGlobalPackageManagers(packageManagers, options) {
|
|
770
|
+
return filterPackageManagers(
|
|
771
|
+
packageManagers,
|
|
772
|
+
// Without the `await`, `Boolean` receives a pending promise and is always
|
|
773
|
+
// `true`, so every package manager passes the filter regardless of whether
|
|
774
|
+
// it is actually installed.
|
|
775
|
+
async (packageManager) => Boolean(await packageManager.globalVersion(options)),
|
|
776
|
+
options
|
|
777
|
+
);
|
|
778
|
+
}
|
|
779
|
+
async function getGlobalVersions(packageManagers, options) {
|
|
780
|
+
const entries = await mapPackageManagers(
|
|
781
|
+
packageManagers,
|
|
782
|
+
async (packageManager) => [packageManager.id, await packageManager.globalVersion(options)],
|
|
783
|
+
options
|
|
784
|
+
);
|
|
785
|
+
return Object.fromEntries(entries);
|
|
786
|
+
}
|
|
787
|
+
async function mapPackageManagers(packageManagers, mapFn, options) {
|
|
788
|
+
return Promise.all(
|
|
789
|
+
selectAllowedPackageManagers(packageManagers, options).map(
|
|
790
|
+
(packageManager) => mapFn(packageManager)
|
|
791
|
+
)
|
|
792
|
+
);
|
|
793
|
+
}
|
|
794
|
+
async function filterPackageManagers(packageManagers, filterFn, options) {
|
|
795
|
+
const matches = await mapPackageManagers(
|
|
796
|
+
packageManagers,
|
|
797
|
+
async (packageManager) => await filterFn(packageManager) ? packageManager : void 0,
|
|
798
|
+
options
|
|
799
|
+
);
|
|
800
|
+
return matches.filter(notFalsy);
|
|
801
|
+
}
|
|
802
|
+
function selectAllowedPackageManagers(packageManagers, options) {
|
|
803
|
+
return packageManagers.filter(
|
|
804
|
+
({ id }) => options?.allowed ? id in options.allowed : true
|
|
805
|
+
);
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
const packageManagerConfigs = [pnpm, yarn, bun, npm];
|
|
809
|
+
function definePackageManagerClient(options) {
|
|
810
|
+
const configs = packageManagerConfigs.map(
|
|
811
|
+
(config) => definePackageManager(config, options)
|
|
812
|
+
);
|
|
813
|
+
return {
|
|
814
|
+
configs,
|
|
815
|
+
findPackageManager: asyncCacheFn(
|
|
816
|
+
async (options2) => await findPackageManager(configs, options2)
|
|
817
|
+
),
|
|
818
|
+
findPackageManagerSafely: asyncCacheFn(
|
|
819
|
+
async (options2) => await findPackageManagerSafely(configs, options2)
|
|
820
|
+
),
|
|
821
|
+
detectPackageManagers: asyncCacheFn(
|
|
822
|
+
async (options2) => await detectPackageManagers(configs, options2)
|
|
823
|
+
),
|
|
824
|
+
detectLockfilePackageManagers: asyncCacheFn(
|
|
825
|
+
async (options2) => detectLockfilePackageManagers(configs, options2)
|
|
826
|
+
),
|
|
827
|
+
detectGlobalPackageManagers: asyncCacheFn(
|
|
828
|
+
async (options2) => detectGlobalPackageManagers(configs, options2)
|
|
829
|
+
),
|
|
830
|
+
globalVersions: asyncCacheFn(
|
|
831
|
+
async (options2) => getGlobalVersions(configs, options2)
|
|
832
|
+
),
|
|
833
|
+
filterPackageManagers: async (filterFn, options2) => filterPackageManagers(configs, filterFn, options2),
|
|
834
|
+
mapPackageManagers: async (mapFn, options2) => mapPackageManagers(configs, mapFn, options2)
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
function isPackageDependency(packageName) {
|
|
839
|
+
return toArray(packageName).every((name) => isDependencyInPackageJson(name));
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
function definePathAliases(aliasDefinitions) {
|
|
843
|
+
const aliasMap = getAliasMap(aliasDefinitions);
|
|
844
|
+
function getFilePath(options, aliases) {
|
|
845
|
+
return getAliasedFilePath(
|
|
846
|
+
{ ...aliasMap, ...aliases },
|
|
847
|
+
options
|
|
848
|
+
);
|
|
849
|
+
}
|
|
850
|
+
return {
|
|
851
|
+
aliasDefinitions,
|
|
852
|
+
aliasMap,
|
|
853
|
+
getFilePath
|
|
854
|
+
};
|
|
855
|
+
}
|
|
856
|
+
function getAliasedFilePath(aliasMap, options) {
|
|
857
|
+
const { to, startingFrom, cwd, checkExistence, glob } = typeof options === "string" || Array.isArray(options) ? { to: options } : options;
|
|
858
|
+
const resolveOptions = { cwd, checkExistence, glob, aliasMap };
|
|
859
|
+
try {
|
|
860
|
+
return startingFrom ? resolveRelativePathTo(to, startingFrom, resolveOptions) : resolvePathTo(to, resolveOptions);
|
|
861
|
+
} catch (error) {
|
|
862
|
+
if (checkExistence || glob) return void 0;
|
|
863
|
+
throw error;
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
function resolveRelativePathTo(to, from, options) {
|
|
867
|
+
const pathFrom = resolvePathTo(from, options);
|
|
868
|
+
const pathTo = resolvePathTo(to, options);
|
|
869
|
+
return path$1.relative(pathFrom, pathTo);
|
|
870
|
+
}
|
|
871
|
+
function resolvePathTo(pathTo, { cwd, checkExistence, glob, aliasMap }) {
|
|
872
|
+
const normalized = normalizePathTo(pathTo, { cwd, aliasMap });
|
|
873
|
+
if (glob) {
|
|
874
|
+
const globPaths = globbySync(normalized, { cwd });
|
|
875
|
+
if (!globPaths[0])
|
|
876
|
+
throw new Error(`No paths found for glob: ${normalized}`);
|
|
877
|
+
return globPaths[0];
|
|
878
|
+
}
|
|
879
|
+
if (!glob && checkExistence && !existsSync(normalized))
|
|
880
|
+
throw new Error(`Path does not exist: ${normalized}`);
|
|
881
|
+
return normalized;
|
|
882
|
+
}
|
|
883
|
+
function normalizePathTo(pathTo, options) {
|
|
884
|
+
const { cwd, aliasMap } = options ?? {};
|
|
885
|
+
const aliasedPath = Array.isArray(pathTo) ? path$1.join(...pathTo.filter(isNotNull)) : pathTo;
|
|
886
|
+
const baseDir = Array.isArray(pathTo) ? pathTo[0] : findAliasToken(aliasedPath, aliasMap);
|
|
887
|
+
if (baseDir === void 0) return aliasedPath;
|
|
888
|
+
const baseDirPath = executeMapFn(aliasMap, baseDir, [{ cwd }]);
|
|
889
|
+
if (typeof baseDirPath !== "string" || baseDirPath.length === 0) {
|
|
890
|
+
throw new Error(`Path alias resolved to no location: ${baseDir}`);
|
|
891
|
+
}
|
|
892
|
+
return resolveAlias(aliasedPath, { [baseDir]: baseDirPath });
|
|
893
|
+
}
|
|
894
|
+
function findAliasToken(pathTo, aliasMap) {
|
|
895
|
+
return Object.keys(aliasMap ?? {}).filter((alias) => pathTo === alias || pathTo.startsWith(`${alias}/`)).sort((a, b) => b.length - a.length)[0];
|
|
896
|
+
}
|
|
897
|
+
function executeMapFn(map, key, args) {
|
|
898
|
+
if (!map || !key || !(key in map)) return void 0;
|
|
899
|
+
const fnArgs = Array.isArray(args) ? args : [args];
|
|
900
|
+
const resolver = map?.[key];
|
|
901
|
+
return typeof resolver === "function" ? resolver?.(...fnArgs) : resolver;
|
|
902
|
+
}
|
|
903
|
+
function getAliasMap(aliasDefs) {
|
|
904
|
+
return Object.fromEntries(
|
|
905
|
+
Object.entries(aliasDefs).flatMap(([alias, v]) => {
|
|
906
|
+
const { resolve, subpaths = [] } = v;
|
|
907
|
+
return [
|
|
908
|
+
[alias, resolve],
|
|
909
|
+
...subpaths.map(({ to }) => {
|
|
910
|
+
const subpathAlias = path$1.join(alias, to);
|
|
911
|
+
return [
|
|
912
|
+
subpathAlias,
|
|
913
|
+
(opts) => resolveAlias(subpathAlias, { [alias]: resolve(opts) })
|
|
914
|
+
];
|
|
915
|
+
})
|
|
916
|
+
];
|
|
917
|
+
})
|
|
918
|
+
);
|
|
919
|
+
}
|
|
920
|
+
function isNotNull(value) {
|
|
921
|
+
return value !== null && value !== void 0;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
const predefinedPathAliases = {
|
|
925
|
+
"<workspace_folder>": {
|
|
926
|
+
resolve: (opts) => getWorkspaceFolder(opts),
|
|
927
|
+
subpaths: [
|
|
928
|
+
{
|
|
929
|
+
to: "node_modules"
|
|
930
|
+
},
|
|
931
|
+
{
|
|
932
|
+
to: "node_modules/.bin"
|
|
933
|
+
}
|
|
934
|
+
]
|
|
935
|
+
},
|
|
936
|
+
"<workspace_folder?>": {
|
|
937
|
+
resolve: (opts) => getWorkspaceFolder({ ...opts, fallbackToGitRoot: false }),
|
|
938
|
+
subpaths: [
|
|
939
|
+
{
|
|
940
|
+
to: "node_modules"
|
|
941
|
+
},
|
|
942
|
+
{
|
|
943
|
+
to: "node_modules/.bin"
|
|
944
|
+
}
|
|
945
|
+
]
|
|
946
|
+
},
|
|
947
|
+
"<package_folder>": {
|
|
948
|
+
resolve: (opts) => getPackageFolder(opts),
|
|
949
|
+
subpaths: [
|
|
950
|
+
{
|
|
951
|
+
to: "node_modules"
|
|
952
|
+
},
|
|
953
|
+
{
|
|
954
|
+
to: "node_modules/.bin"
|
|
955
|
+
},
|
|
956
|
+
{
|
|
957
|
+
to: "src"
|
|
958
|
+
}
|
|
959
|
+
]
|
|
960
|
+
},
|
|
961
|
+
"<gitroot_folder>": {
|
|
962
|
+
resolve: (opts) => getGitRootFolder(opts),
|
|
963
|
+
subpaths: [
|
|
964
|
+
{
|
|
965
|
+
to: "node_modules"
|
|
966
|
+
},
|
|
967
|
+
{
|
|
968
|
+
to: "node_modules/.bin"
|
|
969
|
+
},
|
|
970
|
+
{
|
|
971
|
+
to: ".vscode"
|
|
972
|
+
}
|
|
973
|
+
]
|
|
974
|
+
},
|
|
975
|
+
"<user_home>": {
|
|
976
|
+
resolve: () => os.homedir(),
|
|
977
|
+
subpaths: []
|
|
978
|
+
},
|
|
979
|
+
"<user_tmpdir>": {
|
|
980
|
+
resolve: () => os.tmpdir(),
|
|
981
|
+
subpaths: []
|
|
982
|
+
},
|
|
983
|
+
"<cwd>": {
|
|
984
|
+
resolve: () => process.cwd(),
|
|
985
|
+
subpaths: []
|
|
986
|
+
},
|
|
987
|
+
"<current_file>": {
|
|
988
|
+
// resolve: () => fileURLToPath(import.meta.url),
|
|
989
|
+
resolve: () => _filename({ rootFunctionName: "getFilePath" }) ?? "",
|
|
990
|
+
subpaths: []
|
|
991
|
+
},
|
|
992
|
+
"<current_folder>": {
|
|
993
|
+
// resolve: () => fileURLToPath(import.meta.url),
|
|
994
|
+
resolve: () => _dirname({ rootFunctionName: "getFilePath" }) ?? "",
|
|
995
|
+
subpaths: []
|
|
996
|
+
}
|
|
997
|
+
};
|
|
998
|
+
|
|
999
|
+
const getPath = definePathAliases(predefinedPathAliases).getFilePath;
|
|
1000
|
+
|
|
1001
|
+
function getFolderByPackageName(name, options) {
|
|
1002
|
+
const infoMap = getWorkspacePackageInfoMap({ ...options, includeRoot: true });
|
|
1003
|
+
const info = infoMap?.[name];
|
|
1004
|
+
return info?.dirpath;
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
const jsonSourceResolvers = {
|
|
1008
|
+
data: (json) => {
|
|
1009
|
+
return {
|
|
1010
|
+
text: () => JSON.stringify(json),
|
|
1011
|
+
data: () => json
|
|
1012
|
+
};
|
|
1013
|
+
},
|
|
1014
|
+
text: (text) => {
|
|
1015
|
+
return {
|
|
1016
|
+
text: () => text,
|
|
1017
|
+
data: () => JSON.parse(text)
|
|
1018
|
+
};
|
|
1019
|
+
},
|
|
1020
|
+
filepath: (filepath) => {
|
|
1021
|
+
const text = readFileSync(filepath, "utf-8");
|
|
1022
|
+
return {
|
|
1023
|
+
text: () => text,
|
|
1024
|
+
data: () => JSON.parse(text)
|
|
1025
|
+
};
|
|
1026
|
+
}
|
|
1027
|
+
};
|
|
1028
|
+
function resolveJsonSource(source, as) {
|
|
1029
|
+
const [sourceType, sourceData] = Object.entries(source).find(([_, selected]) => selected !== void 0) ?? [];
|
|
1030
|
+
if (!sourceType || !(sourceType in jsonSourceResolvers) || !sourceData) {
|
|
1031
|
+
throw new Error("Invalid source data");
|
|
1032
|
+
}
|
|
1033
|
+
const sourceTypeResolvers = jsonSourceResolvers[sourceType](
|
|
1034
|
+
sourceData
|
|
1035
|
+
);
|
|
1036
|
+
const resolved = morph(sourceTypeResolvers, (key, v) => {
|
|
1037
|
+
try {
|
|
1038
|
+
const value = v();
|
|
1039
|
+
return !as || key === as ? [key, value] : [];
|
|
1040
|
+
} catch (e) {
|
|
1041
|
+
throw new Error(`Failed to resolve ${key} from ${sourceType}`);
|
|
1042
|
+
}
|
|
1043
|
+
});
|
|
1044
|
+
if (as) {
|
|
1045
|
+
return resolved[as];
|
|
1046
|
+
}
|
|
1047
|
+
return resolved;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
function resolveEditPath(path, options) {
|
|
1051
|
+
const { pathSeparator = "." } = {};
|
|
1052
|
+
const arr = toArray(path);
|
|
1053
|
+
return arr.map((path2) => {
|
|
1054
|
+
if (typeof path2 === "string") {
|
|
1055
|
+
return pathSeparator === false ? path2 : path2.split(pathSeparator);
|
|
1056
|
+
}
|
|
1057
|
+
return path2;
|
|
1058
|
+
}).flat();
|
|
1059
|
+
}
|
|
1060
|
+
function parseJSONCEdits(edits) {
|
|
1061
|
+
if (Array.isArray(edits)) {
|
|
1062
|
+
return edits.map(({ path, ...edit }) => ({
|
|
1063
|
+
path: resolveEditPath(path),
|
|
1064
|
+
...edit
|
|
1065
|
+
}));
|
|
1066
|
+
}
|
|
1067
|
+
return Object.entries(edits).map(
|
|
1068
|
+
([path, value]) => ({
|
|
1069
|
+
path: resolveEditPath(path),
|
|
1070
|
+
...value
|
|
1071
|
+
})
|
|
1072
|
+
);
|
|
1073
|
+
}
|
|
1074
|
+
function modifyJSON({
|
|
1075
|
+
json,
|
|
1076
|
+
edits,
|
|
1077
|
+
defaultEditOptions
|
|
1078
|
+
}) {
|
|
1079
|
+
return checkResult(() => {
|
|
1080
|
+
const text = resolveJsonSource(json, "text");
|
|
1081
|
+
const jsoncEdits = parseJSONCEdits(edits);
|
|
1082
|
+
const editResult = jsoncEdits.flatMap(
|
|
1083
|
+
(edit) => modify(text, edit.path, edit.value, {
|
|
1084
|
+
...defaultEditOptions,
|
|
1085
|
+
...edit.options
|
|
1086
|
+
})
|
|
1087
|
+
);
|
|
1088
|
+
const updated = applyEdits(text, editResult);
|
|
1089
|
+
return resolveJsonSource({ text: updated });
|
|
1090
|
+
});
|
|
1091
|
+
}
|
|
1092
|
+
function modifyJSONFile(filepath, edits, options) {
|
|
1093
|
+
return checkResult(() => {
|
|
1094
|
+
const { autoCommit = true, defaultEditOptions } = options || {};
|
|
1095
|
+
const { data: json, error } = modifyJSON({
|
|
1096
|
+
json: { filepath },
|
|
1097
|
+
edits,
|
|
1098
|
+
defaultEditOptions
|
|
1099
|
+
});
|
|
1100
|
+
if (error) {
|
|
1101
|
+
throw error;
|
|
1102
|
+
}
|
|
1103
|
+
const commit = () => writeFileSync(filepath, json.text);
|
|
1104
|
+
if (autoCommit) {
|
|
1105
|
+
commit();
|
|
1106
|
+
return json;
|
|
1107
|
+
}
|
|
1108
|
+
return {
|
|
1109
|
+
json,
|
|
1110
|
+
commit
|
|
1111
|
+
};
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
function isStorageValue(input) {
|
|
1116
|
+
return input === null || typeof input === "string" || typeof input === "number" || typeof input === "boolean" || typeof input === "object";
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
const storage = createStorage({ driver: defineMemoryDriver() });
|
|
1120
|
+
const tempFileSystem = defineFileSystemStorage({
|
|
1121
|
+
base: join(os.tmpdir(), ".package-manager")
|
|
1122
|
+
});
|
|
1123
|
+
function defineFileSystemEntries(definition) {
|
|
1124
|
+
const fileSystemEntries = Object.entries(definition).map(([k, v]) => {
|
|
1125
|
+
if (v instanceof Function) {
|
|
1126
|
+
const {
|
|
1127
|
+
file,
|
|
1128
|
+
options,
|
|
1129
|
+
serialize = (input) => JSON.stringify(input),
|
|
1130
|
+
deserialize = (input) => {
|
|
1131
|
+
try {
|
|
1132
|
+
return JSON.parse(input);
|
|
1133
|
+
} catch {
|
|
1134
|
+
return input;
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
} = v();
|
|
1138
|
+
return {
|
|
1139
|
+
key: k,
|
|
1140
|
+
value: serialize(file),
|
|
1141
|
+
options,
|
|
1142
|
+
serialize,
|
|
1143
|
+
deserialize
|
|
1144
|
+
};
|
|
1145
|
+
}
|
|
1146
|
+
if (isStorageValue(v)) {
|
|
1147
|
+
return {
|
|
1148
|
+
key: k,
|
|
1149
|
+
value: JSON.stringify(v)
|
|
1150
|
+
};
|
|
1151
|
+
}
|
|
1152
|
+
return void 0;
|
|
1153
|
+
}).filter((v) => v !== void 0);
|
|
1154
|
+
const resolved = {
|
|
1155
|
+
definition,
|
|
1156
|
+
fileSystemEntries
|
|
1157
|
+
};
|
|
1158
|
+
return resolved;
|
|
1159
|
+
}
|
|
1160
|
+
function defineFileSystemStorage(options) {
|
|
1161
|
+
const { base: root, initial, ...storageOptions } = options;
|
|
1162
|
+
let initialFileEntriesData = defineFileSystemEntries(initial ?? {});
|
|
1163
|
+
const storage2 = createStorage({
|
|
1164
|
+
driver: defineFsLiteDriver({ base: root, ...storageOptions })
|
|
1165
|
+
});
|
|
1166
|
+
const fileStorage = {
|
|
1167
|
+
createFile: async (key, data) => {
|
|
1168
|
+
await storage2.setItem(key, data);
|
|
1169
|
+
return {
|
|
1170
|
+
key,
|
|
1171
|
+
filepath: join(root, key),
|
|
1172
|
+
get: async () => storage2.getItem(key),
|
|
1173
|
+
update: async (data2) => storage2.setItem(key, data2)
|
|
1174
|
+
};
|
|
1175
|
+
},
|
|
1176
|
+
async getFilePath(key) {
|
|
1177
|
+
return (await this.getFile(key)).filepath;
|
|
1178
|
+
},
|
|
1179
|
+
getFile: async (key) => {
|
|
1180
|
+
const storageData = await storage2.getItem(key);
|
|
1181
|
+
return {
|
|
1182
|
+
key,
|
|
1183
|
+
filepath: join(root, key),
|
|
1184
|
+
data: storageData,
|
|
1185
|
+
read: async () => (await storage2.getItemRaw(key))?.toString()
|
|
1186
|
+
};
|
|
1187
|
+
},
|
|
1188
|
+
async readFile(key) {
|
|
1189
|
+
const { read } = await this.getFile(key);
|
|
1190
|
+
const content = await read();
|
|
1191
|
+
return content;
|
|
1192
|
+
},
|
|
1193
|
+
snapshotFs: async (base = root) => {
|
|
1194
|
+
return snapshot(storage2, base);
|
|
1195
|
+
},
|
|
1196
|
+
restoreFs: async (snapshot2) => {
|
|
1197
|
+
return restoreSnapshot(storage2, snapshot2);
|
|
1198
|
+
},
|
|
1199
|
+
initializeFs: async (override) => {
|
|
1200
|
+
if (override) {
|
|
1201
|
+
initialFileEntriesData = defineFileSystemEntries(override);
|
|
1202
|
+
}
|
|
1203
|
+
await storage2.clear();
|
|
1204
|
+
return storage2.setItems(initialFileEntriesData.fileSystemEntries);
|
|
1205
|
+
},
|
|
1206
|
+
defineFileSystemEntries,
|
|
1207
|
+
removeAllFiles: storage2.clear,
|
|
1208
|
+
async deleteFileSystem() {
|
|
1209
|
+
await this.removeAllFiles();
|
|
1210
|
+
await storage2.unmount(root, true);
|
|
1211
|
+
await rm(root, { recursive: true, force: true });
|
|
1212
|
+
},
|
|
1213
|
+
storage: storage2,
|
|
1214
|
+
meta: {
|
|
1215
|
+
fileEntriesData: initialFileEntriesData
|
|
1216
|
+
}
|
|
1217
|
+
};
|
|
1218
|
+
if (initial) {
|
|
1219
|
+
return {
|
|
1220
|
+
initialize: async () => {
|
|
1221
|
+
await fileStorage.initializeFs(initial);
|
|
1222
|
+
return fileStorage;
|
|
1223
|
+
}
|
|
1224
|
+
};
|
|
1225
|
+
}
|
|
1226
|
+
return fileStorage;
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
export { _dirname, _filename, defineFileSystemEntries, defineFileSystemStorage, definePackage, definePackageManagerClient, definePathAliases, detectGlobalPackageManagers, detectLockfilePackageManagers, detectPackageManagers, filterPackageManagers, findDependencyInPackageJson, findPackageManager, findPackageManagerSafely, findResolvedModulePath, getAliasMap, getFolderByPackageName, getGitRootFolder, getGlobalVersions, getPackageFolder, getPath, getWorkspaceFolder, importMap, importer, isDependencyInPackageJson, isPackageDependency, isPackageModuleFound, mapPackageManagers, modifyJSON, modifyJSONFile, packageManagerConfigs, predefinedPathAliases, project, resolveModule, resolveModulePath, resolvePackageModulePath, storage, tempFileSystem, workspace };
|