forked from play-co/devkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
releaser.js
121 lines (101 loc) · 3.03 KB
/
releaser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var argv = require("optimist")
.usage('Usage: $0 -p --type [major|minor|patch] --branch --channel')
.boolean("production")
.string("author")
.default("type", "patch")
.default("branch", "master")
.default("channel", "beta")
.default("test", false)
.default("server", "http://dev.gameclosure.com:9091/")
.argv;
var http = require("http");
var common = require("../src/common");
var clc = require("cli-color");
var path = require("path");
var fs = require("fs");
var ff = require("ff");
var url = require("url");
var f = ff(function () {
if (!argv.author) {
var onName = f.slotPlain();
var onEmail = f.slotPlain();
var exec = require("child_process").exec;
exec('git config user.name', function (error, stdout, stderr) {
onName(stdout.replace(/^\s+|\s+$/g, '') || '');
});
exec('git config user.email', function (error, stdout, stderr) {
onEmail(stdout.replace(/^\s+|\s+$/g, '') || '');
});
}
}, function (name, email) {
var author = argv.author;
if (!author && name && email) {
author = name + ' <' + email + '>';
}
var urlObj = url.parse(argv.server, true);
urlObj.pathname = (argv.production ? "approve-release" : "test-release") + "/" + argv.type;
urlObj.query = merge(urlObj.query, {
branch: argv.branch,
channel: argv.channel,
test: (!!argv.test).toString(),
author: author,
version: argv.version
});
var uri = url.format(urlObj);
console.log("Pinging dev server to release:", uri);
var req = http.get(uri, f.slotPlain());
req.on("error", function(res) {
console.log("ERROR:", res);
});
}, function (res) {
res.on('data', function (data) {
_buf += data.toString();
handleBuffer();
});
res.on('close', function () {
if (_buf) {
_buf += "\n";
handleBuffer();
}
console.log("Release complete!");
});
});
var _buf = "";
function handleBuffer() {
while (/\n/.test(_buf)) {
var i = _buf.indexOf('\n');
var line = _buf.substring(0, i);
try {
line = JSON.parse(line);
} catch (e) {
console.log(">>", line);
line = null;
}
if (line) {
if (line.cmd) {
logCommand(line);
} else if (line.stdout) {
console.log("\n" + clc.greenBright(line.stdout) + "\n");
}
if (line.diff) {
if (line.diff.code) {
console.log("diff exited with code", line.diff.code);
console.log(line.diff.stderr);
} else {
var out = path.resolve("basil-release-" + (line.type || Date.now()) + ".patch");
fs.writeFileSync(out, line.diff.stdout);
console.log("wrote diff to", out);
}
}
}
_buf = _buf.substring(i + 1);
}
}
function logCommand(cmd) {
console.log(clc.blueBright("> " + cmd.cmd.map(function (piece) { return JSON.stringify(piece); }).join(" ")));
var stdout = cmd.stdout.replace(/(^\s+|\s+$)/g, '').replace(/\n/g, clc.greenBright("\n || "));
var stderr = cmd.stderr.replace(/(^\s+|\s+$)/g, '').replace(/\n/g, clc.redBright("\n || "));
stdout && console.log(clc.greenBright(" || ") + stdout);
stderr && console.log(clc.redBright(" || ") + stderr);
console.log(!cmd.code ? "(exit code 0)" : clc.redBright("(exit code " + cmd.code + ")"));
}