storage.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. const path = require('path')
  2. const sgit = require('simple-git/promise')
  3. const fs = require('fs-extra')
  4. const _ = require('lodash')
  5. /**
  6. * Get file extension based on content type
  7. */
  8. const getFileExtension = (contentType) => {
  9. switch (contentType) {
  10. case 'markdown':
  11. return 'md'
  12. case 'html':
  13. return 'html'
  14. default:
  15. return 'txt'
  16. }
  17. }
  18. /**
  19. * Inject page metadata into contents
  20. */
  21. const injectMetadata = (page) => {
  22. let meta = [
  23. ['title', page.title],
  24. ['description', page.description]
  25. ]
  26. let metaFormatted = ''
  27. switch (page.contentType) {
  28. case 'markdown':
  29. metaFormatted = meta.map(mt => `[//]: # ${mt[0]}: ${mt[1]}`).join('\n')
  30. break
  31. case 'html':
  32. metaFormatted = meta.map(mt => `<!-- ${mt[0]}: ${mt[1]} -->`).join('\n')
  33. break
  34. default:
  35. metaFormatted = meta.map(mt => `#WIKI ${mt[0]}: ${mt[1]}`).join('\n')
  36. break
  37. }
  38. return `${metaFormatted}\n\n${page.content}`
  39. }
  40. module.exports = {
  41. git: null,
  42. repoPath: path.join(process.cwd(), 'data/repo'),
  43. async activated() {
  44. // not used
  45. },
  46. async deactivated() {
  47. // not used
  48. },
  49. async init() {
  50. WIKI.logger.info('(STORAGE/GIT) Initializing...')
  51. this.repoPath = path.resolve(WIKI.ROOTPATH, this.config.localRepoPath)
  52. await fs.ensureDir(this.repoPath)
  53. this.git = sgit(this.repoPath)
  54. // Initialize repo (if needed)
  55. WIKI.logger.info('(STORAGE/GIT) Checking repository state...')
  56. const isRepo = await this.git.checkIsRepo()
  57. if (!isRepo) {
  58. WIKI.logger.info('(STORAGE/GIT) Initializing local repository...')
  59. await this.git.init()
  60. }
  61. // Set default author
  62. await this.git.raw(['config', '--local', 'user.email', this.config.defaultEmail])
  63. await this.git.raw(['config', '--local', 'user.name', this.config.defaultName])
  64. // Purge existing remotes
  65. WIKI.logger.info('(STORAGE/GIT) Listing existing remotes...')
  66. const remotes = await this.git.getRemotes()
  67. if (remotes.length > 0) {
  68. WIKI.logger.info('(STORAGE/GIT) Purging existing remotes...')
  69. for(let remote of remotes) {
  70. await this.git.removeRemote(remote.name)
  71. }
  72. }
  73. // Add remote
  74. WIKI.logger.info('(STORAGE/GIT) Setting SSL Verification config...')
  75. await this.git.raw(['config', '--local', '--bool', 'http.sslVerify', _.toString(this.config.verifySSL)])
  76. switch (this.config.authType) {
  77. case 'ssh':
  78. WIKI.logger.info('(STORAGE/GIT) Setting SSH Command config...')
  79. await this.git.addConfig('core.sshCommand', `ssh -i "${this.config.sshPrivateKeyPath}" -o StrictHostKeyChecking=no`)
  80. WIKI.logger.info('(STORAGE/GIT) Adding origin remote via SSH...')
  81. await this.git.addRemote('origin', this.config.repoUrl)
  82. break
  83. default:
  84. WIKI.logger.info('(STORAGE/GIT) Adding origin remote via HTTPS...')
  85. await this.git.addRemote('origin', `https://${this.config.basicUsername}:${this.config.basicPassword}@${this.config.repoUrl}`)
  86. break
  87. }
  88. // Fetch updates for remote
  89. WIKI.logger.info('(STORAGE/GIT) Fetch updates from remote...')
  90. await this.git.raw(['remote', 'update', 'origin'])
  91. // Checkout branch
  92. const branches = await this.git.branch()
  93. if (!_.includes(branches.all, this.config.branch) && !_.includes(branches.all, `remotes/origin/${this.config.branch}`)) {
  94. throw new Error('Invalid branch! Make sure it exists on the remote first.')
  95. }
  96. WIKI.logger.info(`(STORAGE/GIT) Checking out branch ${this.config.branch}...`)
  97. await this.git.checkout(this.config.branch)
  98. // Perform initial sync
  99. await this.sync()
  100. WIKI.logger.info('(STORAGE/GIT) Initialization completed.')
  101. },
  102. async sync() {
  103. // Pull rebase
  104. if (_.includes(['sync', 'pull'], this.mode)) {
  105. WIKI.logger.info(`(STORAGE/GIT) Performing pull rebase from origin on branch ${this.config.branch}...`)
  106. await this.git.pull('origin', this.config.branch, ['--rebase'])
  107. }
  108. // Push
  109. if (_.includes(['sync', 'push'], this.mode)) {
  110. WIKI.logger.info(`(STORAGE/GIT) Performing push to origin on branch ${this.config.branch}...`)
  111. let pushOpts = ['--signed=if-asked']
  112. if (this.mode === 'push') {
  113. pushOpts.push('--force')
  114. }
  115. await this.git.push('origin', this.config.branch, pushOpts)
  116. }
  117. },
  118. async created(page) {
  119. WIKI.logger.info(`(STORAGE/GIT) Committing new file ${page.path}...`)
  120. const fileName = `${page.path}.${getFileExtension(page.contentType)}`
  121. const filePath = path.join(this.repoPath, fileName)
  122. await fs.outputFile(filePath, injectMetadata(page), 'utf8')
  123. await this.git.add(`./${fileName}`)
  124. await this.git.commit(`docs: create ${page.path}`, fileName, {
  125. '--author': `"${page.authorName} <${page.authorEmail}>"`
  126. })
  127. },
  128. async updated(page) {
  129. WIKI.logger.info(`(STORAGE/GIT) Committing updated file ${page.path}...`)
  130. const fileName = `${page.path}.${getFileExtension(page.contentType)}`
  131. const filePath = path.join(this.repoPath, fileName)
  132. await fs.outputFile(filePath, injectMetadata(page), 'utf8')
  133. await this.git.add(`./${fileName}`)
  134. await this.git.commit(`docs: update ${page.path}`, fileName, {
  135. '--author': `"${page.authorName} <${page.authorEmail}>"`
  136. })
  137. },
  138. async deleted(page) {
  139. WIKI.logger.info(`(STORAGE/GIT) Committing removed file ${page.path}...`)
  140. const fileName = `${page.path}.${getFileExtension(page.contentType)}`
  141. await this.git.rm(`./${fileName}`)
  142. await this.git.commit(`docs: delete ${page.path}`, fileName, {
  143. '--author': `"${page.authorName} <${page.authorEmail}>"`
  144. })
  145. },
  146. async renamed(page) {
  147. WIKI.logger.info(`(STORAGE/GIT) Committing file move from ${page.sourcePath} to ${page.destinationPath}...`)
  148. const sourceFilePath = `${page.sourcePath}.${getFileExtension(page.contentType)}`
  149. const destinationFilePath = `${page.destinationPath}.${getFileExtension(page.contentType)}`
  150. await this.git.mv(`./${sourceFilePath}`, `./${destinationFilePath}`)
  151. await this.git.commit(`docs: rename ${page.sourcePath} to ${destinationFilePath}`, destinationFilePath, {
  152. '--author': `"${page.authorName} <${page.authorEmail}>"`
  153. })
  154. }
  155. }