-
Notifications
You must be signed in to change notification settings - Fork 12k
/
packages.ts
60 lines (52 loc) · 1.82 KB
/
packages.ts
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
import { getGlobalVariable } from './env';
import { writeFile } from './fs';
import { npm, ProcessOutput, silentNpm, silentYarn } from './process';
export function getActivePackageManager(): 'npm' | 'yarn' {
const value = getGlobalVariable('package-manager');
if (value && value !== 'npm' && value !== 'yarn') {
throw new Error('Invalid package manager value: ' + value);
}
return value || 'npm';
}
export async function installWorkspacePackages(): Promise<void> {
switch (getActivePackageManager()) {
case 'npm':
await silentNpm('install');
break;
case 'yarn':
await silentYarn();
break;
}
}
export async 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);
}
}
export async function uninstallPackage(name: string): Promise<ProcessOutput> {
switch (getActivePackageManager()) {
case 'npm':
return silentNpm('uninstall', name);
case 'yarn':
return silentYarn('remove', name);
}
}
export async function setRegistry(useTestRegistry: boolean): Promise<void> {
const url = useTestRegistry
? getGlobalVariable('package-registry')
: 'https://registry.npmjs.org';
const isCI = getGlobalVariable('ci');
// Ensure local test registry is used when outside a project
if (isCI) {
// Safe to set a user configuration on CI
await npm('config', 'set', 'registry', url);
} else {
// Yarn does not use the environment variable so an .npmrc file is also required
await writeFile('.npmrc', `registry=${url}`);
process.env['NPM_CONFIG_REGISTRY'] = url;
}
}