save output files in a .cache directory

This commit is contained in:
Brian Gough
2015-02-24 14:40:05 +00:00
parent f37004cec6
commit 1923352e66
2 changed files with 52 additions and 1 deletions

View File

@@ -0,0 +1,49 @@
async = require "async"
fs = require "fs"
fse = require "fs-extra"
Path = require "path"
logger = require "logger-sharelatex"
_ = require "underscore"
module.exports = OutputCacheManager =
CACHE_DIR: '.cache/clsi'
saveOutputFiles: (outputFiles, target, callback) ->
# make a target/build_id directory and
# copy all the output files into it
buildId = 'build-' + Date.now()
relDir = OutputCacheManager.CACHE_DIR + '/' + buildId
newDir = target + '/' + relDir
OutputCacheManager.expireOutputFiles target
fse.ensureDir newDir, (err) ->
if err?
callback(err, outputFiles)
else
async.mapSeries outputFiles, (file, cb) ->
newFile = _.clone(file)
newFile.path = relDir + '/' + file.path
src = target + '/' + file.path
dst = target + '/' + newFile.path
#console.log 'src', src, 'dst', dst
fs.stat src, (err, stats) ->
if err?
cb(err)
else if stats.isFile()
#console.log 'isFile: copying'
fse.copy src, dst, (err) ->
cb(err, newFile)
else
# other filetype - shouldn't happen
cb(new Error("output file is not a file"), file)
, (err, results) ->
if err?
callback err, outputFiles
else
callback(err, results)
expireOutputFiles: (target, callback) ->
# look in target for build dirs and delete if > N or age of mod time > T
cacheDir = target + '/' + OutputCacheManager.CACHE_DIR
fs.readdir cacheDir, (err, results) ->
console.log 'CACHEDIR', results
callback(err) if callback?