Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Jun 20, 2013
0 parents commit a1e8c02
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions .jshintignore
@@ -0,0 +1 @@
node_modules
26 changes: 26 additions & 0 deletions .jshintrc
@@ -0,0 +1,26 @@
{
"asi": false,
"expr": true,
"loopfunc": true,
"curly": false,
"evil": true,
"white": true,
"undef": true,
"browser": true,
"es5": true,
"predef": [
"app",
"$",
"FormBot",
"socket",
"confirm",
"alert",
"require",
"__dirname",
"process",
"exports",
"console",
"Buffer",
"module"
]
}
95 changes: 95 additions & 0 deletions index.js
@@ -0,0 +1,95 @@
#!/usr/bin/env node
var request = require('request'),
semver = require('semver'),
async = require('async'),
fs = require('fs'),
exec = require('child_process').exec;

// first we load the package.json and get a list of everything saved in the requires
var config_filename, config, configured_packages = {};
config_filename = process.cwd() + '/package.json';
if (!fs.existsSync(config_filename)) {
console.log('ERROR: No package.json file exists in this directory');
process.exit(1);
}
config = require(config_filename);
[config.dependencies, config.devDependencies].forEach(function (p) {
if (!p) return;
Object.keys(p).forEach(function (pack) {
configured_packages[pack] = p[pack];
});
});

// then we run an npm ls --json to see what's actually installed
var installed_packages = {},
missing_packages = [],
deps;
exec('npm ls --json', function (err, stdout, stderr) {
// iterate over installed packages, we're only checking first level stuff
deps = JSON.parse(stdout).dependencies;
Object.keys(deps).forEach(function (p) {
if (deps[p].missing) {
missing_packages.push(p);
} else {
installed_packages[p] = deps[p].version;
}
});

// if we're missing packages, tell the user to install them before continuing
if (missing_packages.length) {
console.log('ERROR: The following packages are not installed: ' + missing_packages.join(', '));
console.log('ERROR: Please run npm install before using pcheck');
process.exit(1);
}

// now we'll do our first check, and make sure that all installed packages are configured
compareLists(configured_packages, installed_packages, function (notconfigured) {
if (notconfigured) {
console.log('WARN: The following packages are installed, but do not exist in package.json: ' + notconfigured.join(', '));
}
// and then we'll check versions
checkUpdates(configured_packages, installed_packages, function (updates) {
if (updates) {
updates.forEach(function (update) {
console.log('WARN: Package: %s, installed version: %s, latest available version: %s', update.name, update.installed, update.available);
});
}
});
});
});

function compareLists(configured, installed, callback) {
var notconfigured = [];

// let's see what packages are installed, but not configured
var installed_keys = Object.keys(installed),
configured_keys = Object.keys(configured);

installed_keys.forEach(function (key) {
if (!~configured_keys.indexOf(key)) {
notconfigured.push(key);
}
});

callback(notconfigured.length ? notconfigured : null);
}

function checkUpdates(configured, installed, callback) {
var updates = [];
async.eachLimit(Object.keys(installed), 5, function (package, cb) {
if (!configured.hasOwnProperty(package)) return cb();
if (!semver.validRange(configured[package])) return cb();
request.get({ uri: 'https://registry.npmjs.org/' + package + '/latest', json: true }, function (error, res, body) {
if (error) {
console.log('ERROR: Failed to check package version for ' + package);
return cb();
}
if (semver.gt(body.version, installed[package])) {
updates.push({ name: package, installed: installed[package], available: body.version });
}
cb();
});
}, function (err) {
callback(updates.length ? updates : null);
});
}
27 changes: 27 additions & 0 deletions package.json
@@ -0,0 +1,27 @@
{
"name": "pcheck",
"version": "0.0.1",
"description": "simple tool to check for outdated packages",
"bin": {
"pcheck": "./index.js"
},
"repository": {
"type": "git",
"url": "git://github.com/nlf/pcheck.git"
},
"keywords": [
"npm",
"outdated",
"package.json"
],
"author": "Nathan LaFreniere <quitlahok@gmail.com>",
"license": "MIT",
"dependencies": {
"request": "~2.21.0",
"semver": "~2.0.7",
"async": "~0.2.9"
},
"devDependencies": {
"precommit-hook": "~0.3.4"
}
}

0 comments on commit a1e8c02

Please sign in to comment.