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.

101 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. module.exports = {
  8. /**
  9. * Detect the most appropriate start mode
  10. */
  11. startDetect: function () {
  12. if (!process.env.IS_HEROKU) {
  13. return this.startInHerokuMode()
  14. } else {
  15. return this.startInBackgroundMode()
  16. }
  17. },
  18. /**
  19. * Start in background mode
  20. */
  21. startInBackgroundMode: function () {
  22. let spinner = ora('Initializing...').start()
  23. return fs.emptyDirAsync(path.join(__dirname, './logs')).then(() => {
  24. return pm2.connectAsync().then(() => {
  25. return pm2.startAsync({
  26. name: 'wiki',
  27. script: 'server.js',
  28. cwd: __dirname,
  29. output: path.join(__dirname, './logs/wiki-output.log'),
  30. error: path.join(__dirname, './logs/wiki-error.log'),
  31. minUptime: 5000,
  32. maxRestarts: 5
  33. }).then(() => {
  34. spinner.succeed('Wiki.js has started successfully.')
  35. }).finally(() => {
  36. pm2.disconnect()
  37. })
  38. })
  39. }).catch(err => {
  40. spinner.fail(err)
  41. process.exit(1)
  42. })
  43. },
  44. /**
  45. * Start in Heroku mode
  46. */
  47. startInHerokuMode: function () {
  48. let self = this
  49. console.info('Initializing Wiki.js for Heroku...')
  50. let herokuStatePath = path.join(__dirname, './app/heroku.json')
  51. return fs.accessAsync(herokuStatePath).then(() => {
  52. require('./server.js')
  53. }).catch(err => {
  54. if (err.code === 'ENOENT') {
  55. console.info('Wiki.js is not configured yet. Launching configuration wizard...')
  56. self.configure(process.env.PORT)
  57. } else {
  58. console.error(err)
  59. process.exit(1)
  60. }
  61. })
  62. },
  63. /**
  64. * Stop Wiki.js process(es)
  65. */
  66. stop () {
  67. let spinner = ora('Shutting down Wiki.js...').start()
  68. return pm2.connectAsync().then(() => {
  69. return pm2.stopAsync('wiki').then(() => {
  70. spinner.succeed('Wiki.js has stopped successfully.')
  71. }).finally(() => {
  72. pm2.disconnect()
  73. })
  74. }).catch(err => {
  75. spinner.fail(err)
  76. process.exit(1)
  77. })
  78. },
  79. /**
  80. * Restart Wiki.js process(es)
  81. */
  82. restart: function () {
  83. let self = this
  84. return self.stop().delay(1000).then(() => {
  85. self.startDetect()
  86. })
  87. },
  88. /**
  89. * Start the web-based configuration wizard
  90. *
  91. * @param {Number} port Port to bind the HTTP server on
  92. */
  93. configure (port) {
  94. port = port || 3000
  95. let spinner = ora('Initializing interactive setup...').start()
  96. require('./configure')(port, spinner)
  97. }
  98. }