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.

90 lines
2.2 KiB

  1. 'use strict'
  2. const Promise = require('bluebird')
  3. const fs = Promise.promisifyAll(require('fs-extra'))
  4. const pm2 = Promise.promisifyAll(require('pm2'))
  5. const ora = require('ora')
  6. const path = require('path')
  7. const ROOTPATH = process.cwd()
  8. module.exports = {
  9. /**
  10. * Detect the most appropriate start mode
  11. */
  12. startDetect: function () {
  13. if (process.env.WIKI_JS_HEROKU) {
  14. return this.startInHerokuMode()
  15. } else {
  16. return this.startInBackgroundMode()
  17. }
  18. },
  19. /**
  20. * Start in background mode
  21. */
  22. startInBackgroundMode: function () {
  23. let spinner = ora('Initializing...').start()
  24. return fs.emptyDirAsync(path.join(ROOTPATH, './logs')).then(() => {
  25. return pm2.connectAsync().then(() => {
  26. return pm2.startAsync({
  27. name: 'wiki',
  28. script: 'server',
  29. cwd: ROOTPATH,
  30. output: path.join(ROOTPATH, './logs/wiki-output.log'),
  31. error: path.join(ROOTPATH, './logs/wiki-error.log'),
  32. minUptime: 5000,
  33. maxRestarts: 5
  34. }).then(() => {
  35. spinner.succeed('Wiki.js has started successfully.')
  36. }).finally(() => {
  37. pm2.disconnect()
  38. })
  39. })
  40. }).catch(err => {
  41. spinner.fail(err)
  42. process.exit(1)
  43. })
  44. },
  45. /**
  46. * Start in Heroku mode
  47. */
  48. startInHerokuMode: function () {
  49. console.warn('Incorrect command on Heroku, use instead: node server')
  50. process.exit(1)
  51. },
  52. /**
  53. * Stop Wiki.js process(es)
  54. */
  55. stop () {
  56. let spinner = ora('Shutting down Wiki.js...').start()
  57. return pm2.connectAsync().then(() => {
  58. return pm2.stopAsync('wiki').then(() => {
  59. spinner.succeed('Wiki.js has stopped successfully.')
  60. }).finally(() => {
  61. pm2.disconnect()
  62. })
  63. }).catch(err => {
  64. spinner.fail(err)
  65. process.exit(1)
  66. })
  67. },
  68. /**
  69. * Restart Wiki.js process(es)
  70. */
  71. restart: function () {
  72. let self = this
  73. return self.stop().delay(1000).then(() => {
  74. self.startDetect()
  75. })
  76. },
  77. /**
  78. * Start the web-based configuration wizard
  79. *
  80. * @param {Number} port Port to bind the HTTP server on
  81. */
  82. configure (port) {
  83. port = port || 3000
  84. let spinner = ora('Initializing interactive setup...').start()
  85. require('./configure')(port, spinner)
  86. }
  87. }