OutputFileFinder.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 = {}
  29. for (const resource of Array.from(resources)) {
  30. incomingResources[resource.path] = true
  31. }
  32. return OutputFileFinder._getAllFiles(directory, function (error, allFiles) {
  33. if (allFiles == null) {
  34. allFiles = []
  35. }
  36. if (error != null) {
  37. logger.err({ err: error }, 'error finding all output files')
  38. return callback(error)
  39. }
  40. const outputFiles = []
  41. for (const file of Array.from(allFiles)) {
  42. if (!incomingResources[file]) {
  43. outputFiles.push({
  44. path: file,
  45. type: __guard__(file.match(/\.([^\.]+)$/), (x) => x[1])
  46. })
  47. }
  48. }
  49. return callback(null, outputFiles, allFiles)
  50. })
  51. },
  52. _getAllFiles(directory, _callback) {
  53. if (_callback == null) {
  54. _callback = function (error, fileList) {}
  55. }
  56. const callback = function (error, fileList) {
  57. _callback(error, fileList)
  58. return (_callback = function () {})
  59. }
  60. // don't include clsi-specific files/directories in the output list
  61. const EXCLUDE_DIRS = [
  62. '-name',
  63. '.cache',
  64. '-o',
  65. '-name',
  66. '.archive',
  67. '-o',
  68. '-name',
  69. '.project-*'
  70. ]
  71. const args = [
  72. directory,
  73. '(',
  74. ...Array.from(EXCLUDE_DIRS),
  75. ')',
  76. '-prune',
  77. '-o',
  78. '-type',
  79. 'f',
  80. '-print'
  81. ]
  82. logger.log({ args }, 'running find command')
  83. const proc = spawn('find', args)
  84. let stdout = ''
  85. proc.stdout.setEncoding('utf8').on('data', (chunk) => (stdout += chunk))
  86. proc.on('error', callback)
  87. return proc.on('close', function (code) {
  88. if (code !== 0) {
  89. logger.warn(
  90. { directory, code },
  91. "find returned error, directory likely doesn't exist"
  92. )
  93. return callback(null, [])
  94. }
  95. let fileList = stdout.trim().split('\n')
  96. fileList = fileList.map(function (file) {
  97. // Strip leading directory
  98. let path
  99. return (path = Path.relative(directory, file))
  100. })
  101. return callback(null, fileList)
  102. })
  103. }
  104. }
  105. function __guard__(value, transform) {
  106. return typeof value !== 'undefined' && value !== null
  107. ? transform(value)
  108. : undefined
  109. }