2
0

storage.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const path = require('path')
  2. const sgit = require('simple-git/promise')
  3. const fs = require('fs-extra')
  4. const _ = require('lodash')
  5. const repoPath = path.join(WIKI.ROOTPATH, 'data/repo')
  6. /**
  7. * Get file extension based on content type
  8. */
  9. const getFileExtension = (contentType) => {
  10. switch (contentType) {
  11. case 'markdown':
  12. return 'md'
  13. case 'html':
  14. return 'html'
  15. default:
  16. return 'txt'
  17. }
  18. }
  19. /**
  20. * Inject page metadata into contents
  21. */
  22. const injectMetadata = (page) => {
  23. let meta = [
  24. ['title', page.title],
  25. ['description', page.description]
  26. ]
  27. let metaFormatted = ''
  28. switch (page.contentType) {
  29. case 'markdown':
  30. metaFormatted = meta.map(mt => `[//]: # ${mt[0]}: ${mt[1]}`).join('\n')
  31. break
  32. case 'html':
  33. metaFormatted = meta.map(mt => `<!-- ${mt[0]}: ${mt[1]} -->`).join('\n')
  34. break
  35. default:
  36. metaFormatted = meta.map(mt => `#WIKI ${mt[0]}: ${mt[1]}`).join('\n')
  37. break
  38. }
  39. return `${metaFormatted}\n\n${page.content}`
  40. }
  41. module.exports = {
  42. async activated() {
  43. },
  44. async deactivated() {
  45. },
  46. async init() {
  47. await fs.ensureDir(repoPath)
  48. const git = sgit(repoPath)
  49. // Initialize repo (if needed)
  50. const isRepo = await git.checkIsRepo()
  51. if (!isRepo) {
  52. await git.init()
  53. }
  54. // Checkout branch
  55. await git.checkout(this.config.branch)
  56. // Add remote
  57. await git.raw(['config', '--local', '--bool', 'http.sslVerify', _.toString(this.config.verifySSL)])
  58. switch (this.config.authType) {
  59. case 'ssh':
  60. await git.addConfig('core.sshCommand', `ssh -i "${this.config.sshPrivateKeyPath}" -o StrictHostKeyChecking=no`)
  61. await git.addRemote('origin', this.config.repoUrl)
  62. break
  63. default:
  64. await git.addRemote('origin', `https://${this.config.basicUsername}:${this.config.basicPassword}@${this.config.repoUrl}`)
  65. break
  66. }
  67. },
  68. async created() {
  69. const fileName = `${this.page.path}.${getFileExtension(this.page.contentType)}`
  70. const filePath = path.join(repoPath, fileName)
  71. await fs.outputFile(filePath, injectMetadata(this.page), 'utf8')
  72. const git = sgit(repoPath)
  73. await git.add(`./${fileName}`).commit(`docs: create ${this.page.path}`, fileName, {
  74. '--author': `"${this.page.authorName} <${this.page.authorEmail}>"`
  75. })
  76. },
  77. async updated() {
  78. const fileName = `${this.page.path}.${getFileExtension(this.page.contentType)}`
  79. const filePath = path.join(repoPath, fileName)
  80. await fs.outputFile(filePath, injectMetadata(this.page), 'utf8')
  81. const git = sgit(repoPath)
  82. await git.add(`./${fileName}`).commit(`docs: update ${this.page.path}`, fileName, {
  83. '--author': `"${this.page.authorName} <${this.page.authorEmail}>"`
  84. })
  85. },
  86. async deleted() {
  87. const fileName = `${this.page.path}.${getFileExtension(this.page.contentType)}`
  88. const git = sgit(repoPath)
  89. await git.rm(`./${fileName}`).commit(`docs: delete ${this.page.path}`, fileName, {
  90. '--author': `"${this.page.authorName} <${this.page.authorEmail}>"`
  91. })
  92. },
  93. async renamed() {
  94. const sourceFilePath = `${this.page.sourcePath}.${getFileExtension(this.page.contentType)}`
  95. const destinationFilePath = `${this.page.destinationPath}.${getFileExtension(this.page.contentType)}`
  96. const git = sgit(repoPath)
  97. await git.mv(`./${sourceFilePath}`, `./${destinationFilePath}`).commit(`docs: rename ${this.page.sourcePath} to ${destinationFilePath}`, destinationFilePath, {
  98. '--author': `"${this.page.authorName} <${this.page.authorEmail}>"`
  99. })
  100. }
  101. }