-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
109 lines (99 loc) · 2.44 KB
/
main.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
var request = require('request'),
fs = require('fs');
var jsonifyConf = function (jsConf) {
var jsonConf = {
source: jsConf.source,
map: jsConf.map.toString(),
reduces: {}
}
var reduceName, reduceInfo;
for (reduceName in jsConf.reduces) {
reduceInfo = jsConf.reduces[reduceName];
jsonConf.reduces[reduceName] = {
fn: reduceInfo.fn.toString(),
group_levels: reduceInfo.group_levels
}
}
return jsonConf;
}
var dirToJsonConf = function (appPath) {
var jsonConf;
try {
jsonConf = {
source: fs.readFileSync(appPath + '/source', 'utf8').replace(/(\r?\n)+$/, ''),
map: fs.readFileSync(appPath + '/map.js', 'utf8'),
reduces: (function () {
var reduces = {};
var reduceDirs = fs.readdirSync(appPath + '/reduces');
reduceDirs.forEach( function (reduceName) {
var reducePath = appPath + '/reduces/' + reduceName;
reduces[reduceName] = {
"fn": fs.readFileSync(reducePath + '/fn.js', 'utf8'),
group_levels: JSON.parse(fs.readFileSync(reducePath + '/group_levels', 'utf8'))
};
});
return reduces;
}())
};
}
catch (e) {
throw e;
}
return jsonConf;
}
// opts:
// jsonConf - JSON object
// incarnatorUrl - string
// force - boolean
var deploy = function (opts, cb) {
var incarnatorUrl = opts.incarnatorUrl;
var force = opts.force || false;
var jsonConf = opts.jsonConf;
var putNewConf = function () {
request({
method: 'PUT',
uri: incarnatorUrl,
body: JSON.stringify(jsonConf)
},
function (err, res) {
if (err || res.statusCode !== 201) {
cb(new Error());
return;
}
cb();
}
);
}
try {
if (force) {
putNewConf();
return;
}
request({
method: 'GET',
uri: incarnatorUrl
},
function (err, res, state) {
if (err || (res.statusCode !== 200 && res.statusCode !== 404)) {
cb(new Error());
return;
}
var equalJsons = function (a, b) {
var serialize = JSON.stringify;
return serialize(a) === serialize(b);
}
if (equalJsons(jsonConf, JSON.parse(state).conf)) {
cb();
return;
}
putNewConf();
}
);
}
catch (e) {
cb(new Error());
}
}
exports.jsonifyConf = jsonifyConf;
exports.dirToJsonConf = dirToJsonConf;
exports.deploy = deploy;