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.

92 lines
2.9 KiB

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