Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback for cache directories #346

Merged
merged 5 commits into from
Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"eslint-plugin-flowtype": "^2.25.0",
"nyc": "^10.0.0",
"rimraf": "^2.4.3",
"webpack": "^2.1.0-beta.22"
"webpack": "^2.2.0-rc"
},
"scripts": {
"clean": "rimraf lib/",
Expand Down
77 changes: 46 additions & 31 deletions src/fs-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,51 @@ const filename = function(source, identifier, options) {
return hash.read().toString("hex") + ".json.gz";
};

/**
* Handle the cache
*
* @params {String} directory
* @params {Object} params
* @params {Function} callback
*/
const handleCache = function(directory, params, callback) {
const source = params.source;
const options = params.options || {};
const transform = params.transform;
const identifier = params.identifier;
const shouldFallback = typeof params.directory !== "string" && directory !== os.tmpdir();

// Make sure the directory exists.
mkdirp(directory, function(err) {
// Fallback to tmpdir if node_modules folder not writable
if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err);

const file = path.join(directory, filename(source, identifier, options));

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) {
// Fallback to tmpdir if node_modules folder not writable
if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err);

callback(null, result);
});
});
});
};

/**
* Retrieve file from cache, or create a new one for future reads
*
Expand Down Expand Up @@ -117,12 +162,6 @@ const filename = function(source, identifier, options) {
* });
*/
module.exports = function(params, callback) {
// Spread params into named variables
// Forgive user whenever possible
const source = params.source;
const options = params.options || {};
const transform = params.transform;
const identifier = params.identifier;
let directory;

if (typeof params.directory === "string") {
Expand All @@ -131,29 +170,5 @@ module.exports = function(params, callback) {
directory = findCacheDir({ name: "babel-loader" }) || 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);
});
});
});
handleCache(directory, params, callback);
};
1 change: 0 additions & 1 deletion test/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ test.cb("should have one file per module", (t) => {
});
});


test.cb("should generate a new file if the identifier changes", (t) => {
const configs = [
assign({}, globalConfig, {
Expand Down
6 changes: 5 additions & 1 deletion test/helpers/createTestDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import mkdirp from "mkdirp";
import rimraf from "rimraf";

export default function createTestDirectory(baseDirectory, testTitle, cb) {
const directory = path.join(baseDirectory, testTitle.replace(/[\/?<>\\:*|"\s]/g, "_"));
const directory = path.join(baseDirectory, escapeDirectory(testTitle));

rimraf(directory, (err) => {
if (err) return cb(err);
mkdirp(directory, (mkdirErr) => cb(mkdirErr, directory));
});
}

function escapeDirectory(directory) {
return directory.replace(/[\/?<>\\:*|"\s]/g, "_");
}
Loading