wikijs-fork/dev/index.js

93 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-02-14 20:27:08 +00:00
#!/usr/bin/env node
2017-02-14 07:17:25 +00:00
2017-02-26 23:06:20 +00:00
// ===========================================
2019-05-26 01:52:42 +00:00
// Wiki.js DEV UTILITY
2017-02-26 23:06:20 +00:00
// Licensed under AGPLv3
// ===========================================
2017-10-08 02:44:35 +00:00
const Promise = require('bluebird')
const _ = require('lodash')
const chalk = require('chalk')
2017-10-08 02:44:35 +00:00
const init = {
2018-01-27 05:20:49 +00:00
dev() {
2019-05-26 01:52:42 +00:00
const webpack = require('webpack')
const chokidar = require('chokidar')
2018-01-27 05:20:49 +00:00
2019-05-26 01:52:42 +00:00
global.DEV = true
2019-06-02 01:38:21 +00:00
global.WP_CONFIG = require('./webpack/webpack.dev.js')
2019-05-26 01:52:42 +00:00
global.WP = webpack(global.WP_CONFIG)
global.WP_DEV = {
devMiddleware: require('webpack-dev-middleware')(global.WP, {
publicPath: global.WP_CONFIG.output.publicPath
}),
hotMiddleware: require('webpack-hot-middleware')(global.WP)
}
global.WP_DEV.devMiddleware.waitUntilValid(() => {
console.info(chalk.yellow.bold('>>> Starting Wiki.js in DEVELOPER mode...'))
2019-06-02 01:38:21 +00:00
require('../server')
2018-01-27 05:20:49 +00:00
2019-05-26 01:52:42 +00:00
process.stdin.setEncoding('utf8')
process.stdin.on('data', data => {
if (_.trim(data) === 'rs') {
console.warn(chalk.yellow.bold('--- >>>>>>>>>>>>>>>>>>>>>>>> ---'))
console.warn(chalk.yellow.bold('--- Manual restart requested ---'))
console.warn(chalk.yellow.bold('--- <<<<<<<<<<<<<<<<<<<<<<<< ---'))
this.reload()
}
})
2019-05-26 01:52:42 +00:00
const devWatcher = chokidar.watch([
'./server',
'!./server/views/master.pug'
], {
cwd: process.cwd(),
2019-05-26 01:52:42 +00:00
ignoreInitial: true,
atomic: 400
})
devWatcher.on('ready', () => {
devWatcher.on('all', () => {
console.warn(chalk.yellow.bold('--- >>>>>>>>>>>>>>>>>>>>>>>>>>>> ---'))
console.warn(chalk.yellow.bold('--- Changes detected: Restarting ---'))
console.warn(chalk.yellow.bold('--- <<<<<<<<<<<<<<<<<<<<<<<<<<<< ---'))
this.reload()
2018-01-27 22:39:55 +00:00
})
2018-01-27 05:20:49 +00:00
})
2019-05-26 01:52:42 +00:00
})
},
async reload() {
console.warn(chalk.yellow('--- Stopping scheduled jobs...'))
if (global.WIKI.scheduler) {
global.WIKI.scheduler.stop()
}
console.warn(chalk.yellow('--- Closing DB connections...'))
await global.WIKI.models.knex.destroy()
console.warn(chalk.yellow('--- Closing Server connections...'))
2019-02-22 22:05:18 +00:00
if (global.WIKI.server) {
await new Promise((resolve, reject) => global.WIKI.server.destroy(resolve))
}
console.warn(chalk.yellow('--- Purging node modules cache...'))
2018-12-02 04:03:14 +00:00
2019-02-22 22:05:18 +00:00
global.WIKI = {}
Object.keys(require.cache).forEach(id => {
if (/[/\\]server[/\\]/.test(id)) {
delete require.cache[id]
}
})
Object.keys(module.constructor._pathCache).forEach(cacheKey => {
if (/[/\\]server[/\\]/.test(cacheKey)) {
delete module.constructor._pathCache[cacheKey]
}
})
2018-12-02 04:03:14 +00:00
2019-02-22 22:05:18 +00:00
console.warn(chalk.yellow('--- Unregistering process listeners...'))
2018-12-02 04:03:14 +00:00
2019-02-22 22:05:18 +00:00
process.removeAllListeners('unhandledRejection')
process.removeAllListeners('uncaughtException')
2018-12-02 04:03:14 +00:00
2019-06-02 01:38:21 +00:00
require('../server')
2017-10-08 02:44:35 +00:00
}
}
2019-06-02 01:38:21 +00:00
init.dev()