package/lib/cache-group.js000644 0000000532 3560116604 012561 0ustar00000000 000000 'use strict'; const Cache = require("./cache"); module.exports = class CacheGroup { constructor() { this.MODULE_ENTRY = new Cache(); this.PATH = new Cache(); this.REAL_FILE_PATH = new Cache(); this.REAL_DIRECTORY_PATH = new Cache(); Object.freeze(this); } }; //# sourceMappingURL=cache-group.js.mappackage/lib/cache.js000644 0000001462 3560116604 011432 0ustar00000000 000000 'use strict'; function makeCache() { // object with no prototype const cache = Object.create(null); // force the jit to immediately realize this object is a dictionary. This // should prevent the JIT from going wastefully one direction (fast mode) // then going another (dict mode) after cache['_cache'] = 1; delete cache['_cache']; return cache; } module.exports = class Cache { constructor() { this._store = makeCache(); } set(key, value) { return (this._store[key] = value); } get(key) { return this._store[key]; } has(key) { return key in this._store; } delete(key) { delete this._store[key]; } get size() { return Object.keys(this._store).length; } }; //# sourceMappingURL=cache.js.mappackage/lib/index.js000644 0000011452 3560116604 011476 0ustar00000000 000000 'use strict'; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; const path_1 = __importDefault(require("path")); const resolve_package_path_1 = __importDefault(require("./resolve-package-path")); const rethrow_unless_code_1 = __importDefault(require("./rethrow-unless-code")); const ALLOWED_ERROR_CODES = [ // resolve package error codes 'MODULE_NOT_FOUND', // Yarn PnP Error Codes 'UNDECLARED_DEPENDENCY', 'MISSING_PEER_DEPENDENCY', 'MISSING_DEPENDENCY' ]; const CacheGroup = require("./cache-group"); const Cache = require("./cache"); const getRealFilePath = resolve_package_path_1.default._getRealFilePath; const getRealDirectoryPath = resolve_package_path_1.default._getRealDirectoryPath; const __findUpPackagePath = resolve_package_path_1.default._findUpPackagePath; let CACHE = new CacheGroup(); let FIND_UP_CACHE = new Cache(); let pnp; try { // eslint-disable-next-line node/no-missing-require pnp = require('pnpapi'); } catch (error) { // not in Yarn PnP; not a problem } /** * Search each directory in the absolute path `baseDir`, from leaf to root, for * a `package.json`, and return the first match, or `null` if no `package.json` * was found. * * @public * @param {string} baseDir - an absolute path in which to search for a `package.json` * @param {CacheGroup|boolean} [_cache] (optional) * * if true: will choose the default global cache * * if false: will not cache * * if undefined or omitted, will choose the default global cache * * otherwise we assume the argument is an external cache of the form provided by resolve-package-path/lib/cache-group.js * * @return {string|null} a full path to the resolved package.json if found or null if not */ function _findUpPackagePath(baseDir, _cache) { let cache; if (_cache === undefined || _cache === null || _cache === true) { // if no cache specified, or if cache is true then use the global cache cache = FIND_UP_CACHE; } else if (_cache === false) { // if cache is explicity false, create a throw-away cache; cache = new Cache(); } else { // otherwise, assume the user has provided an alternative cache for the following form: // provided by resolve-package-path/lib/cache-group.js cache = _cache; } let absoluteStart = path_1.default.resolve(baseDir); return __findUpPackagePath(cache, absoluteStart); } function resolvePackagePath(target, baseDir, _cache) { let cache; if (_cache === undefined || _cache === null || _cache === true) { // if no cache specified, or if cache is true then use the global cache cache = CACHE; } else if (_cache === false) { // if cache is explicity false, create a throw-away cache; cache = new CacheGroup(); } else { // otherwise, assume the user has provided an alternative cache for the following form: // provided by resolve-package-path/lib/cache-group.js cache = _cache; } if (baseDir.charAt(baseDir.length - 1) !== path_1.default.sep) { baseDir = `${baseDir}${path_1.default.sep}`; } const key = target + '\x00' + baseDir; let pkgPath; if (cache.PATH.has(key)) { pkgPath = cache.PATH.get(key); } else { try { // the custom `pnp` code here can be removed when yarn 1.13 is the // current release. This is due to Yarn 1.13 and resolve interoperating // together seamlessly. pkgPath = pnp ? pnp.resolveToUnqualified(target + '/package.json', baseDir) : (0, resolve_package_path_1.default)(cache, target, baseDir); } catch (e) { (0, rethrow_unless_code_1.default)(e, ...ALLOWED_ERROR_CODES); pkgPath = null; } cache.PATH.set(key, pkgPath); } return pkgPath; } resolvePackagePath._resetCache = function () { CACHE = new CacheGroup(); FIND_UP_CACHE = new Cache(); }; // eslint-disable-next-line no-redeclare (function (resolvePackagePath) { resolvePackagePath._FIND_UP_CACHE = FIND_UP_CACHE; resolvePackagePath.findUpPackagePath = _findUpPackagePath; })(resolvePackagePath || (resolvePackagePath = {})); Object.defineProperty(resolvePackagePath, '_CACHE', { get: function () { return CACHE; }, }); Object.defineProperty(resolvePackagePath, '_FIND_UP_CACHE', { get: function () { return FIND_UP_CACHE; }, }); resolvePackagePath.getRealFilePath = function (filePath) { return getRealFilePath(CACHE.REAL_FILE_PATH, filePath); }; resolvePackagePath.getRealDirectoryPath = function (directoryPath) { return getRealDirectoryPath(CACHE.REAL_DIRECTORY_PATH, directoryPath); }; module.exports = resolvePackagePath; //# sourceMappingURL=index.js.mappackage/lib/resolve-package-path.js000644 0000024762 3560116604 014401 0ustar00000000 000000 'use strict'; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; // credit goes to https://github.com/davecombs // extracted in part from: https://github.com/stefanpenner/hash-for-dep/blob/15b2ebcf22024ceb2eb7907f8c412ae40f87b15e/lib/resolve-package-path.js#L1 // const fs = require("fs"); const path = require("path"); const pathRoot = require("path-root"); const rethrow_unless_code_1 = __importDefault(require("./rethrow-unless-code")); /* * Define a regex that will match against the 'name' value passed into * resolvePackagePath. The regex corresponds to the following test: * Match any of the following 3 alternatives: * * 1) dot, then optional second dot, then / or nothing i.e. . ./ .. ../ OR * 2) / i.e. / OR * 3) (A-Za-z colon - [optional]), then / or \ i.e. optional drive letter + colon, then / or \ * * Basically, the three choices mean "explicitly relative or absolute path, on either * Unix/Linux or Windows" */ const ABSOLUTE_OR_RELATIVE_PATH_REGEX = /^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/; const shouldPreserveSymlinks = require("./should-preserve-symlinks"); const PRESERVE_SYMLINKS = shouldPreserveSymlinks(process); /* * Resolve the real path for a file. Return null if does not * exist or is not a file or FIFO, return the real path otherwise. * * Cache the result in the passed-in cache for performance, * keyed on the filePath passed in. * * NOTE: Because this is a private method, it does not attempt to normalize * the path passed in - it assumes the caller has done that. * * @private * @method _getRealFilePath * @param {Cache} realFilePathCache the Cache object to cache the real (resolved) * path in, keyed by filePath. See lib/cache.js and lib/cache-group.js * @param {String} filePath the path to the file of interest (which must have * been normalized, but not necessarily resolved to a real path). * @return {String} real path or null */ function _getRealFilePath(realFilePathCache, filePath) { if (realFilePathCache.has(filePath)) { return realFilePathCache.get(filePath); // could be null } let realPath = null; // null = 'FILE NOT FOUND' try { const stat = fs.statSync(filePath); // I don't know if Node would handle having the filePath actually // be a FIFO, but as the following is also part of the node-resolution // algorithm in resolve.sync(), we'll do the same check here. if (stat.isFile() || stat.isFIFO()) { if (PRESERVE_SYMLINKS) { realPath = filePath; } else { realPath = fs.realpathSync(filePath); } } } catch (e) { (0, rethrow_unless_code_1.default)(e, 'ENOENT'); } realFilePathCache.set(filePath, realPath); return realPath; } /* * Resolve the real path for a directory, return null if does not * exist or is not a directory, return the real path otherwise. * * @param {Cache} realDirectoryPathCache the Cache object to cache the real (resolved) * path in, keyed by directoryPath. See lib/cache.js and lib/cache-group.js * @param {String} directoryPath the path to the directory of interest (which must have * been normalized, but not necessarily resolved to a real path). * @return {String} real path or null */ function _getRealDirectoryPath(realDirectoryPathCache, directoryPath) { if (realDirectoryPathCache.has(directoryPath)) { return realDirectoryPathCache.get(directoryPath); // could be null } let realPath = null; try { const stat = fs.statSync(directoryPath); if (stat.isDirectory()) { if (PRESERVE_SYMLINKS) { realPath = directoryPath; } else { realPath = fs.realpathSync(directoryPath); } } } catch (e) { (0, rethrow_unless_code_1.default)(e, 'ENOENT', 'ENOTDIR'); } realDirectoryPathCache.set(directoryPath, realPath); return realPath; } /* * Given a package 'name' and starting directory, resolve to a real (existing) file path. * * Do it similar to how it is done in resolve.sync() - travel up the directory hierarchy, * attaching 'node-modules' to each directory and seeing if the directory exists and * has the relevant 'package.json' file we're searching for. It is *much* faster than * resolve.sync(), because we don't test that the requested name is a directory. * This routine assumes that it is only called when we don't already have * the cached entry. * * NOTE: it is valid for 'name' to be an absolute or relative path. * Because this is an internal routine, we'll require that 'dir' be non-empty * if this is called, to make things simpler (see resolvePackagePath). * * @param realFilePathCache the cache containing the real paths corresponding to * various file and directory paths (which may or may not be already resolved). * * @param name the 'name' of the module, i.e. x in require(x), but with * '/package.json' on the end. It is NOT referring to a directory (so we don't * have to do the directory checks that resolve.sync does). * NOTE: because this is an internal routine, for speed it does not check * that '/package.json' is actually the end of the name. * * @param dir the directory (MUST BE non-empty, and valid) to start from, appending the name to the * directory and checking that the file exists. Go up the directory hierarchy from there. * if name is itself an absolute path, * * @result the path to the actual package.json file that's found, or null if not. */ function _findPackagePath(realFilePathCache, name, dir) { const fsRoot = pathRoot(dir); let currPath = dir; while (currPath !== fsRoot) { // when testing for 'node_modules', need to allow names like NODE_MODULES, // which can occur with case-insensitive OSes. let endsWithNodeModules = path.basename(currPath).toLowerCase() === 'node_modules'; let filePath = path.join(currPath, endsWithNodeModules ? '' : 'node_modules', name); let realPath = _getRealFilePath(realFilePathCache, filePath); if (realPath) { return realPath; } if (endsWithNodeModules) { // go up past the ending node_modules directory so the next dirname // goes up past that (if ending in node_modules, going up just one // directory below will then add 'node_modules' on the next loop and // re-process this same node_modules directory. currPath = path.dirname(currPath); } currPath = path.dirname(currPath); } return null; } /* * Resolve the path to the nearest `package.json` from the given initial search * directory. * * @param {Cache} findUpCache - a cache of memoized results that is * prioritized to avoid I/O. * * @param {string} initialSearchDir - the normalized path to start searching * from. * * @return {string | null} - the deepest directory on the path to root from * `initialSearchDir` that contains a {{package.json}}, or `null` if no such * directory exists. */ function _findUpPackagePath(findUpCache, initialSearchDir) { let previous; let dir = initialSearchDir; let maybePackageJsonPath; let result = null; do { if (findUpCache.has(dir)) { result = findUpCache.get(dir); break; } maybePackageJsonPath = path.join(dir, 'package.json'); if (fs.existsSync(maybePackageJsonPath)) { result = maybePackageJsonPath; break; } previous = dir; dir = path.dirname(dir); } while (dir !== previous); findUpCache.set(initialSearchDir, result); return result; } /* * Resolve the path to a module's package.json file, if it exists. The * name and dir are as in hashForDep and ModuleEntry.locate. * * @param caches an instance of CacheGroup where information will be cached * during processing. * * @param name the 'name' of the module. The name may also be a path, * either relative or absolute. The path must be to a module DIRECTORY, NOT to the * package.json file in the directory, as we attach 'package.json' here. * * @param dir (optional) the root directory to run the path resolution from. * if dir is not provided, __dirname for this module is used instead. * * @return the realPath corresponding to the module's package.json file, or null * if that file is not found or is not a file. * * Note: 'name' is expected in the format expected for require(x), i.e., it is * resolved using the Node path-normalization rules. */ function resolvePackagePath(caches, name, dir) { if (typeof name !== 'string' || name.length === 0) { throw new TypeError("resolvePackagePath: 'name' must be a non-zero-length string."); } // Perform tests similar to those in resolve.sync(). let basedir = dir || __dirname; // Ensure that basedir is an absolute path at this point. If it does not refer to // a real directory, go up the path until a real directory is found, or return an error. // BUG!: this will never throw an exception, at least on Unix/Linux. If the path is // relative, path.resolve() will make it absolute by putting the current directory // before it, so it won't fail. If the path is already absolute, / will always be // valid, so again it won't fail. let absoluteStart = path.resolve(basedir); while (_getRealDirectoryPath(caches.REAL_DIRECTORY_PATH, absoluteStart) === null) { absoluteStart = path.dirname(absoluteStart); } if (!absoluteStart) { let error = new TypeError("resolvePackagePath: 'dir' or one of the parent directories in its path must refer to a valid directory."); error.code = 'MODULE_NOT_FOUND'; throw error; } if (ABSOLUTE_OR_RELATIVE_PATH_REGEX.test(name)) { let res = path.resolve(absoluteStart, name); return _getRealFilePath(caches.REAL_FILE_PATH, path.join(res, 'package.json')); // XXX Do we need to handle the core(x) case too? Not sure. } else { return _findPackagePath(caches.REAL_FILE_PATH, path.join(name, 'package.json'), absoluteStart); } } resolvePackagePath._findPackagePath = _findPackagePath; resolvePackagePath._findUpPackagePath = _findUpPackagePath; resolvePackagePath._getRealFilePath = _getRealFilePath; resolvePackagePath._getRealDirectoryPath = _getRealDirectoryPath; module.exports = resolvePackagePath; //# sourceMappingURL=resolve-package-path.js.mappackage/lib/rethrow-unless-code.js000644 0000000724 3560116604 014300 0ustar00000000 000000 "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function rethrowUnlessCode(maybeError, ...codes) { if (maybeError !== null && typeof maybeError === 'object') { const code = maybeError.code; for (const allowed of codes) { if (code === allowed) { return; } } } throw maybeError; } exports.default = rethrowUnlessCode; //# sourceMappingURL=rethrow-unless-code.js.mappackage/lib/should-preserve-symlinks.js000644 0000000577 3560116604 015373 0ustar00000000 000000 'use strict'; function includes(array, entry) { for (let i = 0; i < array.length; i++) { if (array[i] === entry) { return true; } } return false; } module.exports = function (process) { return !!process.env.NODE_PRESERVE_SYMLINKS || includes(process.execArgv, '--preserve-symlinks'); }; //# sourceMappingURL=should-preserve-symlinks.js.mappackage/package.json000644 0000002631 3560116604 011550 0ustar00000000 000000 { "name": "resolve-package-path", "description": "a special purpose fast memoizing way to resolve a node modules package.json", "version": "4.0.3", "repository": "git@github.com:stefanpenner/resolve-package-path.git", "main": "lib/index.js", "types": "lib/index.d.ts", "license": "MIT", "dependencies": { "path-root": "^0.1.1" }, "devDependencies": { "@types/chai": "^4.2.18", "@types/fs-extra": "^9.0.11", "@types/mocha": "^9.0.0", "@types/node": "^16.9.1", "@typescript-eslint/eslint-plugin": "^4.26.0", "@typescript-eslint/parser": "^4.26.0", "chai": "^4.3.4", "eslint": "^7.27.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-mocha": "^9.0.0", "eslint-plugin-node": "^11.1.0", "eslint-plugin-prettier": "^4.0.0", "execa": "^5.1.0", "fixturify-project": "^4.0.0", "fs-extra": "^10.0.0", "mocha": "^9.1.1", "npm-run-all": "^4.1.5", "prettier": "^2.3.0", "ts-mocha": "^8.0.0", "typescript": "^4.3.2" }, "scripts": { "prepare": "tsc -b .", "watch": "tsc --watch .", "clean": "tsc -b --clean .", "test": "ts-mocha tests/*-test.ts", "test:inspect": "ts-mocha inspect tests/*-test.ts", "test:ts:debug": "ts-mocha debug tests/*-test.ts", "lint": "eslint ." }, "files": [ "lib/" ], "engines": { "node": ">= 12" }, "volta": { "node": "12.19.0", "yarn": "1.22.10" } } package/lib/cache-group.js.map000644 0000005630 3560116604 013341 0ustar00000000 000000 {"version":3,"file":"cache-group.js","sourceRoot":"","sources":["../src/cache-group.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,iCAAkC;AA2ClC,iBAAS,MAAM,UAAU;IAKvB;QAJA,iBAAY,GAAG,IAAI,KAAK,EAAE,CAAC;QAC3B,SAAI,GAAG,IAAI,KAAK,EAAE,CAAC;QACnB,mBAAc,GAAG,IAAI,KAAK,EAAE,CAAC;QAC7B,wBAAmB,GAAG,IAAI,KAAK,EAAE,CAAC;QAEhC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;CACF,CAAC","sourcesContent":["'use strict';\n\nimport Cache = require('./cache');\n\n/*\n * CacheGroup is used to both speed up and ensure consistency of hashForDep.\n *\n * The CacheGroup contains three separate caches:\n *\n * - MODULE_ENTRY: the cache of realPathKey => ModuleEntry objects.\n * Each ModuleEntry contains information about a particular module\n * found during hash-for-dep processing. realPathKey is a hash\n * of the resolved real path to the module's package.json file.\n *\n * Having the real path means that when resolving dependencies, we can\n * take the resolved paths, hash them and check quickly for cache entries,\n * speeding creation of the cache. Because many modules may refer to\n * the same module as a dependency, this eliminates duplication and having\n * to reread package.json files.\n *\n * However, that also means we need a second cache to map from the original\n * name and dir passed to hashForDep to the final resolved path for the\n * module's package.json file.\n *\n * - PATH: the cache of nameDirKey => realPathKey. nameDirKey is a hash of the\n * combination of the name and starting directory passed to hashForDep.\n * realPathKey is a hash of the resulting real path to the relevant package.json file.\n *\n * - REAL_FILE_PATH: the cache of filePath => resolved realPath for relevant files.\n * When determining the location of a file, the file path may involve links.\n * This cache is keyed on the original file path, with a value of the real path.\n * This cache helps to speed up path resolution in both cases by minimizing\n * the use of costly 'fs.statSync' calls.\n *\n * - REAL_DIRECTORY_PATH: the cache of filePath => resolved realPath for relevant directories.\n * When determining the location of a file by going up the node_modules chain,\n * paths to intervening directories may contain links. Similar to REAL_FILE_PATH,\n * this cache is keyed on the original directory path, with a value of the real path.\n * This cache helps to speed up path resolution in both cases by minimizing\n * the use of costly 'fs.statSync' calls.\n *\n * Note: when discussing 'real paths' above, the paths are normalized by routines\n * like path.join() or path.resolve(). We do nothing beyond that (e.g., we do not attempt\n * to force the paths into POSIX style.)\n */\nexport = class CacheGroup {\n MODULE_ENTRY = new Cache();\n PATH = new Cache();\n REAL_FILE_PATH = new Cache();\n REAL_DIRECTORY_PATH = new Cache();\n constructor() {\n Object.freeze(this);\n }\n};\n"]}package/lib/cache.js.map000644 0000003264 3560116604 012210 0ustar00000000 000000 {"version":3,"file":"cache.js","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,SAAS,SAAS;IAChB,2BAA2B;IAC3B,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAElC,yEAAyE;IACzE,yEAAyE;IACzE,uCAAuC;IACvC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;IACpB,OAAO,KAAK,CAAC,QAAQ,CAAC,CAAC;IAEvB,OAAO,KAAK,CAAC;AACf,CAAC;AAED,iBAAS,MAAM,KAAK;IAElB;QACE,IAAI,CAAC,MAAM,GAAG,SAAS,EAAE,CAAC;IAC5B,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAU;QACzB,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC;IACpC,CAAC;IAED,GAAG,CAAC,GAAW;QACb,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IAED,GAAG,CAAC,GAAW;QACb,OAAO,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC;IAC5B,CAAC;IAED,MAAM,CAAC,GAAW;QAChB,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IAED,IAAI,IAAI;QACN,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC;IACzC,CAAC;CACF,CAAC","sourcesContent":["'use strict';\n\nfunction makeCache() {\n // object with no prototype\n const cache = Object.create(null);\n\n // force the jit to immediately realize this object is a dictionary. This\n // should prevent the JIT from going wastefully one direction (fast mode)\n // then going another (dict mode) after\n cache['_cache'] = 1;\n delete cache['_cache'];\n\n return cache;\n}\n\nexport = class Cache {\n _store: { [key: string]: string };\n constructor() {\n this._store = makeCache();\n }\n\n set(key: string, value: any) {\n return (this._store[key] = value);\n }\n\n get(key: string) {\n return this._store[key];\n }\n\n has(key: string) {\n return key in this._store;\n }\n\n delete(key: string) {\n delete this._store[key];\n }\n\n get size() {\n return Object.keys(this._store).length;\n }\n};\n"]}package/lib/index.js.map000644 0000017633 3560116604 012261 0ustar00000000 000000 {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;AAEb,gDAAwB;AACxB,kFAA8D;AAC9D,gFAAsD;AACtD,MAAM,mBAAmB,GAAG;IAC1B,8BAA8B;IAC9B,kBAAkB;IAElB,uBAAuB;IACvB,uBAAuB;IACvB,yBAAyB;IACzB,oBAAoB;CACrB,CAAC;AAEF,4CAA6C;AAC7C,iCAAkC;AAClC,MAAM,eAAe,GAAG,8BAAwB,CAAC,gBAAgB,CAAC;AAClE,MAAM,oBAAoB,GAAG,8BAAwB,CAAC,qBAAqB,CAAC;AAC5E,MAAM,mBAAmB,GAAG,8BAAwB,CAAC,kBAAkB,CAAC;AAExE,IAAI,KAAK,GAAG,IAAI,UAAU,EAAE,CAAC;AAC7B,IAAI,aAAa,GAAG,IAAI,KAAK,EAAE,CAAC;AAChC,IAAI,GAAQ,CAAC;AAEb,IAAI;IACF,mDAAmD;IACnD,GAAG,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;CACzB;AAAC,OAAO,KAAK,EAAE;IACd,iCAAiC;CAClC;AAED;;;;;;;;;;;;;;GAcG;AACH,SAAS,kBAAkB,CAAC,OAAe,EAAE,MAAwB;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AAqB;IACvE,OAAO,oBAAoB,CAAC,KAAK,CAAC,mBAAmB,EAAE,aAAa,CAAC,CAAC;AACxE,CAAC,CAAC;AA3EF,iBAAS,kBAAkB,CAAC","sourcesContent":["'use strict';\n\nimport path from 'path';\nimport customResolvePackagePath from './resolve-package-path';\nimport rethrowUnlessCode from './rethrow-unless-code';\nconst ALLOWED_ERROR_CODES = [\n // resolve package error codes\n 'MODULE_NOT_FOUND',\n\n // Yarn PnP Error Codes\n 'UNDECLARED_DEPENDENCY',\n 'MISSING_PEER_DEPENDENCY',\n 'MISSING_DEPENDENCY'\n];\n\nimport CacheGroup = require('./cache-group');\nimport Cache = require('./cache');\nconst getRealFilePath = customResolvePackagePath._getRealFilePath;\nconst getRealDirectoryPath = customResolvePackagePath._getRealDirectoryPath;\nconst __findUpPackagePath = customResolvePackagePath._findUpPackagePath;\n\nlet CACHE = new CacheGroup();\nlet FIND_UP_CACHE = new Cache();\nlet pnp: any;\n\ntry {\n // eslint-disable-next-line node/no-missing-require\n pnp = require('pnpapi');\n} catch (error) {\n // not in Yarn PnP; not a problem\n}\n\n/**\n * Search each directory in the absolute path `baseDir`, from leaf to root, for\n * a `package.json`, and return the first match, or `null` if no `package.json`\n * was found.\n *\n * @public\n * @param {string} baseDir - an absolute path in which to search for a `package.json`\n * @param {CacheGroup|boolean} [_cache] (optional)\n * * if true: will choose the default global cache\n * * if false: will not cache\n * * if undefined or omitted, will choose the default global cache\n * * otherwise we assume the argument is an external cache of the form provided by resolve-package-path/lib/cache-group.js\n *\n * @return {string|null} a full path to the resolved package.json if found or null if not\n */\nfunction _findUpPackagePath(baseDir: string, _cache?: Cache | boolean) {\n let cache;\n if (_cache === undefined || _cache === null || _cache === true) {\n // if no cache specified, or if cache is true then use the global cache\n cache = FIND_UP_CACHE;\n } else if (_cache === false) {\n // if cache is explicity false, create a throw-away cache;\n cache = new Cache();\n } else {\n // otherwise, assume the user has provided an alternative cache for the following form:\n // provided by resolve-package-path/lib/cache-group.js\n cache = _cache;\n }\n\n let absoluteStart = path.resolve(baseDir);\n\n return __findUpPackagePath(cache, absoluteStart);\n}\n\n/*\n * @public\n *\n * @method resolvePackagePathSync\n * @param {string} name name of the dependency module.\n * @param {string} baseDir root dir to run the resolve from\n * @param {Boolean|CustomCache} (optional)\n * * if true: will choose the default global cache\n * * if false: will not cache\n * * if undefined or omitted, will choose the default global cache\n * * otherwise we assume the argument is an external cache of the form provided by resolve-package-path/lib/cache-group.js\n *\n * @return {string|null} a full path to the resolved package.json if found or null if not\n */\nexport = resolvePackagePath;\nfunction resolvePackagePath(\n target: string,\n baseDir: string,\n _cache?: CacheGroup | boolean,\n): string | null {\n let cache;\n\n if (_cache === undefined || _cache === null || _cache === true) {\n // if no cache specified, or if cache is true then use the global cache\n cache = CACHE;\n } else if (_cache === false) {\n // if cache is explicity false, create a throw-away cache;\n cache = new CacheGroup();\n } else {\n // otherwise, assume the user has provided an alternative cache for the following form:\n // provided by resolve-package-path/lib/cache-group.js\n cache = _cache;\n }\n\n if (baseDir.charAt(baseDir.length - 1) !== path.sep) {\n baseDir = `${baseDir}${path.sep}`;\n }\n\n const key = target + '\\x00' + baseDir;\n\n let pkgPath;\n\n if (cache.PATH.has(key)) {\n pkgPath = cache.PATH.get(key);\n } else {\n try {\n // the custom `pnp` code here can be removed when yarn 1.13 is the\n // current release. This is due to Yarn 1.13 and resolve interoperating\n // together seamlessly.\n pkgPath = pnp\n ? pnp.resolveToUnqualified(target + '/package.json', baseDir)\n : customResolvePackagePath(cache, target, baseDir);\n } catch (e) {\n rethrowUnlessCode(e, ...ALLOWED_ERROR_CODES);\n pkgPath = null;\n }\n\n cache.PATH.set(key, pkgPath);\n }\n return pkgPath;\n}\n\nresolvePackagePath._resetCache = function () {\n CACHE = new CacheGroup();\n FIND_UP_CACHE = new Cache();\n};\n// eslint-disable-next-line no-redeclare\nmodule resolvePackagePath {\n export let _CACHE: CacheGroup;\n export let _FIND_UP_CACHE = FIND_UP_CACHE;\n export let findUpPackagePath = _findUpPackagePath;\n}\nObject.defineProperty(resolvePackagePath, '_CACHE', {\n get: function () {\n return CACHE;\n },\n});\nObject.defineProperty(resolvePackagePath, '_FIND_UP_CACHE', {\n get: function () {\n return FIND_UP_CACHE;\n },\n});\n\nresolvePackagePath.getRealFilePath = function (filePath: string) {\n return getRealFilePath(CACHE.REAL_FILE_PATH, filePath);\n};\n\nresolvePackagePath.getRealDirectoryPath = function (directoryPath: string) {\n return getRealDirectoryPath(CACHE.REAL_DIRECTORY_PATH, directoryPath);\n};\n"]}package/lib/resolve-package-path.js.map000644 0000034752 3560116604 015155 0ustar00000000 000000 {"version":3,"file":"resolve-package-path.js","sourceRoot":"","sources":["../src/resolve-package-path.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;AACb,8CAA8C;AAC9C,oJAAoJ;AACpJ,EAAE;AACF,yBAA0B;AAC1B,6BAA8B;AAC9B,sCAAuC;AAIvC,gFAAsD;AAEtD;;;;;;;;;;;GAWG;AAEH,MAAM,+BAA+B,GAAG,yCAAyC,CAAC;AAElF,qEAAsE;AACtE,MAAM,iBAAiB,GAAG,sBAAsB,CAAC,OAAO,CAAC,CAAC;AAC1D;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAS,gBAAgB,CAAC,iBAAwB,EAAE,QAAgB;IAClE,IAAI,iBAAiB,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE;QACnC,OAAO,iBAAiB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,gBAAgB;KACzD;IAED,IAAI,QAAQ,GAAG,IAAI,CAAC,CAAC,0BAA0B;IAE/C,IAAI;QACF,MAAM,IAAI,GAAG,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAEnC,iEAAiE;QACjE,sEAAsE;QACtE,6DAA6D;QAC7D,IAAI,IAAI,CAAC,MAAM,EAAE,IAAI,IAAI,CAAC,MAAM,EAAE,EAAE;YAClC,IAAI,iBAAiB,EAAE;gBACrB,QAAQ,GAAG,QAA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gBAAgB,CAAC,MAAM,CAAC,cAAc,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,cAAc,CAAC,EAAE,aAAa,CAAC,CAAC;KAChG;AACH,CAAC;AAGD,kBAAkB,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;AACvD,kBAAkB,CAAC,kBAAkB,GAAG,kBAAkB,CAAC;AAC3D,kBAAkB,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;AACvD,kBAAkB,CAAC,qBAAqB,GAAG,qBAAqB,CAAC;AAJjE,iBAAS,kBAAkB,CAAC","sourcesContent":["'use strict';\n// credit goes to https://github.com/davecombs\n// extracted in part from: https://github.com/stefanpenner/hash-for-dep/blob/15b2ebcf22024ceb2eb7907f8c412ae40f87b15e/lib/resolve-package-path.js#L1\n//\nimport fs = require('fs');\nimport path = require('path');\nimport pathRoot = require('path-root');\n\nimport Cache = require('./cache');\nimport CacheGroup = require('./cache-group');\nimport rethrowUnlessCode from './rethrow-unless-code';\n\n/*\n * Define a regex that will match against the 'name' value passed into\n * resolvePackagePath. The regex corresponds to the following test:\n * Match any of the following 3 alternatives:\n *\n * 1) dot, then optional second dot, then / or nothing i.e. . ./ .. ../ OR\n * 2) / i.e. / OR\n * 3) (A-Za-z colon - [optional]), then / or \\ i.e. optional drive letter + colon, then / or \\\n *\n * Basically, the three choices mean \"explicitly relative or absolute path, on either\n * Unix/Linux or Windows\"\n */\n\nconst ABSOLUTE_OR_RELATIVE_PATH_REGEX = /^(?:\\.\\.?(?:\\/|$)|\\/|([A-Za-z]:)?[/\\\\])/;\n\nimport shouldPreserveSymlinks = require('./should-preserve-symlinks');\nconst PRESERVE_SYMLINKS = shouldPreserveSymlinks(process);\n/*\n * Resolve the real path for a file. Return null if does not\n * exist or is not a file or FIFO, return the real path otherwise.\n *\n * Cache the result in the passed-in cache for performance,\n * keyed on the filePath passed in.\n *\n * NOTE: Because this is a private method, it does not attempt to normalize\n * the path passed in - it assumes the caller has done that.\n *\n * @private\n * @method _getRealFilePath\n * @param {Cache} realFilePathCache the Cache object to cache the real (resolved)\n * path in, keyed by filePath. See lib/cache.js and lib/cache-group.js\n * @param {String} filePath the path to the file of interest (which must have\n * been normalized, but not necessarily resolved to a real path).\n * @return {String} real path or null\n */\nfunction _getRealFilePath(realFilePathCache: Cache, filePath: string) {\n if (realFilePathCache.has(filePath)) {\n return realFilePathCache.get(filePath); // could be null\n }\n\n let realPath = null; // null = 'FILE NOT FOUND'\n\n try {\n const stat = fs.statSync(filePath);\n\n // I don't know if Node would handle having the filePath actually\n // be a FIFO, but as the following is also part of the node-resolution\n // algorithm in resolve.sync(), we'll do the same check here.\n if (stat.isFile() || stat.isFIFO()) {\n if (PRESERVE_SYMLINKS) {\n realPath = filePath;\n } else {\n realPath = fs.realpathSync(filePath);\n }\n }\n } catch (e) {\n rethrowUnlessCode(e, 'ENOENT');\n }\n\n realFilePathCache.set(filePath, realPath);\n\n return realPath;\n}\n\n/*\n * Resolve the real path for a directory, return null if does not\n * exist or is not a directory, return the real path otherwise.\n *\n * @param {Cache} realDirectoryPathCache the Cache object to cache the real (resolved)\n * path in, keyed by directoryPath. See lib/cache.js and lib/cache-group.js\n * @param {String} directoryPath the path to the directory of interest (which must have\n * been normalized, but not necessarily resolved to a real path).\n * @return {String} real path or null\n */\nfunction _getRealDirectoryPath(realDirectoryPathCache: Cache, directoryPath: string) {\n if (realDirectoryPathCache.has(directoryPath)) {\n return realDirectoryPathCache.get(directoryPath); // could be null\n }\n\n let realPath = null;\n\n try {\n const stat = fs.statSync(directoryPath);\n\n if (stat.isDirectory()) {\n if (PRESERVE_SYMLINKS) {\n realPath = directoryPath;\n } else {\n realPath = fs.realpathSync(directoryPath);\n }\n }\n } catch (e) {\n rethrowUnlessCode(e, 'ENOENT', 'ENOTDIR');\n }\n\n realDirectoryPathCache.set(directoryPath, realPath);\n\n return realPath;\n}\n\n/*\n * Given a package 'name' and starting directory, resolve to a real (existing) file path.\n *\n * Do it similar to how it is done in resolve.sync() - travel up the directory hierarchy,\n * attaching 'node-modules' to each directory and seeing if the directory exists and\n * has the relevant 'package.json' file we're searching for. It is *much* faster than\n * resolve.sync(), because we don't test that the requested name is a directory.\n * This routine assumes that it is only called when we don't already have\n * the cached entry.\n *\n * NOTE: it is valid for 'name' to be an absolute or relative path.\n * Because this is an internal routine, we'll require that 'dir' be non-empty\n * if this is called, to make things simpler (see resolvePackagePath).\n *\n * @param realFilePathCache the cache containing the real paths corresponding to\n * various file and directory paths (which may or may not be already resolved).\n *\n * @param name the 'name' of the module, i.e. x in require(x), but with\n * '/package.json' on the end. It is NOT referring to a directory (so we don't\n * have to do the directory checks that resolve.sync does).\n * NOTE: because this is an internal routine, for speed it does not check\n * that '/package.json' is actually the end of the name.\n *\n * @param dir the directory (MUST BE non-empty, and valid) to start from, appending the name to the\n * directory and checking that the file exists. Go up the directory hierarchy from there.\n * if name is itself an absolute path,\n *\n * @result the path to the actual package.json file that's found, or null if not.\n */\nfunction _findPackagePath(realFilePathCache: Cache, name: string, dir: string) {\n const fsRoot = pathRoot(dir);\n let currPath = dir;\n\n while (currPath !== fsRoot) {\n // when testing for 'node_modules', need to allow names like NODE_MODULES,\n // which can occur with case-insensitive OSes.\n let endsWithNodeModules = path.basename(currPath).toLowerCase() === 'node_modules';\n\n let filePath = path.join(currPath, endsWithNodeModules ? '' : 'node_modules', name);\n\n let realPath = _getRealFilePath(realFilePathCache, filePath);\n\n if (realPath) {\n return realPath;\n }\n\n if (endsWithNodeModules) {\n // go up past the ending node_modules directory so the next dirname\n // goes up past that (if ending in node_modules, going up just one\n // directory below will then add 'node_modules' on the next loop and\n // re-process this same node_modules directory.\n currPath = path.dirname(currPath);\n }\n\n currPath = path.dirname(currPath);\n }\n\n return null;\n}\n\n/*\n * Resolve the path to the nearest `package.json` from the given initial search\n * directory.\n *\n * @param {Cache} findUpCache - a cache of memoized results that is\n * prioritized to avoid I/O.\n *\n * @param {string} initialSearchDir - the normalized path to start searching\n * from.\n *\n * @return {string | null} - the deepest directory on the path to root from\n * `initialSearchDir` that contains a {{package.json}}, or `null` if no such\n * directory exists.\n */\nfunction _findUpPackagePath(findUpCache: Cache, initialSearchDir: string) {\n let previous;\n let dir = initialSearchDir;\n let maybePackageJsonPath;\n let result = null;\n\n do {\n if (findUpCache.has(dir)) {\n result = findUpCache.get(dir);\n break;\n }\n\n maybePackageJsonPath = path.join(dir, 'package.json');\n if (fs.existsSync(maybePackageJsonPath)) {\n result = maybePackageJsonPath;\n break;\n }\n\n previous = dir;\n dir = path.dirname(dir);\n } while (dir !== previous);\n\n findUpCache.set(initialSearchDir, result);\n\n return result;\n}\n\n/*\n * Resolve the path to a module's package.json file, if it exists. The\n * name and dir are as in hashForDep and ModuleEntry.locate.\n *\n * @param caches an instance of CacheGroup where information will be cached\n * during processing.\n *\n * @param name the 'name' of the module. The name may also be a path,\n * either relative or absolute. The path must be to a module DIRECTORY, NOT to the\n * package.json file in the directory, as we attach 'package.json' here.\n *\n * @param dir (optional) the root directory to run the path resolution from.\n * if dir is not provided, __dirname for this module is used instead.\n *\n * @return the realPath corresponding to the module's package.json file, or null\n * if that file is not found or is not a file.\n *\n * Note: 'name' is expected in the format expected for require(x), i.e., it is\n * resolved using the Node path-normalization rules.\n */\nfunction resolvePackagePath(caches: CacheGroup, name?: string, dir?: string) {\n if (typeof name !== 'string' || name.length === 0) {\n throw new TypeError(\"resolvePackagePath: 'name' must be a non-zero-length string.\");\n }\n\n // Perform tests similar to those in resolve.sync().\n let basedir = dir || __dirname;\n\n // Ensure that basedir is an absolute path at this point. If it does not refer to\n // a real directory, go up the path until a real directory is found, or return an error.\n\n // BUG!: this will never throw an exception, at least on Unix/Linux. If the path is\n // relative, path.resolve() will make it absolute by putting the current directory\n // before it, so it won't fail. If the path is already absolute, / will always be\n // valid, so again it won't fail.\n let absoluteStart = path.resolve(basedir);\n\n while (_getRealDirectoryPath(caches.REAL_DIRECTORY_PATH, absoluteStart) === null) {\n absoluteStart = path.dirname(absoluteStart);\n }\n\n if (!absoluteStart) {\n let error = new TypeError(\n \"resolvePackagePath: 'dir' or one of the parent directories in its path must refer to a valid directory.\",\n );\n (error as any).code = 'MODULE_NOT_FOUND';\n throw error;\n }\n\n if (ABSOLUTE_OR_RELATIVE_PATH_REGEX.test(name)) {\n let res = path.resolve(absoluteStart, name);\n return _getRealFilePath(caches.REAL_FILE_PATH, path.join(res, 'package.json'));\n\n // XXX Do we need to handle the core(x) case too? Not sure.\n } else {\n return _findPackagePath(caches.REAL_FILE_PATH, path.join(name, 'package.json'), absoluteStart);\n }\n}\n\nexport = resolvePackagePath;\nresolvePackagePath._findPackagePath = _findPackagePath;\nresolvePackagePath._findUpPackagePath = _findUpPackagePath;\nresolvePackagePath._getRealFilePath = _getRealFilePath;\nresolvePackagePath._getRealDirectoryPath = _getRealDirectoryPath;\n"]}package/lib/rethrow-unless-code.js.map000644 0000001404 3560116604 015050 0ustar00000000 000000 {"version":3,"file":"rethrow-unless-code.js","sourceRoot":"","sources":["../src/rethrow-unless-code.ts"],"names":[],"mappings":";;AAAA,SAAwB,iBAAiB,CAAC,UAAmB,EAAE,GAAG,KAAoB;IACpF,IAAI,UAAU,KAAK,IAAI,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE;QACzD,MAAM,IAAI,GAAI,UAA4B,CAAC,IAAI,CAAC;QAChD,KAAK,MAAM,OAAO,IAAI,KAAK,EAAE;YAC3B,IAAI,IAAI,KAAK,OAAO,EAAE;gBACpB,OAAO;aACR;SACF;KACF;IAED,MAAM,UAAU,CAAC;AACnB,CAAC;AAXD,oCAWC","sourcesContent":["export default function rethrowUnlessCode(maybeError: unknown, ...codes: Array) {\n if (maybeError !== null && typeof maybeError === 'object') {\n const code = (maybeError as { code: any }).code;\n for (const allowed of codes) {\n if (code === allowed) {\n return;\n }\n }\n }\n\n throw maybeError;\n}\n"]}package/lib/should-preserve-symlinks.js.map000644 0000001657 3560116604 016147 0ustar00000000 000000 {"version":3,"file":"should-preserve-symlinks.js","sourceRoot":"","sources":["../src/should-preserve-symlinks.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,SAAS,QAAQ,CAAC,KAAe,EAAE,KAAa;IAC9C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACrC,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,KAAK,EAAE;YACtB,OAAO,IAAI,CAAC;SACb;KACF;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAID,iBAAS,UAAU,OAAY;IAC7B,OAAO,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,sBAAsB,IAAI,QAAQ,CAAC,OAAO,CAAC,QAAQ,EAAE,qBAAqB,CAAC,CAAC;AACnG,CAAC,CAAC","sourcesContent":["'use strict';\n\nfunction includes(array: [string], entry: string) {\n for (let i = 0; i < array.length; i++) {\n if (array[i] === entry) {\n return true;\n }\n }\n return false;\n}\n/*\n * utility to detect if node is respective symlinks or not\n */\nexport = function (process: any) {\n return !!process.env.NODE_PRESERVE_SYMLINKS || includes(process.execArgv, '--preserve-symlinks');\n};\n"]}package/CHANGELOG.md000644 0000000400 3560116604 011063 0ustar00000000 000000 # 4.0.1 * [fixes #24] support PNP and basedir slashes * Improve PNP tests # 4.0.0 * drop Node 10.x * upgrade all dependencies * improve publishing semantics to play nicely in the TS ecosystem (publish inlineSource map files, but don't publish ts files) package/README.md000644 0000005402 3560116604 010540 0ustar00000000 000000 # resolve-package-path [![CI](https://github.com/stefanpenner/resolve-package-path/workflows/CI/badge.svg)](https://github.com/stefanpenner/resolve-package-path/actions/workflows/ci.yml) This project is special-purpose, made to resolve `package.json` files for: - a given module name and basedir or - a given basedir It cannot and does not resolve anything else. To achieve its file-resolution performance, it does two specific things: - It memoizes results identically to node's `require`. Specifically, for a given moduleName and baseDir it will, for the duration of the process, always return the exact same response. - It re-implements the parts of `require.resolve` needed to resolve package.json files ONLY. This removes unneeded I/O. (based on @davecombs approach) ## Usage ```sh yarn add resolve-package-path ``` ```js const resolvePackagePath = require('resolve-package-path'); resolvePackagePath('rsvp', 'base-dir/to/start/the/node_resolution-algorithm-from') // => /path/to/rsvp.json or null const { findUpPackagePath } = resolvePackagePath; findUpPackagePath('base-dir/to/start') // => path/to/package.json or null ``` ## Advanced usage ### Preserve Symlinks Node supports `--preserve-symlinks` and `NODE_PRESERVE_SYMLINKS=1` for compatibility this library respects these. ### Disable default caching Although by default `resolve-package-path` caches or memoizes results, this feature can be disabled: ```js const resolvePackagePath = require('resolve-package-path'); resolvePackagePath('rsvp', 'base-dir/to/start/the/node_resolution-algorithm-from', false) // => uncached result /path/to/rsvp.json or null const { findUpPackagePath } = resolvePackagePath; findUpPackagePath('base-dir/to/start', false) // => path/to/package.json or null ``` ### Purge the cache ```js const resolvePackagePath = require('resolve-package-path'); resolvePackagePath._resetCache(); ``` ### Provide an alternative cache In some advanced circumtances, you may want to gain access to the cache to share between more systems. In that case, a cache instance of the following form can be provided as a third argument: ```js cache = { RESOLVED_PACKAGE_PATH: new Map(), REAL_FILE_PATH: new Map(), REAL_DIRECTORY_PATH: new Map(), }; findUpCache = new Map(); const resolvePackagePath = require('resolve-package-path'); resolvePackagePath('rsvp', 'path/to/start/from', cache); const { findUpPackagePath } = resolvePackagePath; findUpPackagePath('base-dir/to/start', findUpCache) // => path/to/package.json or null ``` ### Use internal helper functions For consumers who also do `getRealFilePath` or `getRealDirectoryPath` calls on relevant paths, we expose them as utilities. These utilties ensure identical functionality to resolve-package-path, and a shared cache, which may help reduce IO. package/lib/cache-group.d.ts000644 0000000335 3560116604 013016 0ustar00000000 000000 import Cache = require('./cache'); declare const _default: { new (): { MODULE_ENTRY: Cache; PATH: Cache; REAL_FILE_PATH: Cache; REAL_DIRECTORY_PATH: Cache; }; }; export = _default; package/lib/cache.d.ts000644 0000000467 3560116604 011672 0ustar00000000 000000 declare const _default: { new (): { _store: { [key: string]: string; }; set(key: string, value: any): any; get(key: string): string; has(key: string): boolean; delete(key: string): void; readonly size: number; }; }; export = _default; package/lib/index.d.ts000644 0000002545 3560116604 011735 0ustar00000000 000000 import CacheGroup = require('./cache-group'); import Cache = require('./cache'); /** * Search each directory in the absolute path `baseDir`, from leaf to root, for * a `package.json`, and return the first match, or `null` if no `package.json` * was found. * * @public * @param {string} baseDir - an absolute path in which to search for a `package.json` * @param {CacheGroup|boolean} [_cache] (optional) * * if true: will choose the default global cache * * if false: will not cache * * if undefined or omitted, will choose the default global cache * * otherwise we assume the argument is an external cache of the form provided by resolve-package-path/lib/cache-group.js * * @return {string|null} a full path to the resolved package.json if found or null if not */ declare function _findUpPackagePath(baseDir: string, _cache?: Cache | boolean): string | null; export = resolvePackagePath; declare function resolvePackagePath(target: string, baseDir: string, _cache?: CacheGroup | boolean): string | null; declare namespace resolvePackagePath { var _resetCache: () => void; var getRealFilePath: (filePath: string) => string | null; var getRealDirectoryPath: (directoryPath: string) => string | null; } declare module resolvePackagePath { let _CACHE: CacheGroup; let _FIND_UP_CACHE: Cache; let findUpPackagePath: typeof _findUpPackagePath; } package/lib/resolve-package-path.d.ts000644 0000001174 3560116604 014625 0ustar00000000 000000 import Cache = require('./cache'); import CacheGroup = require('./cache-group'); declare function resolvePackagePath(caches: CacheGroup, name?: string, dir?: string): string | null; declare namespace resolvePackagePath { var _findPackagePath: (realFilePathCache: Cache, name: string, dir: string) => string | null; var _findUpPackagePath: (findUpCache: Cache, initialSearchDir: string) => string | null; var _getRealFilePath: (realFilePathCache: Cache, filePath: string) => string | null; var _getRealDirectoryPath: (realDirectoryPathCache: Cache, directoryPath: string) => string | null; } export = resolvePackagePath; package/lib/rethrow-unless-code.d.ts000644 0000000137 3560116604 014532 0ustar00000000 000000 export default function rethrowUnlessCode(maybeError: unknown, ...codes: Array): void; package/lib/should-preserve-symlinks.d.ts000644 0000000106 3560116604 015613 0ustar00000000 000000 declare const _default: (process: any) => boolean; export = _default;