-
Notifications
You must be signed in to change notification settings - Fork 408
/
config.js
346 lines (308 loc) · 9.78 KB
/
config.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
config.js
=========
This object returns all config info for the app. It handles reading the `testem.yml`
or `testem.json` config file.
*/
var fs = require('fs')
var yaml = require('js-yaml')
var log = require('npmlog')
var path = require('path')
var async = require('async')
var browser_launcher = require('./browser_launcher')
var Launcher = require('./launcher')
var Chars = require('./chars')
var pad = require('./strutils').pad
var isa = require('./isa')
var fileset = require('fileset')
var fileExists = fs.exists || path.exists
var url = require('url')
function Config(appMode, progOptions, config){
this.appMode = appMode
this.progOptions = progOptions || {}
this.config = config || {}
}
Config.prototype.read = function(callback){
var configFile = this.progOptions.file
var self = this
if (configFile){
this.readConfigFile(configFile, callback)
}else{
log.info('Seeking for config file...')
// Try all testem.json, testem.yml and testem.js
// testem.json gets precedence
var files = ['testem.json', '.testem.json', '.testem.yml', 'testem.yml', 'testem.js', '.testem.js']
async.filter(files.map(this.resolvePath.bind(this)), fileExists, function(matched){
var configFile = matched[0]
if (configFile){
this.readConfigFile(configFile, callback)
}else{
if (callback) callback.call(this)
}
}.bind(this))
}
}
Config.prototype.resolvePath = function(filepath){
if (filepath[0] === "/") {
return filepath
}
return path.resolve(this.cwd(), filepath)
}
Config.prototype.reverseResolvePath = function(filepath){
return path.relative(this.cwd(), filepath)
}
Config.prototype.cwd = function(){
return this.get('cwd') || process.cwd()
}
Config.prototype.readConfigFile = function(configFile, callback){
var self = this
if (!configFile){ // allow empty configFile for programmatic setups
if (callback) callback.call(self)
}else if (configFile.match(/\.js$/)){
this.readJS(configFile, callback)
}else if (configFile.match(/\.json$/)){
this.readJSON(configFile, callback)
}else if (configFile.match(/\.yml$/)){
this.readYAML(configFile, callback)
}else{
log.error('Unrecognized config file format for ' + configFile)
if (callback) callback.call(self)
}
}
Config.prototype.readJS = function(configFile, callback){
this.config = require(this.resolvePath(configFile))
if (callback) callback.call(this)
}
Config.prototype.readYAML = function(configFile, callback){
var self = this
fs.readFile(configFile, function (err, data) {
if (!err){
var cfg = yaml.load(String(data))
self.config = cfg
}
if (callback) callback.call(self)
})
}
Config.prototype.readJSON = function(configFile, callback){
var self = this
fs.readFile(configFile, function (err, data) {
if (!err){
var cfg = JSON.parse(data.toString())
self.config = cfg
self.progOptions.file = configFile
}
if (callback) callback.call(self)
})
}
Config.prototype.defaults = {
host: 'localhost',
port: 7357,
url: function(){
var scheme = 'http';
if (this.get('key') || this.get('pfx')){
scheme = 'https';
}
return scheme + '://' + this.get('host') + ':' + this.get('port') + '/'
},
parallel: 1,
reporter: 'tap'
}
Config.prototype.get = function(key){
var retval = null
if (key in this.progOptions){
retval = this.progOptions[key]
}
if (retval == null && this.config && key in this.config)
retval = this.config[key]
if (!retval && (key in this.defaults)){
var defaultVal = this.defaults[key]
if (typeof defaultVal === 'function'){
return defaultVal.call(this)
}else{
return defaultVal
}
}
return retval
}
Config.prototype.set = function(key, value){
if (!this.config) this.config = {}
this.config[key] = value
}
Config.prototype.isCwdMode = function(){
return !this.get('src_files') && !this.get('test_page')
}
Config.prototype.getAvailableLaunchers = function(cb){
var self = this
browser_launcher.getAvailableBrowsers(function(availableBrowsers){
var availableLaunchers = {}
availableBrowsers.forEach(function(browser){
var newLauncher = new Launcher(browser.name, browser, self)
availableLaunchers[browser.name.toLowerCase()] = newLauncher
})
// add custom launchers
var customLaunchers = self.get('launchers')
if (customLaunchers){
for (var name in customLaunchers){
var newLauncher = new Launcher(name, customLaunchers[name], self)
availableLaunchers[name.toLowerCase()] = newLauncher
}
}
cb(availableLaunchers)
})
}
Config.prototype.getLaunchers = function(cb){
var self = this
this.getAvailableLaunchers(function(availableLaunchers){
self.getWantedLaunchers(availableLaunchers, cb);
})
}
Config.prototype.getWantedLauncherNames = function(available){
var launchers, skip
launchers = this.get('launch')
if (launchers){
launchers = launchers.toLowerCase().split(',')
}else if (this.appMode === 'dev'){
launchers = this.get('launch_in_dev') || []
}else{
launchers = this.get('launch_in_ci') || Object.keys(available)
}
if (skip = this.get('skip')){
skip = skip.toLowerCase().split(',')
launchers = launchers.filter(function(name){
return skip.indexOf(name) === -1
})
}
return launchers
}
Config.prototype.getWantedLaunchers = function(available, cb){
var launchers = []
var wanted = this.getWantedLauncherNames(available)
wanted.forEach(function(name){
var launcher = available[name.toLowerCase()]
if (!launcher){
if (this.appMode === 'dev') {
log.warn('Launcher "' + name + '" is not recognized.')
}
else {
return cb(new Error('Launcher ' + name + ' not found. Not installed?'));
}
}else{
launchers.push(launcher)
}
})
cb(null, launchers);
}
Config.prototype.printLauncherInfo = function(){
var self = this
this.getAvailableLaunchers(function(launchers){
var launch_in_dev = (self.get('launch_in_dev') || [])
.map(function(s){return s.toLowerCase()})
var launch_in_ci = self.get('launch_in_ci')
if (launch_in_ci){
launch_in_ci = launch_in_ci.map(function(s){return s.toLowerCase()})
}
launchers = Object.keys(launchers).map(function(k){return launchers[k]})
console.log('Have ' + launchers.length + ' launchers available; auto-launch info displayed on the right.')
console.log() // newline
console.log('Launcher Type CI Dev')
console.log('------------ ------------ -- ---')
console.log(launchers.map(function(launcher){
var protocol = launcher.settings.protocol
var kind = protocol === 'browser' ?
'browser' : (
protocol === 'tap' ?
'process(TAP)' : 'process')
var color = protocol === 'browser' ? 'green' : 'magenta'
var dev = launch_in_dev.indexOf(launcher.name.toLowerCase()) !== -1 ?
Chars.mark :
' '
var ci = !launch_in_ci || launch_in_ci.indexOf(launcher.name.toLowerCase()) !== -1 ?
Chars.mark :
' '
return (pad(launcher.name, 14, ' ', 1) +
pad(kind, 12, ' ', 1) +
' ' + ci + ' ' + dev + ' ')
}).join('\n'))
})
}
Config.prototype.getFileSet = function(want, dontWant, callback){
var self = this
if (isa(want, String)) want = [want] // want is an Array
if (isa(dontWant, String)) dontWant = [dontWant] // dontWant is an Array
dontWant = dontWant.map(function(p){
return p ? self.resolvePath(p) : p
})
async.reduce(want, [], function(allThatIWant, patternEntry, next){
var pattern = isa(patternEntry, String) ? patternEntry : patternEntry.src
var attrs = patternEntry.attrs || []
var patternUrl = url.parse(pattern)
if (patternUrl.protocol == 'file:'){
pattern = patternUrl.hostname+patternUrl.path
} else if (patternUrl.protocol){
return next(null, allThatIWant.concat({src: pattern, attrs: attrs}))
}
fileset([self.resolvePath(pattern)], dontWant, function(err, files){
if (err) return next(err, allThatIWant)
next(null, allThatIWant.concat(files.map(function(f){
f = self.reverseResolvePath(f)
return {src: f, attrs: attrs}
})))
})
}, function(err, fileEntries){
if (err) return callback(err)
callback(null, fileEntries)
})
}
Config.prototype.getSrcFiles = function(callback){
var srcFiles = this.get('src_files') || '*.js'
var srcFilesIgnore = this.get('src_files_ignore') || ''
this.getFileSet(srcFiles, srcFilesIgnore, callback)
}
Config.prototype.getServeFiles = function(callback){
var want = this.get('serve_files') || this.get('src_files') || '*.js'
var dontWant = this.get('serve_files_ignore') || this.get('src_files_ignore') || ''
this.getFileSet(want, dontWant, callback)
}
Config.prototype.getCSSFiles = function(callback) {
var want = this.get('css_files') || ''
this.getFileSet(want, '', callback)
}
Config.prototype.getAllOptions = function(){
var options = []
function getOptions(o){
if (!o) return
if (o.options){
o.options.forEach(function(o){
options.push(o.name())
})
}
getOptions(o.parent)
}
getOptions(this.progOptions)
return options
}
Config.prototype.getTemplateData = function(cb){
var ret = {}
var options = this.getAllOptions()
for (var key in this.progOptions){
if (options.indexOf(key) !== -1){
ret[key] = this.progOptions[key]
}
}
if (this.config){
for (var key in this.config){
ret[key] = this.config[key]
}
}
this.getServeFiles(function(err, files){
var replaceSlashes = function(f){
return {src: f.src.replace(/\\/g, '/'), attrs: f.attrs}
};
ret.serve_files = files.map(replaceSlashes)
this.getCSSFiles(function(err, files){
ret.css_files = files.map(replaceSlashes)
if (cb) cb(err, ret)
});
}.bind(this))
}
module.exports = Config