"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createPkgGraph = void 0; const path_1 = __importDefault(require("path")); const npm_package_arg_1 = __importDefault(require("@pnpm/npm-package-arg")); const resolve_workspace_range_1 = require("@pnpm/resolve-workspace-range"); const npm_resolver_1 = require("@pnpm/npm-resolver"); const map_1 = __importDefault(require("ramda/src/map")); function createPkgGraph(pkgs, opts) { const pkgMap = createPkgMap(pkgs); const pkgMapValues = Object.values(pkgMap); let pkgMapByManifestName; let pkgMapByDir; const unmatched = []; const graph = (0, map_1.default)((pkg) => ({ dependencies: createNode(pkg), package: pkg, }), pkgMap); return { graph, unmatched }; function createNode(pkg) { const dependencies = { ...(!opts?.ignoreDevDeps && pkg.manifest.devDependencies), ...pkg.manifest.optionalDependencies, ...pkg.manifest.dependencies, }; return Object.entries(dependencies) .map(([depName, rawSpec]) => { let spec; const isWorkspaceSpec = rawSpec.startsWith('workspace:'); try { if (isWorkspaceSpec) { const { fetchSpec, name } = (0, npm_resolver_1.parsePref)((0, npm_resolver_1.workspacePrefToNpm)(rawSpec), depName, 'latest', ''); rawSpec = fetchSpec; depName = name; } spec = npm_package_arg_1.default.resolve(depName, rawSpec, pkg.dir); } catch (err) { // eslint-disable-line return ''; } if (spec.type === 'directory') { pkgMapByDir ??= getPkgMapByDir(pkgMapValues); const resolvedPath = path_1.default.resolve(pkg.dir, spec.fetchSpec); const found = pkgMapByDir[resolvedPath]; if (found) { return found.dir; } // Slow path; only needed when there are case mismatches on case-insensitive filesystems. const matchedPkg = pkgMapValues.find(pkg => path_1.default.relative(pkg.dir, spec.fetchSpec) === ''); if (matchedPkg == null) { return ''; } pkgMapByDir[resolvedPath] = matchedPkg; return matchedPkg.dir; } if (spec.type !== 'version' && spec.type !== 'range') return ''; pkgMapByManifestName ??= getPkgMapByManifestName(pkgMapValues); const pkgs = pkgMapByManifestName[depName]; if (!pkgs || pkgs.length === 0) return ''; const versions = pkgs.filter(({ manifest }) => manifest.version) .map(pkg => pkg.manifest.version); // explicitly check if false, backwards-compatibility (can be undefined) const strictWorkspaceMatching = opts?.linkWorkspacePackages === false && !isWorkspaceSpec; if (strictWorkspaceMatching) { unmatched.push({ pkgName: depName, range: rawSpec }); return ''; } if (isWorkspaceSpec && versions.length === 0) { const matchedPkg = pkgs.find(pkg => pkg.manifest.name === depName); return matchedPkg.dir; } if (versions.includes(rawSpec)) { const matchedPkg = pkgs.find(pkg => pkg.manifest.name === depName && pkg.manifest.version === rawSpec); return matchedPkg.dir; } const matched = (0, resolve_workspace_range_1.resolveWorkspaceRange)(rawSpec, versions); if (!matched) { unmatched.push({ pkgName: depName, range: rawSpec }); return ''; } const matchedPkg = pkgs.find(pkg => pkg.manifest.name === depName && pkg.manifest.version === matched); return matchedPkg.dir; }) .filter(Boolean); } } exports.createPkgGraph = createPkgGraph; function createPkgMap(pkgs) { const pkgMap = {}; for (const pkg of pkgs) { pkgMap[pkg.dir] = pkg; } return pkgMap; } function getPkgMapByManifestName(pkgMapValues) { const pkgMapByManifestName = {}; for (const pkg of pkgMapValues) { if (pkg.manifest.name) { (pkgMapByManifestName[pkg.manifest.name] ??= []).push(pkg); } } return pkgMapByManifestName; } function getPkgMapByDir(pkgMapValues) { const pkgMapByDir = {}; for (const pkg of pkgMapValues) { pkgMapByDir[path_1.default.resolve(pkg.dir)] = pkg; } return pkgMapByDir; } //# sourceMappingURL=index.js.map