Skip to content

Make loadAsFileSync() work the same as async loadAsFile() #146

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

Merged
merged 3 commits into from
Apr 5, 2018
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
36 changes: 36 additions & 0 deletions lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ module.exports = function (x, options) {
throw err;

function loadAsFileSync(x) {
var pkg = loadpkg(path.dirname(x));

if (pkg && pkg.dir && pkg.pkg && opts.pathFilter) {
var rfile = path.relative(pkg.dir, x);
var r = opts.pathFilter(pkg.pkg, x, rfile);
if (r) {
x = path.resolve(pkg.dir, r); // eslint-disable-line no-param-reassign
}
}

if (isFile(x)) {
return x;
}
Expand All @@ -56,6 +66,32 @@ module.exports = function (x, options) {
}
}

function loadpkg(dir) {
if (dir === '' || dir === '/') return;
if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) {
return;
}
if (/[/\\]node_modules[/\\]*$/.test(dir)) return;

var pkgfile = path.join(dir, 'package.json');

if (!isFile(pkgfile)) {
return loadpkg(path.dirname(dir));
}

var body = readFileSync(pkgfile);

try {
var pkg = JSON.parse(body);
} catch (jsonErr) {}

if (pkg && opts.packageFilter) {
pkg = opts.packageFilter(pkg, pkgfile);
}

return { pkg: pkg, dir: dir };
}

function loadAsDirectorySync(x) {
var pkgfile = path.join(x, '/package.json');
if (isFile(pkgfile)) {
Expand Down
6 changes: 6 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ options are:

* `opts.packageFilter(pkg, pkgfile)` - transform the parsed package.json contents before looking at the "main" field

* `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package
* pkg - package data
* path - the path being resolved
* relativePath - the path relative from the package.json location
* returns - a relative path that will be joined from the package.json location

* opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)

* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
Expand Down
9 changes: 8 additions & 1 deletion test/pathfilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,23 @@ test('#62: deep module references and the pathFilter', function (t) {
});

t.test('deep/ref alt', function (st) {
st.plan(4);
st.plan(8);

var pathFilter = pathFilterFactory(st);

var res = resolve.sync(
'deep/ref',
{ basedir: resolverDir, pathFilter: pathFilter }
);
st.equal(res, path.join(resolverDir, 'node_modules/deep/alt.js'));

resolve(
'deep/ref',
{ basedir: resolverDir, pathFilter: pathFilter },
function (err, res, pkg) {
if (err) st.fail(err);
st.equal(res, path.join(resolverDir, 'node_modules/deep/alt.js'));
st.end();
}
);
});
Expand Down