Skip to content

Commit 3170ce1

Browse files
Maël Nisonljharb
Maël Nison
authored andcommitted
[New] Implements a "normalize-options" pseudo-hook
1 parent a4ad3e7 commit 3170ce1

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

lib/async.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var fs = require('fs');
33
var path = require('path');
44
var caller = require('./caller.js');
55
var nodeModulesPaths = require('./node-modules-paths.js');
6+
var normalizeOptions = require('./normalize-options.js');
67

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

2728
module.exports = function resolve(x, options, callback) {
2829
var cb = callback;
29-
var opts = options || {};
30-
if (typeof opts === 'function') {
30+
var opts = options;
31+
if (typeof options === 'function') {
3132
cb = opts;
3233
opts = {};
3334
}
@@ -38,6 +39,8 @@ module.exports = function resolve(x, options, callback) {
3839
});
3940
}
4041

42+
opts = normalizeOptions(x, opts);
43+
4144
var isFile = opts.isFile || defaultIsFile;
4245
var isDirectory = opts.isDirectory || defaultIsDir;
4346
var readFile = opts.readFile || fs.readFile;

lib/normalize-options.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = function (x, opts) {
2+
/**
3+
* This file is purposefully a passthrough. It's expected that third-party
4+
* environments will override it at runtime in order to inject special logic
5+
* into `resolve` (by manipulating the options). One such example is the PnP
6+
* code path in Yarn.
7+
*/
8+
9+
return opts || {};
10+
};

lib/sync.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var fs = require('fs');
33
var path = require('path');
44
var caller = require('./caller.js');
55
var nodeModulesPaths = require('./node-modules-paths.js');
6+
var normalizeOptions = require('./normalize-options.js');
67

78
var defaultIsFile = function isFile(file) {
89
try {
@@ -28,7 +29,8 @@ module.exports = function (x, options) {
2829
if (typeof x !== 'string') {
2930
throw new TypeError('Path must be a string.');
3031
}
31-
var opts = options || {};
32+
var opts = normalizeOptions(x, options);
33+
3234
var isFile = opts.isFile || defaultIsFile;
3335
var isDirectory = opts.isDirectory || defaultIsDir;
3436
var readFileSync = opts.readFileSync || fs.readFileSync;

0 commit comments

Comments
 (0)