always create project directory when syncing resources to disk

avoids errors when project is empty
This commit is contained in:
Brian Gough 2016-03-31 11:25:25 +01:00
parent 308ca01b3c
commit 26e94f2549
2 changed files with 19 additions and 6 deletions

View file

@ -9,6 +9,8 @@ logger = require "logger-sharelatex"
module.exports = ResourceWriter = module.exports = ResourceWriter =
syncResourcesToDisk: (project_id, resources, basePath, callback = (error) ->) -> syncResourcesToDisk: (project_id, resources, basePath, callback = (error) ->) ->
@_createDirectory basePath, (error) =>
return callback(error) if error?
@_removeExtraneousFiles resources, basePath, (error) => @_removeExtraneousFiles resources, basePath, (error) =>
return callback(error) if error? return callback(error) if error?
jobs = for resource in resources jobs = for resource in resources
@ -16,6 +18,17 @@ module.exports = ResourceWriter =
(callback) => @_writeResourceToDisk(project_id, resource, basePath, callback) (callback) => @_writeResourceToDisk(project_id, resource, basePath, callback)
async.parallelLimit jobs, 5, callback async.parallelLimit jobs, 5, callback
_createDirectory: (basePath, callback = (error) ->) ->
fs.mkdir basePath, (err) ->
if err?
if err.code is 'EEXIST'
return callback()
else
logger.log {err: err, dir:basePath}, "error creating directory"
return callback(err)
else
return callback()
_removeExtraneousFiles: (resources, basePath, _callback = (error) ->) -> _removeExtraneousFiles: (resources, basePath, _callback = (error) ->) ->
timer = new Metrics.Timer("unlink-output-files") timer = new Metrics.Timer("unlink-output-files")
callback = (error) -> callback = (error) ->

View file

@ -7,7 +7,7 @@ path = require "path"
describe "ResourceWriter", -> describe "ResourceWriter", ->
beforeEach -> beforeEach ->
@ResourceWriter = SandboxedModule.require modulePath, requires: @ResourceWriter = SandboxedModule.require modulePath, requires:
"fs": @fs = {} "fs": @fs = { mkdir: sinon.stub().callsArg(1) }
"wrench": @wrench = {} "wrench": @wrench = {}
"./UrlCache" : @UrlCache = {} "./UrlCache" : @UrlCache = {}
"mkdirp" : @mkdirp = sinon.stub().callsArg(1) "mkdirp" : @mkdirp = sinon.stub().callsArg(1)