-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Expand file tree
/
Copy pathpackages.ts
More file actions
87 lines (80 loc) · 2.5 KB
/
packages.ts
File metadata and controls
87 lines (80 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { getGlobalVariable } from './env';
import { ProcessOutput, silentBun, silentDeno, silentNpm, silentPnpm, silentYarn } from './process';
export interface PkgInfo {
readonly name: string;
readonly version: string;
readonly path: string;
}
export function getActivePackageManager(): 'npm' | 'yarn' | 'bun' | 'deno' | 'pnpm' {
return getGlobalVariable('package-manager');
}
export async function installWorkspacePackages(options?: { force?: boolean }): Promise<void> {
switch (getActivePackageManager()) {
case 'npm':
const npmArgs = ['install'];
if (options?.force) {
npmArgs.push('--force');
}
await silentNpm(...npmArgs);
break;
case 'yarn':
await silentYarn('install');
break;
case 'pnpm':
await silentPnpm('install');
break;
case 'bun':
await silentBun('install');
break;
case 'deno':
await silentDeno('install');
break;
}
}
export function installPackage(specifier: string, registry?: string): Promise<ProcessOutput> {
const registryOption = registry ? [`--registry=${registry}`] : [];
switch (getActivePackageManager()) {
case 'npm':
return silentNpm('install', specifier, ...registryOption);
case 'yarn':
return silentYarn('add', specifier, ...registryOption);
case 'bun':
return silentBun('add', specifier, ...registryOption);
case 'deno':
return silentDeno('add', specifier, ...registryOption);
case 'pnpm':
return silentPnpm('add', specifier, ...registryOption);
}
}
export async function uninstallPackage(name: string): Promise<void> {
try {
switch (getActivePackageManager()) {
case 'npm':
await silentNpm('uninstall', name);
break;
case 'yarn':
await silentYarn('remove', name);
break;
case 'bun':
await silentBun('remove', name);
break;
case 'deno':
await silentDeno('remove', name);
break;
case 'pnpm':
await silentPnpm('remove', name);
break;
}
} catch (e) {
// Yarn throws an error when trying to remove a package that is not installed.
console.error(e);
}
}
export async function setRegistry(useTestRegistry: boolean): Promise<void> {
const url = useTestRegistry
? getGlobalVariable('package-registry')
: 'https://registry.npmjs.org';
// Ensure local test registry is used when outside a project
// Yarn supports both `NPM_CONFIG_REGISTRY` and `YARN_REGISTRY`.
process.env['NPM_CONFIG_REGISTRY'] = url;
}