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.

68 lines
2.0 KiB

  1. 'use strict'
  2. /* global wiki */
  3. const Promise = require('bluebird')
  4. const fs = Promise.promisifyAll(require('fs-extra'))
  5. const klaw = require('klaw')
  6. const moment = require('moment')
  7. const path = require('path')
  8. const entryHelper = require('../helpers/entry')
  9. module.exports = (job) => {
  10. return wiki.git.resync().then(() => {
  11. // -> Stream all documents
  12. let cacheJobs = []
  13. let jobCbStreamDocsResolve = null
  14. let jobCbStreamDocs = new Promise((resolve, reject) => {
  15. jobCbStreamDocsResolve = resolve
  16. })
  17. klaw(wiki.REPOPATH).on('data', function (item) {
  18. if (path.extname(item.path) === '.md' && path.basename(item.path) !== 'README.md') {
  19. let entryPath = entryHelper.parsePath(entryHelper.getEntryPathFromFullPath(item.path))
  20. let cachePath = entryHelper.getCachePath(entryPath)
  21. // -> Purge outdated cache
  22. cacheJobs.push(
  23. fs.statAsync(cachePath).then((st) => {
  24. return moment(st.mtime).isBefore(item.stats.mtime) ? 'expired' : 'active'
  25. }).catch((err) => {
  26. return (err.code !== 'EEXIST') ? err : 'new'
  27. }).then((fileStatus) => {
  28. // -> Delete expired cache file
  29. if (fileStatus === 'expired') {
  30. return fs.unlinkAsync(cachePath).return(fileStatus)
  31. }
  32. return fileStatus
  33. }).then((fileStatus) => {
  34. // -> Update cache and search index
  35. if (fileStatus !== 'active') {
  36. return global.entries.updateCache(entryPath).then(entry => {
  37. process.send({
  38. action: 'searchAdd',
  39. content: entry
  40. })
  41. return true
  42. })
  43. }
  44. return true
  45. })
  46. )
  47. }
  48. }).on('end', () => {
  49. jobCbStreamDocsResolve(Promise.all(cacheJobs))
  50. })
  51. return jobCbStreamDocs
  52. }).then(() => {
  53. wiki.logger.info('Git remote repository sync: DONE')
  54. return true
  55. })
  56. }