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.

227 lines
6.3 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. .help('h')
  30. .alias('h', 'help')
  31. .argv
  32. let mode = 'build'
  33. if (args.d) {
  34. console.info(colors.bgWhite.black(' Starting Fuse in DEVELOPER mode... '))
  35. mode = 'dev'
  36. } else if (args.c) {
  37. console.info(colors.bgWhite.black(' Starting Fuse in CONFIGURE DEVELOPER mode... '))
  38. mode = 'dev-configure'
  39. } else {
  40. console.info(colors.bgWhite.black(' Starting Fuse in BUILD mode... '))
  41. }
  42. // ======================================================
  43. // Define aliases / shims
  44. // ======================================================
  45. const ALIASES = {
  46. 'brace-ext-modelist': 'brace/ext/modelist.js',
  47. 'simplemde': 'simplemde/dist/simplemde.min.js',
  48. 'socket.io-client': 'socket.io-client/dist/socket.io.min.js',
  49. 'vue': 'vue/dist/vue.min.js'
  50. }
  51. const SHIMS = {
  52. jquery: {
  53. source: 'node_modules/jquery/dist/jquery.js',
  54. exports: '$'
  55. }
  56. }
  57. // ======================================================
  58. // Global Tasks
  59. // ======================================================
  60. console.info(colors.white('└── ') + colors.green('Running global tasks...'))
  61. let globalTasks = Promise.mapSeries([
  62. () => {
  63. return fs.accessAsync('./assets/js/ace').then(() => {
  64. console.info(colors.white(' └── ') + colors.magenta('ACE modes directory already exists. Task aborted.'))
  65. return true
  66. }).catch(err => {
  67. if (err.code === 'ENOENT') {
  68. console.info(colors.white(' └── ') + colors.green('Copy + Minify ACE modes to assets...'))
  69. return fs.ensureDirAsync('./assets/js/ace').then(() => {
  70. return fs.readdirAsync('./node_modules/brace/mode').then(modeList => {
  71. return Promise.map(modeList, mdFile => {
  72. console.info(colors.white(' mode-' + mdFile))
  73. let result = uglify.minify(path.join('./node_modules/brace/mode', mdFile), { output: { 'max_line_len': 1000000 } })
  74. return fs.writeFileAsync(path.join('./assets/js/ace', 'mode-' + mdFile), result.code)
  75. })
  76. })
  77. })
  78. } else {
  79. throw err
  80. }
  81. })
  82. }
  83. ], f => { return f() })
  84. // ======================================================
  85. // Fuse Tasks
  86. // ======================================================
  87. let fuse
  88. globalTasks.then(() => {
  89. switch (mode) {
  90. // =============================================
  91. // DEVELOPER MODE
  92. // =============================================
  93. case 'dev':
  94. // Client
  95. fuse = fsbx.FuseBox.init({
  96. homeDir: './client',
  97. outFile: './assets/js/bundle.min.js',
  98. alias: ALIASES,
  99. shim: SHIMS,
  100. plugins: [
  101. [ fsbx.SassPlugin({ includePaths: ['../core'] }), fsbx.CSSPlugin() ],
  102. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  103. fsbx.JSONPlugin()
  104. ],
  105. debug: false,
  106. log: true
  107. })
  108. fuse.devServer('>index.js', {
  109. port: 4444,
  110. httpServer: false,
  111. hmr: false
  112. })
  113. // Server
  114. _.delay(() => {
  115. nodemon({
  116. script: './server.js',
  117. args: [],
  118. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  119. ext: 'js json',
  120. watch: [
  121. 'controllers',
  122. 'libs',
  123. 'locales',
  124. 'middlewares',
  125. 'models',
  126. 'agent.js',
  127. 'server.js'
  128. ],
  129. env: { 'NODE_ENV': 'development' }
  130. })
  131. }, 1000)
  132. break
  133. // =============================================
  134. // CONFIGURE - DEVELOPER MODE
  135. // =============================================
  136. case 'dev-configure':
  137. // Client
  138. fuse = fsbx.FuseBox.init({
  139. homeDir: './client',
  140. outFile: './assets/js/configure.min.js',
  141. alias: ALIASES,
  142. shim: SHIMS,
  143. plugins: [
  144. [ fsbx.SassPlugin({ includePaths: ['../core'] }), fsbx.CSSPlugin() ],
  145. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  146. fsbx.JSONPlugin()
  147. ],
  148. debug: false,
  149. log: true
  150. })
  151. fuse.devServer('>configure.js', {
  152. port: 4444,
  153. httpServer: false
  154. })
  155. // Server
  156. _.delay(() => {
  157. nodemon({
  158. exec: 'node wiki configure',
  159. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  160. ext: 'js json',
  161. watch: [
  162. 'configure.js'
  163. ],
  164. env: { 'NODE_ENV': 'development' }
  165. })
  166. }, 1000)
  167. break
  168. // =============================================
  169. // BUILD ONLY MODE
  170. // =============================================
  171. case 'build':
  172. fuse = fsbx.FuseBox.init({
  173. homeDir: './client',
  174. alias: ALIASES,
  175. shim: SHIMS,
  176. plugins: [
  177. fsbx.EnvPlugin({ NODE_ENV: 'production' }),
  178. [ fsbx.SassPlugin({ outputStyle: 'compressed', includePaths: ['./node_modules/requarks-core'] }), fsbx.CSSPlugin() ],
  179. fsbx.BabelPlugin({
  180. config: {
  181. comments: false,
  182. presets: ['es2015']
  183. }
  184. }),
  185. fsbx.JSONPlugin(),
  186. fsbx.UglifyJSPlugin({
  187. compress: { unused: false },
  188. output: { 'max_line_len': 1000000 }
  189. })
  190. ],
  191. debug: false,
  192. log: true
  193. })
  194. fuse.bundle({
  195. './assets/js/bundle.min.js': '>index.js',
  196. './assets/js/configure.min.js': '>configure.js'
  197. }).then(() => {
  198. console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
  199. }).catch(err => {
  200. console.error(colors.red(' X Bundle compilation failed! ' + err.message))
  201. process.exit(1)
  202. })
  203. break
  204. }
  205. })