Skip to content

Commit

Permalink
[New] Implements a "normalize-options" pseudo-hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Maël Nison authored and ljharb committed Nov 14, 2018
1 parent a4ad3e7 commit 3170ce1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var fs = require('fs');
var path = require('path');
var caller = require('./caller.js');
var nodeModulesPaths = require('./node-modules-paths.js');
var normalizeOptions = require('./normalize-options.js');

var defaultIsFile = function isFile(file, cb) {
fs.stat(file, function (err, stat) {
Expand All @@ -26,8 +27,8 @@ var defaultIsDir = function isDirectory(dir, cb) {

module.exports = function resolve(x, options, callback) {
var cb = callback;
var opts = options || {};
if (typeof opts === 'function') {
var opts = options;
if (typeof options === 'function') {
cb = opts;
opts = {};
}
Expand All @@ -38,6 +39,8 @@ module.exports = function resolve(x, options, callback) {
});
}

opts = normalizeOptions(x, opts);

var isFile = opts.isFile || defaultIsFile;
var isDirectory = opts.isDirectory || defaultIsDir;
var readFile = opts.readFile || fs.readFile;
Expand Down
10 changes: 10 additions & 0 deletions lib/normalize-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = function (x, opts) {
/**
* This file is purposefully a passthrough. It's expected that third-party
* environments will override it at runtime in order to inject special logic
* into `resolve` (by manipulating the options). One such example is the PnP
* code path in Yarn.
*/

return opts || {};
};
4 changes: 3 additions & 1 deletion lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var fs = require('fs');
var path = require('path');
var caller = require('./caller.js');
var nodeModulesPaths = require('./node-modules-paths.js');
var normalizeOptions = require('./normalize-options.js');

var defaultIsFile = function isFile(file) {
try {
Expand All @@ -28,7 +29,8 @@ module.exports = function (x, options) {
if (typeof x !== 'string') {
throw new TypeError('Path must be a string.');
}
var opts = options || {};
var opts = normalizeOptions(x, options);

var isFile = opts.isFile || defaultIsFile;
var isDirectory = opts.isDirectory || defaultIsDir;
var readFileSync = opts.readFileSync || fs.readFileSync;
Expand Down

0 comments on commit 3170ce1

Please sign in to comment.