-
Notifications
You must be signed in to change notification settings - Fork 313
/
npm-check-updates.js
463 lines (397 loc) · 15.8 KB
/
npm-check-updates.js
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//
// Dependencies
//
'use strict';
const cint = require('cint');
const path = require('path');
const findUp = require('find-up');
const _ = require('lodash');
const getstdin = require('get-stdin');
const Table = require('cli-table');
const chalk = require('chalk');
const {promisify} = require('util');
const fs = require('fs');
const rcLoader = require('rc-config-loader');
const vm = require('./versionmanager');
const packageManagers = require('./package-managers');
const versionUtil = require('./version-util');
const jph = require('json-parse-helpfulerror');
// maps package managers to package file names
const packageFileNames = {
npm: 'package.json',
bower: 'bower.json'
};
// maps string levels to numeric levels
const logLevels = {
silent: 0,
error: 1,
minimal: 2,
warn: 3,
info: 4,
verbose: 5,
silly: 6
};
// time to wait for stdin before printing a warning
const stdinWarningTime = 5000;
const stdinWarningMessage = `Hmmmmm... this is taking a long time. Your console is telling me to wait for input \non stdin, but maybe that is not what you want.\nTry ${chalk.cyan('winpty ncu.cmd')}, or specify a package file explicitly with ${chalk.cyan('--packageFile package.json')}. \nSee https://github.com/tjunnone/npm-check-updates/issues/136#issuecomment-155721102`;
//
// Helper functions
//
function print(options, message, loglevel, method = 'log') {
// not in json mode
// not silent
// not at a loglevel under minimum specified
if (!options.json && options.loglevel !== 'silent' && (loglevel == null || logLevels[options.loglevel] >= logLevels[loglevel])) {
console[method](message);
}
}
function programError(options, message) {
if (options.cli) {
print(options, message, null, 'error');
process.exit(1);
} else {
throw new Error(message);
}
}
function printJson(options, object) {
if (options.loglevel !== 'silent') {
console.log(JSON.stringify(object, null, 2));
}
}
/**
* Gets the name of the package file based on --packageFile or --packageManager.
*/
function getPackageFileName(options) {
return options.packageFile ? options.packageFile :
packageFileNames[options.packageManager];
}
function createDependencyTable() {
return new Table({
colAligns: ['left', 'right', 'right', 'right'],
chars: {
top: '',
'top-mid': '',
'top-left': '',
'top-right': '',
bottom: '',
'bottom-mid': '',
'bottom-left': '',
'bottom-right': '',
left: '',
'left-mid': '',
mid: '',
'mid-mid': '',
right: '',
'right-mid': '',
middle: ''
}
});
}
/**
* @param args.from
* @param args.to
*/
function toDependencyTable(args) {
const table = createDependencyTable();
const rows = Object.keys(args.to).map(dep => {
const from = args.from[dep] || '';
const to = versionUtil.colorizeDiff(args.from[dep], args.to[dep] || '');
return [dep, from, '→', to];
});
rows.forEach(row => table.push(row));
return table;
}
const readPackageFile = cint.partialAt(promisify(fs.readFile), 1, 'utf8');
const writePackageFile = promisify(fs.writeFile);
//
// Main functions
//
function analyzeGlobalPackages(options) {
print(options, 'Getting installed packages...', 'verbose');
return vm.getInstalledPackages({
cwd: options.cwd,
filter: options.filter,
global: options.global,
packageManager: options.packageManager,
prefix: options.prefix,
reject: options.reject
})
.then(globalPackages => {
print(options, 'globalPackages', 'silly');
print(options, globalPackages, 'silly');
print(options, '', 'silly');
print(options, `Fetching ${vm.getVersionTarget(options)} versions...`, 'verbose');
return vm.upgradePackageDefinitions(globalPackages, options)
.then(([upgraded, latest]) => {
print(options, latest, 'silly');
const upgradedPackageNames = Object.keys(upgraded);
const upgradePromise = printUpgrades(options, {
current: globalPackages,
upgraded,
latest,
// since an interactive upgrade of globals is not available, the numUpgraded is always all
numUpgraded: upgradedPackageNames.length,
total: upgradedPackageNames.length
});
let instruction = '[package]';
if (upgraded) {
instruction = upgradedPackageNames.map(pkg => pkg + '@' + upgraded[pkg]).join(' ');
}
if (options.json) {
// since global packages do not have a package.json, return the upgraded deps directly (no version range replacements)
printJson(options, upgraded);
} else if (instruction.length) {
print(options, '\n' + chalk.cyan('ncu') + ' itself cannot upgrade global packages. Run the following to upgrade all global packages: \n\n' + chalk.cyan('npm -g install ' + instruction) + '\n');
}
return upgradePromise;
});
});
}
function analyzeProjectDependencies(options, pkgData, pkgFile) {
let pkg;
try {
if (!pkgData) {
throw new Error('pkgData: ' + pkgData);
} else {
pkg = jph.parse(pkgData);
}
} catch (e) {
programError(options, chalk.red(`Invalid package file${pkgFile ? `: ${pkgFile}` : ' from stdin'}. Error details:\n${e.message}`));
}
const current = vm.getCurrentDependencies(pkg, options);
print(options, `Fetching ${vm.getVersionTarget(options)} versions...`, 'verbose');
return vm.upgradePackageDefinitions(current, options).then(async ([upgraded, latest]) => {
const {newPkgData, selectedNewDependencies} = await vm.upgradePackageData(pkgData, current, upgraded, latest, options);
const output = options.jsonAll ? jph.parse(newPkgData) :
options.jsonDeps ?
_.pick(jph.parse(newPkgData), 'dependencies', 'devDependencies', 'optionalDependencies') :
selectedNewDependencies;
// split the deps into satisfied and unsatisfied to display in two separate tables
const deps = Object.keys(selectedNewDependencies);
const satisfied = cint.toObject(deps, dep =>
cint.keyValue(dep, vm.isSatisfied(latest[dep], current[dep]))
);
const isSatisfied = _.propertyOf(satisfied);
const filteredUpgraded = options.minimal ? cint.filterObject(selectedNewDependencies, cint.not(isSatisfied)) : selectedNewDependencies;
const numUpgraded = Object.keys(filteredUpgraded).length;
// print
if (options.json) {
// use the selectedNewDependencies dependencies data to generate new package data
// INVARIANT: we don't need try-catch here because pkgData has already been parsed as valid JSON, and vm.upgradePackageData simply does a find-and-replace on that
printJson(options, output);
} else {
printUpgrades(options, {
current,
upgraded: filteredUpgraded,
latest,
numUpgraded,
total: Object.keys(upgraded).length
});
}
if (numUpgraded > 0) {
// if error-level is 2, immediately exit with error code
if (options.errorLevel === 2) {
programError(options, '\nDependencies not up-to-date');
}
// if there is a package file, write the new package data
// otherwise, suggest ncu -u
if (pkgFile) {
if (options.upgrade) {
// short-circuit return in order to wait for write operation, but still return the same output
return writePackageFile(pkgFile, newPkgData)
.then(() => {
print(options, `\nRun ${chalk.cyan('npm install')} to install new versions.\n`);
return output;
});
} else {
print(options, `\nRun ${chalk.cyan('ncu -u')} to upgrade ${getPackageFileName(options)}`);
}
}
}
return output;
});
}
/**
* @param {Object} options - Options from the configuration
* @param {Object} args - The arguments passed to the function.
* @param {Object} args.current - The current packages.
* @param {Object} args.upgraded - The packages that should be upgraded.
* @param {number} args.numUpgraded - The number of upgraded packages
* @param {number} args.total - The total number of all possible upgrades
*/
function printUpgrades(options, {current, upgraded, numUpgraded, total}) {
print(options, '');
// print everything is up-to-date
const smiley = chalk.green.bold(':)');
if (numUpgraded === 0 && total === 0) {
if (Object.keys(current).length === 0) {
print(options, 'No dependencies.');
} else if (options.global) {
print(options, `All global packages are up-to-date ${smiley}`);
} else {
print(options, `All dependencies match the ${vm.getVersionTarget(options)} package versions ${smiley}`);
}
} else if (numUpgraded === 0 && total > 0) {
print(options, `All dependencies match the desired package versions ${smiley}`);
}
// print table
if (numUpgraded > 0) {
const table = toDependencyTable({
from: current,
to: upgraded
});
print(options, table.toString());
}
}
//
// Program
//
/** Initializes and consolidates options from the cli. */
function initOptions(options) {
return Object.assign({}, options, {
filter: options.args.join(' ') || options.filter,
// convert silent option to loglevel silent
loglevel: options.silent ? 'silent' : options.loglevel,
minimal: options.minimal === undefined ? false : options.minimal,
// default to 0, except when newest or greatest are set
pre: options.pre ? Boolean(Number(options.pre)) : options.newest || options.greatest,
// add shortcut for any keys that start with 'json'
json: _(options)
.keys()
.filter(_.partial(_.startsWith, _, 'json', 0))
.some(_.propertyOf(options))
});
}
/**
* @typedef {Array} PkgInfo
* @property 0 pkgFile
* @property 1 pkgData
*/
/** Finds the package file and data.
@returns Promise<PkgInfo>
Searches as follows:
--packageData flag
--packageFile flag
--stdin
--findUp
*/
async function findPackage(options) {
let pkgData;
let pkgFile;
let stdinTimer;
print(options, 'Running in local mode...', 'verbose');
print(options, 'Finding package file data...', 'verbose');
const pkgFileName = getPackageFileName(options);
// returns: string
function getPackageDataFromFile(pkgFile, pkgFileName) {
// exit if no pkgFile to read from fs
if (pkgFile != null) {
// print a message if we are using a descendant package file
const relPathToPackage = path.resolve(pkgFile);
if (relPathToPackage !== pkgFileName) {
print(options, `${options.upgrade ? 'Upgrading' : 'Checking'} ${relPathToPackage}`);
}
} else {
programError(options, `${chalk.red(`No ${pkgFileName}`)}\n\nPlease add a ${pkgFileName} to the current directory, specify the ${chalk.cyan('--packageFile')} or ${chalk.cyan('--packageData')} options, or pipe a ${pkgFileName} to stdin.`);
}
return readPackageFile(pkgFile);
}
// get the package data from the various input possibilities
if (options.packageData) {
pkgFile = null;
pkgData = Promise.resolve(options.packageData);
} else if (options.packageFile) {
pkgFile = options.packageFile;
pkgData = getPackageDataFromFile(pkgFile, pkgFileName);
} else if (!process.stdin.isTTY) {
print(options, 'Waiting for package data on stdin...', 'verbose');
// warn the user after a while if still waiting for stdin
// this is a way to mitigate #136 where Windows unexpectedly waits for stdin
stdinTimer = setTimeout(() => {
console.log(stdinWarningMessage);
}, stdinWarningTime);
// get data from stdin
// trim stdin to account for \r\n
// clear the warning timer once stdin returns
const stdinData = await getstdin();
const data = stdinData.trim().length > 0 ? stdinData : null;
clearTimeout(stdinTimer);
// if no stdin content fall back to searching for package.json from pwd and up to root
pkgFile = data || !pkgFileName ? null : findUp.sync(pkgFileName);
pkgData = data || getPackageDataFromFile(await pkgFile, pkgFileName);
} else {
// find the closest package starting from the current working directory and going up to the root
pkgFile = pkgFileName ? findUp.sync(pkgFileName) : null;
pkgData = getPackageDataFromFile(pkgFile, pkgFileName);
}
return Promise.all([pkgData, pkgFile]);
}
/** main entry point */
async function run(options={}) {
// exit with non-zero error code when there is an unhandled promise rejection
process.on('unhandledRejection', err => {
throw err;
});
// if not executed on the command-line (i.e. executed as a node module), set some defaults
if (!options.cli) {
options = _.defaults({}, options, {
jsonUpgraded: true,
loglevel: 'silent',
packageManager: 'npm',
args: []
});
}
options = initOptions(options);
print(options, 'Initializing...', 'verbose');
if (options.packageManager === 'npm' && !options.prefix) {
options.prefix = await packageManagers.npm.defaultPrefix(options); // eslint-disable-line require-atomic-updates
}
let timeout;
let timeoutPromise = new Promise(r => r);
if (options.timeout) {
const timeoutMs = _.isString(options.timeout) ? parseInt(options.timeout, 10) : options.timeout;
timeoutPromise = new Promise((resolve, reject) => {
timeout = setTimeout(() => {
// must catch the error and reject explicitly since we are in a setTimeout
const error = `Exceeded global timeout of ${timeoutMs}ms`;
reject(error);
try {
programError(options, chalk.red(error));
} catch (e) {/* noop */}
}, timeoutMs);
});
}
async function getAnalysis() {
if (options.global) {
const analysis = await analyzeGlobalPackages(options);
clearTimeout(timeout);
return analysis;
} else {
const [pkgData, pkgFile] = await findPackage(options);
const analysis = await analyzeProjectDependencies(options, pkgData, pkgFile);
clearTimeout(timeout);
return analysis;
}
}
return await Promise.race([timeoutPromise, getAnalysis()]);
}
/**
* @param {PlainObject} [cfg]
* @param {string} [cfg.configFileName=".ncurc"]
* @param {string} [cfg.configFilePath]
* @param {string} [cfg.packageFile]
* @returns {PlainObject|undefined}
*/
function getNcurc({configFileName, configFilePath, packageFile} = {}) {
const rcFile = rcLoader('ncurc', {
configFileName: configFileName || '.ncurc',
defaultExtension: ['.json', '.yml', '.js'],
cwd: configFilePath ||
(packageFile ? path.dirname(packageFile) : undefined)
});
return rcFile && rcFile.config;
}
module.exports = Object.assign({
run,
getNcurc
}, vm);