formatters.js 2.8 KB

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