2014-02-12 12:27:43 -05:00
|
|
|
async = require "async"
|
|
|
|
fs = require "fs"
|
|
|
|
Path = require "path"
|
2014-12-09 06:07:58 -05:00
|
|
|
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 =
|
|
|
|
findOutputFiles: (resources, directory, callback = (error, outputFiles) ->) ->
|
|
|
|
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 = []) ->
|
|
|
|
return callback(error) if error?
|
2014-02-12 12:27:43 -05:00
|
|
|
jobs = []
|
|
|
|
outputFiles = []
|
|
|
|
for file in allFiles
|
|
|
|
do (file) ->
|
|
|
|
jobs.push (callback) ->
|
2014-12-09 06:07:58 -05:00
|
|
|
if incomingResources[file]
|
2014-02-12 12:27:43 -05:00
|
|
|
return callback()
|
|
|
|
else
|
2014-12-09 06:07:58 -05:00
|
|
|
outputFiles.push {
|
|
|
|
path: file
|
|
|
|
type: file.match(/\.([^\.]+)$/)?[1]
|
|
|
|
}
|
|
|
|
callback()
|
2014-02-12 12:27:43 -05:00
|
|
|
|
|
|
|
async.series jobs, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback null, outputFiles
|
|
|
|
|
2014-12-09 06:07:58 -05:00
|
|
|
_getAllFiles: (directory, _callback = (error, fileList) ->) ->
|
|
|
|
callback = (error, fileList) ->
|
|
|
|
_callback(error, fileList)
|
2014-02-12 12:27:43 -05:00
|
|
|
_callback = () ->
|
2014-12-09 06:16:16 -05:00
|
|
|
|
|
|
|
args = [directory, "-type", "f"]
|
|
|
|
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)
|
2014-12-09 06:07:58 -05:00
|
|
|
stdout = ""
|
|
|
|
proc.stdout.on "data", (chunk) ->
|
|
|
|
stdout += chunk.toString()
|
|
|
|
proc.on "error", callback
|
|
|
|
proc.on "close", (code) ->
|
|
|
|
if code != 0
|
2014-12-09 06:25:23 -05:00
|
|
|
logger.warn {directory, code}, "find returned error, directory likely doesn't exist"
|
|
|
|
return callback null, []
|
2014-12-09 06:07:58 -05:00
|
|
|
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
|
|
|
|