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.

304 lines
8.8 KiB

  1. 'use strict'
  2. /**
  3. * FUSEBOX
  4. *
  5. * Client & Server compiler / bundler / watcher
  6. */
  7. const _ = require('lodash')
  8. const Promise = require('bluebird')
  9. const colors = require('colors/safe')
  10. const fs = Promise.promisifyAll(require('fs-extra'))
  11. const fsbx = require('fuse-box')
  12. const nodemon = require('nodemon')
  13. const path = require('path')
  14. const uglify = require('uglify-js')
  15. // ======================================================
  16. // Parse cmd arguments
  17. // ======================================================
  18. const args = require('yargs')
  19. .option('d', {
  20. alias: 'dev',
  21. describe: 'Start in Developer mode',
  22. type: 'boolean'
  23. })
  24. .option('c', {
  25. alias: 'dev-configure',
  26. describe: 'Start in Configure Developer mode',
  27. type: 'boolean'
  28. })
  29. .option('i', {
  30. alias: 'inspect',
  31. describe: 'Enable Inspector for debugging',
  32. type: 'boolean',
  33. implies: 'd'
  34. })
  35. .help('h')
  36. .alias('h', 'help')
  37. .argv
  38. let mode = 'build'
  39. if (args.d) {
  40. console.info(colors.bgWhite.black(' Starting Fuse in DEVELOPER mode... '))
  41. mode = 'dev'
  42. } else if (args.c) {
  43. console.info(colors.bgWhite.black(' Starting Fuse in CONFIGURE DEVELOPER mode... '))
  44. mode = 'dev-configure'
  45. } else {
  46. console.info(colors.bgWhite.black(' Starting Fuse in BUILD mode... '))
  47. }
  48. // ======================================================
  49. // Define aliases / shims
  50. // ======================================================
  51. const ALIASES = {
  52. 'brace-ext-modelist': 'brace/ext/modelist.js',
  53. 'simplemde': 'simplemde/dist/simplemde.min.js',
  54. 'socket.io-client': 'socket.io-client/dist/socket.io.min.js',
  55. 'vue': 'vue/dist/vue.min.js'
  56. }
  57. const SHIMS = {
  58. _preinit: {
  59. source: '.build/_preinit.js',
  60. exports: '_preinit'
  61. },
  62. jquery: {
  63. source: 'node_modules/jquery/dist/jquery.js',
  64. exports: '$'
  65. },
  66. mathjax: {
  67. source: 'node_modules/mathjax/MathJax.js',
  68. exports: 'MathJax'
  69. }
  70. }
  71. // ======================================================
  72. // Global Tasks
  73. // ======================================================
  74. console.info(colors.white('└── ') + colors.green('Running global tasks...'))
  75. let globalTasks = Promise.mapSeries([
  76. /**
  77. * ACE Modes
  78. */
  79. () => {
  80. return fs.accessAsync('./assets/js/ace').then(() => {
  81. console.info(colors.white(' └── ') + colors.magenta('ACE modes directory already exists. Task aborted.'))
  82. return true
  83. }).catch(err => {
  84. if (err.code === 'ENOENT') {
  85. console.info(colors.white(' └── ') + colors.green('Copy + Minify ACE modes to assets...'))
  86. return fs.ensureDirAsync('./assets/js/ace').then(() => {
  87. return fs.readdirAsync('./node_modules/brace/mode').then(modeList => {
  88. return Promise.map(modeList, mdFile => {
  89. console.info(colors.white(' mode-' + mdFile))
  90. let result = uglify.minify(path.join('./node_modules/brace/mode', mdFile), { output: { 'max_line_len': 1000000 } })
  91. return fs.writeFileAsync(path.join('./assets/js/ace', 'mode-' + mdFile), result.code)
  92. })
  93. })
  94. })
  95. } else {
  96. throw err
  97. }
  98. })
  99. },
  100. /**
  101. * MathJax
  102. */
  103. () => {
  104. return fs.accessAsync('./assets/js/mathjax').then(() => {
  105. console.info(colors.white(' └── ') + colors.magenta('MathJax directory already exists. Task aborted.'))
  106. return true
  107. }).catch(err => {
  108. if (err.code === 'ENOENT') {
  109. console.info(colors.white(' └── ') + colors.green('Copy MathJax dependencies to assets...'))
  110. return fs.ensureDirAsync('./assets/js/mathjax').then(() => {
  111. return fs.copyAsync('./node_modules/mathjax', './assets/js/mathjax', { filter: (src, dest) => {
  112. let srcNormalized = src.replace(/\\/g, '/')
  113. let shouldCopy = false
  114. console.log(srcNormalized)
  115. _.forEach([
  116. '/node_modules/mathjax',
  117. '/node_modules/mathjax/jax',
  118. '/node_modules/mathjax/jax/input',
  119. '/node_modules/mathjax/jax/output'
  120. ], chk => {
  121. if (srcNormalized.endsWith(chk)) {
  122. shouldCopy = true
  123. }
  124. })
  125. _.forEach([
  126. '/node_modules/mathjax/extensions',
  127. '/node_modules/mathjax/MathJax.js',
  128. '/node_modules/mathjax/jax/element',
  129. '/node_modules/mathjax/jax/input/MathML',
  130. '/node_modules/mathjax/jax/input/TeX',
  131. '/node_modules/mathjax/jax/output/SVG'
  132. ], chk => {
  133. if (srcNormalized.indexOf(chk) > 0) {
  134. shouldCopy = true
  135. }
  136. })
  137. if (shouldCopy && srcNormalized.indexOf('/fonts/') > 0 && srcNormalized.indexOf('/STIX-Web') <= 1) {
  138. shouldCopy = false
  139. }
  140. return shouldCopy
  141. }})
  142. })
  143. } else {
  144. throw err
  145. }
  146. })
  147. },
  148. /**
  149. * Bundle pre-init scripts
  150. */
  151. () => {
  152. console.info(colors.white(' └── ') + colors.green('Bundling pre-init scripts...'))
  153. let preInitContent = ''
  154. return fs.readdirAsync('./client/js/pre-init').map(f => {
  155. let fPath = path.join('./client/js/pre-init/', f)
  156. return fs.readFileAsync(fPath, 'utf8').then(fContent => {
  157. preInitContent += fContent + ';\n'
  158. })
  159. }).then(() => {
  160. return fs.outputFileAsync('./.build/_preinit.js', preInitContent, 'utf8')
  161. })
  162. }
  163. ], f => { return f() })
  164. // ======================================================
  165. // Fuse Tasks
  166. // ======================================================
  167. let fuse
  168. globalTasks.then(() => {
  169. switch (mode) {
  170. // =============================================
  171. // DEVELOPER MODE
  172. // =============================================
  173. case 'dev':
  174. // Client
  175. fuse = fsbx.FuseBox.init({
  176. homeDir: './client',
  177. output: './assets/js/$name.min.js',
  178. alias: ALIASES,
  179. shim: SHIMS,
  180. plugins: [
  181. fsbx.VuePlugin(),
  182. [ '.scss', fsbx.SassPlugin(), fsbx.CSSPlugin() ],
  183. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  184. fsbx.JSONPlugin()
  185. ],
  186. debug: false,
  187. log: true
  188. })
  189. fuse.dev({
  190. port: 4444,
  191. httpServer: false
  192. })
  193. fuse.bundle('bundle')
  194. .instructions('> index.js')
  195. .watch()
  196. fuse.run().then(() => {
  197. nodemon({
  198. exec: (args.i) ? 'node --inspect server' : 'node server',
  199. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  200. ext: 'js json',
  201. watch: ['server'],
  202. env: { 'NODE_ENV': 'development' }
  203. })
  204. })
  205. break
  206. // =============================================
  207. // CONFIGURE - DEVELOPER MODE
  208. // =============================================
  209. case 'dev-configure':
  210. // Client
  211. fuse = fsbx.FuseBox.init({
  212. homeDir: './client',
  213. output: './assets/js/$name.min.js',
  214. alias: ALIASES,
  215. shim: SHIMS,
  216. plugins: [
  217. [ '.scss', fsbx.SassPlugin(), fsbx.CSSPlugin() ],
  218. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  219. fsbx.JSONPlugin()
  220. ],
  221. debug: false,
  222. log: true
  223. })
  224. fuse.dev({
  225. port: 4444,
  226. httpServer: false
  227. })
  228. fuse.bundle('configure')
  229. .instructions('> configure.js')
  230. .watch()
  231. fuse.run().then(() => {
  232. nodemon({
  233. exec: 'node wiki configure',
  234. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  235. ext: 'js json',
  236. watch: ['server/configure.js'],
  237. env: { 'NODE_ENV': 'development' }
  238. })
  239. })
  240. break
  241. // =============================================
  242. // BUILD ONLY MODE
  243. // =============================================
  244. case 'build':
  245. fuse = fsbx.FuseBox.init({
  246. homeDir: './client',
  247. output: './assets/js/$name.min.js',
  248. alias: ALIASES,
  249. shim: SHIMS,
  250. plugins: [
  251. fsbx.EnvPlugin({ NODE_ENV: 'production' }),
  252. fsbx.VuePlugin(),
  253. [ '.scss', fsbx.SassPlugin({ outputStyle: 'compressed' }), fsbx.CSSPlugin() ],
  254. fsbx.BabelPlugin({
  255. config: {
  256. comments: false,
  257. presets: ['es2015']
  258. }
  259. }),
  260. fsbx.JSONPlugin(),
  261. fsbx.UglifyJSPlugin({
  262. compress: { unused: false },
  263. output: { 'max_line_len': 1000000 }
  264. })
  265. ],
  266. debug: false,
  267. log: true
  268. })
  269. fuse.bundle('bundle').instructions('> index.js')
  270. fuse.bundle('configure').instructions('> configure.js')
  271. fuse.run().then(() => {
  272. console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
  273. }).catch(err => {
  274. console.error(colors.red(' X Bundle compilation failed! ' + err.message))
  275. process.exit(1)
  276. })
  277. break
  278. }
  279. })