-
Notifications
You must be signed in to change notification settings - Fork 663
/
Copy pathnpm.ts
190 lines (160 loc) · 5.44 KB
/
npm.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { PackageJson } from '@ionic/cli-framework';
import { Subprocess } from '@ionic/utils-subprocess';
import { NpmClient } from '../../definitions';
interface PkgManagerVocabulary {
// commands
install: string;
bareInstall: string;
uninstall: string;
run: string;
dedupe: string;
rebuild: string;
// flags
global: string;
save: string;
saveDev: string;
saveExact: string;
nonInteractive: string;
lockFileOnly: string;
}
export type PkgManagerCommand = 'dedupe' | 'rebuild' | 'install' | 'uninstall' | 'run' | 'info';
export interface PkgManagerOptions {
command: PkgManagerCommand;
pkg?: string | string[];
script?: string;
scriptArgs?: string[];
global?: boolean;
save?: boolean;
saveDev?: boolean;
saveExact?: boolean;
json?: boolean;
lockFileOnly?: boolean;
}
/**
* Resolves pkg manager intent with command args.
*
* TODO: this is a weird function and should be split up
*
* @return Promise<args> If the args is an empty array, it means the pkg manager doesn't have that command.
*/
export async function pkgManagerArgs(npmClient: NpmClient, options: PkgManagerOptions): Promise<string[]> {
let vocab: PkgManagerVocabulary;
const cmd = options.command;
if (cmd === 'dedupe') {
delete options.pkg;
}
if (cmd === 'dedupe' || cmd === 'rebuild') {
delete options.global;
delete options.save;
delete options.saveDev;
}
if (cmd === 'dedupe' || cmd === 'rebuild' || cmd === 'uninstall') {
delete options.saveExact;
}
if (cmd === 'install' || cmd === 'uninstall') {
if (options.global) { // Turn off all save flags for global context
options.save = false;
options.saveDev = false;
options.saveExact = false;
} else if (options.pkg && typeof options.save === 'undefined' && typeof options.saveDev === 'undefined') { // Prefer save flag
options.save = true;
}
if (cmd === 'install' && options.pkg && typeof options.saveExact === 'undefined') { // For single package installs, prefer to save exact versions
options.saveExact = true;
}
}
const installerArgs: string[] = [];
switch (npmClient) {
case 'npm':
vocab = { run: 'run', install: 'i', bareInstall: 'i', uninstall: 'uninstall', dedupe: 'dedupe', rebuild: 'rebuild', global: '-g', save: '--save', saveDev: '-D', saveExact: '-E', nonInteractive: '', lockFileOnly: '--package-lock-only' };
break;
case 'yarn':
vocab = { run: 'run', install: 'add', bareInstall: 'install', uninstall: 'remove', dedupe: '', rebuild: 'install', global: '', save: '', saveDev: '--dev', saveExact: '--exact', nonInteractive: '--non-interactive', lockFileOnly: '' };
if (options.global) { // yarn installs packages globally under the 'global' prefix, instead of having a flag
installerArgs.push('global');
}
break;
case 'pnpm':
vocab = { run: 'run', install: 'add', bareInstall: 'install', uninstall: 'remove', dedupe: '', rebuild: 'rebuild', global: '--global', save: '', saveDev: '--save-dev', saveExact: '--save-exact', nonInteractive: '', lockFileOnly: '--lockfile-only' };
break;
default:
throw new Error(`unknown installer: ${npmClient}`);
}
if (cmd === 'install') {
if (options.pkg) {
installerArgs.push(vocab.install);
} else {
installerArgs.push(vocab.bareInstall);
}
if (options.lockFileOnly) {
installerArgs.push(vocab.lockFileOnly)
}
} else if (cmd === 'uninstall') {
installerArgs.push(vocab.uninstall);
} else if (cmd === 'dedupe') {
if (vocab.dedupe) {
installerArgs.push(vocab.dedupe);
} else {
return [];
}
} else if (cmd === 'rebuild') {
installerArgs.push(vocab.rebuild);
} else {
installerArgs.push(cmd);
}
if (options.global && vocab.global) {
installerArgs.push(vocab.global);
}
if (options.save && vocab.save) {
installerArgs.push(vocab.save);
}
if (options.saveDev && vocab.saveDev) {
installerArgs.push(vocab.saveDev);
}
if (options.saveExact && vocab.saveExact) {
installerArgs.push(vocab.saveExact);
}
if (vocab.nonInteractive) { // Some CLIs offer a flag that disables all interactivity, which we want to opt-into
installerArgs.push(vocab.nonInteractive);
}
if (options.pkg) {
if (typeof options.pkg === 'string'){
installerArgs.push(options.pkg);
}
if (Array.isArray(options.pkg)){
installerArgs.push(...options.pkg)
}
}
if (cmd === 'run' && options.script) {
installerArgs.push(options.script);
}
if (npmClient === 'yarn') {
if (cmd === 'rebuild') {
installerArgs.push('--force');
}
}
if (cmd === 'run' && options.script && options.scriptArgs && options.scriptArgs.length > 0) {
if (npmClient === 'npm' || npmClient === 'pnpm') {
installerArgs.push('--');
}
for (const arg of options.scriptArgs) {
installerArgs.push(arg);
}
}
if (options.json) {
installerArgs.push('--json');
}
return [npmClient, ...installerArgs];
}
/**
* @return Promise<package.json on registry or `undefined`>
*/
export async function pkgFromRegistry(npmClient: NpmClient, options: Partial<PkgManagerOptions>): Promise<PackageJson | undefined> {
const [ manager, ...managerArgs ] = await pkgManagerArgs(npmClient, { command: 'info', json: true, ...options });
const cmd = new Subprocess(manager, managerArgs);
const result = await cmd.output();
if (result) {
const json = JSON.parse(result);
return manager === 'yarn' ? json.data : json;
}
}