{
  "version": 3,
  "sources": ["../../../../src/packages/real-hoist/index.ts"],
  "sourcesContent": ["import { LockfileMissingDependencyError } from '../error/index.ts';\nimport { nameVerFromPkgSnapshot } from '../lockfile.utils/index.ts';\nimport * as dp from '../dependency-path/index.ts';\nimport {\n  hoist as _hoist,\n  HoisterDependencyKind,\n  type HoisterTree,\n  type HoisterResult,\n} from '@yarnpkg/nm/hoist';\nimport type { LockfileObject, ProjectId } from '../lockfile.types/index.ts';\n\nexport type HoistingLimits = Map<string, Set<string>>;\n\nexport type { HoisterResult };\n\nexport function hoist(\n  lockfile: LockfileObject,\n  opts?:\n    | {\n        hoistingLimits: HoistingLimits;\n        // This option was added for Bit CLI in order to prevent pnpm from overwriting dependencies linked by Bit.\n        // However, in the future it might be useful to use it in pnpm for skipping any dependencies added by external tools.\n        externalDependencies?: Set<string> | undefined;\n        autoInstallPeers?: boolean | undefined;\n      }\n    | undefined\n): HoisterResult {\n  const nodes = new Map<string, HoisterTree>();\n\n  const ctx = {\n    autoInstallPeers: opts?.autoInstallPeers,\n    nodes,\n    lockfile,\n    depPathByPkgId: new Map<string, string>(),\n  };\n\n  const _toTree = toTree.bind(null, ctx);\n\n  const node: HoisterTree = {\n    name: '.',\n    identName: '.',\n    reference: '',\n    peerNames: new Set<string>([]),\n    dependencyKind: HoisterDependencyKind.WORKSPACE,\n    dependencies: _toTree({\n      ...lockfile.importers?.['.' as ProjectId]?.dependencies,\n      ...lockfile.importers?.['.' as ProjectId]?.devDependencies,\n      ...lockfile.importers?.['.' as ProjectId]?.optionalDependencies,\n      ...Array.from(opts?.externalDependencies ?? []).reduce(\n        (acc, dep) => {\n          // It doesn't matter what version spec is used here.\n          // This dependency will be removed from the tree anyway.\n          // It is only needed to prevent the hoister from hoisting deps with this name to the root of node_modules.\n          acc[dep] = 'link:';\n          return acc;\n        },\n        {} as Record<string, string>\n      ),\n    }),\n  };\n\n  for (const [importerId, importer] of Object.entries(\n    lockfile.importers ?? {}\n  )) {\n    if (importerId === '.') {\n      continue;\n    }\n\n    const importerNode: HoisterTree = {\n      name: encodeURIComponent(importerId),\n      identName: encodeURIComponent(importerId),\n      reference: `workspace:${importerId}`,\n      peerNames: new Set<string>([]),\n      dependencyKind: HoisterDependencyKind.WORKSPACE,\n      dependencies: _toTree({\n        ...importer.dependencies,\n        ...importer.devDependencies,\n        ...importer.optionalDependencies,\n      }),\n    };\n\n    node.dependencies.add(importerNode);\n  }\n\n  const hoisterResult = _hoist(node, opts);\n\n  if (opts?.externalDependencies) {\n    for (const hoistedDep of hoisterResult.dependencies.values()) {\n      if (opts.externalDependencies.has(hoistedDep.name)) {\n        hoisterResult.dependencies.delete(hoistedDep);\n      }\n    }\n  }\n\n  return hoisterResult;\n}\n\nfunction toTree(\n  {\n    nodes,\n    lockfile,\n    depPathByPkgId,\n    autoInstallPeers,\n  }: {\n    autoInstallPeers?: boolean | undefined;\n    depPathByPkgId: Map<string, string>;\n    lockfile: LockfileObject;\n    nodes: Map<string, HoisterTree>;\n  },\n  deps: Record<string, string>\n): Set<HoisterTree> {\n  return new Set(\n    Object.entries(deps).map(([alias, ref]: [string, string]): HoisterTree => {\n      const depPath = dp.refToRelative(ref, alias);\n\n      if (depPath === null) {\n        const key = `${alias}:${ref}`;\n\n        let node = nodes.get(key);\n\n        if (typeof node === 'undefined') {\n          node = {\n            name: alias,\n            identName: alias,\n            reference: ref,\n            dependencyKind: HoisterDependencyKind.REGULAR,\n            dependencies: new Set(),\n            peerNames: new Set(),\n          };\n\n          nodes.set(key, node);\n        }\n\n        return node;\n      }\n\n      const key = `${alias}:${depPath}`;\n\n      let node = nodes.get(key);\n\n      if (typeof node === 'undefined') {\n        const pkgSnapshot = lockfile.packages?.[depPath];\n\n        if (typeof pkgSnapshot === 'undefined') {\n          throw new LockfileMissingDependencyError(depPath);\n        }\n\n        const { name: pkgName, version } = nameVerFromPkgSnapshot(\n          depPath,\n          pkgSnapshot\n        );\n\n        const id = `${pkgName}@${version}`;\n\n        if (depPathByPkgId.has(id) !== true) {\n          depPathByPkgId.set(id, depPath);\n        }\n\n        node = {\n          name: alias,\n          identName: pkgName,\n          reference: depPath,\n          dependencyKind: HoisterDependencyKind.REGULAR,\n          dependencies: new Set(),\n          peerNames: new Set(\n            autoInstallPeers === true\n              ? []\n              : [\n                  ...Object.keys(pkgSnapshot.peerDependencies ?? {}),\n                  ...(pkgSnapshot.transitivePeerDependencies ?? []),\n                ]\n          ),\n        };\n\n        if (typeof node !== 'undefined') {\n          nodes.set(key, node);\n\n          node.dependencies = toTree(\n            { nodes, lockfile, depPathByPkgId, autoInstallPeers },\n            { ...pkgSnapshot.dependencies, ...pkgSnapshot.optionalDependencies }\n          );\n\n          return node;\n        }\n      }\n\n      return node;\n    })\n  );\n}\n"],
  "mappings": "AAAA,SAAS,sCAAsC;AAC/C,SAAS,8BAA8B;AACvC,YAAY,QAAQ;AACpB;AAAA,EACE,SAAS;AAAA,EACT;AAAA,OAGK;AAOA,SAAS,MACd,UACA,MASe;AACf,QAAM,QAAQ,oBAAI,IAAyB;AAE3C,QAAM,MAAM;AAAA,IACV,kBAAkB,MAAM;AAAA,IACxB;AAAA,IACA;AAAA,IACA,gBAAgB,oBAAI,IAAoB;AAAA,EAC1C;AAEA,QAAM,UAAU,OAAO,KAAK,MAAM,GAAG;AAErC,QAAM,OAAoB;AAAA,IACxB,MAAM;AAAA,IACN,WAAW;AAAA,IACX,WAAW;AAAA,IACX,WAAW,oBAAI,IAAY,CAAC,CAAC;AAAA,IAC7B,gBAAgB,sBAAsB;AAAA,IACtC,cAAc,QAAQ;AAAA,MACpB,GAAG,SAAS,YAAY,GAAgB,GAAG;AAAA,MAC3C,GAAG,SAAS,YAAY,GAAgB,GAAG;AAAA,MAC3C,GAAG,SAAS,YAAY,GAAgB,GAAG;AAAA,MAC3C,GAAG,MAAM,KAAK,MAAM,wBAAwB,CAAC,CAAC,EAAE;AAAA,QAC9C,CAAC,KAAK,QAAQ;AAIZ,cAAI,GAAG,IAAI;AACX,iBAAO;AAAA,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