2019-01-28 06:27:04 +00:00
|
|
|
const path = require('path')
|
|
|
|
const sgit = require('simple-git/promise')
|
|
|
|
const fs = require('fs-extra')
|
|
|
|
const _ = require('lodash')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get file extension based on content type
|
|
|
|
*/
|
|
|
|
const getFileExtension = (contentType) => {
|
|
|
|
switch (contentType) {
|
|
|
|
case 'markdown':
|
|
|
|
return 'md'
|
|
|
|
case 'html':
|
|
|
|
return 'html'
|
|
|
|
default:
|
|
|
|
return 'txt'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 05:12:43 +00:00
|
|
|
module.exports = {
|
2019-02-03 22:08:06 +00:00
|
|
|
git: null,
|
|
|
|
repoPath: path.join(process.cwd(), 'data/repo'),
|
2018-07-22 20:25:39 +00:00
|
|
|
async activated() {
|
2019-02-03 22:08:06 +00:00
|
|
|
// not used
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2018-07-22 20:25:39 +00:00
|
|
|
async deactivated() {
|
2019-02-03 22:08:06 +00:00
|
|
|
// not used
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2018-07-22 20:25:39 +00:00
|
|
|
async init() {
|
2019-02-03 07:48:30 +00:00
|
|
|
WIKI.logger.info('(STORAGE/GIT) Initializing...')
|
2019-02-03 22:08:06 +00:00
|
|
|
this.repoPath = path.resolve(WIKI.ROOTPATH, this.config.localRepoPath)
|
|
|
|
await fs.ensureDir(this.repoPath)
|
|
|
|
this.git = sgit(this.repoPath)
|
2019-01-28 06:27:04 +00:00
|
|
|
|
|
|
|
// Initialize repo (if needed)
|
2019-02-03 07:48:30 +00:00
|
|
|
WIKI.logger.info('(STORAGE/GIT) Checking repository state...')
|
2019-02-03 22:08:06 +00:00
|
|
|
const isRepo = await this.git.checkIsRepo()
|
2019-01-28 06:27:04 +00:00
|
|
|
if (!isRepo) {
|
2019-02-03 07:48:30 +00:00
|
|
|
WIKI.logger.info('(STORAGE/GIT) Initializing local repository...')
|
2019-02-03 22:08:06 +00:00
|
|
|
await this.git.init()
|
2019-01-28 06:27:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 07:48:30 +00:00
|
|
|
// Set default author
|
2019-02-03 22:08:06 +00:00
|
|
|
await this.git.raw(['config', '--local', 'user.email', this.config.defaultEmail])
|
|
|
|
await this.git.raw(['config', '--local', 'user.name', this.config.defaultName])
|
2019-02-03 07:48:30 +00:00
|
|
|
|
|
|
|
// Purge existing remotes
|
|
|
|
WIKI.logger.info('(STORAGE/GIT) Listing existing remotes...')
|
2019-02-03 22:08:06 +00:00
|
|
|
const remotes = await this.git.getRemotes()
|
2019-02-03 07:48:30 +00:00
|
|
|
if (remotes.length > 0) {
|
|
|
|
WIKI.logger.info('(STORAGE/GIT) Purging existing remotes...')
|
|
|
|
for(let remote of remotes) {
|
2019-02-03 22:08:06 +00:00
|
|
|
await this.git.removeRemote(remote.name)
|
2019-02-03 07:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-08 05:12:43 +00:00
|
|
|
|
2019-01-28 06:27:04 +00:00
|
|
|
// Add remote
|
2019-02-03 07:48:30 +00:00
|
|
|
WIKI.logger.info('(STORAGE/GIT) Setting SSL Verification config...')
|
2019-02-03 22:08:06 +00:00
|
|
|
await this.git.raw(['config', '--local', '--bool', 'http.sslVerify', _.toString(this.config.verifySSL)])
|
2019-01-28 06:27:04 +00:00
|
|
|
switch (this.config.authType) {
|
|
|
|
case 'ssh':
|
2019-02-03 07:48:30 +00:00
|
|
|
WIKI.logger.info('(STORAGE/GIT) Setting SSH Command config...')
|
2019-02-03 22:08:06 +00:00
|
|
|
await this.git.addConfig('core.sshCommand', `ssh -i "${this.config.sshPrivateKeyPath}" -o StrictHostKeyChecking=no`)
|
2019-02-03 07:48:30 +00:00
|
|
|
WIKI.logger.info('(STORAGE/GIT) Adding origin remote via SSH...')
|
2019-02-03 22:08:06 +00:00
|
|
|
await this.git.addRemote('origin', this.config.repoUrl)
|
2019-01-28 06:27:04 +00:00
|
|
|
break
|
|
|
|
default:
|
2019-02-03 07:48:30 +00:00
|
|
|
WIKI.logger.info('(STORAGE/GIT) Adding origin remote via HTTPS...')
|
2019-02-03 22:08:06 +00:00
|
|
|
await this.git.addRemote('origin', `https://${this.config.basicUsername}:${this.config.basicPassword}@${this.config.repoUrl}`)
|
2019-01-28 06:27:04 +00:00
|
|
|
break
|
|
|
|
}
|
2019-02-03 07:48:30 +00:00
|
|
|
|
|
|
|
// Fetch updates for remote
|
|
|
|
WIKI.logger.info('(STORAGE/GIT) Fetch updates from remote...')
|
2019-02-03 22:08:06 +00:00
|
|
|
await this.git.raw(['remote', 'update', 'origin'])
|
2019-02-03 07:48:30 +00:00
|
|
|
|
|
|
|
// Checkout branch
|
2019-02-03 22:08:06 +00:00
|
|
|
const branches = await this.git.branch()
|
2019-02-03 07:48:30 +00:00
|
|
|
if (!_.includes(branches.all, this.config.branch) && !_.includes(branches.all, `remotes/origin/${this.config.branch}`)) {
|
|
|
|
throw new Error('Invalid branch! Make sure it exists on the remote first.')
|
|
|
|
}
|
|
|
|
WIKI.logger.info(`(STORAGE/GIT) Checking out branch ${this.config.branch}...`)
|
2019-02-03 22:08:06 +00:00
|
|
|
await this.git.checkout(this.config.branch)
|
|
|
|
|
|
|
|
// Perform initial sync
|
|
|
|
await this.sync()
|
2019-02-03 07:48:30 +00:00
|
|
|
|
2019-02-03 22:08:06 +00:00
|
|
|
WIKI.logger.info('(STORAGE/GIT) Initialization completed.')
|
|
|
|
},
|
|
|
|
async sync() {
|
2019-02-03 07:48:30 +00:00
|
|
|
// Pull rebase
|
|
|
|
if (_.includes(['sync', 'pull'], this.mode)) {
|
|
|
|
WIKI.logger.info(`(STORAGE/GIT) Performing pull rebase from origin on branch ${this.config.branch}...`)
|
2019-02-03 22:08:06 +00:00
|
|
|
await this.git.pull('origin', this.config.branch, ['--rebase'])
|
2019-02-03 07:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push
|
|
|
|
if (_.includes(['sync', 'push'], this.mode)) {
|
|
|
|
WIKI.logger.info(`(STORAGE/GIT) Performing push to origin on branch ${this.config.branch}...`)
|
|
|
|
let pushOpts = ['--signed=if-asked']
|
|
|
|
if (this.mode === 'push') {
|
|
|
|
pushOpts.push('--force')
|
|
|
|
}
|
2019-02-03 22:08:06 +00:00
|
|
|
await this.git.push('origin', this.config.branch, pushOpts)
|
2019-02-03 07:48:30 +00:00
|
|
|
}
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2019-02-03 22:08:06 +00:00
|
|
|
async created(page) {
|
|
|
|
WIKI.logger.info(`(STORAGE/GIT) Committing new file ${page.path}...`)
|
|
|
|
const fileName = `${page.path}.${getFileExtension(page.contentType)}`
|
|
|
|
const filePath = path.join(this.repoPath, fileName)
|
2019-02-10 23:06:03 +00:00
|
|
|
await fs.outputFile(filePath, page.injectMetadata(), 'utf8')
|
2019-02-03 22:08:06 +00:00
|
|
|
|
|
|
|
await this.git.add(`./${fileName}`)
|
|
|
|
await this.git.commit(`docs: create ${page.path}`, fileName, {
|
|
|
|
'--author': `"${page.authorName} <${page.authorEmail}>"`
|
2019-01-28 06:27:04 +00:00
|
|
|
})
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2019-02-03 22:08:06 +00:00
|
|
|
async updated(page) {
|
|
|
|
WIKI.logger.info(`(STORAGE/GIT) Committing updated file ${page.path}...`)
|
|
|
|
const fileName = `${page.path}.${getFileExtension(page.contentType)}`
|
|
|
|
const filePath = path.join(this.repoPath, fileName)
|
2019-02-10 23:06:03 +00:00
|
|
|
await fs.outputFile(filePath, page.injectMetadata(), 'utf8')
|
2019-02-03 22:08:06 +00:00
|
|
|
|
|
|
|
await this.git.add(`./${fileName}`)
|
|
|
|
await this.git.commit(`docs: update ${page.path}`, fileName, {
|
|
|
|
'--author': `"${page.authorName} <${page.authorEmail}>"`
|
2019-01-28 06:27:04 +00:00
|
|
|
})
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2019-02-03 22:08:06 +00:00
|
|
|
async deleted(page) {
|
|
|
|
WIKI.logger.info(`(STORAGE/GIT) Committing removed file ${page.path}...`)
|
|
|
|
const fileName = `${page.path}.${getFileExtension(page.contentType)}`
|
2018-07-08 05:12:43 +00:00
|
|
|
|
2019-02-03 22:08:06 +00:00
|
|
|
await this.git.rm(`./${fileName}`)
|
|
|
|
await this.git.commit(`docs: delete ${page.path}`, fileName, {
|
|
|
|
'--author': `"${page.authorName} <${page.authorEmail}>"`
|
2019-01-28 06:27:04 +00:00
|
|
|
})
|
2018-07-08 05:12:43 +00:00
|
|
|
},
|
2019-02-03 22:08:06 +00:00
|
|
|
async renamed(page) {
|
|
|
|
WIKI.logger.info(`(STORAGE/GIT) Committing file move from ${page.sourcePath} to ${page.destinationPath}...`)
|
|
|
|
const sourceFilePath = `${page.sourcePath}.${getFileExtension(page.contentType)}`
|
|
|
|
const destinationFilePath = `${page.destinationPath}.${getFileExtension(page.contentType)}`
|
|
|
|
|
|
|
|
await this.git.mv(`./${sourceFilePath}`, `./${destinationFilePath}`)
|
|
|
|
await this.git.commit(`docs: rename ${page.sourcePath} to ${destinationFilePath}`, destinationFilePath, {
|
|
|
|
'--author': `"${page.authorName} <${page.authorEmail}>"`
|
2019-01-28 06:27:04 +00:00
|
|
|
})
|
2018-07-08 05:12:43 +00:00
|
|
|
}
|
|
|
|
}
|