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.

102 lines
2.4 KiB

  1. #!/usr/bin/env node
  2. // ===========================================
  3. // Wiki.js
  4. // 2.0
  5. // Licensed under AGPLv3
  6. // ===========================================
  7. const Promise = require('bluebird')
  8. const fs = Promise.promisifyAll(require('fs-extra'))
  9. const pm2 = Promise.promisifyAll(require('pm2'))
  10. const ora = require('ora')
  11. const path = require('path')
  12. const ROOTPATH = process.cwd()
  13. const init = {
  14. /**
  15. * Start in background mode
  16. */
  17. start () {
  18. let spinner = ora('Initializing...').start()
  19. return fs.emptyDirAsync(path.join(ROOTPATH, './logs')).then(() => {
  20. return pm2.connectAsync().then(() => {
  21. return pm2.startAsync({
  22. name: 'wiki',
  23. script: 'server',
  24. cwd: ROOTPATH,
  25. output: path.join(ROOTPATH, './logs/wiki-output.log'),
  26. error: path.join(ROOTPATH, './logs/wiki-error.log'),
  27. minUptime: 5000,
  28. maxRestarts: 5
  29. }).then(() => {
  30. spinner.succeed('Wiki.js has started successfully.')
  31. }).finally(() => {
  32. pm2.disconnect()
  33. })
  34. })
  35. }).catch(err => {
  36. spinner.fail(err)
  37. process.exit(1)
  38. })
  39. },
  40. /**
  41. * Stop Wiki.js process(es)
  42. */
  43. stop () {
  44. let spinner = ora('Shutting down Wiki.js...').start()
  45. return pm2.connectAsync().then(() => {
  46. return pm2.stopAsync('wiki').then(() => {
  47. spinner.succeed('Wiki.js has stopped successfully.')
  48. }).finally(() => {
  49. pm2.disconnect()
  50. })
  51. }).catch(err => {
  52. spinner.fail(err)
  53. process.exit(1)
  54. })
  55. },
  56. /**
  57. * Restart Wiki.js process(es)
  58. */
  59. restart: function () {
  60. let self = this
  61. return self.stop().delay(1000).then(() => {
  62. self.startDetect()
  63. })
  64. }
  65. }
  66. require('yargs') // eslint-disable-line no-unused-expressions
  67. .usage('Usage: node $0 <cmd> [args]')
  68. .command({
  69. command: 'start',
  70. alias: ['boot', 'init'],
  71. desc: 'Start Wiki.js process',
  72. handler: argv => {
  73. init.start()
  74. }
  75. })
  76. .command({
  77. command: 'stop',
  78. alias: ['quit', 'exit'],
  79. desc: 'Stop Wiki.js process',
  80. handler: argv => {
  81. init.stop()
  82. }
  83. })
  84. .command({
  85. command: 'restart',
  86. alias: ['reload'],
  87. desc: 'Restart Wiki.js process',
  88. handler: argv => {
  89. init.restart()
  90. }
  91. })
  92. .recommendCommands()
  93. .demandCommand(1, 'You must provide one of the accepted commands above.')
  94. .help()
  95. .version()
  96. .epilogue('Read the docs at https://docs.requarks.io/wiki')
  97. .argv