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.

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