package-management 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +501 -115
- package/dist/index.d.cts +158 -121
- package/dist/index.d.mts +158 -121
- package/dist/index.d.ts +158 -121
- package/dist/index.mjs +478 -117
- package/package.json +30 -4
package/dist/index.mjs
CHANGED
|
@@ -1,39 +1,376 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import 'node:fs';
|
|
1
|
+
import path$1, { normalize } from 'pathe';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
3
|
import { execa } from 'execa';
|
|
4
|
-
import { isPackageExists } from 'local-pkg';
|
|
5
4
|
import { asyncCacheFn } from 'async-cache-fn';
|
|
6
5
|
import { findUp } from 'find-up';
|
|
7
6
|
import { readFile } from 'node:fs/promises';
|
|
7
|
+
import { resolvePathSync } from 'mlly';
|
|
8
|
+
import { globbySync } from 'globby';
|
|
9
|
+
import gitignoreParser from 'parse-gitignore';
|
|
10
|
+
import * as WST from 'workspace-tools';
|
|
11
|
+
import process from 'node:process';
|
|
12
|
+
import path from 'node:path';
|
|
8
13
|
|
|
9
|
-
|
|
10
|
-
|
|
14
|
+
const toArray = (data) => Array.isArray(data) ? data : [data];
|
|
15
|
+
const entriesOf = (o) => Object.entries(o);
|
|
16
|
+
const fromEntries = (entries) => Object.fromEntries(entries);
|
|
17
|
+
const notFalsy = (value) => [false, null, void 0].every((v) => v !== value);
|
|
18
|
+
const select = (obj, selection, mode) => {
|
|
19
|
+
if (!selection)
|
|
20
|
+
return obj;
|
|
21
|
+
const filtered = entriesOf(obj).filter(([key]) => {
|
|
22
|
+
return mode === "omit" ? !selection[key] : selection[key];
|
|
23
|
+
});
|
|
24
|
+
return fromEntries(filtered);
|
|
25
|
+
};
|
|
26
|
+
function normalizePath(path, option) {
|
|
27
|
+
if (typeof option === "function") {
|
|
28
|
+
return normalizePath(path);
|
|
29
|
+
}
|
|
30
|
+
if (option === false) {
|
|
31
|
+
return path;
|
|
32
|
+
}
|
|
33
|
+
return normalize(path);
|
|
34
|
+
}
|
|
35
|
+
const invariant = (predicate, message) => {
|
|
36
|
+
if (!predicate) {
|
|
37
|
+
throw new Error(message);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
async function resolveModule(module) {
|
|
42
|
+
const resolved = await module;
|
|
11
43
|
return resolved.default || resolved;
|
|
12
44
|
}
|
|
45
|
+
function isPackageModuleFound(name, options) {
|
|
46
|
+
return Boolean(resolvePackageModulePath(name, options));
|
|
47
|
+
}
|
|
48
|
+
function resolvePackageModulePath(name, options) {
|
|
49
|
+
const resolvedPath = findResolvedModulePath(
|
|
50
|
+
[`${name}/package.json`, name],
|
|
51
|
+
options
|
|
52
|
+
);
|
|
53
|
+
if (resolvedPath === void 0) {
|
|
54
|
+
console.error(`Could not resolve package ${name}`);
|
|
55
|
+
return void 0;
|
|
56
|
+
}
|
|
57
|
+
return resolvedPath;
|
|
58
|
+
}
|
|
59
|
+
function findResolvedModulePath(paths, options) {
|
|
60
|
+
for (const path of paths) {
|
|
61
|
+
const resolvedPath = resolveModulePath(path, options);
|
|
62
|
+
if (resolvedPath === void 0)
|
|
63
|
+
continue;
|
|
64
|
+
return resolvedPath;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function resolveModulePath(modulePath, options) {
|
|
68
|
+
const { normalize = true, paths, ...rest } = options ?? {};
|
|
69
|
+
try {
|
|
70
|
+
const path = normalizePath(modulePath, normalize);
|
|
71
|
+
return resolvePathSync(path, {
|
|
72
|
+
url: paths,
|
|
73
|
+
...rest
|
|
74
|
+
});
|
|
75
|
+
} catch (e) {
|
|
76
|
+
return void 0;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const tsconfig = (tsconfigDir) => ({
|
|
81
|
+
get paths() {
|
|
82
|
+
return globbySync(["tsconfig.json", "tsconfig.*.json"], {
|
|
83
|
+
cwd: tsconfigDir
|
|
84
|
+
}) ?? [];
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
function parseGitignoreFile(gitignorePath, options) {
|
|
89
|
+
return gitignoreParser(gitignorePath, options);
|
|
90
|
+
}
|
|
91
|
+
function getGitignoreData(gitignorePath) {
|
|
92
|
+
const gitignoreFileContent = readFileSync(gitignorePath, "utf-8");
|
|
93
|
+
const gitignores = parseGitignoreFile(gitignoreFileContent);
|
|
94
|
+
return gitignores;
|
|
95
|
+
}
|
|
96
|
+
const gitignore = (gitignorePath) => ({
|
|
97
|
+
get data() {
|
|
98
|
+
return getGitignoreData(gitignorePath);
|
|
99
|
+
},
|
|
100
|
+
get patterns() {
|
|
101
|
+
return getGitignoreData(gitignorePath).patterns;
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const dependencyTypeMap = {
|
|
106
|
+
dependency: "dependencies",
|
|
107
|
+
devDependency: "devDependencies",
|
|
108
|
+
peerDependency: "peerDependencies",
|
|
109
|
+
optionalDependency: "optionalDependencies"
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
function isDependencyInPackageJson(options, packageJson) {
|
|
113
|
+
return Boolean(findDependencyInPackageJson(options, packageJson));
|
|
114
|
+
}
|
|
115
|
+
function findDependencyInPackageJson(option, packageJson) {
|
|
116
|
+
const { name, type } = typeof option === "string" ? { name: option, type: void 0 } : option;
|
|
117
|
+
if (!packageJson)
|
|
118
|
+
return void 0;
|
|
119
|
+
const allowedTypes = typeof type === "string" ? {
|
|
120
|
+
[type]: true
|
|
121
|
+
} : type ?? {
|
|
122
|
+
dependency: true,
|
|
123
|
+
devDependency: true
|
|
124
|
+
};
|
|
125
|
+
const allowedDependencyTypes = getAllowedDependencyTypeList(allowedTypes);
|
|
126
|
+
const matches = allowedDependencyTypes.map((type2) => {
|
|
127
|
+
return getPackageJsonDependencyItem({ name, type: type2 }, packageJson);
|
|
128
|
+
}).filter(notFalsy);
|
|
129
|
+
if (matches.length === 0)
|
|
130
|
+
return void 0;
|
|
131
|
+
return {
|
|
132
|
+
firstMatch: matches[0],
|
|
133
|
+
matches
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function getPackageJsonDependencyItem({
|
|
137
|
+
name,
|
|
138
|
+
type
|
|
139
|
+
}, packageJson) {
|
|
140
|
+
if (!packageJson)
|
|
141
|
+
return void 0;
|
|
142
|
+
const dependencyMap = getPackageJsonDependencyMap(type, packageJson);
|
|
143
|
+
const version = dependencyMap?.[name];
|
|
144
|
+
if (version === void 0)
|
|
145
|
+
return void 0;
|
|
146
|
+
return {
|
|
147
|
+
name,
|
|
148
|
+
version,
|
|
149
|
+
type
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
function getAllowedDependencyTypeList(selected) {
|
|
153
|
+
return Object.keys(dependencyTypeMap).filter(
|
|
154
|
+
(key) => selected[key]
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
function getPackageJsonDependencyMap(type, packageJson) {
|
|
158
|
+
return packageJson[dependencyTypeMap[type]];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function getWorkspaceFolder(options) {
|
|
162
|
+
const {
|
|
163
|
+
cwd = process.cwd(),
|
|
164
|
+
fallbackToGitRoot = true,
|
|
165
|
+
throwIfNotFound = true
|
|
166
|
+
} = options ?? {};
|
|
167
|
+
const getFolder = fallbackToGitRoot ? WST.getWorkspaceRoot : WST.getWorkspaceRoot;
|
|
168
|
+
const folder = getFolder(cwd);
|
|
169
|
+
if (folder === void 0 && throwIfNotFound) {
|
|
170
|
+
throw new Error(`Could not find workspace folder`);
|
|
171
|
+
}
|
|
172
|
+
return folder;
|
|
173
|
+
}
|
|
174
|
+
getWorkspaceFolder();
|
|
175
|
+
|
|
176
|
+
function getPackageFolder(options) {
|
|
177
|
+
return WST.findPackageRoot(options?.cwd ?? process.cwd());
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function getPackageInfo(options) {
|
|
181
|
+
const packageDir = getPackageFolder(options);
|
|
182
|
+
return readPackageInfo({ packageDir });
|
|
183
|
+
}
|
|
184
|
+
function readPackageInfo({
|
|
185
|
+
packageDir
|
|
186
|
+
}) {
|
|
187
|
+
const packageJsonPath = path.join(packageDir, "package.json");
|
|
188
|
+
const packageJson = JSON.parse(
|
|
189
|
+
readFileSync(packageJsonPath, "utf-8")
|
|
190
|
+
);
|
|
191
|
+
return {
|
|
192
|
+
name: packageJson.name,
|
|
193
|
+
path: packageJsonPath,
|
|
194
|
+
dirpath: packageDir,
|
|
195
|
+
packageJson
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function getWorkspaceProjectInfo(options) {
|
|
200
|
+
try {
|
|
201
|
+
const workspaceDir = getWorkspaceFolder(options);
|
|
202
|
+
return readPackageInfo({ packageDir: workspaceDir });
|
|
203
|
+
} catch (e) {
|
|
204
|
+
console.error(e);
|
|
205
|
+
throw new Error("Root workspace not found");
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function getWorkspacePackageInfoList(options) {
|
|
210
|
+
const { workspaceDir, resolveWstList } = common(options);
|
|
211
|
+
const wstList = WST.getWorkspaces(workspaceDir);
|
|
212
|
+
return resolveWstList(wstList);
|
|
213
|
+
}
|
|
214
|
+
function common(options) {
|
|
215
|
+
const { cwd, includeRoot: includeWorkspace } = options ?? {};
|
|
216
|
+
const workspaceDir = getWorkspaceFolder({ cwd });
|
|
217
|
+
function resolveWstList(list) {
|
|
218
|
+
if (!workspaceDir)
|
|
219
|
+
return [];
|
|
220
|
+
if (includeWorkspace)
|
|
221
|
+
return [getWorkspaceProjectInfo(), ...list];
|
|
222
|
+
return list;
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
workspaceDir,
|
|
226
|
+
includeWorkspace,
|
|
227
|
+
resolveWstList
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function getWorkspacePackageInfoMap(options) {
|
|
232
|
+
const workspaceInfo = getWorkspacePackageInfoList(options);
|
|
233
|
+
const entries = workspaceInfo.map((info) => [info.name, info]);
|
|
234
|
+
return Object.fromEntries(entries);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function getProjectInfoByName(name, options) {
|
|
238
|
+
const { cwd } = options ?? {};
|
|
239
|
+
return getWorkspacePackageInfoMap({ cwd })[name];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function getProjectInfo(folder, options) {
|
|
243
|
+
if (isWorkspaceFolderTypeOption(folder)) {
|
|
244
|
+
return getWorkspaceProjectInfo(
|
|
245
|
+
parseWorkspaceFolderTypeOptions(folder, options)
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
if (folder === "<package_folder>") {
|
|
249
|
+
return getPackageInfo(options);
|
|
250
|
+
}
|
|
251
|
+
if (folder === "<gitroot_folder>") {
|
|
252
|
+
return getPackageInfo(options);
|
|
253
|
+
}
|
|
254
|
+
return getProjectInfoByName(folder.packageName, options);
|
|
255
|
+
}
|
|
256
|
+
function isWorkspaceFolderTypeOption(option) {
|
|
257
|
+
return Boolean(
|
|
258
|
+
option === "<workspace_folder>" || typeof option === "object" && "<workspace_folder>" in option
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
function parseWorkspaceFolderTypeOptions(option, defaultOptions) {
|
|
262
|
+
if (typeof option === "string") {
|
|
263
|
+
return defaultOptions ?? {};
|
|
264
|
+
}
|
|
265
|
+
return { ...defaultOptions, ...option["<workspace_folder>"] };
|
|
266
|
+
}
|
|
267
|
+
getProjectInfo({
|
|
268
|
+
"<workspace_folder>": {}
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
const project = (...args) => {
|
|
272
|
+
const [source, projectOptions] = args;
|
|
273
|
+
const { packageJson, packageJsonPath, packageName, projectDir } = info();
|
|
274
|
+
const {
|
|
275
|
+
findPackageManager: findPackageManager2,
|
|
276
|
+
detectPackageManagers,
|
|
277
|
+
detectLockfilePackageManagers,
|
|
278
|
+
detectGlobalPackageManagers,
|
|
279
|
+
filterPackageManagers
|
|
280
|
+
} = definePackageManagerClient({ cwd: projectDir });
|
|
281
|
+
return {
|
|
282
|
+
packageJson,
|
|
283
|
+
packageJsonPath,
|
|
284
|
+
packageName,
|
|
285
|
+
projectDir,
|
|
286
|
+
findPackageManager: findPackageManager2,
|
|
287
|
+
detectPackageManagers,
|
|
288
|
+
detectGlobalPackageManagers,
|
|
289
|
+
detectLockfilePackageManagers,
|
|
290
|
+
tsconfig: tsconfig(projectDir ?? ""),
|
|
291
|
+
gitignore: gitignore(path$1.join(projectDir ?? "", ".gitignore")),
|
|
292
|
+
filterPackageManagers,
|
|
293
|
+
getPackageJson: () => get("packageJson"),
|
|
294
|
+
findDependencyInPackageJson: (options) => findDependencyInPackageJson(options, get("packageJson")),
|
|
295
|
+
isDependencyInPackageJson: (options) => isDependencyInPackageJson(options, get("packageJson"))
|
|
296
|
+
};
|
|
297
|
+
function info() {
|
|
298
|
+
const {
|
|
299
|
+
packageJson: packageJson2,
|
|
300
|
+
name: packageName2,
|
|
301
|
+
dirpath: projectDir2,
|
|
302
|
+
path: packageJsonPath2
|
|
303
|
+
} = getProjectInfo(source, projectOptions) ?? {};
|
|
304
|
+
return {
|
|
305
|
+
packageJson: packageJson2,
|
|
306
|
+
packageJsonPath: packageJsonPath2,
|
|
307
|
+
packageName: packageName2,
|
|
308
|
+
projectDir: projectDir2
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
function get(key) {
|
|
312
|
+
return info()[key];
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
function getWorkspacePackageNames(options) {
|
|
317
|
+
return getWorkspacePackageInfoList(options).map((e) => e.name);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const workspace = {
|
|
321
|
+
packageNames: getWorkspacePackageNames,
|
|
322
|
+
packageGraph: getWorkspacePackageInfoMap,
|
|
323
|
+
packageList: getWorkspacePackageInfoList,
|
|
324
|
+
workspaceRootDir: getWorkspaceFolder,
|
|
325
|
+
getProject: project
|
|
326
|
+
};
|
|
13
327
|
|
|
14
328
|
function importer(imports, options) {
|
|
15
|
-
const { install = true } = options ?? {};
|
|
329
|
+
const { install: defaultInstall = true, installer } = options ?? {};
|
|
16
330
|
return Promise.all(
|
|
17
|
-
imports.map(async (
|
|
18
|
-
|
|
19
|
-
|
|
331
|
+
imports.map(async (e) => {
|
|
332
|
+
const importOpt = resolveImportOption(e);
|
|
333
|
+
const shouldInstall = importOpt.install ?? defaultInstall;
|
|
334
|
+
if (shouldInstall && importOpt.name) {
|
|
335
|
+
await installImport(importOpt.name, importOpt, installer);
|
|
20
336
|
}
|
|
21
|
-
const
|
|
22
|
-
return resolveModule(
|
|
337
|
+
const m = await importOpt.import();
|
|
338
|
+
return resolveModule(m);
|
|
23
339
|
})
|
|
24
340
|
);
|
|
25
341
|
}
|
|
26
|
-
|
|
342
|
+
const definePackage = (option) => {
|
|
343
|
+
const { name, ...rest } = typeof option === "string" ? { name: option } : option;
|
|
344
|
+
return {
|
|
345
|
+
name,
|
|
346
|
+
import: () => import(name),
|
|
347
|
+
...rest
|
|
348
|
+
};
|
|
349
|
+
};
|
|
350
|
+
function resolveImportOption(option) {
|
|
27
351
|
if (typeof option === "function") {
|
|
28
|
-
return
|
|
352
|
+
return {
|
|
353
|
+
import: option
|
|
354
|
+
};
|
|
29
355
|
}
|
|
30
|
-
if (
|
|
31
|
-
return
|
|
356
|
+
if (option instanceof Promise) {
|
|
357
|
+
return {
|
|
358
|
+
import: () => option
|
|
359
|
+
};
|
|
32
360
|
}
|
|
33
361
|
return option;
|
|
34
362
|
}
|
|
363
|
+
async function installImport(packageName, options, installer) {
|
|
364
|
+
if (!packageName)
|
|
365
|
+
return;
|
|
366
|
+
const { checkExists } = options ?? {};
|
|
367
|
+
if (checkExists && isPackageDependency(packageName))
|
|
368
|
+
return;
|
|
369
|
+
const installerFn = installer ?? (await workspace.getProject("<package_folder>").findPackageManager()).installPackage;
|
|
370
|
+
await installerFn(packageName, options);
|
|
371
|
+
}
|
|
35
372
|
|
|
36
|
-
async
|
|
373
|
+
const importMap = async (importMap2, options) => {
|
|
37
374
|
const keys = Object.keys(importMap2);
|
|
38
375
|
const imported = await importer(
|
|
39
376
|
keys.map((key) => importMap2[key]),
|
|
@@ -42,43 +379,55 @@ async function importMap(importMap2, options) {
|
|
|
42
379
|
return Object.fromEntries(
|
|
43
380
|
keys.map((key, index) => [key, imported[index]])
|
|
44
381
|
);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const toArray = (data) => Array.isArray(data) ? data : [data];
|
|
48
|
-
const entriesOf = (o) => Object.entries(o);
|
|
49
|
-
const fromEntries = (entries) => Object.fromEntries(entries);
|
|
50
|
-
const notFalsy = (value) => [false, null, void 0].every((v) => v !== value);
|
|
51
|
-
const select = (obj, selection, mode) => {
|
|
52
|
-
if (!selection)
|
|
53
|
-
return obj;
|
|
54
|
-
const filtered = entriesOf(obj).filter(([key]) => {
|
|
55
|
-
return mode === "omit" ? !selection[key] : selection[key];
|
|
56
|
-
});
|
|
57
|
-
return fromEntries(filtered);
|
|
58
|
-
};
|
|
59
|
-
const invariant = (predicate, message) => {
|
|
60
|
-
if (!predicate) {
|
|
61
|
-
throw new Error(message);
|
|
62
|
-
}
|
|
63
382
|
};
|
|
64
383
|
|
|
65
|
-
function
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async function ensurePackage(name, options) {
|
|
70
|
-
if (isPackageDependency(name))
|
|
71
|
-
return;
|
|
72
|
-
await installPackage(name, options);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function definePackageManager(config) {
|
|
384
|
+
function definePackageManager(config, options) {
|
|
385
|
+
const { cwd: defaultCwd } = options ?? {};
|
|
76
386
|
const { command, args: agentArgs, options: agentOptions } = config;
|
|
77
|
-
const findLockfilePath = asyncCacheFn(async (
|
|
78
|
-
const { cwd } =
|
|
387
|
+
const findLockfilePath = asyncCacheFn(async (options2) => {
|
|
388
|
+
const { cwd = defaultCwd } = options2 ?? {};
|
|
79
389
|
const lockfiles = toArray(config.meta.lockfile);
|
|
80
390
|
return await findUp(lockfiles, { cwd });
|
|
81
391
|
});
|
|
392
|
+
const installPackage = async (packageName, options2) => {
|
|
393
|
+
const install = agentArgs.install;
|
|
394
|
+
const { dev, preferOffline } = select(
|
|
395
|
+
install.options,
|
|
396
|
+
{
|
|
397
|
+
preferOffline: true,
|
|
398
|
+
cwd: defaultCwd,
|
|
399
|
+
...options2
|
|
400
|
+
},
|
|
401
|
+
"pick"
|
|
402
|
+
);
|
|
403
|
+
const packageNames = toArray(packageName);
|
|
404
|
+
try {
|
|
405
|
+
await $$({
|
|
406
|
+
command,
|
|
407
|
+
args: [install.command, dev, preferOffline, ...packageNames],
|
|
408
|
+
cwd: defaultCwd,
|
|
409
|
+
...options2
|
|
410
|
+
});
|
|
411
|
+
} catch (e) {
|
|
412
|
+
throw new Error(`Failed to install: ${packageNames.join(", ")}`);
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
const uninstallPackage = async (packageName, options2) => {
|
|
416
|
+
const uninstall = agentArgs.uninstall;
|
|
417
|
+
await Promise.all(
|
|
418
|
+
toArray(packageName).map(async (name) => {
|
|
419
|
+
try {
|
|
420
|
+
await $$({
|
|
421
|
+
command,
|
|
422
|
+
args: [uninstall.command, name],
|
|
423
|
+
cwd: defaultCwd,
|
|
424
|
+
...options2
|
|
425
|
+
});
|
|
426
|
+
} catch (e) {
|
|
427
|
+
}
|
|
428
|
+
})
|
|
429
|
+
);
|
|
430
|
+
};
|
|
82
431
|
return {
|
|
83
432
|
id: config.id,
|
|
84
433
|
config,
|
|
@@ -94,50 +443,27 @@ function definePackageManager(config) {
|
|
|
94
443
|
return readFile(lockfilePath, "utf8");
|
|
95
444
|
}),
|
|
96
445
|
globalVersion: asyncCacheFn(async (...args) => {
|
|
97
|
-
const [
|
|
446
|
+
const [options2] = args;
|
|
98
447
|
try {
|
|
99
448
|
const { stdout } = await $$({
|
|
100
449
|
command,
|
|
101
450
|
args: [agentOptions.version],
|
|
102
|
-
|
|
451
|
+
cwd: defaultCwd,
|
|
452
|
+
...options2
|
|
103
453
|
});
|
|
104
454
|
return `${stdout}`;
|
|
105
455
|
} catch (e) {
|
|
106
456
|
return void 0;
|
|
107
457
|
}
|
|
108
458
|
}),
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
try {
|
|
118
|
-
await $$({
|
|
119
|
-
command,
|
|
120
|
-
args: [
|
|
121
|
-
install.command,
|
|
122
|
-
isDevDependency,
|
|
123
|
-
preferOffline,
|
|
124
|
-
...packageNames
|
|
125
|
-
],
|
|
126
|
-
...options
|
|
127
|
-
});
|
|
128
|
-
} catch (e) {
|
|
129
|
-
throw new Error(`Failed to install: ${packageNames.join(", ")}`);
|
|
130
|
-
}
|
|
131
|
-
},
|
|
132
|
-
uninstallPackage: async (packageName, options) => {
|
|
133
|
-
const uninstall = agentArgs.uninstall;
|
|
134
|
-
if (!isPackageDependency(packageName))
|
|
135
|
-
return;
|
|
136
|
-
await $$({
|
|
137
|
-
command,
|
|
138
|
-
args: [uninstall.command, ...toArray(packageName)],
|
|
139
|
-
...options
|
|
140
|
-
}).catch((e) => {
|
|
459
|
+
definePackage,
|
|
460
|
+
installPackage,
|
|
461
|
+
uninstallPackage,
|
|
462
|
+
defineImportMap(imports, options2) {
|
|
463
|
+
const { install = true } = options2 ?? {};
|
|
464
|
+
return importMap(imports, {
|
|
465
|
+
install,
|
|
466
|
+
installer: installPackage
|
|
141
467
|
});
|
|
142
468
|
}
|
|
143
469
|
};
|
|
@@ -151,7 +477,11 @@ async function $$(options) {
|
|
|
151
477
|
});
|
|
152
478
|
}
|
|
153
479
|
|
|
154
|
-
|
|
480
|
+
function definePackageManagerConfig(config) {
|
|
481
|
+
return config;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const bun = definePackageManagerConfig({
|
|
155
485
|
id: "bun",
|
|
156
486
|
name: "Bun",
|
|
157
487
|
command: "bun",
|
|
@@ -163,7 +493,7 @@ const bun = definePackageManager({
|
|
|
163
493
|
install: {
|
|
164
494
|
command: "install",
|
|
165
495
|
options: {
|
|
166
|
-
|
|
496
|
+
dev: "-D",
|
|
167
497
|
preferOffline: "--prefer-offline"
|
|
168
498
|
}
|
|
169
499
|
},
|
|
@@ -176,7 +506,7 @@ const bun = definePackageManager({
|
|
|
176
506
|
}
|
|
177
507
|
});
|
|
178
508
|
|
|
179
|
-
const npm =
|
|
509
|
+
const npm = definePackageManagerConfig({
|
|
180
510
|
id: "npm",
|
|
181
511
|
name: "NPM",
|
|
182
512
|
command: "npm",
|
|
@@ -188,7 +518,7 @@ const npm = definePackageManager({
|
|
|
188
518
|
install: {
|
|
189
519
|
command: "install",
|
|
190
520
|
options: {
|
|
191
|
-
|
|
521
|
+
dev: "-D",
|
|
192
522
|
preferOffline: "--prefer-offline"
|
|
193
523
|
}
|
|
194
524
|
},
|
|
@@ -201,7 +531,7 @@ const npm = definePackageManager({
|
|
|
201
531
|
}
|
|
202
532
|
});
|
|
203
533
|
|
|
204
|
-
const pnpm =
|
|
534
|
+
const pnpm = definePackageManagerConfig({
|
|
205
535
|
id: "pnpm",
|
|
206
536
|
name: "PNPM",
|
|
207
537
|
command: "pnpm",
|
|
@@ -213,12 +543,12 @@ const pnpm = definePackageManager({
|
|
|
213
543
|
install: {
|
|
214
544
|
command: "install",
|
|
215
545
|
options: {
|
|
216
|
-
|
|
546
|
+
dev: "-D",
|
|
217
547
|
preferOffline: "--prefer-offline"
|
|
218
548
|
}
|
|
219
549
|
},
|
|
220
550
|
uninstall: {
|
|
221
|
-
command: "
|
|
551
|
+
command: "remove"
|
|
222
552
|
}
|
|
223
553
|
},
|
|
224
554
|
options: {
|
|
@@ -226,7 +556,7 @@ const pnpm = definePackageManager({
|
|
|
226
556
|
}
|
|
227
557
|
});
|
|
228
558
|
|
|
229
|
-
const yarn =
|
|
559
|
+
const yarn = definePackageManagerConfig({
|
|
230
560
|
id: "yarn",
|
|
231
561
|
name: "Yarn",
|
|
232
562
|
command: "yarn",
|
|
@@ -238,7 +568,7 @@ const yarn = definePackageManager({
|
|
|
238
568
|
install: {
|
|
239
569
|
command: "add",
|
|
240
570
|
options: {
|
|
241
|
-
|
|
571
|
+
dev: "-D",
|
|
242
572
|
preferOffline: "--prefer-offline"
|
|
243
573
|
}
|
|
244
574
|
},
|
|
@@ -251,51 +581,82 @@ const yarn = definePackageManager({
|
|
|
251
581
|
}
|
|
252
582
|
});
|
|
253
583
|
|
|
254
|
-
|
|
255
|
-
[pnpm, yarn, bun, npm].map((e) => [e.id, e])
|
|
256
|
-
);
|
|
257
|
-
|
|
258
|
-
async function findPackageManager(options) {
|
|
584
|
+
async function findPackageManager(packageManagers, options) {
|
|
259
585
|
const { ...rest } = options ?? {};
|
|
260
|
-
const packageManager = await findPackageManagerSafely(rest);
|
|
586
|
+
const packageManager = await findPackageManagerSafely(packageManagers, rest);
|
|
261
587
|
invariant(packageManager, "No package manager found");
|
|
262
588
|
return packageManager;
|
|
263
589
|
}
|
|
264
|
-
async function findPackageManagerSafely(options) {
|
|
265
|
-
const lockfilePm = (await detectLockfilePackageManagers(options))[0];
|
|
590
|
+
async function findPackageManagerSafely(packageManagers, options) {
|
|
591
|
+
const lockfilePm = (await detectLockfilePackageManagers(packageManagers, options))[0];
|
|
266
592
|
if (lockfilePm) {
|
|
267
593
|
return lockfilePm;
|
|
268
594
|
}
|
|
269
|
-
const globalPm = (await detectGlobalPackageManagers(options))[0];
|
|
595
|
+
const globalPm = (await detectGlobalPackageManagers(packageManagers, options))[0];
|
|
270
596
|
return globalPm;
|
|
271
597
|
}
|
|
272
|
-
async function detectPackageManagers(options) {
|
|
598
|
+
async function detectPackageManagers(packageManagers, options) {
|
|
273
599
|
return [
|
|
274
|
-
...await detectLockfilePackageManagers(options),
|
|
275
|
-
...await detectGlobalPackageManagers(options)
|
|
600
|
+
...await detectLockfilePackageManagers(packageManagers, options),
|
|
601
|
+
...await detectGlobalPackageManagers(packageManagers, options)
|
|
276
602
|
];
|
|
277
603
|
}
|
|
278
|
-
async function detectLockfilePackageManagers(options) {
|
|
279
|
-
(
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
604
|
+
async function detectLockfilePackageManagers(packageManagers, options) {
|
|
605
|
+
return filterPackageManagers(
|
|
606
|
+
packageManagers,
|
|
607
|
+
(e) => e.hasLockfile(options),
|
|
608
|
+
options
|
|
609
|
+
);
|
|
284
610
|
}
|
|
285
|
-
async function
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
return key in options.allowed;
|
|
291
|
-
}
|
|
611
|
+
async function detectGlobalPackageManagers(packageManagers, options) {
|
|
612
|
+
return filterPackageManagers(
|
|
613
|
+
packageManagers,
|
|
614
|
+
async (e) => Boolean(e.globalVersion(options)),
|
|
615
|
+
options
|
|
292
616
|
);
|
|
617
|
+
}
|
|
618
|
+
async function filterPackageManagers(packageManagers, filterFn, options) {
|
|
619
|
+
const allowedPackageManagers = packageManagers.filter(({ id }) => {
|
|
620
|
+
if (!options?.allowed)
|
|
621
|
+
return true;
|
|
622
|
+
return id in options.allowed;
|
|
623
|
+
});
|
|
293
624
|
return (await Promise.all(
|
|
294
|
-
allowedPackageManagers.map(async (
|
|
625
|
+
allowedPackageManagers.map(async (pm) => {
|
|
295
626
|
const valid = await filterFn(pm);
|
|
296
627
|
return valid ? pm : void 0;
|
|
297
628
|
})
|
|
298
629
|
)).filter(notFalsy);
|
|
299
630
|
}
|
|
300
631
|
|
|
301
|
-
|
|
632
|
+
const packageManagerConfigs = [pnpm, yarn, bun, npm];
|
|
633
|
+
function definePackageManagerClient(options) {
|
|
634
|
+
const configs = packageManagerConfigs.map(
|
|
635
|
+
(config) => definePackageManager(config, options)
|
|
636
|
+
);
|
|
637
|
+
return {
|
|
638
|
+
configs,
|
|
639
|
+
findPackageManager: asyncCacheFn(
|
|
640
|
+
async (options2) => await findPackageManager(configs, options2)
|
|
641
|
+
),
|
|
642
|
+
findPackageManagerSafely: asyncCacheFn(
|
|
643
|
+
async (options2) => await findPackageManagerSafely(configs, options2)
|
|
644
|
+
),
|
|
645
|
+
detectPackageManagers: asyncCacheFn(
|
|
646
|
+
async (options2) => await detectPackageManagers(configs, options2)
|
|
647
|
+
),
|
|
648
|
+
detectLockfilePackageManagers: asyncCacheFn(
|
|
649
|
+
async (options2) => detectLockfilePackageManagers(configs, options2)
|
|
650
|
+
),
|
|
651
|
+
detectGlobalPackageManagers: asyncCacheFn(
|
|
652
|
+
async (options2) => detectGlobalPackageManagers(configs, options2)
|
|
653
|
+
),
|
|
654
|
+
filterPackageManagers: async (filterFn, options2) => filterPackageManagers(configs, filterFn, options2)
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
function isPackageDependency(packageName) {
|
|
659
|
+
return toArray(packageName).every((name) => isDependencyInPackageJson(name));
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
export { definePackage, definePackageManagerClient, detectGlobalPackageManagers, detectLockfilePackageManagers, detectPackageManagers, filterPackageManagers, findPackageManager, findPackageManagerSafely, findResolvedModulePath, importMap, importer, isPackageDependency, isPackageModuleFound, packageManagerConfigs, resolveModule, resolveModulePath, resolvePackageModulePath };
|