index.js 2.74 KB
Newer Older
NGPixel's avatar
NGPixel committed
1
#!/usr/bin/env node
NGPixel's avatar
NGPixel committed
2

3
// ===========================================
Nick's avatar
Nick committed
4
// Wiki.js DEV UTILITY
5 6 7
// Licensed under AGPLv3
// ===========================================

8 9
const _ = require('lodash')
const chalk = require('chalk')
NGPixel's avatar
NGPixel committed
10 11

const init = {
NGPixel's avatar
NGPixel committed
12
  dev() {
Nick's avatar
Nick committed
13 14
    const webpack = require('webpack')
    const chokidar = require('chokidar')
NGPixel's avatar
NGPixel committed
15

16 17 18 19
    console.info(chalk.yellow.bold('--- ====================== ---'))
    console.info(chalk.yellow.bold('--- Wiki.js DEVELOPER MODE ---'))
    console.info(chalk.yellow.bold('--- ====================== ---'))

Nick's avatar
Nick committed
20
    global.DEV = true
Nick's avatar
Nick committed
21
    global.WP_CONFIG = require('./webpack/webpack.dev.js')
Nick's avatar
Nick committed
22 23 24 25 26 27 28 29 30
    global.WP = webpack(global.WP_CONFIG)
    global.WP_DEV = {
      devMiddleware: require('webpack-dev-middleware')(global.WP, {
        publicPath: global.WP_CONFIG.output.publicPath
      }),
      hotMiddleware: require('webpack-hot-middleware')(global.WP)
    }
    global.WP_DEV.devMiddleware.waitUntilValid(() => {
      console.info(chalk.yellow.bold('>>> Starting Wiki.js in DEVELOPER mode...'))
Nick's avatar
Nick committed
31
      require('../server')
NGPixel's avatar
NGPixel committed
32

Nick's avatar
Nick committed
33 34 35 36 37 38 39 40 41
      process.stdin.setEncoding('utf8')
      process.stdin.on('data', data => {
        if (_.trim(data) === 'rs') {
          console.warn(chalk.yellow.bold('--- >>>>>>>>>>>>>>>>>>>>>>>> ---'))
          console.warn(chalk.yellow.bold('--- Manual restart requested ---'))
          console.warn(chalk.yellow.bold('--- <<<<<<<<<<<<<<<<<<<<<<<< ---'))
          this.reload()
        }
      })
42

Nick's avatar
Nick committed
43 44 45 46
      const devWatcher = chokidar.watch([
        './server',
        '!./server/views/master.pug'
      ], {
47
        cwd: process.cwd(),
Nick's avatar
Nick committed
48 49 50 51
        ignoreInitial: true,
        atomic: 400
      })
      devWatcher.on('ready', () => {
52
        devWatcher.on('all', _.debounce(() => {
Nick's avatar
Nick committed
53 54 55 56
          console.warn(chalk.yellow.bold('--- >>>>>>>>>>>>>>>>>>>>>>>>>>>> ---'))
          console.warn(chalk.yellow.bold('--- Changes detected: Restarting ---'))
          console.warn(chalk.yellow.bold('--- <<<<<<<<<<<<<<<<<<<<<<<<<<<< ---'))
          this.reload()
57
        }, 500))
NGPixel's avatar
NGPixel committed
58
      })
Nick's avatar
Nick committed
59
    })
60 61
  },
  async reload() {
62
    console.warn(chalk.yellow('--- Gracefully stopping server...'))
63
    await global.WIKI.kernel.shutdown(true)
64

Nick's avatar
Nick committed
65
    console.warn(chalk.yellow('--- Purging node modules cache...'))
66

Nick's avatar
Nick committed
67 68 69 70 71 72 73 74 75 76 77
    global.WIKI = {}
    Object.keys(require.cache).forEach(id => {
      if (/[/\\]server[/\\]/.test(id)) {
        delete require.cache[id]
      }
    })
    Object.keys(module.constructor._pathCache).forEach(cacheKey => {
      if (/[/\\]server[/\\]/.test(cacheKey)) {
        delete module.constructor._pathCache[cacheKey]
      }
    })
78

Nick's avatar
Nick committed
79
    console.warn(chalk.yellow('--- Unregistering process listeners...'))
80

Nick's avatar
Nick committed
81 82
    process.removeAllListeners('unhandledRejection')
    process.removeAllListeners('uncaughtException')
83

Nick's avatar
Nick committed
84
    require('../server')
NGPixel's avatar
NGPixel committed
85 86
  }
}
87

Nick's avatar
Nick committed
88
init.dev()