UNPKG

@pnpm/link-bins

Version:
61 lines 2.65 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getBinNodePaths = getBinNodePaths; const fs_1 = require("fs"); const path_1 = __importDefault(require("path")); /** * Returns the node_modules paths relevant to a binary in the virtual store layout. * For a binary at `.pnpm/pkg@version/node_modules/pkg/bin/cli.js`, this returns: * 1. `.pnpm/pkg@version/node_modules/pkg/node_modules` (bundled dependencies) * 2. `.pnpm/pkg@version/node_modules` (sibling/regular dependencies) * * These directories must be in NODE_PATH so that tools like `import-local` * (used by jest, eslint, etc.) which resolve from CWD can find the correct * dependency versions. */ async function getBinNodePaths(target) { const targetDir = path_1.default.dirname(target); let dir; try { dir = await fs_1.promises.realpath(targetDir); } catch (err) { if (err.code !== 'ENOENT') { throw err; } dir = targetDir; } // Walk up from the resolved directory to find the first non-nested node_modules let currentDir = dir; while (true) { if (path_1.default.basename(currentDir) === 'node_modules') { // Skip nested node_modules (e.g., node_modules/node_modules) if (path_1.default.basename(path_1.default.dirname(currentDir)) !== 'node_modules') { const nodeModulesDir = currentDir; const result = []; // Determine the package directory from the relative path between // node_modules and the resolved binary directory const rel = path_1.default.relative(nodeModulesDir, dir); if (rel) { const relSegments = rel.split(path_1.default.sep); // For scoped packages, the package dir is two levels deep: @scope/pkg const pkgDir = relSegments[0].startsWith('@') ? path_1.default.join(nodeModulesDir, relSegments[0], relSegments[1]) : path_1.default.join(nodeModulesDir, relSegments[0]); result.push(path_1.default.join(pkgDir, 'node_modules')); } result.push(nodeModulesDir); return result; } } const parent = path_1.default.dirname(currentDir); if (parent === currentDir) break; currentDir = parent; } return []; } //# sourceMappingURL=getBinNodePaths.js.map