github-pages-deploy-action/node_modules/resolve/lib/sync.js

184 lines
5.8 KiB
JavaScript
Raw Normal View History

2020-03-07 11:45:40 +08:00
var isCore = require('./is-core');
2020-01-28 13:08:03 +08:00
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 {
var stat = fs.statSync(file);
} catch (e) {
if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
throw e;
}
return stat.isFile() || stat.isFIFO();
};
var defaultIsDir = function isDirectory(dir) {
try {
var stat = fs.statSync(dir);
} catch (e) {
if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
throw e;
}
return stat.isDirectory();
};
var maybeUnwrapSymlink = function maybeUnwrapSymlink(x, opts) {
if (opts && opts.preserveSymlinks === false) {
try {
return fs.realpathSync(x);
} catch (realPathErr) {
if (realPathErr.code !== 'ENOENT') {
throw realPathErr;
}
}
}
return x;
};
2020-03-07 11:45:40 +08:00
var getPackageCandidates = function getPackageCandidates(x, start, opts) {
var dirs = nodeModulesPaths(start, opts, x);
for (var i = 0; i < dirs.length; i++) {
dirs[i] = path.join(dirs[i], x);
}
return dirs;
};
module.exports = function resolveSync(x, options) {
2020-01-28 13:08:03 +08:00
if (typeof x !== 'string') {
throw new TypeError('Path must be a string.');
}
var opts = normalizeOptions(x, options);
var isFile = opts.isFile || defaultIsFile;
var readFileSync = opts.readFileSync || fs.readFileSync;
var isDirectory = opts.isDirectory || defaultIsDir;
2020-03-07 11:45:40 +08:00
var packageIterator = opts.packageIterator;
2020-01-28 13:08:03 +08:00
var extensions = opts.extensions || ['.js'];
var basedir = opts.basedir || path.dirname(caller());
var parent = opts.filename || basedir;
opts.paths = opts.paths || [];
// ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory
var absoluteStart = maybeUnwrapSymlink(path.resolve(basedir), opts);
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
var res = path.resolve(absoluteStart, x);
2020-03-07 11:45:40 +08:00
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
2020-01-28 13:08:03 +08:00
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
if (m) return maybeUnwrapSymlink(m, opts);
2020-03-07 11:45:40 +08:00
} else if (isCore(x)) {
2020-01-28 13:08:03 +08:00
return x;
} else {
var n = loadNodeModulesSync(x, absoluteStart);
if (n) return maybeUnwrapSymlink(n, opts);
}
var err = new Error("Cannot find module '" + x + "' from '" + parent + "'");
err.code = 'MODULE_NOT_FOUND';
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;
}
for (var i = 0; i < extensions.length; i++) {
var file = x + extensions[i];
if (isFile(file)) {
return file;
}
}
}
function loadpkg(dir) {
if (dir === '' || dir === '/') return;
if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) {
return;
}
if ((/[/\\]node_modules[/\\]*$/).test(dir)) return;
2020-03-07 11:45:40 +08:00
var pkgfile = path.join(maybeUnwrapSymlink(dir, opts), 'package.json');
2020-01-28 13:08:03 +08:00
if (!isFile(pkgfile)) {
return loadpkg(path.dirname(dir));
}
var body = readFileSync(pkgfile);
try {
var pkg = JSON.parse(body);
} catch (jsonErr) {}
if (pkg && opts.packageFilter) {
2020-03-07 11:45:40 +08:00
// v2 will pass pkgfile
pkg = opts.packageFilter(pkg, /*pkgfile,*/ dir); // eslint-disable-line spaced-comment
2020-01-28 13:08:03 +08:00
}
return { pkg: pkg, dir: dir };
}
function loadAsDirectorySync(x) {
2020-03-07 11:45:40 +08:00
var pkgfile = path.join(maybeUnwrapSymlink(x, opts), '/package.json');
2020-01-28 13:08:03 +08:00
if (isFile(pkgfile)) {
try {
var body = readFileSync(pkgfile, 'UTF8');
var pkg = JSON.parse(body);
} catch (e) {}
2020-03-07 11:45:40 +08:00
if (pkg && opts.packageFilter) {
// v2 will pass pkgfile
pkg = opts.packageFilter(pkg, /*pkgfile,*/ x); // eslint-disable-line spaced-comment
2020-01-28 13:08:03 +08:00
}
2020-03-07 11:45:40 +08:00
if (pkg && pkg.main) {
2020-01-28 13:08:03 +08:00
if (typeof pkg.main !== 'string') {
var mainError = new TypeError('package “' + pkg.name + '” `main` must be a string');
mainError.code = 'INVALID_PACKAGE_MAIN';
throw mainError;
}
if (pkg.main === '.' || pkg.main === './') {
pkg.main = 'index';
}
try {
var m = loadAsFileSync(path.resolve(x, pkg.main));
if (m) return m;
var n = loadAsDirectorySync(path.resolve(x, pkg.main));
if (n) return n;
} catch (e) {}
}
}
return loadAsFileSync(path.join(x, '/index'));
}
function loadNodeModulesSync(x, start) {
2020-03-07 11:45:40 +08:00
var thunk = function () { return getPackageCandidates(x, start, opts); };
var dirs = packageIterator ? packageIterator(x, start, thunk, opts) : thunk();
2020-01-28 13:08:03 +08:00
for (var i = 0; i < dirs.length; i++) {
var dir = dirs[i];
2020-03-07 11:45:40 +08:00
if (isDirectory(path.dirname(dir))) {
var m = loadAsFileSync(dir);
2020-01-28 13:08:03 +08:00
if (m) return m;
2020-03-07 11:45:40 +08:00
var n = loadAsDirectorySync(dir);
2020-01-28 13:08:03 +08:00
if (n) return n;
}
}
}
};