Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / karma / lib / init / formatters.js
1 var fs = require('fs')
2 var util = require('util')
3
4 var JS_TEMPLATE_PATH = __dirname + '/../../config.tpl.js'
5 var COFFEE_TEMPLATE_PATH = __dirname + '/../../config.tpl.coffee'
6 var JS_REQUIREJS_TEMPLATE_PATH = __dirname + '/../../requirejs.config.tpl.js'
7 var COFFEE_REQUIREJS_TEMPLATE_PATH = __dirname + '/../../requirejs.config.tpl.coffee'
8 var COFFEE_REGEXP = /\.coffee$/
9 var LIVE_TEMPLATE_PATH = __dirname + '/../../config.tpl.ls'
10 var LIVE_REGEXP = /\.ls$/
11
12 var isCoffeeFile = function (filename) {
13   return COFFEE_REGEXP.test(filename)
14 }
15
16 var isLiveFile = function (filename) {
17   return LIVE_REGEXP.test(filename)
18 }
19
20 var JavaScriptFormatter = function () {
21   var quote = function (value) {
22     return "'" + value + "'"
23   }
24
25   var quoteNonIncludedPattern = function (value) {
26     return util.format('{pattern: %s, included: false}', quote(value))
27   }
28
29   var pad = function (str, pad) {
30     return str.replace(/\n/g, '\n' + pad).replace(/\s+$/gm, '')
31   }
32
33   var formatQuottedList = function (list) {
34     return list.map(quote).join(', ')
35   }
36
37   this.TEMPLATE_FILE_PATH = JS_TEMPLATE_PATH
38   this.REQUIREJS_TEMPLATE_FILE = JS_REQUIREJS_TEMPLATE_PATH
39
40   this.formatFiles = function (includedFiles, onlyServedFiles) {
41     var files = includedFiles.map(quote)
42
43     onlyServedFiles.forEach(function (onlyServedFile) {
44       files.push(quoteNonIncludedPattern(onlyServedFile))
45     })
46
47     files = files.map(function (file) {
48       return '\n      ' + file
49     })
50
51     return files.join(',')
52   }
53
54   this.formatPreprocessors = function (preprocessors) {
55     var lines = []
56     Object.keys(preprocessors).forEach(function (pattern) {
57       lines.push('  ' + quote(pattern) + ': [' + formatQuottedList(preprocessors[pattern]) + ']')
58     })
59
60     return pad('{\n' + lines.join(',\n') + '\n}', '    ')
61   }
62
63   this.formatFrameworks = formatQuottedList
64
65   this.formatBrowsers = formatQuottedList
66
67   this.formatAnswers = function (answers) {
68     return {
69       DATE: new Date(),
70       BASE_PATH: answers.basePath,
71       FRAMEWORKS: this.formatFrameworks(answers.frameworks),
72       FILES: this.formatFiles(answers.files, answers.onlyServedFiles),
73       EXCLUDE: this.formatFiles(answers.exclude, []),
74       AUTO_WATCH: answers.autoWatch ? 'true' : 'false',
75       BROWSERS: this.formatBrowsers(answers.browsers),
76       PREPROCESSORS: this.formatPreprocessors(answers.preprocessors)
77     }
78   }
79
80   this.generateConfigFile = function (answers) {
81     var template = fs.readFileSync(this.TEMPLATE_FILE_PATH).toString()
82     var replacements = this.formatAnswers(answers)
83
84     return template.replace(/%(.*)%/g, function (a, key) {
85       return replacements[key]
86     })
87   }
88
89   this.writeConfigFile = function (path, answers) {
90     fs.writeFileSync(path, this.generateConfigFile(answers))
91   }
92
93   this.generateRequirejsConfigFile = function () {
94     var template = fs.readFileSync(this.REQUIREJS_TEMPLATE_FILE).toString()
95     return template
96   }
97
98   this.writeRequirejsConfigFile = function (path) {
99     fs.writeFileSync(path, this.generateRequirejsConfigFile())
100   }
101 }
102
103 var CoffeeFormatter = function () {
104   JavaScriptFormatter.call(this)
105
106   this.TEMPLATE_FILE_PATH = COFFEE_TEMPLATE_PATH
107   this.REQUIREJS_TEMPLATE_FILE = COFFEE_REQUIREJS_TEMPLATE_PATH
108 }
109
110 var LiveFormatter = function () {
111   JavaScriptFormatter.call(this)
112
113   this.TEMPLATE_FILE_PATH = LIVE_TEMPLATE_PATH
114 }
115
116 exports.JavaScript = JavaScriptFormatter
117 exports.Coffee = CoffeeFormatter
118 exports.Live = LiveFormatter
119
120 exports.createForPath = function (path) {
121   if (isCoffeeFile(path)) {
122     return new CoffeeFormatter()
123   }
124
125   if (isLiveFile(path)) {
126     return new LiveFormatter()
127   }
128
129   return new JavaScriptFormatter()
130 }