Permalink
Browse files

Finished auto updating outdated dependencies

  • Loading branch information...
1 parent 76b14b2 commit 0c1156e85f4b0a1312b27f30efc9eaa5f0f06c21 @pksunkara committed Jan 22, 2012
Showing with 44 additions and 8 deletions.
  1. +43 −7 lib/police/check.js
  2. +1 −1 lib/police/github.js
View
@@ -35,27 +35,33 @@ var fields = [
];
/*
+ * Outdated dependencies
+ */
+check.dependencies = {};
+check.devDependencies = {};
+
+/*
* The main function which take package.json
*/
-check.pkg = function (pkg, callback) {
+check.pkg = function (pkg, local, callback) {
var pkgs = [];
for(dep in pkg.dependencies) {
- pkgs.push({ name: dep, version: pkg.dependencies[dep] });
+ pkgs.push({ name: dep, version: pkg.dependencies[dep], dev: false });
}
for(dep in pkg.devDependencies) {
- pkgs.push({ name: dep, version: pkg.devDependencies[dep] });
+ pkgs.push({ name: dep, version: pkg.devDependencies[dep], dev: true });
}
police.async.forEachSeries(pkgs, check.npm, function (err) {
if (err) callback(err);
- check.suggest(pkg, callback);
+ check.suggest(pkg, local, callback);
});
}
/*
* Suggest missing fields for package.json
*/
-check.suggest = function (pkg, callback) {
+check.suggest = function (pkg, local, callback) {
var suggestions = [];
fields.forEach(function (field) {
if (!pkg[field]) suggestions.push(field.yellow.bold);
@@ -65,8 +71,21 @@ check.suggest = function (pkg, callback) {
} else {
police.winston.info(''.red.bold + ' Suggest adding ' + suggestions.join(', '));
}
- if (police.add || police.edit) {
+ if (police.update) {
+ pkg.dependencies = check.dependencies;
+ pkg.devDependencies = check.devDependencies;
+ }
+ if (!local && (police.add || police.edit)) {
police.ask.fields(pkg, fields, callback);
+ } else if (police.update) {
+ fs.writeFile(local, JSON.stringify(pkg, null, 2) + '\n', function (err) {
+ if (err) {
+ police.winston.error('Unable to write package.json'.red.bold);
+ police.exit(1);
+ } else {
+ callback();
+ }
+ });
} else {
callback();
}
@@ -94,7 +113,7 @@ check.local = function (file) {
} else {
police.winston.info('Read file ' + file.cyan);
try {
- check.pkg(JSON.parse(data), police.exit);
+ check.pkg(JSON.parse(data), path.resolve(file), police.exit);
} catch (err) {
police.winston.error('Unable to parse package.json'.red.bold);
police.exit(1);
@@ -106,6 +125,17 @@ check.local = function (file) {
}
/*
+ * Add dependencies to the proper object
+ */
+check.add = function (name, version, dev) {
+ if (dev) {
+ check.devDependencies[name] = version;
+ } else {
+ check.dependencies[name] = version;
+ }
+}
+
+/*
* Get version for the npm package
*/
check.npm = function (dep, callback) {
@@ -122,8 +152,14 @@ check.npm = function (dep, callback) {
} else {
body = JSON.parse(body);
if (!police.semver.satisfies(body.version, dep.version)) {
+ if (police.update) {
+ check.add(dep.name, body.version, dep.dev);
+ }
police.winston.info(''.red.bold + ' Need to update ' + dep.name.blue + ' to ' + body.version.yellow);
} else {
+ if (police.update) {
+ check.add(dep.name, dep.version, dep.dev);
+ }
police.winston.info(''.green.bold + ' Checked ' + dep.name.blue);
}
callback();
@@ -91,7 +91,7 @@ github.singleModule = function (name, callback) {
var sname = name.split('/');
police.ask.properties(pkg, {name: name, user: sname[0], tree: tree.sha, mode: blob[0].mode, commit: commit.sha, content: content});
}
- police.check.pkg(pkg, callback);
+ police.check.pkg(pkg, false, callback);
});
} else {
police.winston.warn('package.json not found on master'.red.bold);

0 comments on commit 0c1156e

Please sign in to comment.