Skip to content

Commit

Permalink
Merge pull request #91 from clinternet/integration
Browse files Browse the repository at this point in the history
Allow integration with 3rd party code
  • Loading branch information
raineorshine committed Jul 1, 2015
2 parents 2fc1f89 + d829b86 commit 470ac64
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 42 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ Options
versions (maintaining existing policy)
-V, --version output the version number

Integration
--------------
The tool allows integration with 3-rd party code:

```javascript
var checkUpdates = require('npm-check-updates');

checkUpdates.run({
upgrade: true, // see available options above
force: true
}).then(function() {
console.log('done upgrading dependencies');
});
```

Motivation
--------------

Expand Down
24 changes: 23 additions & 1 deletion bin/npm-check-updates
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
#!/usr/bin/env node

var main = require('../lib/npm-check-updates');
var program = require('commander'),
main = require('../lib/npm-check-updates');

program
.version(require('../package').version)
.description('[filter] is a list or regex of package names to check (all others will be ignored).')
.usage('[options] [filter]')
.option('-d, --dev', 'check only devDependencies')
.option('-e, --error-level <n>', 'set the error-level. 1: exits with error code 0 if no errors occur. 2: exits with error code 0 if no packages need updating (useful for continuous integration). Default is 1.', cint.partialAt(parseInt, 1, 10), 1)
.option('-f, --force', 'force upgrade even when the latest version satisfies the declared semver dependency')
.option('-g, --global', 'check global packages instead of in the current project')
// program.json is set to true in programInit if any options that begin with 'json' are true
.option('-j, --jsonAll', 'output new package.json instead of human-readable message')
.option('--jsonUpgraded', 'output upgraded dependencies in json')
.option('-p, --prod', 'check only dependencies (not devDependencies)')
.option('-r, --registry <url>', 'specify third-party npm registry')
.option('-s, --silent', "don't output anything")
.option('-t, --greatest', "find the highest versions available instead of the latest stable versions")
.option('-u, --upgrade', 'upgrade package.json dependencies to match latest versions (maintaining existing policy)')

program.parse(process.argv);

main.run(program);
77 changes: 36 additions & 41 deletions lib/npm-check-updates.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
var program = require('commander');
var options;

//
// Dependencies
//

var async = require('async');
var cint = require('cint');
var path = require('path');
Expand All @@ -13,7 +18,12 @@ var stdin = require('get-stdin-promise');
// Helper functions
//

var print = program.silent ? _.noop : console.log;
function print(message) {
if (!options.silent) {
console.log(message);
}
}

var readPackageFile = cint.partialAt(fs.readFileAsync, 1, 'utf8');
var writePackageFile = fs.writeFileAsync;

