You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
6.1 KiB

  1. 'use strict'
  2. const _ = require('lodash')
  3. const Promise = require('bluebird')
  4. const colors = require('colors/safe')
  5. const fs = Promise.promisifyAll(require('fs-extra'))
  6. const path = require('path')
  7. const uglify = require('uglify-es')
  8. module.exports = Promise.mapSeries([
  9. /**
  10. * SimpleMDE
  11. */
  12. () => {
  13. return fs.accessAsync('./assets/js/simplemde').then(() => {
  14. console.info(colors.white(' └── ') + colors.magenta('SimpleMDE directory already exists. Task aborted.'))
  15. return true
  16. }).catch(err => {
  17. if (err.code === 'ENOENT') {
  18. console.info(colors.white(' └── ') + colors.green('Copy + Minify SimpleMDE to assets...'))
  19. return fs.copy('./node_modules/simplemde/dist/simplemde.min.js', './assets/js/simplemde/simplemde.min.js')
  20. } else {
  21. throw err
  22. }
  23. })
  24. },
  25. /**
  26. * ACE Modes
  27. */
  28. () => {
  29. return fs.accessAsync('./assets/js/ace').then(() => {
  30. console.info(colors.white(' └── ') + colors.magenta('ACE modes directory already exists. Task aborted.'))
  31. return true
  32. }).catch(err => {
  33. if (err.code === 'ENOENT') {
  34. console.info(colors.white(' └── ') + colors.green('Copy + Minify ACE modes to assets...'))
  35. return fs.ensureDirAsync('./assets/js/ace').then(() => {
  36. return Promise.join(
  37. // Core
  38. Promise.all([
  39. fs.readFileAsync('./node_modules/brace/index.js', 'utf8'),
  40. fs.readFileAsync('./node_modules/brace/ext/modelist.js', 'utf8'),
  41. fs.readFileAsync('./node_modules/brace/theme/dawn.js', 'utf8'),
  42. fs.readFileAsync('./node_modules/brace/theme/tomorrow_night.js', 'utf8'),
  43. fs.readFileAsync('./node_modules/brace/mode/markdown.js', 'utf8')
  44. ]).then(items => {
  45. console.info(colors.white(' ace.js'))
  46. let result = uglify.minify(items.join(';\n'), { output: { 'max_line_len': 1000000 } })
  47. return fs.writeFileAsync('./assets/js/ace/ace.js', result.code)
  48. }),
  49. // Modes
  50. fs.readdirAsync('./node_modules/brace/mode').then(modeList => {
  51. return Promise.map(modeList, mdFile => {
  52. return fs.readFileAsync(path.join('./node_modules/brace/mode', mdFile), 'utf8').then(modeCode => {
  53. console.info(colors.white(' mode-' + mdFile))
  54. let result = uglify.minify(modeCode, { output: { 'max_line_len': 1000000 } })
  55. return fs.writeFileAsync(path.join('./assets/js/ace', 'mode-' + mdFile), result.code)
  56. })
  57. }, { concurrency: 3 })
  58. })
  59. )
  60. })
  61. } else {
  62. throw err
  63. }
  64. })
  65. },
  66. /**
  67. * MathJax
  68. */
  69. () => {
  70. return fs.accessAsync('./assets/js/mathjax').then(() => {
  71. console.info(colors.white(' └── ') + colors.magenta('MathJax directory already exists. Task aborted.'))
  72. return true
  73. }).catch(err => {
  74. if (err.code === 'ENOENT') {
  75. console.info(colors.white(' └── ') + colors.green('Copy MathJax dependencies to assets...'))
  76. return fs.ensureDirAsync('./assets/js/mathjax').then(() => {
  77. return fs.copyAsync('./node_modules/mathjax', './assets/js/mathjax', {
  78. filter: (src, dest) => {
  79. let srcNormalized = src.replace(/\\/g, '/')
  80. let shouldCopy = false
  81. console.info(colors.white(' ' + srcNormalized))
  82. _.forEach([
  83. '/node_modules/mathjax',
  84. '/node_modules/mathjax/jax',
  85. '/node_modules/mathjax/jax/input',
  86. '/node_modules/mathjax/jax/output'
  87. ], chk => {
  88. if (srcNormalized.endsWith(chk)) {
  89. shouldCopy = true
  90. }
  91. })
  92. _.forEach([
  93. '/node_modules/mathjax/extensions',
  94. '/node_modules/mathjax/MathJax.js',
  95. '/node_modules/mathjax/jax/element',
  96. '/node_modules/mathjax/jax/input/MathML',
  97. '/node_modules/mathjax/jax/input/TeX',
  98. '/node_modules/mathjax/jax/output/SVG'
  99. ], chk => {
  100. if (srcNormalized.indexOf(chk) > 0) {
  101. shouldCopy = true
  102. }
  103. })
  104. if (shouldCopy && srcNormalized.indexOf('/fonts/') > 0 && srcNormalized.indexOf('/STIX-Web') <= 1) {
  105. shouldCopy = false
  106. }
  107. return shouldCopy
  108. }
  109. })
  110. })
  111. } else {
  112. throw err
  113. }
  114. })
  115. },
  116. /**
  117. * i18n
  118. */
  119. () => {
  120. console.info(colors.white(' └── ') + colors.green('Copying i18n client files...'))
  121. return fs.ensureDirAsync('./assets/js/i18n').then(() => {
  122. return fs.readJsonAsync('./server/locales/en/browser.json').then(enContent => {
  123. return fs.readdirAsync('./server/locales').then(langs => {
  124. return Promise.map(langs, lang => {
  125. console.info(colors.white(' ' + lang + '.json'))
  126. let outputPath = path.join('./assets/js/i18n', lang + '.json')
  127. return fs.readJsonAsync(path.join('./server/locales', lang + '.json'), 'utf8').then((content) => {
  128. return fs.outputJsonAsync(outputPath, _.defaultsDeep(content, enContent))
  129. }).catch(err => { // eslint-disable-line handle-callback-err
  130. return fs.outputJsonAsync(outputPath, enContent)
  131. })
  132. })
  133. })
  134. })
  135. })
  136. },
  137. /**
  138. * Bundle pre-init scripts
  139. */
  140. () => {
  141. console.info(colors.white(' └── ') + colors.green('Bundling pre-init scripts...'))
  142. let preInitContent = ''
  143. return fs.readdirAsync('./client/js/pre-init').map(f => {
  144. let fPath = path.join('./client/js/pre-init/', f)
  145. return fs.readFileAsync(fPath, 'utf8').then(fContent => {
  146. preInitContent += fContent + ';\n'
  147. })
  148. }).then(() => {
  149. return fs.outputFileAsync('./.build/_preinit.js', preInitContent, 'utf8')
  150. })
  151. },
  152. /**
  153. * Delete Fusebox cache
  154. */
  155. () => {
  156. console.info(colors.white(' └── ') + colors.green('Clearing fuse-box cache...'))
  157. return fs.emptyDirAsync('./.fusebox')
  158. }
  159. ], f => { return f() })