index.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env node
  2. // ===========================================
  3. // Wiki.js DEV UTILITY
  4. // Licensed under AGPLv3
  5. // ===========================================
  6. const _ = require('lodash')
  7. const chalk = require('chalk')
  8. const init = {
  9. dev() {
  10. const webpack = require('webpack')
  11. const chokidar = require('chokidar')
  12. console.info(chalk.yellow.bold('--- ====================== ---'))
  13. console.info(chalk.yellow.bold('--- Wiki.js DEVELOPER MODE ---'))
  14. console.info(chalk.yellow.bold('--- ====================== ---'))
  15. global.DEV = true
  16. global.WP_CONFIG = require('./webpack/webpack.dev.js')
  17. global.WP = webpack(global.WP_CONFIG)
  18. global.WP_DEV = {
  19. devMiddleware: require('webpack-dev-middleware')(global.WP, {
  20. publicPath: global.WP_CONFIG.output.publicPath
  21. }),
  22. hotMiddleware: require('webpack-hot-middleware')(global.WP)
  23. }
  24. global.WP_DEV.devMiddleware.waitUntilValid(() => {
  25. console.info(chalk.yellow.bold('>>> Starting Wiki.js in DEVELOPER mode...'))
  26. require('../server')
  27. process.stdin.setEncoding('utf8')
  28. process.stdin.on('data', data => {
  29. if (_.trim(data) === 'rs') {
  30. console.warn(chalk.yellow.bold('--- >>>>>>>>>>>>>>>>>>>>>>>> ---'))
  31. console.warn(chalk.yellow.bold('--- Manual restart requested ---'))
  32. console.warn(chalk.yellow.bold('--- <<<<<<<<<<<<<<<<<<<<<<<< ---'))
  33. this.reload()
  34. }
  35. })
  36. const devWatcher = chokidar.watch([
  37. './server',
  38. '!./server/views/master.pug'
  39. ], {
  40. cwd: process.cwd(),
  41. ignoreInitial: true,
  42. atomic: 400
  43. })
  44. devWatcher.on('ready', () => {
  45. devWatcher.on('all', _.debounce(() => {
  46. console.warn(chalk.yellow.bold('--- >>>>>>>>>>>>>>>>>>>>>>>>>>>> ---'))
  47. console.warn(chalk.yellow.bold('--- Changes detected: Restarting ---'))
  48. console.warn(chalk.yellow.bold('--- <<<<<<<<<<<<<<<<<<<<<<<<<<<< ---'))
  49. this.reload()
  50. }, 500))
  51. })
  52. })
  53. },
  54. async reload() {
  55. console.warn(chalk.yellow('--- Gracefully stopping server...'))
  56. await global.WIKI.kernel.shutdown(true)
  57. console.warn(chalk.yellow('--- Purging node modules cache...'))
  58. global.WIKI = {}
  59. Object.keys(require.cache).forEach(id => {
  60. if (/[/\\]server[/\\]/.test(id)) {
  61. delete require.cache[id]
  62. }
  63. })
  64. Object.keys(module.constructor._pathCache).forEach(cacheKey => {
  65. if (/[/\\]server[/\\]/.test(cacheKey)) {
  66. delete module.constructor._pathCache[cacheKey]
  67. }
  68. })
  69. console.warn(chalk.yellow('--- Unregistering process listeners...'))
  70. process.removeAllListeners('unhandledRejection')
  71. process.removeAllListeners('uncaughtException')
  72. require('../server')
  73. }
  74. }
  75. init.dev()