1 | 'use strict'
|
---|
2 |
|
---|
3 | const path = require('path')
|
---|
4 | const FileUtils = require('../utils/file-utils')
|
---|
5 |
|
---|
6 | function quote (value) {
|
---|
7 | return `'${value}'`
|
---|
8 | }
|
---|
9 |
|
---|
10 | function formatLine (items) {
|
---|
11 | return items.map(quote).join(', ')
|
---|
12 | }
|
---|
13 |
|
---|
14 | function formatMultiLines (items) {
|
---|
15 | return items
|
---|
16 | .map((file) => '\n ' + file)
|
---|
17 | .join(',')
|
---|
18 | }
|
---|
19 |
|
---|
20 | function formatFiles (includedFiles, onlyServedFiles) {
|
---|
21 | const lines = []
|
---|
22 | .concat(includedFiles.map(quote))
|
---|
23 | .concat(onlyServedFiles.map((file) => `{ pattern: ${quote(file)}, included: false }`))
|
---|
24 |
|
---|
25 | return formatMultiLines(lines)
|
---|
26 | }
|
---|
27 |
|
---|
28 | function formatPreprocessors (preprocessors) {
|
---|
29 | const lines = Object.keys(preprocessors)
|
---|
30 | .map((pattern) => `${quote(pattern)}: [${formatLine(preprocessors[pattern])}]`)
|
---|
31 |
|
---|
32 | return formatMultiLines(lines)
|
---|
33 | }
|
---|
34 |
|
---|
35 | function getConfigPath (file) {
|
---|
36 | return path.join(__dirname, `/../../${file}`)
|
---|
37 | }
|
---|
38 |
|
---|
39 | class JavaScriptFormatter {
|
---|
40 | constructor () {
|
---|
41 | this.TEMPLATE_FILE_PATH = getConfigPath('config.tpl.js')
|
---|
42 | this.REQUIREJS_TEMPLATE_FILE = getConfigPath('requirejs.config.tpl.js')
|
---|
43 | }
|
---|
44 |
|
---|
45 | generateConfigFile (answers) {
|
---|
46 | const replacements = this.formatAnswers(answers)
|
---|
47 |
|
---|
48 | return FileUtils
|
---|
49 | .readFile(this.TEMPLATE_FILE_PATH)
|
---|
50 | .replace(/%(.*)%/g, (a, key) => replacements[key])
|
---|
51 | }
|
---|
52 |
|
---|
53 | writeConfigFile (path, answers) {
|
---|
54 | FileUtils.saveFile(path, this.generateConfigFile(answers))
|
---|
55 | }
|
---|
56 |
|
---|
57 | writeRequirejsConfigFile (path) {
|
---|
58 | FileUtils.copyFile(this.REQUIREJS_TEMPLATE_FILE, path)
|
---|
59 | }
|
---|
60 |
|
---|
61 | formatAnswers (answers) {
|
---|
62 | return {
|
---|
63 | DATE: new Date(),
|
---|
64 | BASE_PATH: answers.basePath,
|
---|
65 | FRAMEWORKS: formatLine(answers.frameworks),
|
---|
66 | FILES: formatFiles(answers.files, answers.onlyServedFiles),
|
---|
67 | EXCLUDE: formatFiles(answers.exclude, []),
|
---|
68 | AUTO_WATCH: answers.autoWatch ? 'true' : 'false',
|
---|
69 | BROWSERS: formatLine(answers.browsers),
|
---|
70 | PREPROCESSORS: formatPreprocessors(answers.preprocessors)
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | class CoffeeFormatter extends JavaScriptFormatter {
|
---|
76 | constructor () {
|
---|
77 | super()
|
---|
78 | this.TEMPLATE_FILE_PATH = getConfigPath('config.tpl.coffee')
|
---|
79 | this.REQUIREJS_TEMPLATE_FILE = getConfigPath('requirejs.config.tpl.coffee')
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | class LiveFormatter extends JavaScriptFormatter {
|
---|
84 | constructor () {
|
---|
85 | super()
|
---|
86 | this.TEMPLATE_FILE_PATH = getConfigPath('config.tpl.ls')
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | class TypeFormatter extends JavaScriptFormatter {
|
---|
91 | constructor () {
|
---|
92 | super()
|
---|
93 | this.TEMPLATE_FILE_PATH = getConfigPath('config.tpl.ts')
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | exports.JavaScript = JavaScriptFormatter
|
---|
98 | exports.Coffee = CoffeeFormatter
|
---|
99 | exports.Live = LiveFormatter
|
---|
100 | exports.Type = TypeFormatter
|
---|
101 |
|
---|
102 | exports.createForPath = function (path) {
|
---|
103 | if (/\.coffee$/.test(path)) {
|
---|
104 | return new CoffeeFormatter()
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (/\.ls$/.test(path)) {
|
---|
108 | return new LiveFormatter()
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (/\.ts$/.test(path)) {
|
---|
112 | return new TypeFormatter()
|
---|
113 | }
|
---|
114 |
|
---|
115 | return new JavaScriptFormatter()
|
---|
116 | }
|
---|