mirror of https://github.com/requarks/wiki
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.
137 lines
4.1 KiB
137 lines
4.1 KiB
8 years ago
|
'use strict'
|
||
|
|
||
8 years ago
|
/* global winston */
|
||
|
|
||
8 years ago
|
const Promise = require('bluebird')
|
||
8 years ago
|
const crypto = require('crypto')
|
||
8 years ago
|
const fs = Promise.promisifyAll(require('fs-extra'))
|
||
8 years ago
|
const https = require('follow-redirects').https
|
||
|
const klaw = require('klaw')
|
||
8 years ago
|
const path = require('path')
|
||
8 years ago
|
const pm2 = Promise.promisifyAll(require('pm2'))
|
||
8 years ago
|
const tar = require('tar')
|
||
8 years ago
|
const through2 = require('through2')
|
||
8 years ago
|
const zlib = require('zlib')
|
||
|
const _ = require('lodash')
|
||
|
|
||
|
module.exports = {
|
||
|
|
||
|
_remoteFile: 'https://github.com/Requarks/wiki/releases/download/{0}/wiki-js.tar.gz',
|
||
|
_installDir: '',
|
||
|
|
||
8 years ago
|
/**
|
||
|
* Install a version of Wiki.js
|
||
|
*
|
||
|
* @param {any} targetTag The version to install
|
||
|
* @returns {Promise} Promise of the operation
|
||
|
*/
|
||
8 years ago
|
install (targetTag) {
|
||
|
let self = this
|
||
|
|
||
|
self._installDir = path.resolve(ROOTPATH, appconfig.paths.data, 'install')
|
||
|
|
||
|
return fs.ensureDirAsync(self._installDir).then(() => {
|
||
8 years ago
|
return fs.emptyDirAsync(self._installDir)
|
||
|
}).then(() => {
|
||
8 years ago
|
let remoteURL = _.replace(self._remoteFile, '{0}', targetTag)
|
||
|
|
||
|
return new Promise((resolve, reject) => {
|
||
8 years ago
|
/**
|
||
|
* Fetch tarball and extract to temporary folder
|
||
|
*/
|
||
8 years ago
|
https.get(remoteURL, resp => {
|
||
|
if (resp.statusCode !== 200) {
|
||
|
return reject(new Error('Remote file not found'))
|
||
|
}
|
||
8 years ago
|
winston.info('[SERVER.System] Install tarball found. Downloading...')
|
||
8 years ago
|
|
||
|
resp.pipe(zlib.createGunzip())
|
||
8 years ago
|
.pipe(tar.Extract({ path: self._installDir }))
|
||
8 years ago
|
.on('error', err => reject(err))
|
||
|
.on('end', () => {
|
||
8 years ago
|
winston.info('[SERVER.System] Tarball extracted. Comparing files...')
|
||
|
/**
|
||
|
* Replace old files
|
||
|
*/
|
||
|
klaw(self._installDir)
|
||
|
.on('error', err => reject(err))
|
||
|
.on('end', () => {
|
||
|
winston.info('[SERVER.System] All files were updated successfully.')
|
||
|
resolve(true)
|
||
|
})
|
||
|
.pipe(self.replaceFile())
|
||
8 years ago
|
})
|
||
8 years ago
|
})
|
||
8 years ago
|
})
|
||
|
}).then(() => {
|
||
|
winston.info('[SERVER.System] Cleaning install leftovers...')
|
||
|
return fs.removeAsync(self._installDir).then(() => {
|
||
|
winston.info('[SERVER.System] Restarting Wiki.js...')
|
||
8 years ago
|
return pm2.restartAsync('wiki').catch(err => { // eslint-disable-line handle-callback-err
|
||
8 years ago
|
winston.error('Unable to restart Wiki.js via pm2... Do a manual restart!')
|
||
|
process.exit()
|
||
|
})
|
||
|
})
|
||
|
}).catch(err => {
|
||
|
winston.warn(err)
|
||
|
})
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* Replace file if different
|
||
|
*/
|
||
|
replaceFile () {
|
||
|
let self = this
|
||
|
return through2.obj((item, enc, next) => {
|
||
|
if (!item.stats.isDirectory()) {
|
||
|
self.digestFile(item.path).then(sourceHash => {
|
||
|
let destFilePath = _.replace(item.path, self._installDir, ROOTPATH)
|
||
|
return self.digestFile(destFilePath).then(targetHash => {
|
||
|
if (sourceHash === targetHash) {
|
||
|
winston.log('verbose', '[SERVER.System] Skipping ' + destFilePath)
|
||
|
return fs.removeAsync(item.path).then(() => {
|
||
|
return next() || true
|
||
|
})
|
||
|
} else {
|
||
|
winston.log('verbose', '[SERVER.System] Updating ' + destFilePath + '...')
|
||
|
return fs.moveAsync(item.path, destFilePath, { overwrite: true }).then(() => {
|
||
|
return next() || true
|
||
|
})
|
||
|
}
|
||
|
})
|
||
|
}).catch(err => {
|
||
|
throw err
|
||
|
})
|
||
|
} else {
|
||
|
next()
|
||
|
}
|
||
|
})
|
||
|
},
|
||
8 years ago
|
|
||
8 years ago
|
/**
|
||
|
* Generate the hash of a file
|
||
|
*
|
||
|
* @param {String} filePath The absolute path of the file
|
||
|
* @return {Promise<String>} Promise of the hash result
|
||
|
*/
|
||
|
digestFile: (filePath) => {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
let hash = crypto.createHash('sha1')
|
||
|
hash.setEncoding('hex')
|
||
|
fs.createReadStream(filePath)
|
||
8 years ago
|
.on('error', err => { reject(err) })
|
||
|
.on('end', () => {
|
||
|
hash.end()
|
||
|
resolve(hash.read())
|
||
|
})
|
||
|
.pipe(hash)
|
||
8 years ago
|
}).catch(err => {
|
||
|
if (err.code === 'ENOENT') {
|
||
|
return '0'
|
||
|
} else {
|
||
|
throw err
|
||
|
}
|
||
8 years ago
|
})
|
||
|
}
|
||
|
}
|