-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·241 lines (232 loc) · 8.8 KB
/
index.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env node
var Koa = require('koa'),
app = new Koa(),
launcher = require('./launcher'),
config = require('./config').config,
yaml = require('js-yaml'),
cwd = process.cwd(),
path = require('path'),
fs = require('fs'),
program = require('commander'),
encoding = {encoding: 'utf8'};
var urlNotSpecified = 'one url specified at least';
var placeHolder = '@@__@@';
var indexToName = ['name'];
/**
* @description make url => { url: 'http://xxxx', name: 'xxx', script: '' };
* @param {*} url
*/
function urlFormat(urls, deploy_type, options) {
var _urls = [],
isArr = urls instanceof Array;
for (var key in urls) {
var url = urls[key];
if (typeof url === 'string') {
var _url = {};
url = format(decodeURIComponent(url), deploy_type, options);
url.replace(/(^|[ ])http[s]?:\/\/[^\[\]\(\) \r\n]+[ ]?/g, function(u) {
_url.url = u.trim();
return placeHolder;
}).split(placeHolder).forEach(function(parts, index) {
if (indexToName[index]) {
_url[indexToName[index]] = parts.trim();
}
});
if (!isArr) _url[indexToName[0]] = key;
url = _url;
} else {
url.url = format(url.url, deploy_type, options);
}
_urls.push(url);
}
return _urls;
}
function formatRewriteUrls(rules) {
var ruleMap;
if (rules) {
ruleMap = {};
rules.forEach(function(item, index) {
if (item.on !== false) item.on = true;
if (!item.matchUrl) item.matchUrl = '*';
if (item.rules) {
item.rules = item.rules.map(function(rule) {
if (typeof rule === 'string') {
var _rule = {};
rule.replace(/(^[^ ]+)[ ]+([^ ]+)(.*$)/g, function(mat, fr, to, title) {
_rule.match = fr.trim();
_rule.replace = to.trim();
_rule.title = title;
});
rule = _rule;
}
if (rule.on !== false) rule.on = true;
if (('requestRules' in rule) || ('responseRules' in rule)) {
rule.type = 'headerRule';
['requestRules', 'responseRules'].forEach(function(key) {
var val = rule[key];
var str = [];
if (val) {
for (var r in val) {
str.push('set ' + r + ': ' + val[r]);
}
}
rule[key] = str.join(';');
});
} else {
rule.type = 'normalOverride';
}
return rule;
});
} else {
item.on = false;
}
ruleMap[item.id = 'd' + index] = item;
})
}
return ruleMap;
}
function format(url, deploy_type, options) {
var reg = /\$\{([^\}]+)\}/g;
url = url.replace(reg, function(mat, val) {
var configValue = options[val] || '';
configValue = deploy_type in configValue ? configValue[deploy_type] : configValue;
return configValue;
});
if (url.match(reg)) return format(url, deploy_type, options);
return url;
}
function runRegression(body) {
return new Promise(function(resolve, reject) {
var type = body.type,
data = decodeURIComponent(body.data),
err;
if (type === 'json') {
try {
data = JSON.parse(data);
} catch(e) {
err = e;
}
} else {
try {
data = yaml.safeLoad(data);
} catch(e) {
err = e;
}
}
var urls = data.urls;
if (!urls) {
err = urlNotSpecified;
} else {
data.isMobile = 'isMobile' in data ? data.isMobile : body.isMobile;
data.mode = body.mode;
var deploy_type = body.deploy_type || 'beta';
data.gid = encodeURIComponent(cwd) + '_' + deploy_type;
data.hosts = data.hosts && data.hosts[deploy_type];
var rewriteUrls = data.rewriteUrls;
rewriteUrls = rewriteUrls && rewriteUrls[deploy_type] || rewriteUrls;
data.rewriteUrls = formatRewriteUrls(rewriteUrls);
// different deploy_type different urls
if (!(urls instanceof Array)) {
var differentUrls = urls[deploy_type];
if (deploy_type === 'prod' && !differentUrls) {
for (var dType in urls) {
if (String(dType).indexOf('beta') === 0) {
differentUrls = urls[dType];
break;
}
}
}
} else {
differentUrls = urls;
}
if (differentUrls) {
data.urls = urlFormat(differentUrls, deploy_type, data);
var url = body.url;
if (url === true || url === 'true') {
url = 0;
}
if (+url >= 0) {
url = data.urls[+url];
url = url && url.url;
} else if (url.indexOf('http') !== 0) {
url = data.urls.find(function(item) {
item.name === url;
});
}
return launcher.launch(data, url).then(function(res) {
resolve(res);
}, function(e) {
reject({
err: e
})
});
}
err = urlNotSpecified;
}
if (err) {
reject({ err: err });
}
});
}
program
.version('0.0.1');
program
.command('start')
.description('start chrome with specified hosts')
.option('-y,--yaml [value]', 'specify a yaml for auto regression, default is: auto-regression-testing.yaml', 'auto-regression-testing.yaml')
.option('-d,--deploy_type [value]', 'specify deploy_type, can be: dev, beta or prod, determines which hosts to be used, default is: dev', 'dev')
.option('-m,--mode [value]', 'specify mode, if mode === browsing, just start the browser, default is: testing', 'testing')
.option('-u, --url [value]', 'specify a url to open if mode === browsing', false)
// .option('-p, --port [value]', 'specify the port which selenium standalone listens at, default is: 4444', 4444)
// .option('-H, --hostname [value]', 'specify the hostname which selenium standalone uses, default is:127.0.0.1', '127.0.0.1')
.action(function(options) {
// if there is auto-regression-testing.yaml file, must be dev
var ifYAML = path.join(cwd, options.yaml);
if (fs.existsSync(ifYAML)) {
ifYAML = fs.readFileSync(ifYAML, encoding);
if (ifYAML) {
return runRegression({
data: ifYAML,
type: 'yaml',
deploy_type: options.deploy_type,
mode: options.mode,
url: options.url
}).then(function(res) {
if (res && res.html) {
var html = path.join(cwd, 'auto-regression-testing.html');
fs.writeFileSync(html, res.html, encoding);
}
}, function(err) {
console.log('err', err);
});
}
} else {
console.log('err', ifYAML, 'not found');
}
});
program
.command('server')
.description('start a server listen to post')
.option('-p,--port [value]', 'specify the port which post server listens', config.httpServerPort)
.action(function(options) {
var router = require('koa-router')(),
koaBody = require('koa-body');
var port = options.port;
console.log('start listening at port:', port);
router
.post('/auto-regression-testing', koaBody(), function(ctx) {
return runRegression(ctx.request.body).then(function(res) {
ctx.body = res;
}, function(err) {
ctx.body = err;
})
}).get('*', function(ctx) {
ctx.body = {err: "POST ONLY"};
});
app.use(router.middleware());
app.listen(port);
});
program.parse(process.argv);
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at:', p, 'reason:', reason);
});