-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
67 lines (55 loc) · 1.56 KB
/
router.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
/**
* Module dependencies
*/
var Heroku = require('heroku-client');
module.exports = function(opts) {
opts = opts || {};
var client = new Heroku({token: opts.token});
var router = opts.router;
function route(task) {
function error(err) {
console.log(err.stack || err);
task.emit('error', err);
}
return function() {
// TODO handle race conditions
var config = client.apps(router).configVars();
config.info(function(err, ENV) {
if (err) error(err);
var name = task.info.name;
var NAME = format(name);
var branch = task.info.branch;
var BRANCH = format(branch);
var id = +(new Date);
ENV['APPS'] = add(name, ENV['APPS']);
ENV[NAME + '_MOUNT'] = task.info.mount || '/' + name;
ENV[NAME + '_BRANCHES'] = add(branch, ENV[NAME + '_BRANCHES']);
ENV[NAME + '_BRANCH_' + BRANCH] = id + ':1';
ENV[NAME + '_URL_' + id] = 'http://' + task.info.deployment;
config.update(ENV, function(err) {
if (err) error(err);
garbageCollect(client, ENV, name, NAME, branch, BRANCH);
});
});
};
}
return function(app) {
app.on('task', function(task) {
task.on('success', route(task));
});
};
};
function format(name) {
return name
.replace(/-/g, '_')
.toUpperCase();
}
function add(item, list) {
if (!list) return item;
var arr = list.split(',');
if (~arr.indexOf(item)) return list;
return list + ',' + item;
}
function garbageCollect(ENV, name, NAME, branch, BRANCH, fn) {
// TODO
}