CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
sync.js217 linesDownload Raw Back to lib
1var isCore = require('is-core-module');2var fs = require('fs');3var path = require('path');4var $Error = require('es-errors');5var $TypeError = require('es-errors/type');6 7var getHomedir = require('./homedir');8var caller = require('./caller');9var nodeModulesPaths = require('./node-modules-paths');10var normalizeOptions = require('./normalize-options');11 12var realpathFS = process.platform !== 'win32' && fs.realpathSync && typeof fs.realpathSync.native === 'function' ? fs.realpathSync.native : fs.realpathSync;13 14var relativePathRegex = /^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/;15var windowsDriveRegex = /^\w:[/\\]*$/;16var nodeModulesRegex = /[/\\]node_modules[/\\]*$/;17 18var homedir = getHomedir();19function defaultPaths() {20    if (!homedir) return [];21    return [22        path.join(homedir, '.node_modules'),23        path.join(homedir, '.node_libraries')24    ];25}26 27var defaultIsFile = function isFile(file) {28    try {29        var stat = fs.statSync(file, { throwIfNoEntry: false });30    } catch (e) {31        if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;32        throw e;33    }34    return !!stat && (stat.isFile() || stat.isFIFO());35};36 37var defaultIsDir = function isDirectory(dir) {38    try {39        var stat = fs.statSync(dir, { throwIfNoEntry: false });40    } catch (e) {41        if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;42        throw e;43    }44    return !!stat && stat.isDirectory();45};46 47var defaultRealpathSync = function realpathSync(x) {48    try {49        return realpathFS(x);50    } catch (realpathErr) {51        if (realpathErr.code !== 'ENOENT') {52            throw realpathErr;53        }54    }55    return x;56};57 58function maybeRealpathSync(realpathSync, x, opts) {59    if (opts && opts.preserveSymlinks === false) {60        return realpathSync(x);61    }62    return x;63}64 65function defaultReadPackageSync(readFileSync, pkgfile) {66    var body = readFileSync(pkgfile);67    try {68        var pkg = JSON.parse(body);69        return pkg;70    } catch (jsonErr) {}71}72 73function getPackageCandidates(x, start, opts) {74    var dirs = nodeModulesPaths(start, opts, x);75    for (var i = 0; i < dirs.length; i++) {76        dirs[i] = path.join(dirs[i], x);77    }78    return dirs;79}80 81module.exports = function resolveSync(x, options) {82    if (typeof x !== 'string') {83        throw new $TypeError('Path must be a string.');84    }85    var opts = normalizeOptions(x, options);86 87    var isFile = opts.isFile || defaultIsFile;88    var readFileSync = opts.readFileSync || fs.readFileSync;89    var isDirectory = opts.isDirectory || defaultIsDir;90    var realpathSync = opts.realpathSync || defaultRealpathSync;91    var readPackageSync = opts.readPackageSync || defaultReadPackageSync;92    if (opts.readFileSync && opts.readPackageSync) {93        throw new $TypeError('`readFileSync` and `readPackageSync` are mutually exclusive.');94    }95    var packageIterator = opts.packageIterator;96 97    var extensions = opts.extensions || ['.js'];98    var includeCoreModules = opts.includeCoreModules !== false;99    var basedir = opts.basedir || path.dirname(caller());100    var parent = opts.filename || basedir;101 102    opts.paths = opts.paths || defaultPaths();103 104    // ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory105    var absoluteStart = maybeRealpathSync(realpathSync, path.resolve(basedir), opts);106 107    if (relativePathRegex.test(x)) {108        var res = path.resolve(absoluteStart, x);109        if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';110        var m = loadAsFileSync(res) || loadAsDirectorySync(res);111        if (m) return maybeRealpathSync(realpathSync, m, opts);112    } else if (includeCoreModules && isCore(x)) {113        return x;114    } else {115        var n = loadNodeModulesSync(x, absoluteStart);116        if (n) return maybeRealpathSync(realpathSync, n, opts);117    }118 119    var err = new $Error("Cannot find module '" + x + "' from '" + parent + "'");120    err.code = 'MODULE_NOT_FOUND';121    throw err;122 123    function loadAsFileSync(x) {124        var pkg = loadpkg(path.dirname(x));125 126        if (pkg && pkg.dir && pkg.pkg && opts.pathFilter) {127            var rfile = path.relative(pkg.dir, x);128            var r = opts.pathFilter(pkg.pkg, x, rfile);129            if (r) {130                x = path.resolve(pkg.dir, r); // eslint-disable-line no-param-reassign131            }132        }133 134        if (isFile(x)) {135            return x;136        }137 138        for (var i = 0; i < extensions.length; i++) {139            var file = x + extensions[i];140            if (isFile(file)) {141                return file;142            }143        }144    }145 146    function loadpkg(dir) {147        if (dir === '' || dir === '/') return;148        if (process.platform === 'win32' && windowsDriveRegex.test(dir)) {149            return;150        }151        if (nodeModulesRegex.test(dir)) return;152 153        var pkgfile = path.join(maybeRealpathSync(realpathSync, dir, opts), 'package.json');154 155        if (!isFile(pkgfile)) {156            return loadpkg(path.dirname(dir));157        }158 159        var pkg = readPackageSync(readFileSync, pkgfile);160 161        if (pkg && opts.packageFilter) {162            // v2 will pass pkgfile163            pkg = opts.packageFilter(pkg, /*pkgfile,*/ dir); // eslint-disable-line spaced-comment164        }165 166        return { pkg: pkg, dir: dir };167    }168 169    function loadAsDirectorySync(x) {170        var pkgfile = path.join(maybeRealpathSync(realpathSync, x, opts), '/package.json');171        if (isFile(pkgfile)) {172            try {173                var pkg = readPackageSync(readFileSync, pkgfile);174            } catch (e) {}175 176            if (pkg && opts.packageFilter) {177                // v2 will pass pkgfile178                pkg = opts.packageFilter(pkg, /*pkgfile,*/ x); // eslint-disable-line spaced-comment179            }180 181            if (pkg && pkg.main) {182                if (typeof pkg.main !== 'string') {183                    var mainError = new $TypeError('package “' + pkg.name + '” `main` must be a string');184                    mainError.code = 'INVALID_PACKAGE_MAIN';185                    throw mainError;186                }187                if (pkg.main === '.' || pkg.main === './') {188                    pkg.main = 'index';189                }190                try {191                    var m = loadAsFileSync(path.resolve(x, pkg.main));192                    if (m) return m;193                    var n = loadAsDirectorySync(path.resolve(x, pkg.main));194                    if (n) return n;195                } catch (e) {}196            }197        }198 199        return loadAsFileSync(path.join(x, '/index'));200    }201 202    function loadNodeModulesSync(x, start) {203        var thunk = function () { return getPackageCandidates(x, start, opts); };204        var dirs = packageIterator ? packageIterator(x, start, thunk, opts) : thunk();205 206        for (var i = 0; i < dirs.length; i++) {207            var dir = dirs[i];208            if (isDirectory(path.dirname(dir))) {209                var m = loadAsFileSync(dir);210                if (m) return m;211                var n = loadAsDirectorySync(dir);212                if (n) return n;213            }214        }215    }216};217