Skip to content

Commit

Permalink
add update-all flag
Browse files Browse the repository at this point in the history
  • Loading branch information
markus.wirsing committed Mar 2, 2018
1 parent 6bcc392 commit 098b721
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const pkg = require('../package.json');
const npmCheck = require('./index');
const staticOutput = require('./out/static-output');
const interactiveUpdate = require('./out/interactive-update');
const updateAll = require('./out/update-all');
const debug = require('./state/debug');
const pkgDir = require('pkg-dir');

Expand All @@ -24,6 +25,7 @@ const cli = meow({
Options
-u, --update Interactive update.
-y, --udpate-all Uninteractive update. Apply all updates without prompting.
-g, --global Look at global modules.
-s, --skip-unused Skip check for unused packages.
-p, --production Skip devDependencies.
Expand All @@ -43,6 +45,7 @@ const cli = meow({
{
alias: {
u: 'update',
y: 'update-all',
g: 'global',
s: 'skip-unused',
p: 'production',
Expand All @@ -57,6 +60,7 @@ const cli = meow({
},
boolean: [
'update',
'update-all',
'global',
'skip-unused',
'production',
Expand All @@ -75,6 +79,7 @@ const cli = meow({
const options = {
cwd: cli.input[0] || cli.flags.dir,
update: cli.flags.update,
updateAll: cli.flags.updateAll,
global: cli.flags.global,
skipUnused: cli.flags.skipUnused,
ignoreDev: cli.flags.production,
Expand All @@ -97,6 +102,9 @@ npmCheck(options)
.then(currentState => {
currentState.inspectIfDebugMode();

if (options.updateAll) {
return updateAll(currentState);
}
if (options.update) {
return interactiveUpdate(currentState);
}
Expand Down
56 changes: 56 additions & 0 deletions lib/out/update-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

const _ = require('lodash');
const inquirer = require('inquirer');
const chalk = require('chalk');
const table = require('text-table');
const installPackages = require('./install-packages');
const emoji = require('./emoji');

function updateAll(currentState) {
const packages = currentState.get('packages');

if (currentState.get('debug')) {
console.log('packages', packages);
}

const packagesToUpdate = packages.filter(packageEntry => packageEntry.mismatch || packageEntry.notInstalled || packageEntry.bump );

if (!packagesToUpdate.length) {
console.log(`${emoji(':heart: ')}Your modules look ${chalk.bold('amazing')}. Keep up the great work.${emoji(' :heart:')}`);
return;
}

const saveDependencies = packagesToUpdate
.filter(pkg => !pkg.devDependency)
.map(pkg => pkg.moduleName + '@' + pkg.latest);

const saveDevDependencies = packagesToUpdate
.filter(pkg => pkg.devDependency)
.map(pkg => pkg.moduleName + '@' + pkg.latest);

const updatedPackages = packagesToUpdate
.map(pkg => pkg.moduleName + '@' + pkg.latest).join(', ');

if (!currentState.get('global')) {
if (saveDependencies.length) {
saveDependencies.unshift('--save');
}

if (saveDevDependencies.length) {
saveDevDependencies.unshift('--save-dev');
}
}

return installPackages(saveDependencies, currentState)
.then(currentState => installPackages(saveDevDependencies, currentState))
.then(currentState => {
console.log('');
console.log(chalk.green(`[npm-check] Update complete!`));
console.log(chalk.green('[npm-check] ' + updatedPackages));
console.log(chalk.green(`[npm-check] You should re-run your tests to make sure everything works with the updates.`));
return currentState;
});
}

module.exports = updateAll;
1 change: 1 addition & 0 deletions lib/state/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const debug = require('./debug');

const defaultOptions = {
update: false,
updateAll: false,
global: false,
cwd: process.cwd(),
nodeModulesPath: false,
Expand Down

0 comments on commit 098b721

Please sign in to comment.