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