UNPKG

2.39 kBJavaScriptView Raw
1'use strict';
2
3const chalk = require('chalk');
4const installPackages = require('./install-packages');
5const emoji = require('./emoji');
6
7function buildPackageToUpdate(moduleName, version, isYarn, saveExact) {
8 // handle adding ^ for yarn, npm seems to handle this if not exact
9 return (isYarn && !saveExact) ? moduleName + '@^' + version : moduleName + '@' + version;
10}
11
12function updateAll(currentState) {
13 const packages = currentState.get('packages');
14
15 if (currentState.get('debug')) {
16 console.log('packages', packages);
17 }
18
19 const packagesToUpdate = packages.filter(packageEntry => packageEntry.mismatch || packageEntry.notInstalled || packageEntry.bump );
20
21 if (!packagesToUpdate.length) {
22 console.log(`${emoji(':heart: ')}Your modules look ${chalk.bold('amazing')}. Keep up the great work.${emoji(' :heart:')}`);
23 return;
24 }
25
26 const isYarn = currentState.get('installer') === 'yarn';
27 const saveExact = currentState.get('saveExact');
28
29 const saveDependencies = packagesToUpdate
30 .filter(pkg => !pkg.devDependency)
31 .map(pkg => buildPackageToUpdate(pkg.moduleName, pkg.latest, isYarn, saveExact));
32
33 const saveDevDependencies = packagesToUpdate
34 .filter(pkg => pkg.devDependency)
35 .map(pkg => buildPackageToUpdate(pkg.moduleName, pkg.latest, isYarn, saveExact));
36
37 const updatedPackages = packagesToUpdate
38 .map(pkg => buildPackageToUpdate(pkg.moduleName, pkg.latest, isYarn, saveExact)).join(', ');
39
40 if (!currentState.get('global')) {
41 if (saveDependencies.length) {
42 !isYarn && saveDependencies.push('--save');
43 }
44
45 if (saveDevDependencies.length) {
46 isYarn
47 ? saveDevDependencies.push('--dev')
48 : saveDevDependencies.push('--save-dev');
49 }
50 }
51
52 return installPackages(saveDependencies, currentState)
53 .then(currentState => installPackages(saveDevDependencies, currentState))
54 .then(currentState => {
55 console.log('');
56 console.log(chalk.green(`[npm-check] Update complete!`));
57 console.log(chalk.green('[npm-check] ' + updatedPackages));
58 console.log(chalk.green(`[npm-check] You should re-run your tests to make sure everything works with the updates.`));
59 return currentState;
60 });
61}
62
63module.exports = updateAll;