overleaf/services/clsi/app/js/OutputFileFinder.js

79 lines
2 KiB
JavaScript
Raw Normal View History

let OutputFileFinder
const Path = require('path')
2020-12-18 10:16:46 -05:00
const _ = require('lodash')
const { spawn } = require('child_process')
const logger = require('@overleaf/logger')
2014-02-12 12:27:43 -05:00
module.exports = OutputFileFinder = {
findOutputFiles(resources, directory, callback) {
2021-07-13 07:04:48 -04:00
const incomingResources = new Set(resources.map(resource => resource.path))
2014-02-12 12:27:43 -05:00
2020-12-18 10:16:07 -05:00
OutputFileFinder._getAllFiles(directory, function (error, allFiles) {
if (allFiles == null) {
allFiles = []
}
2020-12-18 10:01:25 -05:00
if (error) {
logger.err({ err: error }, 'error finding all output files')
return callback(error)
}
const outputFiles = []
2020-12-18 10:10:53 -05:00
for (const file of allFiles) {
2020-12-18 09:56:53 -05:00
if (!incomingResources.has(file)) {
outputFiles.push({
path: file,
2021-07-13 07:04:48 -04:00
type: Path.extname(file).replace(/^\./, '') || undefined,
})
}
}
2020-12-18 10:02:40 -05:00
callback(null, outputFiles, allFiles)
})
},
2020-12-18 10:16:46 -05:00
_getAllFiles(directory, callback) {
callback = _.once(callback)
// don't include clsi-specific files/directories in the output list
const EXCLUDE_DIRS = [
'-name',
'.cache',
'-o',
'-name',
'.archive',
'-o',
'-name',
2021-07-13 07:04:48 -04:00
'.project-*',
]
const args = [
directory,
'(',
2020-12-18 10:10:53 -05:00
...EXCLUDE_DIRS,
')',
'-prune',
'-o',
'-type',
'f',
2021-07-13 07:04:48 -04:00
'-print',
]
logger.debug({ args }, 'running find command')
2014-02-12 12:27:43 -05:00
const proc = spawn('find', args)
let stdout = ''
2021-07-13 07:04:48 -04:00
proc.stdout.setEncoding('utf8').on('data', chunk => (stdout += chunk))
proc.on('error', callback)
2020-12-18 10:16:07 -05:00
proc.on('close', function (code) {
if (code !== 0) {
logger.warn(
{ directory, code },
"find returned error, directory likely doesn't exist"
)
return callback(null, [])
}
let fileList = stdout.trim().split('\n')
2020-08-10 12:01:11 -04:00
fileList = fileList.map(function (file) {
// Strip leading directory
2020-12-18 10:17:20 -05:00
return Path.relative(directory, file)
})
2020-12-18 10:02:40 -05:00
callback(null, fileList)
})
2021-07-13 07:04:48 -04:00
},
}