UNPKG

1.76 kBPlain TextView Raw
1#!/usr/bin/env node
2var prompt = require('prompt');
3var fs = require('fs');
4var exec = require('child_process').exec;
5
6var schema = {
7 properties: {
8 version: {
9 description: 'version? (old is '+version()+')',
10 pattern: /^[0-9]\.[0-9]+\.[0-9](-.+)?/,
11 message: 'Must be a valid semver string i.e. 1.0.2, 2.3.0-beta.1',
12 required: true
13 }
14 }
15};
16
17prompt.start();
18
19prompt.get(schema, function(err, result) {
20 var rawVersion = result.version;
21 var version = 'v'+rawVersion;
22 updateJSON('package', rawVersion);
23 updateJSON('bower', rawVersion);
24 commit(version, function() {
25 tag(version, function() {
26 publish(version);
27 })
28 });
29});
30
31function commit(version, cb) {
32 exec('git commit -am "release '+version+'"', execHandler(cb));
33}
34
35function tag(version, cb) {
36 exec('git tag '+version, execHandler(cb));
37}
38
39function publish(version) {
40 exec('git push origin master', execHandler());
41 exec('git push origin '+version, execHandler());
42 exec('npm publish', execHandler());
43}
44
45function execHandler(cb) {
46 return function(err, stdout, stderr) {
47 if (err) throw new Error(err);
48 console.log(stdout);
49 console.log(stderr);
50 if (cb) cb();
51 }
52}
53
54function updateJSON(pkg, version) {
55 var path = './'+pkg+'.json';
56 var json = readJSON(path);
57 json.version = version;
58 writeJSON(path, json);
59}
60
61function version() {
62 try {
63 return readJSON('./package.json').version;
64 } catch(e) {
65 console.log('Could not read package.json version');
66 }
67}
68
69function readJSON(path) {
70 try {
71 return JSON.parse(fs.readFileSync(path).toString());
72 } catch(e) {
73 console.log('Could not read package.json version');
74 }
75}
76
77function writeJSON(path, data) {
78 fs.writeFileSync(path, JSON.stringify(data, null, 2));
79}
80