source: trip-planner-front/node_modules/karma/lib/init/formatters.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.8 KB
Line 
1'use strict'
2
3const path = require('path')
4const FileUtils = require('../utils/file-utils')
5
6function quote (value) {
7 return `'${value}'`
8}
9
10function formatLine (items) {
11 return items.map(quote).join(', ')
12}
13
14function formatMultiLines (items) {
15 return items
16 .map((file) => '\n ' + file)
17 .join(',')
18}
19
20function 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
28function formatPreprocessors (preprocessors) {
29 const lines = Object.keys(preprocessors)
30 .map((pattern) => `${quote(pattern)}: [${formatLine(preprocessors[pattern])}]`)
31
32 return formatMultiLines(lines)
33}
34
35function getConfigPath (file) {
36 return path.join(__dirname, `/../../${file}`)
37}
38
39class 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
75class 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
83class LiveFormatter extends JavaScriptFormatter {
84 constructor () {
85 super()
86 this.TEMPLATE_FILE_PATH = getConfigPath('config.tpl.ls')
87 }
88}
89
90class TypeFormatter extends JavaScriptFormatter {
91 constructor () {
92 super()
93 this.TEMPLATE_FILE_PATH = getConfigPath('config.tpl.ts')
94 }
95}
96
97exports.JavaScript = JavaScriptFormatter
98exports.Coffee = CoffeeFormatter
99exports.Live = LiveFormatter
100exports.Type = TypeFormatter
101
102exports.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}
Note: See TracBrowser for help on using the repository browser.