2017-03-27 02:07:40 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FUSEBOX
|
|
|
|
*
|
2017-04-02 17:18:33 +00:00
|
|
|
* Client & Server compiler / bundler / watcher
|
2017-03-27 02:07:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
const _ = require('lodash')
|
2017-04-02 17:18:33 +00:00
|
|
|
const Promise = require('bluebird')
|
|
|
|
const colors = require('colors/safe')
|
2017-04-07 00:55:45 +00:00
|
|
|
const fs = Promise.promisifyAll(require('fs-extra'))
|
2017-04-02 17:18:33 +00:00
|
|
|
const fsbx = require('fuse-box')
|
|
|
|
const nodemon = require('nodemon')
|
|
|
|
const path = require('path')
|
|
|
|
const uglify = require('uglify-js')
|
2017-03-27 02:07:40 +00:00
|
|
|
|
2017-04-02 17:18:33 +00:00
|
|
|
// ======================================================
|
2017-03-27 02:07:40 +00:00
|
|
|
// Parse cmd arguments
|
2017-04-02 17:18:33 +00:00
|
|
|
// ======================================================
|
2017-03-27 02:07:40 +00:00
|
|
|
|
|
|
|
const args = require('yargs')
|
2017-04-02 17:18:33 +00:00
|
|
|
.option('d', {
|
|
|
|
alias: 'dev',
|
|
|
|
describe: 'Start in Developer mode',
|
|
|
|
type: 'boolean'
|
|
|
|
})
|
|
|
|
.option('c', {
|
|
|
|
alias: 'dev-configure',
|
|
|
|
describe: 'Start in Configure Developer mode',
|
|
|
|
type: 'boolean'
|
|
|
|
})
|
2017-04-18 02:44:04 +00:00
|
|
|
.option('i', {
|
|
|
|
alias: 'inspect',
|
|
|
|
describe: 'Enable Inspector for debugging',
|
|
|
|
type: 'boolean',
|
|
|
|
implies: 'd'
|
|
|
|
})
|
2017-04-02 17:18:33 +00:00
|
|
|
.help('h')
|
|
|
|
.alias('h', 'help')
|
|
|
|
.argv
|
2017-03-27 02:07:40 +00:00
|
|
|
|
2017-04-02 17:18:33 +00:00
|
|
|
let mode = 'build'
|
|
|
|
if (args.d) {
|
|
|
|
console.info(colors.bgWhite.black(' Starting Fuse in DEVELOPER mode... '))
|
|
|
|
mode = 'dev'
|
|
|
|
} else if (args.c) {
|
|
|
|
console.info(colors.bgWhite.black(' Starting Fuse in CONFIGURE DEVELOPER mode... '))
|
|
|
|
mode = 'dev-configure'
|
|
|
|
} else {
|
|
|
|
console.info(colors.bgWhite.black(' Starting Fuse in BUILD mode... '))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ======================================================
|
|
|
|
// Define aliases / shims
|
|
|
|
// ======================================================
|
2017-04-01 21:07:01 +00:00
|
|
|
|
|
|
|
const ALIASES = {
|
2017-04-02 04:22:52 +00:00
|
|
|
'brace-ext-modelist': 'brace/ext/modelist.js',
|
2017-04-01 21:07:01 +00:00
|
|
|
'simplemde': 'simplemde/dist/simplemde.min.js',
|
|
|
|
'socket.io-client': 'socket.io-client/dist/socket.io.min.js',
|
2017-04-06 00:33:16 +00:00
|
|
|
'vue': 'vue/dist/vue.min.js'
|
2017-04-01 21:07:01 +00:00
|
|
|
}
|
|
|
|
const SHIMS = {
|
2017-04-15 17:15:23 +00:00
|
|
|
_preinit: {
|
|
|
|
source: '.build/_preinit.js',
|
|
|
|
exports: '_preinit'
|
|
|
|
},
|
2017-04-01 21:07:01 +00:00
|
|
|
jquery: {
|
|
|
|
source: 'node_modules/jquery/dist/jquery.js',
|
|
|
|
exports: '$'
|
2017-04-14 18:15:11 +00:00
|
|
|
},
|
|
|
|
mathjax: {
|
|
|
|
source: 'node_modules/mathjax/MathJax.js',
|
|
|
|
exports: 'MathJax'
|
2017-04-01 21:07:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-02 17:18:33 +00:00
|
|
|
// ======================================================
|
|
|
|
// Global Tasks
|
|
|
|
// ======================================================
|
2017-03-27 02:07:40 +00:00
|
|
|
|
2017-04-02 17:18:33 +00:00
|
|
|
console.info(colors.white('└── ') + colors.green('Running global tasks...'))
|
2017-03-27 02:07:40 +00:00
|
|
|
|
2017-04-02 17:18:33 +00:00
|
|
|
let globalTasks = Promise.mapSeries([
|
2017-04-14 18:15:11 +00:00
|
|
|
/**
|
|
|
|
* ACE Modes
|
|
|
|
*/
|
2017-04-02 17:18:33 +00:00
|
|
|
() => {
|
2017-04-08 20:46:29 +00:00
|
|
|
return fs.accessAsync('./assets/js/ace').then(() => {
|
2017-04-08 20:16:13 +00:00
|
|
|
console.info(colors.white(' └── ') + colors.magenta('ACE modes directory already exists. Task aborted.'))
|
|
|
|
return true
|
|
|
|
}).catch(err => {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
console.info(colors.white(' └── ') + colors.green('Copy + Minify ACE modes to assets...'))
|
|
|
|
return fs.ensureDirAsync('./assets/js/ace').then(() => {
|
|
|
|
return fs.readdirAsync('./node_modules/brace/mode').then(modeList => {
|
|
|
|
return Promise.map(modeList, mdFile => {
|
|
|
|
console.info(colors.white(' mode-' + mdFile))
|
|
|
|
let result = uglify.minify(path.join('./node_modules/brace/mode', mdFile), { output: { 'max_line_len': 1000000 } })
|
|
|
|
return fs.writeFileAsync(path.join('./assets/js/ace', 'mode-' + mdFile), result.code)
|
|
|
|
})
|
|
|
|
})
|
2017-04-07 00:55:45 +00:00
|
|
|
})
|
2017-04-08 20:16:13 +00:00
|
|
|
} else {
|
|
|
|
throw err
|
|
|
|
}
|
2017-03-29 00:19:01 +00:00
|
|
|
})
|
2017-04-14 18:15:11 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* MathJax
|
|
|
|
*/
|
|
|
|
() => {
|
|
|
|
return fs.accessAsync('./assets/js/mathjax').then(() => {
|
|
|
|
console.info(colors.white(' └── ') + colors.magenta('MathJax directory already exists. Task aborted.'))
|
|
|
|
return true
|
|
|
|
}).catch(err => {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
console.info(colors.white(' └── ') + colors.green('Copy MathJax dependencies to assets...'))
|
|
|
|
return fs.ensureDirAsync('./assets/js/mathjax').then(() => {
|
|
|
|
return fs.copyAsync('./node_modules/mathjax', './assets/js/mathjax', { filter: (src, dest) => {
|
|
|
|
let srcNormalized = src.replace(/\\/g, '/')
|
|
|
|
let shouldCopy = false
|
|
|
|
console.log(srcNormalized)
|
|
|
|
_.forEach([
|
|
|
|
'/node_modules/mathjax',
|
|
|
|
'/node_modules/mathjax/jax',
|
|
|
|
'/node_modules/mathjax/jax/input',
|
|
|
|
'/node_modules/mathjax/jax/output'
|
|
|
|
], chk => {
|
|
|
|
if (srcNormalized.endsWith(chk)) {
|
|
|
|
shouldCopy = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
_.forEach([
|
|
|
|
'/node_modules/mathjax/extensions',
|
|
|
|
'/node_modules/mathjax/MathJax.js',
|
|
|
|
'/node_modules/mathjax/jax/element',
|
|
|
|
'/node_modules/mathjax/jax/input/MathML',
|
|
|
|
'/node_modules/mathjax/jax/input/TeX',
|
|
|
|
'/node_modules/mathjax/jax/output/SVG'
|
|
|
|
], chk => {
|
|
|
|
if (srcNormalized.indexOf(chk) > 0) {
|
|
|
|
shouldCopy = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (shouldCopy && srcNormalized.indexOf('/fonts/') > 0 && srcNormalized.indexOf('/STIX-Web') <= 1) {
|
|
|
|
shouldCopy = false
|
|
|
|
}
|
|
|
|
return shouldCopy
|
|
|
|
}})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
})
|
2017-04-15 17:15:23 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Bundle pre-init scripts
|
|
|
|
*/
|
|
|
|
() => {
|
|
|
|
console.info(colors.white(' └── ') + colors.green('Bundling pre-init scripts...'))
|
2017-04-19 00:23:42 +00:00
|
|
|
let preInitContent = ''
|
2017-04-15 17:15:23 +00:00
|
|
|
return fs.readdirAsync('./client/js/pre-init').map(f => {
|
|
|
|
let fPath = path.join('./client/js/pre-init/', f)
|
|
|
|
return fs.readFileAsync(fPath, 'utf8').then(fContent => {
|
|
|
|
preInitContent += fContent + ';\n'
|
|
|
|
})
|
|
|
|
}).then(() => {
|
|
|
|
return fs.outputFileAsync('./.build/_preinit.js', preInitContent, 'utf8')
|
|
|
|
})
|
2017-04-02 17:18:33 +00:00
|
|
|
}
|
|
|
|
], f => { return f() })
|
|
|
|
|
|
|
|
// ======================================================
|
|
|
|
// Fuse Tasks
|
|
|
|
// ======================================================
|
|
|
|
|
|
|
|
let fuse
|
|
|
|
|
|
|
|
globalTasks.then(() => {
|
|
|
|
switch (mode) {
|
|
|
|
// =============================================
|
|
|
|
// DEVELOPER MODE
|
|
|
|
// =============================================
|
|
|
|
case 'dev':
|
|
|
|
// Client
|
|
|
|
|
|
|
|
fuse = fsbx.FuseBox.init({
|
|
|
|
homeDir: './client',
|
|
|
|
outFile: './assets/js/bundle.min.js',
|
|
|
|
alias: ALIASES,
|
|
|
|
shim: SHIMS,
|
|
|
|
plugins: [
|
|
|
|
[ fsbx.SassPlugin({ includePaths: ['../core'] }), fsbx.CSSPlugin() ],
|
|
|
|
fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
|
|
|
|
fsbx.JSONPlugin()
|
|
|
|
],
|
|
|
|
debug: false,
|
|
|
|
log: true
|
|
|
|
})
|
2017-03-29 00:19:01 +00:00
|
|
|
|
2017-04-02 17:18:33 +00:00
|
|
|
fuse.devServer('>index.js', {
|
|
|
|
port: 4444,
|
|
|
|
httpServer: false,
|
|
|
|
hmr: false
|
|
|
|
})
|
2017-03-29 00:19:01 +00:00
|
|
|
|
2017-04-02 17:18:33 +00:00
|
|
|
// Server
|
|
|
|
|
|
|
|
_.delay(() => {
|
|
|
|
nodemon({
|
2017-04-18 02:44:04 +00:00
|
|
|
exec: (args.i) ? 'node --inspect server' : 'node server',
|
2017-04-02 17:18:33 +00:00
|
|
|
ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
|
|
|
|
ext: 'js json',
|
2017-04-28 21:58:38 +00:00
|
|
|
watch: ['server'],
|
2017-04-02 17:18:33 +00:00
|
|
|
env: { 'NODE_ENV': 'development' }
|
|
|
|
})
|
|
|
|
}, 1000)
|
|
|
|
break
|
|
|
|
// =============================================
|
|
|
|
// CONFIGURE - DEVELOPER MODE
|
|
|
|
// =============================================
|
|
|
|
case 'dev-configure':
|
|
|
|
// Client
|
|
|
|
|
|
|
|
fuse = fsbx.FuseBox.init({
|
|
|
|
homeDir: './client',
|
|
|
|
outFile: './assets/js/configure.min.js',
|
|
|
|
alias: ALIASES,
|
|
|
|
shim: SHIMS,
|
|
|
|
plugins: [
|
|
|
|
[ fsbx.SassPlugin({ includePaths: ['../core'] }), fsbx.CSSPlugin() ],
|
|
|
|
fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
|
|
|
|
fsbx.JSONPlugin()
|
|
|
|
],
|
|
|
|
debug: false,
|
|
|
|
log: true
|
|
|
|
})
|
2017-03-27 02:07:40 +00:00
|
|
|
|
2017-04-02 17:18:33 +00:00
|
|
|
fuse.devServer('>configure.js', {
|
|
|
|
port: 4444,
|
|
|
|
httpServer: false
|
|
|
|
})
|
2017-03-27 02:07:40 +00:00
|
|
|
|
2017-04-02 17:18:33 +00:00
|
|
|
// Server
|
|
|
|
|
|
|
|
_.delay(() => {
|
|
|
|
nodemon({
|
|
|
|
exec: 'node wiki configure',
|
|
|
|
ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
|
|
|
|
ext: 'js json',
|
2017-04-28 21:58:38 +00:00
|
|
|
watch: ['server/configure.js'],
|
2017-04-02 17:18:33 +00:00
|
|
|
env: { 'NODE_ENV': 'development' }
|
|
|
|
})
|
|
|
|
}, 1000)
|
|
|
|
break
|
|
|
|
// =============================================
|
|
|
|
// BUILD ONLY MODE
|
|
|
|
// =============================================
|
|
|
|
case 'build':
|
|
|
|
fuse = fsbx.FuseBox.init({
|
|
|
|
homeDir: './client',
|
|
|
|
alias: ALIASES,
|
|
|
|
shim: SHIMS,
|
|
|
|
plugins: [
|
2017-04-06 00:33:16 +00:00
|
|
|
fsbx.EnvPlugin({ NODE_ENV: 'production' }),
|
2017-04-02 17:18:33 +00:00
|
|
|
[ fsbx.SassPlugin({ outputStyle: 'compressed', includePaths: ['./node_modules/requarks-core'] }), fsbx.CSSPlugin() ],
|
|
|
|
fsbx.BabelPlugin({
|
|
|
|
config: {
|
|
|
|
comments: false,
|
|
|
|
presets: ['es2015']
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
fsbx.JSONPlugin(),
|
|
|
|
fsbx.UglifyJSPlugin({
|
|
|
|
compress: { unused: false },
|
|
|
|
output: { 'max_line_len': 1000000 }
|
|
|
|
})
|
|
|
|
],
|
|
|
|
debug: false,
|
|
|
|
log: true
|
2017-03-27 02:07:40 +00:00
|
|
|
})
|
|
|
|
|
2017-04-02 17:18:33 +00:00
|
|
|
fuse.bundle({
|
|
|
|
'./assets/js/bundle.min.js': '>index.js',
|
|
|
|
'./assets/js/configure.min.js': '>configure.js'
|
|
|
|
}).then(() => {
|
|
|
|
console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(colors.red(' X Bundle compilation failed! ' + err.message))
|
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|