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

116 lines
3.1 KiB
JavaScript
Raw Normal View History

/* eslint-disable
handle-callback-err,
no-return-assign,
no-unused-vars,
no-useless-escape,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS103: Rewrite code to no longer use __guard__
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let OutputFileFinder
const async = require('async')
const fs = require('fs')
const Path = require('path')
const { spawn } = require('child_process')
const logger = require('logger-sharelatex')
2014-02-12 12:27:43 -05:00
module.exports = OutputFileFinder = {
findOutputFiles(resources, directory, callback) {
if (callback == null) {
2020-08-10 12:01:11 -04:00
callback = function (error, outputFiles, allFiles) {}
}
const incomingResources = {}
for (const resource of Array.from(resources)) {
incomingResources[resource.path] = true
}
2014-02-12 12:27:43 -05:00
2020-08-10 12:01:11 -04:00
return OutputFileFinder._getAllFiles(directory, function (error, allFiles) {
if (allFiles == null) {
allFiles = []
}
if (error != null) {
logger.err({ err: error }, 'error finding all output files')
return callback(error)
}
const outputFiles = []
for (const file of Array.from(allFiles)) {
if (!incomingResources[file]) {
outputFiles.push({
path: file,
2020-08-10 12:01:11 -04:00
type: __guard__(file.match(/\.([^\.]+)$/), (x) => x[1])
})
}
}
return callback(null, outputFiles, allFiles)
})
},
_getAllFiles(directory, _callback) {
if (_callback == null) {
2020-08-10 12:01:11 -04:00
_callback = function (error, fileList) {}
}
2020-08-10 12:01:11 -04:00
const callback = function (error, fileList) {
_callback(error, fileList)
2020-08-10 12:01:11 -04:00
return (_callback = function () {})
}
2014-02-12 12:27:43 -05:00
// don't include clsi-specific files/directories in the output list
const EXCLUDE_DIRS = [
'-name',
'.cache',
'-o',
'-name',
'.archive',
'-o',
'-name',
'.project-*'
]
const args = [
directory,
'(',
...Array.from(EXCLUDE_DIRS),
')',
'-prune',
'-o',
'-type',
'f',
'-print'
]
logger.log({ args }, 'running find command')
2014-02-12 12:27:43 -05:00
const proc = spawn('find', args)
let stdout = ''
2020-08-10 12:01:11 -04:00
proc.stdout.setEncoding('utf8').on('data', (chunk) => (stdout += chunk))
proc.on('error', callback)
2020-08-10 12:01:11 -04:00
return 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
let path
return (path = Path.relative(directory, file))
})
return callback(null, fileList)
})
}
}
function __guard__(value, transform) {
return typeof value !== 'undefined' && value !== null
? transform(value)
: undefined
}