Skip to content

Commit

Permalink
fix cache directory not writable (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatih Altinok authored and danez committed Dec 15, 2016
1 parent 35272ef commit 814da31
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions src/fs-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
};

0 comments on commit 814da31

Please sign in to comment.