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.

151 lines
5.9 KiB

  1. 'use strict'
  2. const Promise = require('bluebird')
  3. const exec = require('execa')
  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 inquirer = require('inquirer')
  11. const colors = require('colors/safe')
  12. const _ = require('lodash')
  13. let installDir = path.resolve(__dirname, '../..')
  14. console.info(colors.yellow(
  15. ' __ __ _ _ _ _ \n' +
  16. '/ / /\\ \\ (_) | _(_) (_)___ \n' +
  17. '\\ \\/ \\/ / | |/ / | | / __| \n' +
  18. ' \\ /\\ /| | <| |_ | \\__ \\ \n' +
  19. ' \\/ \\/ |_|_|\\_\\_(_)/ |___/ \n' +
  20. ' |__/\n'))
  21. var ora = require('ora')({ text: 'Initializing...', spinner: 'dots12' }).start()
  22. ora.text = 'Looking for running instances...'
  23. pm2.connectAsync().then(() => {
  24. return pm2.describeAsync('wiki').then(() => {
  25. ora.text = 'Stopping and deleting process from pm2...'
  26. return pm2.deleteAsync('wiki')
  27. }).catch(err => { // eslint-disable-line handle-callback-err
  28. return true
  29. })
  30. }).then(() => {
  31. /**
  32. * Fetch version from npm package
  33. */
  34. return fs.readJsonAsync('package.json').then((packageObj) => {
  35. let versionGet = _.chain(packageObj.version).split('.').take(4).join('.')
  36. let remoteURL = _.replace('https://github.com/Requarks/wiki/releases/download/v{0}/wiki-js.tar.gz', '{0}', versionGet)
  37. return new Promise((resolve, reject) => {
  38. /**
  39. * Fetch tarball
  40. */
  41. ora.text = 'Looking for latest release...'
  42. https.get(remoteURL, resp => {
  43. if (resp.statusCode !== 200) {
  44. return reject(new Error('Remote file not found'))
  45. }
  46. ora.text = 'Install tarball found. Downloading...'
  47. /**
  48. * Extract tarball
  49. */
  50. resp.pipe(zlib.createGunzip())
  51. .pipe(tar.Extract({ path: installDir }))
  52. .on('error', err => reject(err))
  53. .on('end', () => {
  54. ora.text = 'Tarball extracted successfully.'
  55. resolve(true)
  56. })
  57. })
  58. })
  59. })
  60. }).then(() => {
  61. ora.text = 'Installing Wiki.js npm dependencies...'
  62. return exec.stdout('npm', ['install', '--only=production', '--no-optional'], {
  63. cwd: installDir
  64. }).then(results => {
  65. ora.text = 'Wiki.js npm dependencies installed successfully.'
  66. return true
  67. })
  68. }).then(() => {
  69. fs.accessAsync(path.join(installDir, 'config.yml')).then(() => {
  70. /**
  71. * Upgrade mode
  72. */
  73. ora.succeed('Upgrade completed.')
  74. console.info(colors.yellow('\n!!! IMPORTANT !!!'))
  75. console.info(colors.yellow('Running the configuration wizard is optional but recommended after an upgrade to ensure your config file is using the latest available settings.'))
  76. console.info(colors.yellow('Note that the contents of your config file will be displayed during the configuration wizard. It is therefor highly recommended to run the wizard on a non-publicly accessible port or skip this step completely.\n'))
  77. return false
  78. }).catch(err => {
  79. /**
  80. * Install mode
  81. */
  82. if (err.code === 'ENOENT') {
  83. ora.text = 'First-time install, creating a new config.yml...'
  84. return fs.copyAsync(path.join(installDir, 'config.sample.yml'), path.join(installDir, 'config.yml')).then(() => {
  85. ora.succeed('Installation succeeded.')
  86. return true
  87. })
  88. } else {
  89. return err
  90. }
  91. }).then((isNewInstall) => {
  92. if (process.stdout.isTTY) {
  93. inquirer.prompt([{
  94. type: 'list',
  95. name: 'action',
  96. message: 'Continue with configuration wizard?',
  97. default: 'default',
  98. choices: [
  99. { name: 'Yes, run configuration wizard on port 3000 (recommended)', value: 'default', short: 'Yes' },
  100. { name: 'Yes, run configuration wizard on a custom port...', value: 'custom', short: 'Yes' },
  101. { name: 'No, I\'ll configure the config file manually', value: 'exit', short: 'No' }
  102. ]
  103. }, {
  104. type: 'input',
  105. name: 'customport',
  106. message: 'Custom port to use:',
  107. default: 3000,
  108. validate: (val) => {
  109. val = _.toNumber(val)
  110. return (_.isNumber(val) && val > 0) ? true : 'Invalid Port!'
  111. },
  112. when: (ans) => {
  113. return ans.action === 'custom'
  114. }
  115. }]).then((ans) => {
  116. switch (ans.action) {
  117. case 'default':
  118. console.info(colors.bold.cyan('> Browse to http://your-server:3000/ to configure your wiki! (Replaced your-server with the hostname or IP of your server!)'))
  119. ora = require('ora')({ text: 'I\'ll wait until you\'re done ;)', color: 'yellow', spinner: 'pong' }).start()
  120. return exec.stdout('node', ['wiki', 'configure'], {
  121. cwd: installDir
  122. })
  123. case 'custom':
  124. console.info(colors.bold.cyan('> Browse to http://your-server:' + ans.customport + '/ to configure your wiki! (Replaced your-server with the hostname or IP of your server!)'))
  125. ora = require('ora')({ text: 'I\'ll wait until you\'re done ;)', color: 'yellow', spinner: 'pong' }).start()
  126. return exec.stdout('node', ['wiki', 'configure', ans.customport], {
  127. cwd: installDir
  128. })
  129. default:
  130. console.info(colors.bold.cyan('> You can run the configuration wizard using command:') + colors.bold.white(' node wiki configure') + colors.bold.cyan('. Then start Wiki.js using command: ') + colors.bold.white('node wiki start'))
  131. return process.exit(0)
  132. }
  133. }).then(() => {
  134. ora.succeed(colors.bold.green('Wiki.js has been configured successfully. It is now starting up and should be accessible very soon!'))
  135. })
  136. } else {
  137. console.info(colors.cyan('[WARNING] Non-interactive terminal detected. You must manually start the configuration wizard using the command: node wiki configure'))
  138. }
  139. })
  140. }).catch(err => {
  141. ora.fail(err)
  142. }).finally(() => {
  143. pm2.disconnect()
  144. })