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.

103 lines
2.6 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.IS_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. let self = this
  50. console.info('Initializing Wiki.js for Heroku...')
  51. let herokuStatePath = path.join(__dirname, './app/heroku.json')
  52. return fs.accessAsync(herokuStatePath).then(() => {
  53. require('./server.js')
  54. }).catch(err => {
  55. if (err.code === 'ENOENT') {
  56. console.info('Wiki.js is not configured yet. Launching configuration wizard...')
  57. self.configure(process.env.PORT)
  58. } else {
  59. console.error(err)
  60. process.exit(1)
  61. }
  62. })
  63. },
  64. /**
  65. * Stop Wiki.js process(es)
  66. */
  67. stop () {
  68. let spinner = ora('Shutting down Wiki.js...').start()
  69. return pm2.connectAsync().then(() => {
  70. return pm2.stopAsync('wiki').then(() => {
  71. spinner.succeed('Wiki.js has stopped successfully.')
  72. }).finally(() => {
  73. pm2.disconnect()
  74. })
  75. }).catch(err => {
  76. spinner.fail(err)
  77. process.exit(1)
  78. })
  79. },
  80. /**
  81. * Restart Wiki.js process(es)
  82. */
  83. restart: function () {
  84. let self = this
  85. return self.stop().delay(1000).then(() => {
  86. self.startDetect()
  87. })
  88. },
  89. /**
  90. * Start the web-based configuration wizard
  91. *
  92. * @param {Number} port Port to bind the HTTP server on
  93. */
  94. configure (port) {
  95. port = port || 3000
  96. let spinner = ora('Initializing interactive setup...').start()
  97. require('./configure')(port, spinner)
  98. }
  99. }