-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathnpm.js
More file actions
342 lines (297 loc) · 8.13 KB
/
npm.js
File metadata and controls
342 lines (297 loc) · 8.13 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// @ts-check
const fs = require('fs-extra');
const _ = require('lodash');
const semver = require('semver');
const listify = require('listify');
const validateNpmPackageName = require('validate-npm-package-name');
const log = require('./util/log');
const execCommand = require('./util/execCommand');
const json = require('./formats/json');
const packageJson = require('./files/packageJson');
const MrmError = require('./error');
/**
* @typedef Options
* @property {boolean} [dev]
* @property {boolean} [yarn]
* @property {boolean} [yarnBerry]
* @property {boolean} [pnpm]
* @property {Record<string, string>} [versions]
*/
/**
* @typedef RunOptions
* @property {boolean} [dev]
* @property {boolean} [remove]
* @property {boolean} [stdio]
* @property {string} [cwd]
*/
/**
* Install or update given npm packages if needed
* @param {Record<string, string> | string[] | string} deps
* @param {Options} [options]
* @param {Function} exec
*/
function install(deps, options = {}, exec) {
const dev = options.dev !== false;
const run = getRunFunction(options);
// options.versions is a min versions mapping,
// the list of packages to install will be taken from deps
let versions = options.versions || {};
/** @type string[] */
let dependencies = [];
if (typeof deps === 'string') {
dependencies = [deps];
} else if (Array.isArray(deps)) {
dependencies = deps;
} else if (typeof deps === 'object' && deps !== null) {
// deps is an object with required versions
// prettier-ignore
versions = deps;
dependencies = Object.keys(deps);
}
const newDeps = getUnsatisfiedDeps(dependencies, versions, { dev });
if (newDeps.length === 0) {
return;
}
log.info(`Installing ${listify(newDeps)}...`);
const versionedDeps = newDeps.map(dep => getVersionedDep(dep, versions));
// eslint-disable-next-line consistent-return
return run(versionedDeps, { dev }, exec);
}
/**
* Uninstall given npm packages
* @param {string[] | string} deps
* @param {Options} [options]
* @param {Function} exec
*/
function uninstall(deps, options = {}, exec) {
deps = _.castArray(deps);
const dev = options.dev !== false;
const run = getRunFunction(options);
const installed = getOwnDependencies({ dev });
const newDeps = deps.filter(dep => installed[dep]);
if (newDeps.length === 0) {
return;
}
log.info(`Uninstalling ${listify(newDeps)}...`);
// eslint-disable-next-line consistent-return
return run(newDeps, { remove: true, dev }, exec);
}
/**
* Return suitable run function
*
* @param {Options} [options]
*/
function getRunFunction(options = {}) {
if (options.yarnBerry || isUsingYarnBerry()) {
return runYarnBerry;
} else if (options.yarn || isUsingYarn()) {
return runYarn;
} else if (options.pnpm || isUsingPnpm()) {
return runPnpm;
} else if (options.bun || isUsingBun()) {
return runBun;
} else {
return runNpm;
}
}
/**
* Install or uninstall given npm packages
*
* @param {string[]} deps
* @param {RunOptions} [options]
* @param {Function} [exec]
*/
function runNpm(deps, options = {}, exec) {
const args = [
options.remove ? 'uninstall' : 'install',
options.dev ? '--save-dev' : '--save',
].concat(deps);
return execCommand(exec, 'npm', args, {
stdio: options.stdio === undefined ? 'inherit' : options.stdio,
cwd: options.cwd,
});
}
/**
* Install or uninstall given Yarn packages
*
* This will use yarn's `--ignore-workspace-root-check` to allow additions of packages
* inside a repository that is using yarn's workspaces feature. If the current
* repository is _not_ using workspaces, then that flag is simply ignored.
*
* @see https://classic.yarnpkg.com/en/docs/cli/add/#toc-yarn-add-ignore-workspace-root-check-w
*
* @param {string[]} deps
* @param {RunOptions} [options]
* @param {Function} [exec]
*/
function runYarn(deps, options = {}, exec) {
const add = options.dev
? ['add', '--dev', '--ignore-workspace-root-check']
: ['add', '--ignore-workspace-root-check'];
const remove = ['remove'];
const args = (options.remove ? remove : add).concat(deps);
return execCommand(exec, 'yarn', args, {
stdio: options.stdio === undefined ? 'inherit' : options.stdio,
cwd: options.cwd,
});
}
/**
* Install or uninstall given Yarn@berry packages
*
* @param {string[]} deps
* @param {RunOptions} [options]
* @param {Function} [exec]
*/
function runYarnBerry(deps, options = {}, exec) {
const add = options.dev ? ['add', '--dev'] : ['add'];
const remove = ['remove'];
const args = (options.remove ? remove : add).concat(deps);
return execCommand(exec, 'yarn', args, {
stdio: options.stdio === undefined ? 'inherit' : options.stdio,
cwd: options.cwd,
});
}
/**
* Install or uninstall given pnpm packages
*
* @param {string[]} deps
* @param {RunOptions} [options]
* @param {Function} [exec]
*/
function runPnpm(deps, options = {}, exec) {
const args = [
options.remove ? 'remove' : 'add',
options.dev ? '--save-dev' : '--save-prod',
].concat(deps);
return execCommand(exec, 'pnpm', args, {
stdio: options.stdio === undefined ? 'inherit' : options.stdio,
cwd: options.cwd,
});
}
/**
* Install or uninstall given Bun packages
*
* @param {string[]} deps
* @param {RunOptions} [options={}]
* @param {Function} [exec]
*/
function runBun(deps, options = {}, exec) {
const args = [
options.remove ? 'remove' : 'add',
].concat(deps);
return execCommand(exec, 'bun', args, {
stdio: options.stdio === undefined ? 'inherit' : options.stdio,
cwd: options.cwd,
});
}
/**
* Add version or latest to package name
* @param {string} dep
* @param {Record<string, string>} versions
*/
function getVersionedDep(dep, versions) {
// Handle non-registry packages (Github, bitbucket, etc.)
if (!validateNpmPackageName(dep).validForNewPackages) {
// If we were explicitly passed a version, attempt to
// load it via the `#semver:<semver>` syntax.
if (versions[dep]) {
return `${dep}#semver:${versions[dep]}`;
} else {
return dep;
}
}
const version = versions[dep] || 'latest';
return `${dep}@${version}`;
}
/**
*
* @param {Options} options
* @return {Record<string, string>}
*/
function getOwnDependencies(options) {
const pkg = packageJson({
dependencies: {},
devDependencies: {},
});
return pkg.get(options.dev ? 'devDependencies' : 'dependencies') || {};
}
/**
* Return version of installed npm package
*
* @param {string} name
* @return {string}
*/
function getInstalledVersion(name) {
return json(`./node_modules/${name}/package.json`).get('version');
}
/**
* Return only not installed dependencies, or dependencies which installed
* version doesn't satisfy range.
*
* @param {string[]} deps
* @param {Record<string, string>} versions
* @param {Options} options
* @return {string[]}
*/
function getUnsatisfiedDeps(deps, versions, options) {
const ownDependencies = getOwnDependencies(options);
return deps.filter(dep => {
const required = versions[dep];
// Handle non-registry packages (github, bitbucket, etc.)
// Because these packages can shift contents without updating version
// numbers, always attempt an install
if (!validateNpmPackageName(dep).validForNewPackages) {
return true;
}
if (required && !semver.validRange(required)) {
throw new MrmError(
`Invalid npm version: ${required}. Use proper semver range syntax.`
);
}
const installed = getInstalledVersion(dep);
// Package isn’t installed yet
if (!installed) {
return true;
}
// Module is installed but not in package.json dependencies
if (!ownDependencies[dep]) {
return true;
}
// No required version specified
if (!required) {
// Install if the pacakge isn’t installed
return !installed;
}
// Install if installed version doesn't satisfy range
return !semver.satisfies(installed, required);
});
}
/*
* Is project using Yarn?
*/
function isUsingYarn() {
return fs.existsSync('yarn.lock');
}
/*
* Is project using Yarn@berry?
*/
function isUsingYarnBerry() {
return isUsingYarn() && fs.existsSync('.yarnrc.yml');
}
/*
* Is project using pnpm?
*/
function isUsingPnpm() {
return fs.existsSync('pnpm-lock.yaml');
}
/**
* Is project using Bun?
*/
function isUsingBun() {
return fs.existsSync('bun.lockb');
}
module.exports = {
install,
uninstall,
isUsingYarnBerry,
};