forked from totorojs/totoro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle-cfg.js
276 lines (228 loc) · 7.42 KB
/
handle-cfg.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
'use strict';
var path = require('path')
var fs = require('fs')
var common = require('totoro-common')
var isExistedFile = common.isExistedFile
var logger = require('./logger')
var defaultCfg = {
//runner: undefined,
//adapter :undefined,
//root: undefined,
//browsers: undefined,
charset: 'utf-8',
timeout: 5,
clientHost: common.getExternalIpAddress(),
clientPort: 9998,
host: 'server.totorojs.org',
port: 9999
}
module.exports = handleCfg
function handleCfg(cfg, isList) {
var home = process.platform === 'win32' ? process.env.USERPROFILE : process.env.HOME
var globalCfg = common.readCfgFile(path.join(home, '.totoro', 'config.json'))
var projectCfg = common.readCfgFile('totoro-config.json')
cfg = common.mix(cfg, projectCfg, globalCfg, defaultCfg)
// if cfg.list, don't need to handle options relate to test
if (isList) return cfg
handleCode(cfg)
handleRunner(cfg)
handleAdapter(cfg)
handleClientRoot(cfg)
return cfg
}
function handleCode(cfg) {
if (!cfg.code) return;
var code = cfg.code;
var runner = 'totoro-code-runner.html'
var script;
if (/\.js$/.test(code)) {
script = '<script src="' + code + '"></script>'
} else {
script = '<script>console.log(' + code + ')</script>'
}
var content = '<!DOCTYPE html><html><head><meta charset="utf-8">' +
'<title>Totoro Code Runner</title></head><body>' +
script +
'<script>totoro.end()</script>' +
'</body></html>'
fs.writeFileSync(runner, content, {encoding: cfg.charset})
cfg.runner = runner
cfg.adapter = 'no'
}
function handleClientRoot(cfg) {
var root = cfg.root
var runner = cfg.runner
var adapter = cfg.adapter
if (isExistedFile(runner)) {
var runnerRoot = findRunnerRoot(runner, cfg.charset)
var adapterRoot = isExistedFile(adapter) && path.dirname(adapter)
var commonRoot = leastCommonRoot(runnerRoot, adapterRoot)
if(root) {
logger.debug('Specified root <' + root + '>')
root = path.resolve(root)
if (commonRoot.indexOf(root) !== 0) {
root = commonRoot
logger.warn('Specified root is not appropriate, found one <' + root + '>')
}
} else {
root = commonRoot
logger.debug('Not specified root, found one <' + root + '>')
}
cfg.runner = relative(root, runner)
if (isExistedFile(adapter)) {
cfg.adapter = relative(root, adapter)
}
cfg.root = root
} else {
logger.debug('None of runner or adapter is existed file, not need root.')
;delete cfg.root
;delete cfg.clientHost
;delete cfg.clientPort
}
}
function findRunnerRoot(runner, charset) {
//--runner=/path/to/local/file?query#hash
runner = runner.replace(/[?#].+$/, '')
var runnerDir = path.dirname(runner)
var runnerRoot = runnerDir
var pathReg = /<(?:script|link)[^>]+?(?:src|href)\s*=\s*(["'])(.+?)\1/gi
var absPathReg = /^\/|https?:\/\//i
var content = fs.readFileSync(runner, {encoding: charset})
var matched
while((matched = pathReg.exec(content)) !== null) {
var p = matched[2]
if (!absPathReg.test(p)) {
runnerRoot = leastCommonRoot(runnerRoot, path.dirname(path.join(runnerDir, p)))
/*
console.log('Current runner root.', {
path: p,
root: runnerRoot
})
*/
}
}
if (runnerRoot === runnerDir) {
// logger.debug('Let runner root at least be the parent directory of runner.')
runnerRoot = path.join(runnerRoot, '..')
}
logger.debug('Found runner root <' + runnerRoot + '>.')
return runnerRoot
}
function leastCommonRoot(dir1, dir2) {
if (dir1) {
dir1 = path.resolve(dir1)
}
if (dir2) {
dir2 = path.resolve(dir2)
}
if (dir1 && dir2) {
var arr1 = dir1.split(path.sep)
var arr2 = dir2.split(path.sep)
var commonRoot = []
for(var i = 0; i < arr1.length; i++){
if (arr1[i] === arr2[i]) {
commonRoot.push(arr1[i])
} else {
break
}
}
if (commonRoot.length) {
// on mac, if dir1 is /usr/... and dir2 is /fool2fish/...
// then commonRoot is [''], commonRoot.join(path.sep) is ''
// but path.sep is expected
return commonRoot.join(path.sep) || path.sep
} else {
logger.error('Cannot decide a common root for 2 directories.')
}
} else {
return dir1 || dir2
}
}
function handleRunner(cfg) {
if (cfg.runner) {
var runner = cfg.runner
if (!common.isUrl(runner)) {
if (isExistedFile(runner)) {
cfg.runner = path.resolve(runner)
} else {
logger.error('Specified runner <' + runner + '> is not available.')
}
}
} else {
cfg.runner = findRunner()
}
}
function findRunner() {
var testDir
var cwd = process.cwd()
if (/\/tests?$/.test(cwd)) {
testDir = cwd
} else if (fs.existsSync('test')) {
testDir = path.resolve('test')
} else if (fs.existsSync('tests')) {
testDir = path.resolve('tests')
}
if (testDir) {
var runner = path.join(testDir, 'runner.html')
var runner2 = path.join(testDir, 'index.html')
if (isExistedFile(runner)) {
logger.info('Found runner <' + runner + '>')
return runner
} else if (isExistedFile(runner2)) {
logger.info('Found runner <' + runner2 + '>')
return runner2
} else {
logger.error('Not found runner.')
}
} else {
logger.error('Not found test dir.')
}
}
function handleAdapter(cfg) {
if (cfg.adapter) {
var runner = cfg.runner
var adapter = cfg.adapter
/*
* NOTE
* if runner is url, adapter must not be local file
* if runner is local file, adapter must not be url
* see #80
*/
if (common.isKeyword(adapter)) {
// do nothing
} else if (common.isUrl(adapter)) {
if (!common.isUrl(runner)) {
logger.error('Runner is file, can not specify a url adapter.')
}
} else {
if (common.isUrl(runner)) {
logger.error('Runner is url, can not specify a file adapter.')
}
if (isExistedFile(adapter)) {
if (path.extname(adapter) === '.js') {
cfg.adapter = path.resolve(adapter)
} else {
logger.error('Specified adapter <' + adapter + '> is not a js file.')
}
} else {
logger.error('Specified adapter <' + adapter + '> dose not exist.')
}
}
} else {
cfg.adapter = findAdapter(cfg)
}
}
function findAdapter(cfg) {
var runner = cfg.runner
var adapter = path.join(path.dirname(runner), 'totoro-adapter.js')
if (isExistedFile(adapter)) {
logger.info('Found adapter file <' + adapter + '>')
return adapter
} else {
logger.debug('Not found adapter file, will auto decide.')
return
}
}
function relative(from, to) {
return path.relative(from, to).replace(path.sep, '/')
}