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
|
|
|
// ===========================================
|
|
|
|
// Wiki.js
|
2017-10-08 02:44:35 +00:00
|
|
|
// 2.0
|
2017-02-26 23:06:20 +00:00
|
|
|
// Licensed under AGPLv3
|
|
|
|
// ===========================================
|
|
|
|
|
2017-10-08 02:44:35 +00:00
|
|
|
const Promise = require('bluebird')
|
|
|
|
const fs = Promise.promisifyAll(require('fs-extra'))
|
|
|
|
const pm2 = Promise.promisifyAll(require('pm2'))
|
|
|
|
const ora = require('ora')
|
|
|
|
const path = require('path')
|
2018-01-27 05:20:49 +00:00
|
|
|
const cluster = require('cluster')
|
2017-10-08 02:44:35 +00:00
|
|
|
|
|
|
|
const ROOTPATH = process.cwd()
|
|
|
|
|
|
|
|
const init = {
|
|
|
|
/**
|
|
|
|
* Start in background mode
|
|
|
|
*/
|
|
|
|
start () {
|
|
|
|
let spinner = ora('Initializing...').start()
|
|
|
|
return fs.emptyDirAsync(path.join(ROOTPATH, './logs')).then(() => {
|
|
|
|
return pm2.connectAsync().then(() => {
|
|
|
|
return pm2.startAsync({
|
|
|
|
name: 'wiki',
|
|
|
|
script: 'server',
|
|
|
|
cwd: ROOTPATH,
|
|
|
|
output: path.join(ROOTPATH, './logs/wiki-output.log'),
|
|
|
|
error: path.join(ROOTPATH, './logs/wiki-error.log'),
|
|
|
|
minUptime: 5000,
|
|
|
|
maxRestarts: 5
|
|
|
|
}).then(() => {
|
|
|
|
spinner.succeed('Wiki.js has started successfully.')
|
|
|
|
}).finally(() => {
|
|
|
|
pm2.disconnect()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}).catch(err => {
|
|
|
|
spinner.fail(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Stop Wiki.js process(es)
|
|
|
|
*/
|
|
|
|
stop () {
|
|
|
|
let spinner = ora('Shutting down Wiki.js...').start()
|
|
|
|
return pm2.connectAsync().then(() => {
|
|
|
|
return pm2.stopAsync('wiki').then(() => {
|
|
|
|
spinner.succeed('Wiki.js has stopped successfully.')
|
|
|
|
}).finally(() => {
|
|
|
|
pm2.disconnect()
|
|
|
|
})
|
|
|
|
}).catch(err => {
|
|
|
|
spinner.fail(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Restart Wiki.js process(es)
|
|
|
|
*/
|
|
|
|
restart: function () {
|
2018-01-27 05:20:49 +00:00
|
|
|
return this.stop().delay(1000).then(() => {
|
|
|
|
this.start()
|
2017-10-08 02:44:35 +00:00
|
|
|
})
|
2018-01-27 05:20:49 +00:00
|
|
|
},
|
|
|
|
dev() {
|
|
|
|
if (cluster.isMaster) {
|
|
|
|
const webpack = require('webpack')
|
|
|
|
const chokidar = require('chokidar')
|
|
|
|
|
2018-01-27 22:39:55 +00:00
|
|
|
global.WPCONFIG = require('./dev/webpack/webpack.dev.js')
|
2018-01-27 05:20:49 +00:00
|
|
|
global.DEV = true
|
2018-01-27 22:39:55 +00:00
|
|
|
global.WP = webpack(global.WPCONFIG)
|
|
|
|
require('./server')
|
2018-01-27 05:20:49 +00:00
|
|
|
|
2018-01-27 22:39:55 +00:00
|
|
|
const devWatcher = chokidar.watch('./server')
|
|
|
|
devWatcher.on('ready', () => {
|
|
|
|
devWatcher.on('all', () => {
|
|
|
|
console.warn('--- >>>>>>>>>>>>>>>>>>>>>>>>>>>> ---')
|
|
|
|
console.warn('--- Changes detected: Restarting ---')
|
|
|
|
console.warn('--- <<<<<<<<<<<<<<<<<<<<<<<<<<<< ---')
|
|
|
|
global.wiki.server.destroy(() => {
|
|
|
|
global.wiki = {}
|
|
|
|
for (const workerId in cluster.workers) {
|
|
|
|
cluster.workers[workerId].kill()
|
|
|
|
}
|
|
|
|
Object.keys(require.cache).forEach(function(id) {
|
|
|
|
if (/[/\\]server[/\\]/.test(id)) delete require.cache[id]
|
2018-01-27 05:20:49 +00:00
|
|
|
})
|
2018-01-27 22:39:55 +00:00
|
|
|
require('./server')
|
2018-01-27 05:20:49 +00:00
|
|
|
})
|
2018-01-27 22:39:55 +00:00
|
|
|
})
|
2018-01-27 05:20:49 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
require('./server')
|
|
|
|
}
|
2017-10-08 02:44:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 00:23:42 +00:00
|
|
|
|
|
|
|
require('yargs') // eslint-disable-line no-unused-expressions
|
|
|
|
.usage('Usage: node $0 <cmd> [args]')
|
|
|
|
.command({
|
|
|
|
command: 'start',
|
|
|
|
alias: ['boot', 'init'],
|
|
|
|
desc: 'Start Wiki.js process',
|
|
|
|
handler: argv => {
|
2017-10-08 02:44:35 +00:00
|
|
|
init.start()
|
2017-04-18 02:44:04 +00:00
|
|
|
}
|
2017-02-14 07:17:25 +00:00
|
|
|
})
|
2017-04-19 00:23:42 +00:00
|
|
|
.command({
|
|
|
|
command: 'stop',
|
|
|
|
alias: ['quit', 'exit'],
|
|
|
|
desc: 'Stop Wiki.js process',
|
|
|
|
handler: argv => {
|
|
|
|
init.stop()
|
|
|
|
}
|
2017-02-14 07:17:25 +00:00
|
|
|
})
|
2017-04-19 00:23:42 +00:00
|
|
|
.command({
|
|
|
|
command: 'restart',
|
|
|
|
alias: ['reload'],
|
|
|
|
desc: 'Restart Wiki.js process',
|
|
|
|
handler: argv => {
|
|
|
|
init.restart()
|
|
|
|
}
|
2017-02-26 23:06:20 +00:00
|
|
|
})
|
2018-01-27 05:20:49 +00:00
|
|
|
.command({
|
|
|
|
command: 'dev',
|
|
|
|
desc: 'Start in Developer Mode',
|
|
|
|
handler: argv => {
|
|
|
|
init.dev()
|
|
|
|
}
|
|
|
|
})
|
2017-04-19 00:23:42 +00:00
|
|
|
.recommendCommands()
|
|
|
|
.demandCommand(1, 'You must provide one of the accepted commands above.')
|
|
|
|
.help()
|
|
|
|
.version()
|
2017-10-08 02:44:35 +00:00
|
|
|
.epilogue('Read the docs at https://docs.requarks.io/wiki')
|
2017-04-19 00:23:42 +00:00
|
|
|
.argv
|