install.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict'
  2. const ora = require('ora')('Initializing...').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. /**
  14. * Fetch version from npm package
  15. */
  16. fs.readJsonAsync('package.json').then((packageObj) => {
  17. let remoteURL = _.replace('https://github.com/Requarks/wiki/releases/download/v{0}/wiki-js.tar.gz', '{0}', packageObj.version)
  18. return new Promise((resolve, reject) => {
  19. /**
  20. * Fetch tarball
  21. */
  22. ora.text = 'Looking for latest release...'
  23. https.get(remoteURL, resp => {
  24. if (resp.statusCode !== 200) {
  25. return reject(new Error('Remote file not found'))
  26. }
  27. ora.text = 'Install tarball found. Downloading...'
  28. /**
  29. * Extract tarball
  30. */
  31. resp.pipe(zlib.createGunzip())
  32. .pipe(tar.Extract({ path: installDir }))
  33. .on('error', err => reject(err))
  34. .on('end', () => {
  35. ora.text = 'Tarball extracted successfully.'
  36. resolve(true)
  37. })
  38. })
  39. })
  40. }).then(() => {
  41. return new Promise((resolve, reject) => {
  42. ora.text = 'Installing Wiki.js npm dependencies...'
  43. let npmInstallProc = exec('npm install --only=production', {
  44. cwd: installDir
  45. })
  46. npmInstallProc.stdout.pipe(process.stdout)
  47. npmInstallProc.on('error', err => {
  48. reject(err)
  49. })
  50. .on('exit', () => {
  51. ora.text = 'Wiki.js npm dependencies installed successfully.'
  52. resolve(true)
  53. })
  54. })
  55. }).then(() => {
  56. fs.accessAsync(path.join(installDir, 'config.yml')).then(() => {
  57. /**
  58. * Upgrade mode
  59. */
  60. ora.text = 'Upgrade succeeded. Reloading Wiki.js...'
  61. return pm2.connectAsync().then(() => {
  62. return pm2.restartAsync('wiki').catch(err => { // eslint-disable-line handle-callback-err
  63. return new Error('Unable to restart Wiki.js via pm2... Do a manual restart!')
  64. }).then(() => {
  65. ora.succeed('Wiki.js has restarted. Upgrade completed.')
  66. })
  67. }).catch(err => {
  68. ora.fail(err)
  69. }).finally(() => {
  70. pm2.disconnect()
  71. })
  72. }).catch(err => {
  73. /**
  74. * Install mode
  75. */
  76. if (err.code === 'ENOENT') {
  77. ora.text = 'First-time install, creating a new config.yml...'
  78. return fs.copyAsync(path.join(installDir, 'config.sample.yml'), path.join(installDir, 'config.yml')).then(() => {
  79. ora.succeed('Installation succeeded. You can now continue with the configuration steps. Check out https://docs.wiki.requarks.io/install for more info.')
  80. })
  81. } else {
  82. return err
  83. }
  84. })
  85. }).catch(err => {
  86. ora.fail(err)
  87. })