Skip to content

Commit 299abf6

Browse files
committed
New release script
1 parent 6f328b2 commit 299abf6

11 files changed

+118
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
},
4040
"scripts": {
4141
"preinstall": "node -e 'process.exit(0)'",
42-
"prepublish": "node ./tools/prepublish.js",
4342
"install": "node-pre-gyp install --fallback-to-build",
4443
"rebuild": "node-pre-gyp rebuild",
44+
"release": "node ./tools/release.js $@",
4545
"test": "mocha --debug"
4646
}
4747
}

tools/annotate-tag.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var exec = require('./exec');
2+
3+
module.exports = function tag(version) {
4+
var history = require('./history')(version);
5+
var tagname = 'v' + version;
6+
exec('git tag -a "' + tagname + '" -m "' + history + '"');
7+
};

tools/commit-changes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var exec = require('./exec');
2+
3+
module.exports = function commit(version) {
4+
var changed = [/*'ChangeLog.md',*/ 'package.json'].join(' ');
5+
exec('git commit -m "' + version + '" ' + changed);
6+
};

tools/exec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var _exec = require('child_process').execSync;
2+
3+
module.exports = function(expression) {
4+
return String(_exec(expression)).trim();
5+
};

tools/history.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var exec = require('./exec');
2+
3+
module.exports = function history() {
4+
var _lasttag = exec('git rev-list --tags --max-count=1');
5+
var _version = exec('git describe --tags --abbrev=0 ' + _lasttag);
6+
var version = _version ? ' ' + _version + '..' : '';
7+
return ' ' + exec('git log --no-merges --pretty="format: * %s (%an) %H%n"' + version);
8+
}

tools/prepublish.js renamed to tools/prepublish-to-npm.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
var npm_argv = JSON.parse(process.env.npm_config_argv);
2-
if (npm_argv.original[0] !== 'publish') process.exit(0);
3-
41
var rimraf = require('rimraf');
52
var extend = require('util')._extend;
63
var gyp = require('node-pre-gyp');
@@ -26,9 +23,7 @@ Object.keys(matrix).forEach(function(arch) {
2623
});
2724
}, []);
2825

29-
iterate();
30-
31-
function iterate(err) {
26+
module.exports = function prepublish(err) {
3227
if (err) {
3328
console.log(err.message);
3429
return process.exit(1);
@@ -40,5 +35,5 @@ function iterate(err) {
4035

4136
var prog = extend(new gyp.Run(), {opts: target});
4237

43-
prog.commands.install([], iterate);
44-
}
38+
prog.commands.install([], prepublish);
39+
};

tools/publish-to-npm.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var exec = require('./exec');
2+
3+
module.exports = function publish(version) {
4+
exec('npm publish');
5+
};

tools/push-to-githab.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var exec = require('./exec');
2+
3+
module.exports = function push(version) {
4+
var tag = 'v' + version;
5+
exec('git push && git push origin "' + tag + '"');
6+
};

tools/release.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var args = process.argv.slice(2);
2+
var version = args.splice(-1);
3+
4+
var npm = require('./update-npm-version');
5+
var changelog = require('./update-changelog');
6+
var tag = require('./annotate-tag');
7+
var commit = require('./commit-changes');
8+
var push = require('./push-to-githab');
9+
10+
var prepublish = require('./prepublish-to-npm');
11+
var publish = require('./publish-to-npm');
12+
13+
var EXAMPLE = ' Example:\n' +
14+
'node release.js 1.0.0 --build\n' +
15+
'node release.js 1.0.0 --publish'
16+
17+
var SEMVER = /^\d+(\.\d+(\.\d+(-.*)?)?(-.*)?)?(-.*)?$/;
18+
19+
console.assert(version, 'Wrong usage.' + EXAMPLE);
20+
console.assert(SEMVER.test(version), version + ' is not correct semver');
21+
22+
var BUILD = args.some(function(arg) {
23+
return /^(-b|--build)$/.test(arg);
24+
});
25+
26+
var PUBLISH = args.some(function(arg) {
27+
return /^(-p|--publish)$/.test(arg);
28+
});
29+
30+
console.assert(BUILD || PUBLISH, 'No mode selected.' + EXAMPLE);
31+
32+
if (BUILD) {
33+
console.log('--Update the version in package.json--');
34+
npm(version);
35+
36+
// TODO: enable changelog on 1.0 version
37+
// console.log('--Update ChangeLog.md--');
38+
// changelog();
39+
40+
console.log('--Commit the changes--');
41+
commit(version);
42+
43+
console.log('--Tag the release--')
44+
tag(version);
45+
46+
console.log('--Push to github--');
47+
push(version);
48+
49+
} else if (PUBLISH) {
50+
console.log('--Download prebuilt binaries--');
51+
prepublish();
52+
53+
console.log('--Publish to npm--');
54+
publish();
55+
}

tools/update-changelog.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var fs = require('fs');
2+
var exists = fs.existsSync;
3+
var read = fs.readFileSync;
4+
var write = fs.writeFileSync;
5+
6+
var history = require('./history');
7+
8+
module.exports = function changelog(filename) {
9+
filename = filename || 'CHANGELOG.md';
10+
11+
var _changelog = exists(filename) ? read(filename) : '';
12+
var _history = history();
13+
14+
write(filename, _history + '\n' + _changelog);
15+
};
16+
17+
//test -n "$EDITOR" && $EDITOR $CHANGELOG

0 commit comments

Comments
 (0)