2 var util = require('util')
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$/
12 var isCoffeeFile = function (filename) {
13 return COFFEE_REGEXP.test(filename)
16 var isLiveFile = function (filename) {
17 return LIVE_REGEXP.test(filename)
20 var JavaScriptFormatter = function () {
21 var quote = function (value) {
22 return "'" + value + "'"
25 var quoteNonIncludedPattern = function (value) {
26 return util.format('{pattern: %s, included: false}', quote(value))
29 var pad = function (str, pad) {
30 return str.replace(/\n/g, '\n' + pad).replace(/\s+$/gm, '')
33 var formatQuottedList = function (list) {
34 return list.map(quote).join(', ')
37 this.TEMPLATE_FILE_PATH = JS_TEMPLATE_PATH
38 this.REQUIREJS_TEMPLATE_FILE = JS_REQUIREJS_TEMPLATE_PATH
40 this.formatFiles = function (includedFiles, onlyServedFiles) {
41 var files = includedFiles.map(quote)
43 onlyServedFiles.forEach(function (onlyServedFile) {
44 files.push(quoteNonIncludedPattern(onlyServedFile))
47 files = files.map(function (file) {
51 return files.join(',')
54 this.formatPreprocessors = function (preprocessors) {
56 Object.keys(preprocessors).forEach(function (pattern) {
57 lines.push(' ' + quote(pattern) + ': [' + formatQuottedList(preprocessors[pattern]) + ']')
60 return pad('{\n' + lines.join(',\n') + '\n}', ' ')
63 this.formatFrameworks = formatQuottedList
65 this.formatBrowsers = formatQuottedList
67 this.formatAnswers = function (answers) {
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)
80 this.generateConfigFile = function (answers) {
81 var template = fs.readFileSync(this.TEMPLATE_FILE_PATH).toString()
82 var replacements = this.formatAnswers(answers)
84 return template.replace(/%(.*)%/g, function (a, key) {
85 return replacements[key]
89 this.writeConfigFile = function (path, answers) {
90 fs.writeFileSync(path, this.generateConfigFile(answers))
93 this.generateRequirejsConfigFile = function () {
94 var template = fs.readFileSync(this.REQUIREJS_TEMPLATE_FILE).toString()
98 this.writeRequirejsConfigFile = function (path) {
99 fs.writeFileSync(path, this.generateRequirejsConfigFile())
103 var CoffeeFormatter = function () {
104 JavaScriptFormatter.call(this)
106 this.TEMPLATE_FILE_PATH = COFFEE_TEMPLATE_PATH
107 this.REQUIREJS_TEMPLATE_FILE = COFFEE_REQUIREJS_TEMPLATE_PATH
110 var LiveFormatter = function () {
111 JavaScriptFormatter.call(this)
113 this.TEMPLATE_FILE_PATH = LIVE_TEMPLATE_PATH
116 exports.JavaScript = JavaScriptFormatter
117 exports.Coffee = CoffeeFormatter
118 exports.Live = LiveFormatter
120 exports.createForPath = function (path) {
121 if (isCoffeeFile(path)) {
122 return new CoffeeFormatter()
125 if (isLiveFile(path)) {
126 return new LiveFormatter()
129 return new JavaScriptFormatter()