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.

91 lines
2.8 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. ignoreInitial: true,
  39. atomic: 400
  40. })
  41. devWatcher.on('ready', () => {
  42. devWatcher.on('all', () => {
  43. console.warn(chalk.yellow.bold('--- >>>>>>>>>>>>>>>>>>>>>>>>>>>> ---'))
  44. console.warn(chalk.yellow.bold('--- Changes detected: Restarting ---'))
  45. console.warn(chalk.yellow.bold('--- <<<<<<<<<<<<<<<<<<<<<<<<<<<< ---'))
  46. this.reload()
  47. })
  48. })
  49. })
  50. },
  51. async reload() {
  52. console.warn(chalk.yellow('--- Stopping scheduled jobs...'))
  53. if (global.WIKI.scheduler) {
  54. global.WIKI.scheduler.stop()
  55. }
  56. console.warn(chalk.yellow('--- Closing DB connections...'))
  57. await global.WIKI.models.knex.destroy()
  58. console.warn(chalk.yellow('--- Closing Server connections...'))
  59. if (global.WIKI.server) {
  60. await new Promise((resolve, reject) => global.WIKI.server.destroy(resolve))
  61. }
  62. console.warn(chalk.yellow('--- Purging node modules cache...'))
  63. global.WIKI = {}
  64. Object.keys(require.cache).forEach(id => {
  65. if (/[/\\]server[/\\]/.test(id)) {
  66. delete require.cache[id]
  67. }
  68. })
  69. Object.keys(module.constructor._pathCache).forEach(cacheKey => {
  70. if (/[/\\]server[/\\]/.test(cacheKey)) {
  71. delete module.constructor._pathCache[cacheKey]
  72. }
  73. })
  74. console.warn(chalk.yellow('--- Unregistering process listeners...'))
  75. process.removeAllListeners('unhandledRejection')
  76. process.removeAllListeners('uncaughtException')
  77. require('../server')
  78. }
  79. }
  80. init.dev()