wallet-core/node_modules/resolve/lib/sync.js

94 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-10-10 03:43:44 +02:00
var core = require('./core');
var fs = require('fs');
var path = require('path');
var caller = require('./caller.js');
var nodeModulesPaths = require('./node-modules-paths.js');
2017-05-28 00:38:50 +02:00
module.exports = function (x, options) {
if (typeof x !== 'string') {
throw new TypeError('Path must be a string.');
}
var opts = options || {};
2016-10-10 03:43:44 +02:00
var isFile = opts.isFile || function (file) {
2017-05-28 00:38:50 +02:00
try {
var stat = fs.statSync(file);
} catch (e) {
if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
throw e;
}
2016-10-10 03:43:44 +02:00
return stat.isFile() || stat.isFIFO();
};
var readFileSync = opts.readFileSync || fs.readFileSync;
2017-05-28 00:38:50 +02:00
var extensions = opts.extensions || ['.js'];
2016-10-10 03:43:44 +02:00
var y = opts.basedir || path.dirname(caller());
opts.paths = opts.paths || [];
2017-05-28 00:38:50 +02:00
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) {
2016-10-10 03:43:44 +02:00
var res = path.resolve(y, x);
2017-05-28 00:38:50 +02:00
if (x === '..' || x.slice(-1) === '/') res += '/';
2016-10-10 03:43:44 +02:00
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
if (m) return m;
} else {
var n = loadNodeModulesSync(x, y);
if (n) return n;
}
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
if (core[x]) return x;
2017-05-28 00:38:50 +02:00
var err = new Error("Cannot find module '" + x + "' from '" + y + "'");
err.code = 'MODULE_NOT_FOUND';
throw err;
function loadAsFileSync(x) {
2016-10-10 03:43:44 +02:00
if (isFile(x)) {
return x;
}
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
for (var i = 0; i < extensions.length; i++) {
var file = x + extensions[i];
if (isFile(file)) {
return file;
}
}
}
2017-05-28 00:38:50 +02:00
function loadAsDirectorySync(x) {
2016-10-10 03:43:44 +02:00
var pkgfile = path.join(x, '/package.json');
if (isFile(pkgfile)) {
try {
2017-08-14 05:01:11 +02:00
var body = readFileSync(pkgfile, 'UTF8');
2016-10-10 03:43:44 +02:00
var pkg = JSON.parse(body);
2017-08-14 05:01:11 +02:00
2016-10-10 03:43:44 +02:00
if (opts.packageFilter) {
pkg = opts.packageFilter(pkg, x);
}
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
if (pkg.main) {
2017-08-14 05:01:11 +02:00
if (pkg.main === '.' || pkg.main === './') {
pkg.main = 'index';
}
2016-10-10 03:43:44 +02:00
var m = loadAsFileSync(path.resolve(x, pkg.main));
if (m) return m;
var n = loadAsDirectorySync(path.resolve(x, pkg.main));
if (n) return n;
}
2017-05-28 00:38:50 +02:00
} catch (e) {}
2016-10-10 03:43:44 +02:00
}
2017-05-28 00:38:50 +02:00
return loadAsFileSync(path.join(x, '/index'));
2016-10-10 03:43:44 +02:00
}
2017-05-28 00:38:50 +02:00
function loadNodeModulesSync(x, start) {
2016-10-10 03:43:44 +02:00
var dirs = nodeModulesPaths(start, opts);
for (var i = 0; i < dirs.length; i++) {
var dir = dirs[i];
2017-05-28 00:38:50 +02:00
var m = loadAsFileSync(path.join(dir, '/', x));
2016-10-10 03:43:44 +02:00
if (m) return m;
2017-05-28 00:38:50 +02:00
var n = loadAsDirectorySync(path.join(dir, '/', x));
2016-10-10 03:43:44 +02:00
if (n) return n;
}
}
};