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