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