From 2970b137f415c74e4fce979392f36518fff8b686 Mon Sep 17 00:00:00 2001 From: Fatih Altinok Date: Thu, 24 Nov 2016 17:01:27 +0300 Subject: [PATCH] fix cache directory not writable (#327) --- src/fs-cache.js | 51 +++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/src/fs-cache.js b/src/fs-cache.js index 9262c444..aaabbfff 100644 --- a/src/fs-cache.js +++ b/src/fs-cache.js @@ -125,35 +125,36 @@ module.exports = function(params, callback) { const identifier = params.identifier; let directory; - if (typeof params.directory === "string") { - directory = params.directory; - } else { - directory = findCacheDir({ name: "babel-loader" }) || os.tmpdir(); + try { + if (typeof params.directory === "string") { + directory = params.directory; + mkdirp.sync(directory); + } else { + directory = findCacheDir({ name: "babel-loader", create: true }); + } + } catch (e) { + // Make sure the directory exists and is writable. + directory = os.tmpdir(); } const file = path.join(directory, filename(source, identifier, options)); - // Make sure the directory exists. - return mkdirp(directory, function(err) { - if (err) { return callback(err); } - - return read(file, function(err, content) { - let result = {}; - // No errors mean that the file was previously cached - // we just need to return it - if (!err) { return callback(null, content); } - - // Otherwise just transform the file - // return it to the user asap and write it in cache - try { - result = transform(source, options); - } catch (error) { - return callback(error); - } - - return write(file, result, function(err) { - return callback(err, result); - }); + return read(file, function(err, content) { + let result = {}; + // No errors mean that the file was previously cached + // we just need to return it + if (!err) { return callback(null, content); } + + // Otherwise just transform the file + // return it to the user asap and write it in cache + try { + result = transform(source, options); + } catch (error) { + return callback(error); + } + + return write(file, result, function(err) { + return callback(err, result); }); }); };