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.

153 lines
4.0 KiB

  1. #!/usr/bin/env node
  2. // ===========================================
  3. // Wiki.js
  4. // 2.0
  5. // Licensed under AGPLv3
  6. // ===========================================
  7. const Promise = require('bluebird')
  8. const fs = Promise.promisifyAll(require('fs-extra'))
  9. const pm2 = Promise.promisifyAll(require('pm2'))
  10. const ora = require('ora')
  11. const path = require('path')
  12. const cluster = require('cluster')
  13. const ROOTPATH = process.cwd()
  14. const init = {
  15. /**
  16. * Start in background mode
  17. */
  18. start () {
  19. let spinner = ora('Initializing...').start()
  20. return fs.emptyDirAsync(path.join(ROOTPATH, './logs')).then(() => {
  21. return pm2.connectAsync().then(() => {
  22. return pm2.startAsync({
  23. name: 'wiki',
  24. script: 'server',
  25. cwd: ROOTPATH,
  26. output: path.join(ROOTPATH, './logs/wiki-output.log'),
  27. error: path.join(ROOTPATH, './logs/wiki-error.log'),
  28. minUptime: 5000,
  29. maxRestarts: 5
  30. }).then(() => {
  31. spinner.succeed('Wiki.js has started successfully.')
  32. }).finally(() => {
  33. pm2.disconnect()
  34. })
  35. })
  36. }).catch(err => {
  37. spinner.fail(err)
  38. process.exit(1)
  39. })
  40. },
  41. /**
  42. * Stop Wiki.js process(es)
  43. */
  44. stop () {
  45. let spinner = ora('Shutting down Wiki.js...').start()
  46. return pm2.connectAsync().then(() => {
  47. return pm2.stopAsync('wiki').then(() => {
  48. spinner.succeed('Wiki.js has stopped successfully.')
  49. }).finally(() => {
  50. pm2.disconnect()
  51. })
  52. }).catch(err => {
  53. spinner.fail(err)
  54. process.exit(1)
  55. })
  56. },
  57. /**
  58. * Restart Wiki.js process(es)
  59. */
  60. restart: function () {
  61. return this.stop().delay(1000).then(() => {
  62. this.start()
  63. })
  64. },
  65. dev() {
  66. if (cluster.isMaster) {
  67. const webpack = require('webpack')
  68. const chokidar = require('chokidar')
  69. global.DEV = true
  70. global.WP_CONFIG = require('./dev/webpack/webpack.dev.js')
  71. global.WP = webpack(global.WP_CONFIG)
  72. global.WP_DEV = {
  73. devMiddleware: require('webpack-dev-middleware')(global.WP, {
  74. publicPath: global.WP_CONFIG.output.publicPath
  75. }),
  76. hotMiddleware: require('webpack-hot-middleware')(global.WP)
  77. }
  78. global.WP_DEV.devMiddleware.waitUntilValid(() => {
  79. console.info('>>> Starting Wiki.js in DEVELOPER mode...')
  80. require('./server')
  81. const devWatcher = chokidar.watch([
  82. './server',
  83. '!./server/views/master.pug'
  84. ])
  85. devWatcher.on('ready', () => {
  86. devWatcher.on('all', () => {
  87. console.warn('--- >>>>>>>>>>>>>>>>>>>>>>>>>>>> ---')
  88. console.warn('--- Changes detected: Restarting ---')
  89. console.warn('--- <<<<<<<<<<<<<<<<<<<<<<<<<<<< ---')
  90. global.WIKI.server.destroy(() => {
  91. global.WIKI = {}
  92. for (const workerId in cluster.workers) {
  93. cluster.workers[workerId].kill()
  94. }
  95. Object.keys(require.cache).forEach(function(id) {
  96. if (/[/\\]server[/\\]/.test(id)) delete require.cache[id]
  97. })
  98. require('./server')
  99. })
  100. })
  101. })
  102. })
  103. } else {
  104. require('./server')
  105. }
  106. }
  107. }
  108. require('yargs') // eslint-disable-line no-unused-expressions
  109. .usage('Usage: node $0 <cmd> [args]')
  110. .command({
  111. command: 'start',
  112. alias: ['boot', 'init'],
  113. desc: 'Start Wiki.js process',
  114. handler: argv => {
  115. init.start()
  116. }
  117. })
  118. .command({
  119. command: 'stop',
  120. alias: ['quit', 'exit'],
  121. desc: 'Stop Wiki.js process',
  122. handler: argv => {
  123. init.stop()
  124. }
  125. })
  126. .command({
  127. command: 'restart',
  128. alias: ['reload'],
  129. desc: 'Restart Wiki.js process',
  130. handler: argv => {
  131. init.restart()
  132. }
  133. })
  134. .command({
  135. command: 'dev',
  136. desc: 'Start in Developer Mode',
  137. handler: argv => {
  138. init.dev()
  139. }
  140. })
  141. .recommendCommands()
  142. .demandCommand(1, 'You must provide one of the accepted commands above.')
  143. .help()
  144. .version()
  145. .epilogue('Read the docs at https://docs.requarks.io/wiki')
  146. .argv