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.

306 lines
8.9 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. outFile: './assets/js/bundle.min.js',
  178. alias: ALIASES,
  179. shim: SHIMS,
  180. plugins: [
  181. [ fsbx.SassPlugin({ includePaths: ['../core'] }), fsbx.CSSPlugin() ],
  182. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  183. fsbx.JSONPlugin()
  184. ],
  185. debug: false,
  186. log: true
  187. })
  188. fuse.devServer('>index.js', {
  189. port: 4444,
  190. httpServer: false,
  191. hmr: false
  192. })
  193. // Server
  194. _.delay(() => {
  195. nodemon({
  196. exec: (args.i) ? 'node --inspect server' : 'node server',
  197. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  198. ext: 'js json',
  199. watch: [
  200. 'controllers',
  201. 'libs',
  202. 'locales',
  203. 'middlewares',
  204. 'models',
  205. 'agent.js',
  206. 'server.js'
  207. ],
  208. env: { 'NODE_ENV': 'development' }
  209. })
  210. }, 1000)
  211. break
  212. // =============================================
  213. // CONFIGURE - DEVELOPER MODE
  214. // =============================================
  215. case 'dev-configure':
  216. // Client
  217. fuse = fsbx.FuseBox.init({
  218. homeDir: './client',
  219. outFile: './assets/js/configure.min.js',
  220. alias: ALIASES,
  221. shim: SHIMS,
  222. plugins: [
  223. [ fsbx.SassPlugin({ includePaths: ['../core'] }), fsbx.CSSPlugin() ],
  224. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  225. fsbx.JSONPlugin()
  226. ],
  227. debug: false,
  228. log: true
  229. })
  230. fuse.devServer('>configure.js', {
  231. port: 4444,
  232. httpServer: false
  233. })
  234. // Server
  235. _.delay(() => {
  236. nodemon({
  237. exec: 'node wiki configure',
  238. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  239. ext: 'js json',
  240. watch: [
  241. 'configure.js'
  242. ],
  243. env: { 'NODE_ENV': 'development' }
  244. })
  245. }, 1000)
  246. break
  247. // =============================================
  248. // BUILD ONLY MODE
  249. // =============================================
  250. case 'build':
  251. fuse = fsbx.FuseBox.init({
  252. homeDir: './client',
  253. alias: ALIASES,
  254. shim: SHIMS,
  255. plugins: [
  256. fsbx.EnvPlugin({ NODE_ENV: 'production' }),
  257. [ fsbx.SassPlugin({ outputStyle: 'compressed', includePaths: ['./node_modules/requarks-core'] }), fsbx.CSSPlugin() ],
  258. fsbx.BabelPlugin({
  259. config: {
  260. comments: false,
  261. presets: ['es2015']
  262. }
  263. }),
  264. fsbx.JSONPlugin(),
  265. fsbx.UglifyJSPlugin({
  266. compress: { unused: false },
  267. output: { 'max_line_len': 1000000 }
  268. })
  269. ],
  270. debug: false,
  271. log: true
  272. })
  273. fuse.bundle({
  274. './assets/js/bundle.min.js': '>index.js',
  275. './assets/js/configure.min.js': '>configure.js'
  276. }).then(() => {
  277. console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
  278. }).catch(err => {
  279. console.error(colors.red(' X Bundle compilation failed! ' + err.message))
  280. process.exit(1)
  281. })
  282. break
  283. }
  284. })