Skip to content

Instantly share code, notes, and snippets.

@Elzair
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Elzair/2bedc4ceee20e9c2547c to your computer and use it in GitHub Desktop.
Save Elzair/2bedc4ceee20e9c2547c to your computer and use it in GitHub Desktop.
update_angular_modules script
node_modules/
{
"name": "update-angular-modules",
"version": "0.1.0",
"description": "Script to ease updating AngularJS module NPMs",
"main": "update_angular_modules",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git@gist.github.com:/2bedc4ceee20e9c2547c.git"
},
"keywords": [
"angular"
],
"author": "Philip Woods <elzairthesorcerer@gmail.com>",
"license": "ISC",
"private": true,
"dependencies": {
"co": "~3.0.6",
"co-child-process": "0.0.3",
"thunkify": "~2.1.2",
"co-fs": "~1.2.0",
"request": "~2.36.0"
}
}
#!/usr/local/bin/node --harmony
var co = require('co')
, fs = require('co-fs')
, request = require('request')
, spawn = require('co-child-process')
, thunkify = require('thunkify')
, util = require('util')
;
var base_dir = process.env['HOME'] + '/Development/Nodejs/';
var files = [
['angular-module-animate', 'angular-animate']
, ['angular-module-cookies', 'angular-cookies']
, ['angular-module-core', 'angular']
, ['angular-module-loader', 'angular-loader']
, ['angular-module-mocks', 'angular-mocks']
, ['angular-module-resource', 'angular-resource']
, ['angular-module-route', 'angular-route']
, ['angular-module-sanitize', 'angular-sanitize']
, ['angular-module-touch', 'angular-touch']
];
co(function *() {
// Get new version number and make sure it is a valid semver
var version = process.argv[2];
var first = process.argv[3] || 0;
var last = process.argv[4] || files.length;
if (!/^\d+\.\d+\.\d+$/.exec(version)) {
console.error('Version number must have the form x.y.z!');
return;
}
if (first < 0 || first >= files.length) {
console.error('Invalid start number!');
return;
}
if (last < 1 || last > files.length) {
console.error('Invalid past-end number!');
return;
}
for (var i=first; i<last; i++) {
var path = base_dir + files[i][0];
// Update repo to latest version
yield spawn('git', ['fetch', 'origin'], {cwd: path});
yield spawn('git', ['merge', 'origin/master', '-Xtheirs', '-m', 'Merging remote changes']);
console.log(util.format('Got remote changes for %s', files[i][0]));
// Fetch latest version of file
var url = util.format('https://ajax.googleapis.com/ajax/libs/angularjs/%s/%s.js', version, files[i][1]);
console.log(util.format('Fetching %s', url));
var file = yield thunkify(request.get)(url);
yield fs.writeFile(util.format('%s/%s.js', path, files[i][1]), file[1]);
// Update package.json to latest version
var pkg_json = JSON.parse(yield fs.readFile(util.format('%s/package.json', path), {encoding: 'utf8'}));
pkg_json.version = version;
yield fs.writeFile(util.format('%s/package.json', path), JSON.stringify(pkg_json, null, 2));
console.log('package.json for %s updated to %s', files[i][0], version);
// Commit changes
yield spawn('git', ['add', 'package.json', util.format('%s.js', files[i][1])], {cwd: path});
yield spawn('git', ["commit", "-m", util.format("Updated to version v%s", version)], {cwd: path});
// Create new tag
yield spawn('git', ['tag', util.format('v%s', version)], {cwd: path});
yield spawn('git', ['push', 'origin', util.format('v%s', version)], {cwd: path});
// Publish module
yield spawn('npm', ['publish'], {cwd: path});
console.log(util.format('%s updated!', files[i][0]));
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment