overleaf/services/clsi/app/coffee/OutputFileFinder.coffee

53 lines
1.7 KiB
CoffeeScript
Raw Normal View History

2014-02-12 12:27:43 -05:00
async = require "async"
fs = require "fs"
Path = require "path"
spawn = require("child_process").spawn
2014-12-09 06:16:16 -05:00
logger = require "logger-sharelatex"
2014-02-12 12:27:43 -05:00
module.exports = OutputFileFinder =
2017-09-07 08:51:36 -04:00
findOutputFiles: (resources, directory, callback = (error, outputFiles, allFiles) ->) ->
2014-02-12 12:27:43 -05:00
incomingResources = {}
for resource in resources
incomingResources[resource.path] = true
2014-12-09 06:16:16 -05:00
logger.log directory: directory, "getting output files"
2014-02-12 12:27:43 -05:00
2014-12-04 16:37:09 -05:00
OutputFileFinder._getAllFiles directory, (error, allFiles = []) ->
2016-05-27 09:45:39 -04:00
if error?
logger.err err:error, "error finding all output files"
return callback(error)
2014-02-12 12:27:43 -05:00
outputFiles = []
for file in allFiles
if !incomingResources[file]
outputFiles.push {
path: file
type: file.match(/\.([^\.]+)$/)?[1]
}
2017-09-07 08:51:36 -04:00
callback null, outputFiles, allFiles
2014-02-12 12:27:43 -05:00
_getAllFiles: (directory, _callback = (error, fileList) ->) ->
callback = (error, fileList) ->
_callback(error, fileList)
2014-02-12 12:27:43 -05:00
_callback = () ->
# don't include clsi-specific files/directories in the output list
EXCLUDE_DIRS = ["-name", ".cache", "-o", "-name", ".archive","-o", "-name", ".project-*"]
args = [directory, "(", EXCLUDE_DIRS..., ")", "-prune", "-o", "-type", "f", "-print"]
2014-12-09 06:16:16 -05:00
logger.log args: args, "running find command"
2014-02-12 12:27:43 -05:00
2014-12-09 06:16:16 -05:00
proc = spawn("find", args)
stdout = ""
proc.stdout.on "data", (chunk) ->
stdout += chunk.toString()
proc.on "error", callback
proc.on "close", (code) ->
if code != 0
logger.warn {directory, code}, "find returned error, directory likely doesn't exist"
return callback null, []
fileList = stdout.trim().split("\n")
fileList = fileList.map (file) ->
# Strip leading directory
path = Path.relative(directory, file)
return callback null, fileList
2014-02-12 12:27:43 -05:00