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.

107 lines
3.3 KiB

  1. 'use strict'
  2. const ora = require('ora')({ text: 'Initializing...', spinner: 'dots12' }).start()
  3. const Promise = require('bluebird')
  4. const exec = require('child_process').exec
  5. const fs = Promise.promisifyAll(require('fs-extra'))
  6. const https = require('follow-redirects').https
  7. const path = require('path')
  8. const pm2 = Promise.promisifyAll(require('pm2'))
  9. const tar = require('tar')
  10. const zlib = require('zlib')
  11. const _ = require('lodash')
  12. let installDir = path.resolve(__dirname, '../..')
  13. ora.text = 'Looking for running instances...'
  14. pm2.connectAsync().then(() => {
  15. return pm2.describeAsync('wiki').then(() => {
  16. ora.text = 'Stopping and deleting process from pm2...'
  17. return pm2.deleteAsync('wiki')
  18. }).catch(err => { // eslint-disable-line handle-callback-err
  19. return true
  20. })
  21. }).then(() => {
  22. /**
  23. * Fetch version from npm package
  24. */
  25. return fs.readJsonAsync('package.json').then((packageObj) => {
  26. let versionGet = _.chain(packageObj.version).split('.').take(4).join('.')
  27. let remoteURL = _.replace('https://github.com/Requarks/wiki/releases/download/v{0}/wiki-js.tar.gz', '{0}', versionGet)
  28. return new Promise((resolve, reject) => {
  29. /**
  30. * Fetch tarball
  31. */
  32. ora.text = 'Looking for latest release...'
  33. https.get(remoteURL, resp => {
  34. if (resp.statusCode !== 200) {
  35. return reject(new Error('Remote file not found'))
  36. }
  37. ora.text = 'Install tarball found. Downloading...'
  38. /**
  39. * Extract tarball
  40. */
  41. resp.pipe(zlib.createGunzip())
  42. .pipe(tar.Extract({ path: installDir }))
  43. .on('error', err => reject(err))
  44. .on('end', () => {
  45. ora.text = 'Tarball extracted successfully.'
  46. resolve(true)
  47. })
  48. })
  49. })
  50. })
  51. }).then(() => {
  52. return new Promise((resolve, reject) => {
  53. ora.text = 'Installing Wiki.js npm dependencies...'
  54. let npmInstallProc = exec('npm install --only=production --no-optional', {
  55. cwd: installDir
  56. })
  57. npmInstallProc.stdout.pipe(process.stdout)
  58. npmInstallProc.on('error', err => {
  59. reject(err)
  60. })
  61. .on('exit', () => {
  62. ora.text = 'Wiki.js npm dependencies installed successfully.'
  63. resolve(true)
  64. })
  65. })
  66. }).then(() => {
  67. fs.accessAsync(path.join(installDir, 'config.yml')).then(() => {
  68. /**
  69. * Upgrade mode
  70. */
  71. return new Promise((resolve, reject) => {
  72. ora.text = 'Upgrade succeeded. Starting Wiki.js...'
  73. let npmInstallProc = exec('node wiki start', {
  74. cwd: installDir
  75. })
  76. npmInstallProc.stdout.pipe(process.stdout)
  77. npmInstallProc.on('error', err => {
  78. reject(err)
  79. })
  80. .on('exit', () => {
  81. ora.succeed('Wiki.js has started. Upgrade completed.')
  82. resolve(true)
  83. })
  84. })
  85. }).catch(err => {
  86. /**
  87. * Install mode
  88. */
  89. if (err.code === 'ENOENT') {
  90. ora.text = 'First-time install, creating a new config.yml...'
  91. return fs.copyAsync(path.join(installDir, 'config.sample.yml'), path.join(installDir, 'config.yml')).then(() => {
  92. ora.succeed('Installation succeeded. You can now continue with the configuration steps. Check out https://docs.wiki.requarks.io/install for more info.')
  93. })
  94. } else {
  95. return err
  96. }
  97. })
  98. }).catch(err => {
  99. ora.fail(err)
  100. }).finally(() => {
  101. pm2.disconnect()
  102. })