Expand All @@ -24,10 +34,10 @@ var writePackageFile = fs.writeFileAsync;
function upgradePackageDefinitions(currentDependencies) {
var dependencyList = Object.keys(currentDependencies);
return vm.getLatestVersions(dependencyList, {
versionTarget: program.greatest ? 'greatest' : 'latest',
registry: program.registry ? program.registry : null,
versionTarget: options.greatest ? 'greatest' : 'latest',
registry: options.registry ? options.registry : null,
}).then(function (latestVersions) {
var upgradedDependencies = vm.upgradeDependencies(currentDependencies, latestVersions, { force: program.force });
var upgradedDependencies = vm.upgradeDependencies(currentDependencies, latestVersions, { force: options.force });
return [upgradedDependencies, latestVersions];
});
}
Expand All @@ -45,9 +55,9 @@ function analyzeGlobalPackages() {
function analyzeProjectDependencies(pkgData, pkgFile) {
var pkg = JSON.parse(pkgData);
var current = vm.getCurrentDependencies(pkg, {
prod: program.prod,
dev: program.dev,
filter: program.args[0]
prod: options.prod,
dev: options.dev,
filter: options.args[0]
});

return Promise.all([
Expand All @@ -61,18 +71,18 @@ function analyzeProjectDependencies(pkgData, pkgFile) {
})
.spread(function (current, installed, upgraded, latest) {

if(program.json) {
if(options.json) {
var newPkgData = vm.updatePackageData(pkgData, current, upgraded);
print(program.jsonAll ? JSON.parse(newPkgData) :
program.jsonDeps ? _.pick(JSON.parse(newPkgData), 'dependencies', 'devDependencies') :
print(options.jsonAll ? JSON.parse(newPkgData) :
options.jsonDeps ? _.pick(JSON.parse(newPkgData), 'dependencies', 'devDependencies') :
upgraded
);
}
else {
printLocalUpgrades(current, upgraded, installed, latest);

if(pkgFile && !_.isEmpty(upgraded)) {
if (program.upgrade) {
if (options.upgrade) {
var newPkgData = vm.updatePackageData(pkgData, current, upgraded);
writePackageFile(pkgFile, newPkgData)
.then(function () {
Expand All @@ -81,7 +91,7 @@ function analyzeProjectDependencies(pkgData, pkgFile) {
} else {
print("Run with '-u' to upgrade your package.json");
}
if(program.errorLevel >= 2) {
if(options.errorLevel >= 2) {
throw new Error('Dependencies not up-to-date');
}
}
Expand All @@ -98,7 +108,7 @@ function printGlobalUpgrades(current, upgraded) {
print('"' + dep + '" can be updated from ' +
current[dep] + ' to ' + upgraded[dep]);
}
if(program.errorLevel >= 2) {
if(options.errorLevel >= 2) {
throw new Error('Dependencies not up-to-date');
}
}
Expand All @@ -107,7 +117,7 @@ function printGlobalUpgrades(current, upgraded) {

function printLocalUpgrades(current, upgraded, installed, latest) {
print('');
var superlative = program.greatest ? "Greatest" : "Latest";
var superlative = options.greatest ? "Greatest" : "Latest";
if (_.isEmpty(upgraded)) {
print("All dependencies match the " + superlative.toLowerCase() + " package versions :)");
} else {
Expand All @@ -134,24 +144,22 @@ function programInit() {
print('You can now use "ncu" for less typing!');
}

if (program.global && program.upgrade) {
if (options.global && options.upgrade) {
print("npm-check-updates cannot update global packages.");
print("Run 'npm install -g [package]' to upgrade a global package.");
process.exit(1);
}

// add shortcut for any keys that start with 'json'
program.json = _(program)
options.json = _(options)
.keys()
.filter(_.partial(_.startsWith, _, 'json', 0))
.some(_.propertyOf(program));
.some(_.propertyOf(options));
}

function programRun() {
programInit();
program.global ?
programRunGlobal() :
programRunLocal()
return options.global ? programRunGlobal() : programRunLocal();
}

function programRunGlobal() {
Expand Down Expand Up @@ -186,24 +194,11 @@ function programRunLocal() {
return pkgData.then(_.partial(analyzeProjectDependencies, _, pkgFile));
}

program
.version(require('../package').version)
.description('[filter] is a list or regex of package names to check (all others will be ignored).')
.usage('[options] [filter]')
.option('-d, --dev', 'check only devDependencies')
.option('-e, --error-level <n>', 'set the error-level. 1: exits with error code 0 if no errors occur. 2: exits with error code 0 if no packages need updating (useful for continuous integration). Default is 1.', cint.partialAt(parseInt, 1, 10), 1)
.option('-f, --force', 'force upgrade even when the latest version satisfies the declared semver dependency')
.option('-g, --global', 'check global packages instead of in the current project')
// program.json is set to true in programInit if any options that begin with 'json' are true
.option('-j, --jsonAll', 'output new package.json instead of human-readable message')
.option('--jsonUpgraded', 'output upgraded dependencies in json')
.option('-p, --prod', 'check only dependencies (not devDependencies)')
.option('-r, --registry <url>', 'specify third-party npm registry')
.option('-s, --silent', "don't output anything")
.option('-t, --greatest', "find the highest versions available instead of the latest stable versions")
.option('-u, --upgrade', 'upgrade package.json dependencies to match latest versions (maintaining existing policy)')

program.parse(process.argv);

vm.initialize(program.global).then(programRun);
module.exports = {
run : function (opts) {
options = opts || {};
options.args = options.args || [];

return vm.initialize(options.global).then(programRun);
}
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "npm-check-updates",
"version": "2.0.0-alpha.10",
"author": "Tomas Junnonen <tomas1@gmail.com>",
"license": "MIT",
"contributors": [
"Raine Lourie (http://github.com/metaraine)"
],
Expand Down

0 comments on commit 470ac64

Please sign in to comment.