OutputFileFinder.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-return-assign,
  4. no-unused-vars,
  5. no-useless-escape,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS103: Rewrite code to no longer use __guard__
  14. * DS207: Consider shorter variations of null checks
  15. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  16. */
  17. let OutputFileFinder
  18. const async = require('async')
  19. const fs = require('fs')
  20. const Path = require('path')
  21. const { spawn } = require('child_process')
  22. const logger = require('logger-sharelatex')
  23. module.exports = OutputFileFinder = {
  24. findOutputFiles(resources, directory, callback) {
  25. if (callback == null) {
  26. callback = function (error, outputFiles, allFiles) {}
  27. }
  28. const incomingResources = new Set(
  29. resources.map((resource) => resource.path)
  30. )
  31. return OutputFileFinder._getAllFiles(directory, function (error, allFiles) {
  32. if (allFiles == null) {
  33. allFiles = []
  34. }
  35. if (error != null) {
  36. logger.err({ err: error }, 'error finding all output files')
  37. return callback(error)
  38. }
  39. const outputFiles = []
  40. for (const file of Array.from(allFiles)) {
  41. if (!incomingResources.has(file)) {
  42. outputFiles.push({
  43. path: file,
  44. type: __guard__(file.match(/\.([^\.]+)$/), (x) => x[1]),
  45. })
  46. }
  47. }
  48. return callback(null, outputFiles, allFiles)
  49. })
  50. },
  51. _getAllFiles(directory, _callback) {
  52. if (_callback == null) {
  53. _callback = function (error, fileList) {}
  54. }
  55. const callback = function (error, fileList) {
  56. _callback(error, fileList)
  57. return (_callback = function () {})
  58. }
  59. // don't include clsi-specific files/directories in the output list
  60. const EXCLUDE_DIRS = [
  61. '-name',
  62. '.cache',
  63. '-o',
  64. '-name',
  65. '.archive',
  66. '-o',
  67. '-name',
  68. '.project-*',
  69. ]
  70. const args = [
  71. directory,
  72. '(',
  73. ...Array.from(EXCLUDE_DIRS),
  74. ')',
  75. '-prune',
  76. '-o',
  77. '-type',
  78. 'f',
  79. '-print',
  80. ]
  81. logger.log({ args }, 'running find command')
  82. const proc = spawn('find', args)
  83. let stdout = ''
  84. proc.stdout.setEncoding('utf8').on('data', (chunk) => (stdout += chunk))
  85. proc.on('error', callback)
  86. return proc.on('close', function (code) {
  87. if (code !== 0) {
  88. logger.warn(
  89. { directory, code },
  90. "find returned error, directory likely doesn't exist"
  91. )
  92. return callback(null, [])
  93. }
  94. let fileList = stdout.trim().split('\n')
  95. fileList = fileList.map(function (file) {
  96. // Strip leading directory
  97. let path
  98. return (path = Path.relative(directory, file))
  99. })
  100. return callback(null, fileList)
  101. })
  102. },
  103. }
  104. function __guard__(value, transform) {
  105. return typeof value !== 'undefined' && value !== null
  106. ? transform(value)
  107. : undefined
  108. }