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.

200 lines
7.1 KiB

7 years ago
7 years ago
7 years ago
  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. const os = require('os')
  14. let installDir = path.resolve(__dirname, '../..')
  15. console.info(colors.yellow(
  16. ' __ __ _ _ _ _ \n' +
  17. '/ / /\\ \\ (_) | _(_) (_)___ \n' +
  18. '\\ \\/ \\/ / | |/ / | | / __| \n' +
  19. ' \\ /\\ /| | <| |_ | \\__ \\ \n' +
  20. ' \\/ \\/ |_|_|\\_\\_(_)/ |___/ \n' +
  21. ' |__/\n'))
  22. var ora = require('ora')({ text: 'Initializing...', spinner: 'dots12' }).start()
  23. ora.text = 'Looking for running instances...'
  24. pm2.connectAsync().then(() => {
  25. /**
  26. * Stop and delete existing instances
  27. */
  28. return pm2.describeAsync('wiki').then(() => {
  29. ora.text = 'Stopping and deleting process from pm2...'
  30. return pm2.deleteAsync('wiki')
  31. }).catch(err => { // eslint-disable-line handle-callback-err
  32. return true
  33. })
  34. }).then(() => {
  35. /**
  36. * Check for sufficient memory
  37. */
  38. if (os.totalmem() < 1024 * 1024 * 768) {
  39. throw new Error('Not enough memory to install dependencies. Minimum is 768 MB.')
  40. }
  41. return true
  42. }).then(() => {
  43. /**
  44. * Install via local tarball if present
  45. */
  46. let skipHttp = true
  47. let tbPath = path.join(installDir, 'wiki-js.tar.gz')
  48. return fs.accessAsync(tbPath)
  49. .catch(err => { // eslint-disable-line handle-callback-err
  50. skipHttp = false
  51. }).then(() => {
  52. if (skipHttp) {
  53. ora.text = 'Local tarball found. Extracting...'
  54. return new Promise((resolve, reject) => {
  55. fs.createReadStream(tbPath).pipe(zlib.createGunzip())
  56. .pipe(tar.Extract({ path: installDir }))
  57. .on('error', err => reject(err))
  58. .on('end', () => {
  59. ora.text = 'Tarball extracted successfully.'
  60. resolve(true)
  61. })
  62. })
  63. } else {
  64. return false
  65. }
  66. })
  67. }).then((skipHttp) => {
  68. /**
  69. * Install via remote tarball
  70. */
  71. if (skipHttp) { return true }
  72. /**
  73. * Fetch version from npm package
  74. */
  75. return fs.readJsonAsync('package.json').then((packageObj) => {
  76. let versionGet = _.chain(packageObj.version).split('.').take(4).join('.')
  77. let remoteURL = _.replace('https://github.com/Requarks/wiki/releases/download/v{0}/wiki-js.tar.gz', '{0}', versionGet)
  78. return new Promise((resolve, reject) => {
  79. /**
  80. * Fetch tarball
  81. */
  82. ora.text = 'Looking for latest release...'
  83. https.get(remoteURL, resp => {
  84. if (resp.statusCode !== 200) {
  85. return reject(new Error('Remote file not found'))
  86. }
  87. ora.text = 'Install tarball found. Downloading...'
  88. /**
  89. * Extract tarball
  90. */
  91. resp.pipe(zlib.createGunzip())
  92. .pipe(tar.Extract({ path: installDir }))
  93. .on('error', err => reject(err))
  94. .on('end', () => {
  95. ora.text = 'Tarball extracted successfully.'
  96. resolve(true)
  97. })
  98. })
  99. })
  100. })
  101. }).then(() => {
  102. ora.text = 'Installing Wiki.js npm dependencies...'
  103. return exec.stdout('npm', ['install', '--only=production', '--no-optional'], {
  104. cwd: installDir
  105. }).then(results => {
  106. ora.text = 'Wiki.js npm dependencies installed successfully.'
  107. return true
  108. })
  109. }).then(() => {
  110. fs.accessAsync(path.join(installDir, 'config.yml')).then(() => {
  111. /**
  112. * Upgrade mode
  113. */
  114. ora.succeed('Upgrade completed.')
  115. console.info(colors.yellow('\n!!! IMPORTANT !!!'))
  116. 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.'))
  117. 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'))
  118. return false
  119. }).catch(err => {
  120. /**
  121. * Install mode
  122. */
  123. if (err.code === 'ENOENT') {
  124. ora.text = 'First-time install, creating a new config.yml...'
  125. return fs.copyAsync(path.join(installDir, 'config.sample.yml'), path.join(installDir, 'config.yml')).then(() => {
  126. ora.succeed('Installation succeeded.')
  127. return true
  128. })
  129. } else {
  130. return err
  131. }
  132. }).then((isNewInstall) => {
  133. if (process.stdout.isTTY) {
  134. inquirer.prompt([{
  135. type: 'list',
  136. name: 'action',
  137. message: 'Continue with configuration wizard?',
  138. default: 'default',
  139. choices: [
  140. { name: 'Yes, run configuration wizard on port 3000 (recommended)', value: 'default', short: 'Yes' },
  141. { name: 'Yes, run configuration wizard on a custom port...', value: 'custom', short: 'Yes' },
  142. { name: 'No, I\'ll configure the config file manually', value: 'exit', short: 'No' }
  143. ]
  144. }, {
  145. type: 'input',
  146. name: 'customport',
  147. message: 'Custom port to use:',
  148. default: 3000,
  149. validate: (val) => {
  150. val = _.toNumber(val)
  151. return (_.isNumber(val) && val > 0) ? true : 'Invalid Port!'
  152. },
  153. when: (ans) => {
  154. return ans.action === 'custom'
  155. }
  156. }]).then((ans) => {
  157. switch (ans.action) {
  158. case 'default':
  159. 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!)'))
  160. ora = require('ora')({ text: 'I\'ll wait until you\'re done ;)', color: 'yellow', spinner: 'pong' }).start()
  161. return exec.stdout('node', ['wiki', 'configure'], {
  162. cwd: installDir
  163. })
  164. case 'custom':
  165. 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!)'))
  166. ora = require('ora')({ text: 'I\'ll wait until you\'re done ;)', color: 'yellow', spinner: 'pong' }).start()
  167. return exec.stdout('node', ['wiki', 'configure', ans.customport], {
  168. cwd: installDir
  169. })
  170. default:
  171. console.info(colors.bold.cyan('\n> You can run the configuration wizard using command:') + colors.bold.white(' node wiki configure') + colors.bold.cyan('.\n> Then start Wiki.js using command: ') + colors.bold.white('node wiki start'))
  172. return Promise.delay(7000).then(() => {
  173. process.exit(0)
  174. })
  175. }
  176. }).then(() => {
  177. ora.succeed(colors.bold.green('Wiki.js has been configured successfully. It is now starting up and should be accessible very soon!'))
  178. return Promise.delay(3000).then(() => {
  179. console.info('npm is finishing... please wait...')
  180. })
  181. })
  182. } else {
  183. console.info(colors.cyan('[WARNING] Non-interactive terminal detected. You must manually start the configuration wizard using the command: node wiki configure'))
  184. }
  185. })
  186. }).catch(err => {
  187. ora.fail(err)
  188. }).finally(() => {
  189. pm2.disconnect()
  190. })