wikijs-fork/fuse.js

280 lines
9.1 KiB
JavaScript
Raw Normal View History

2017-03-27 02:07:40 +00:00
'use strict'
/**
* FUSEBOX
*
* Client & Server compiler / bundler / watcher
2017-03-27 02:07:40 +00:00
*/
const _ = require('lodash')
const Promise = require('bluebird')
const colors = require('colors/safe')
const fs = Promise.promisifyAll(require('fs-extra'))
const fsbx = require('fuse-box')
const nodemon = require('nodemon')
const path = require('path')
const uglify = require('uglify-es')
2017-03-27 02:07:40 +00:00
// ======================================================
2017-03-27 02:07:40 +00:00
// Parse cmd arguments
// ======================================================
2017-03-27 02:07:40 +00:00
const args = require('yargs')
.option('d', {
alias: 'dev',
describe: 'Start in Developer mode',
type: 'boolean'
})
.option('c', {
alias: 'dev-configure',
describe: 'Start in Configure Developer mode',
type: 'boolean'
})
.help('h')
.alias('h', 'help')
.argv
2017-03-27 02:07:40 +00:00
let mode = 'build'
2017-05-13 21:03:32 +00:00
const dev = args.d || args.c
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... '))
}
// ======================================================
// BUILD VARS
// ======================================================
const ALIASES = {
'brace-ext-modelist': 'brace/ext/modelist.js',
'simplemde': 'simplemde/dist/simplemde.min.js',
'socket-io-client': 'socket.io-client/dist/socket.io.js',
'vue': (dev) ? 'vue/dist/vue.js' : 'vue/dist/vue.min.js',
'vue-lodash': 'vue-lodash/dist/vue-lodash.min.js'
}
const SHIMS = {
_preinit: {
source: '.build/_preinit.js',
exports: '_preinit'
},
jquery: {
source: 'node_modules/jquery/dist/jquery.js',
exports: '$'
},
mathjax: {
source: 'node_modules/mathjax/MathJax.js',
exports: 'MathJax'
}
}
// ======================================================
// Global Tasks
// ======================================================
2017-03-27 02:07:40 +00:00
console.info(colors.white('└── ') + colors.green('Running global tasks...'))
2017-03-27 02:07:40 +00:00
let globalTasks = Promise.mapSeries([
2017-04-14 18:15:11 +00:00
/**
* ACE Modes
*/
() => {
2017-04-08 20:46:29 +00:00
return fs.accessAsync('./assets/js/ace').then(() => {
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 Promise.join(
// Core
Promise.all([
fs.readFileAsync('./node_modules/brace/index.js', 'utf8'),
fs.readFileAsync('./node_modules/brace/theme/dawn.js', 'utf8'),
fs.readFileAsync('./node_modules/brace/mode/markdown.js', 'utf8')
]).then(items => {
console.info(colors.white(' source-view.js'))
let result = uglify.minify(items.join(';\n'), { output: { 'max_line_len': 1000000 } })
return fs.writeFileAsync('./assets/js/ace/source-view.js', result.code)
}),
// Modes
fs.readdirAsync('./node_modules/brace/mode').then(modeList => {
return Promise.map(modeList, mdFile => {
return fs.readFileAsync(path.join('./node_modules/brace/mode', mdFile), 'utf8').then(modeCode => {
console.info(colors.white(' mode-' + mdFile))
let result = uglify.minify(modeCode, { output: { 'max_line_len': 1000000 } })
return fs.writeFileAsync(path.join('./assets/js/ace', 'mode-' + mdFile), result.code)
})
}, { concurrency: 3 })
})
)
})
} 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(() => {
2017-05-21 03:21:16 +00:00
return fs.copyAsync('./node_modules/mathjax', './assets/js/mathjax', {
filter: (src, dest) => {
let srcNormalized = src.replace(/\\/g, '/')
let shouldCopy = false
console.info(colors.white(' ' + 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
2017-04-14 18:15:11 +00:00
}
2017-05-21 03:21:16 +00:00
return shouldCopy
2017-04-14 18:15:11 +00:00
}
2017-05-21 03:21:16 +00:00
})
2017-04-14 18:15:11 +00:00
})
} else {
throw err
}
})
2017-04-15 17:15:23 +00:00
},
2017-05-21 03:21:16 +00:00
/**
* i18n
*/
() => {
console.info(colors.white(' └── ') + colors.green('Copying i18n client files...'))
return fs.ensureDirAsync('./assets/js/i18n').then(() => {
return fs.readJsonAsync('./server/locales/en/browser.json').then(enContent => {
return fs.readdirAsync('./server/locales').then(langs => {
return Promise.map(langs, lang => {
console.info(colors.white(' ' + lang + '.json'))
let outputPath = path.join('./assets/js/i18n', lang + '.json')
return fs.readJsonAsync(path.join('./server/locales', lang + '.json'), 'utf8').then((content) => {
return fs.outputJsonAsync(outputPath, _.defaultsDeep(content, enContent))
}).catch(err => { // eslint-disable-line handle-callback-err
return fs.outputJsonAsync(outputPath, enContent)
})
})
})
})
})
},
2017-04-15 17:15:23 +00:00
/**
* Bundle pre-init scripts
*/
() => {
console.info(colors.white(' └── ') + colors.green('Bundling pre-init scripts...'))
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-05-13 21:03:32 +00:00
},
/**
* Delete Fusebox cache
*/
() => {
2017-05-21 03:21:16 +00:00
console.info(colors.white(' └── ') + colors.green('Clearing fuse-box cache...'))
2017-05-13 21:03:32 +00:00
return fs.emptyDirAsync('./.fusebox')
}
], f => { return f() })
// ======================================================
// Fuse Tasks
// ======================================================
globalTasks.then(() => {
2017-05-13 21:03:32 +00:00
let fuse = fsbx.FuseBox.init({
homeDir: './client',
output: './assets/js/$name.min.js',
alias: ALIASES,
shim: SHIMS,
plugins: [
fsbx.EnvPlugin({ NODE_ENV: (dev) ? 'development' : 'production' }),
fsbx.VuePlugin(),
2017-05-21 03:21:16 +00:00
['.scss', fsbx.SassPlugin({ outputStyle: (dev) ? 'nested' : 'compressed' }), fsbx.CSSPlugin()],
dev && fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
2017-05-13 21:03:32 +00:00
fsbx.JSONPlugin(),
!dev && fsbx.UglifyESPlugin({
2017-05-13 21:03:32 +00:00
compress: { unused: false },
output: { 'max_line_len': 1000000 }
})
2017-05-13 21:03:32 +00:00
],
debug: false,
log: true
})
2017-03-29 00:19:01 +00:00
2017-05-13 21:03:32 +00:00
if (dev) {
fuse.dev({
port: 4444,
httpServer: false
})
}
// const bundleLibs = fuse.bundle('libs').instructions('~ index.js - brace')
// const bundleApp = fuse.bundle('app').instructions('!> [index.js]')
const bundleApp = fuse.bundle('app').instructions('> index.js')
2017-05-13 21:03:32 +00:00
const bundleSetup = fuse.bundle('configure').instructions('> configure.js')
2017-05-05 02:41:33 +00:00
2017-05-13 21:03:32 +00:00
switch (mode) {
case 'dev':
// bundleLibs.watch()
2017-05-13 21:03:32 +00:00
bundleApp.watch()
break
case 'dev-configure':
2017-05-13 21:03:32 +00:00
bundleSetup.watch()
break
2017-05-13 21:03:32 +00:00
}
2017-03-27 02:07:40 +00:00
2017-05-13 21:03:32 +00:00
fuse.run().then(() => {
console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
2017-05-05 02:41:33 +00:00
2017-05-13 21:03:32 +00:00
if (dev) {
nodemon({
exec: (args.d) ? 'node server' : 'node wiki configure',
ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
ext: 'js json',
watch: (args.d) ? ['server'] : ['server/configure.js'],
env: { 'NODE_ENV': 'development' }
})
2017-05-13 21:03:32 +00:00
}
}).catch(err => {
console.error(colors.red(' X Bundle compilation failed! ' + err.message))
process.exit(1)
})
})