install.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict'
  2. const ora = require('ora')('Initializing...').start()
  3. const Promise = require('bluebird')
  4. const fs = Promise.promisifyAll(require('fs-extra'))
  5. const https = require('follow-redirects').https
  6. const path = require('path')
  7. const pm2 = Promise.promisifyAll(require('pm2'))
  8. const tar = require('tar')
  9. const zlib = require('zlib')
  10. const _ = require('lodash')
  11. let installDir = path.resolve(__dirname, '../..')
  12. /**
  13. * Fetch version from npm package
  14. */
  15. fs.readJsonAsync('package.json').then((packageObj) => {
  16. let remoteURL = _.replace('https://github.com/Requarks/wiki/releases/download/v{0}/wiki-js.tar.gz', '{0}', packageObj.version)
  17. return new Promise((resolve, reject) => {
  18. /**
  19. * Fetch tarball
  20. */
  21. ora.text = 'Looking for latest release...'
  22. https.get(remoteURL, resp => {
  23. if (resp.statusCode !== 200) {
  24. return reject(new Error('Remote file not found'))
  25. }
  26. ora.text = 'Install tarball found. Downloading...'
  27. /**
  28. * Extract tarball
  29. */
  30. resp.pipe(zlib.createGunzip())
  31. .pipe(tar.Extract({ path: installDir }))
  32. .on('error', err => reject(err))
  33. .on('end', () => {
  34. ora.text = 'Tarball extracted successfully.'
  35. resolve(true)
  36. })
  37. })
  38. })
  39. }).then(() => {
  40. fs.accessAsync(path.join(installDir, 'config.yml')).then(() => {
  41. /**
  42. * Upgrade mode
  43. */
  44. ora.text = 'Upgrade succeeded. Reloading Wiki.js...'
  45. return pm2.restartAsync('wiki').catch(err => { // eslint-disable-line handle-callback-err
  46. return new Error('Unable to restart Wiki.js via pm2... Do a manual restart!')
  47. }).then(() => {
  48. ora.succeed('Wiki.js has restarted. Upgrade completed.')
  49. })
  50. }).catch(err => {
  51. /**
  52. * Install mode
  53. */
  54. if (err.code === 'ENOENT') {
  55. ora.succeed('Installation succeeded. You can now continue with the configuration steps. Check out https://docs.wiki.requarks.io/install for more info.')
  56. } else {
  57. return err
  58. }
  59. })
  60. }).catch(err => {
  61. ora.fail(err)
  62. })