OutputFileFinder.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. * DS103: Rewrite code to no longer use __guard__
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. let OutputFileFinder
  16. const async = require('async')
  17. const fs = require('fs')
  18. const Path = require('path')
  19. const { spawn } = require('child_process')
  20. const logger = require('logger-sharelatex')
  21. module.exports = OutputFileFinder = {
  22. findOutputFiles(resources, directory, callback) {
  23. const incomingResources = new Set(
  24. resources.map((resource) => resource.path)
  25. )
  26. return OutputFileFinder._getAllFiles(directory, function (error, allFiles) {
  27. if (allFiles == null) {
  28. allFiles = []
  29. }
  30. if (error) {
  31. logger.err({ err: error }, 'error finding all output files')
  32. return callback(error)
  33. }
  34. const outputFiles = []
  35. for (const file of Array.from(allFiles)) {
  36. if (!incomingResources.has(file)) {
  37. const type = Path.extname(file)
  38. outputFiles.push({
  39. path: file,
  40. type: Path.extname(file) || undefined,
  41. })
  42. }
  43. }
  44. callback(null, outputFiles, allFiles)
  45. })
  46. },
  47. _getAllFiles(directory, _callback) {
  48. // don't include clsi-specific files/directories in the output list
  49. const EXCLUDE_DIRS = [
  50. '-name',
  51. '.cache',
  52. '-o',
  53. '-name',
  54. '.archive',
  55. '-o',
  56. '-name',
  57. '.project-*',
  58. ]
  59. const args = [
  60. directory,
  61. '(',
  62. ...Array.from(EXCLUDE_DIRS),
  63. ')',
  64. '-prune',
  65. '-o',
  66. '-type',
  67. 'f',
  68. '-print',
  69. ]
  70. logger.log({ args }, 'running find command')
  71. const proc = spawn('find', args)
  72. let stdout = ''
  73. proc.stdout.setEncoding('utf8').on('data', (chunk) => (stdout += chunk))
  74. proc.on('error', callback)
  75. return proc.on('close', function (code) {
  76. if (code !== 0) {
  77. logger.warn(
  78. { directory, code },
  79. "find returned error, directory likely doesn't exist"
  80. )
  81. return callback(null, [])
  82. }
  83. let fileList = stdout.trim().split('\n')
  84. fileList = fileList.map(function (file) {
  85. // Strip leading directory
  86. let path
  87. return (path = Path.relative(directory, file))
  88. })
  89. callback(null, fileList)
  90. })
  91. },
  92. }
  93. function __guard__(value, transform) {
  94. return typeof value !== 'undefined' && value !== null
  95. ? transform(value)
  96. : undefined
  97. }