{
  "version": 3,
  "sources": ["../../../../src/packages/license-scanner/lockfileToLicenseNodeTree.ts"],
  "sourcesContent": ["import type { LockfileObject } from '../lockfile.types/index.ts';\nimport { nameVerFromPkgSnapshot } from '../lockfile.utils/index.ts';\nimport { packageIsInstallable } from '../package-is-installable/index.ts';\nimport {\n  lockfileWalkerGroupImporterSteps,\n  type LockfileWalkerStep,\n} from '../lockfile.walker/index.ts';\nimport {\n  type DepTypes,\n  DepType,\n  detectDepTypes,\n} from '../lockfile.detect-dep-types/index.ts';\nimport type {\n  SupportedArchitectures,\n  DependenciesField,\n  ProjectId,\n  Registries,\n  ModulesDir,\n} from '../types/index.ts';\nimport { getPkgInfo } from './getPkgInfo.ts';\nimport mapValues from 'ramda/src/map';\nimport type { TarballResolution } from '../resolver-base/index.ts';\n\nexport type LicenseNode = {\n  name?: string | undefined;\n  version?: string | undefined;\n  license: string;\n  licenseContents?: string | undefined;\n  dir: string;\n  author?: string | undefined;\n  homepage?: string | undefined;\n  description?: string | undefined;\n  repository?: string | undefined;\n  integrity?: string | undefined;\n  requires?: Record<string, string | undefined> | undefined;\n  dependencies?: { [name: string]: LicenseNode } | undefined;\n  dev: boolean;\n};\n\nexport type LicenseNodeTree = Omit<\n  LicenseNode,\n  'dir' | 'license' | 'licenseContents' | 'author' | 'homepages' | 'repository'\n>;\n\nexport interface LicenseExtractOptions {\n  storeDir: string;\n  virtualStoreDir: string;\n  virtualStoreDirMaxLength: number;\n  modulesDir?: ModulesDir | undefined;\n  dir: string;\n  registries: Registries;\n  supportedArchitectures?: SupportedArchitectures | undefined;\n  depTypes: DepTypes;\n}\n\nexport async function lockfileToLicenseNode(\n  step: LockfileWalkerStep,\n  options: LicenseExtractOptions\n): Promise<Record<string, LicenseNode>> {\n  const dependencies: Record<string, LicenseNode> = Object.fromEntries(\n    (\n      await Promise.all(\n        step.dependencies.map(\n          async (dependency): Promise<[string, LicenseNode] | null> => {\n            const { depPath, pkgSnapshot, next } = dependency;\n            const { name, version } = nameVerFromPkgSnapshot(\n              depPath,\n              pkgSnapshot\n            );\n\n            const packageInstallable = await packageIsInstallable(\n              pkgSnapshot.id ?? depPath,\n              {\n                name,\n                version,\n                cpu: pkgSnapshot.cpu,\n                os: pkgSnapshot.os,\n                libc: pkgSnapshot.libc,\n              },\n              {\n                optional: pkgSnapshot.optional ?? false,\n                lockfileDir: options.dir,\n                supportedArchitectures: options.supportedArchitectures,\n              }\n            );\n\n            // If the package is not installable on the given platform, we ignore the\n            // package, typically the case for platform prebuild packages\n            if (!packageInstallable) {\n              return null;\n            }\n\n            const packageInfo = await getPkgInfo(\n              {\n                id: pkgSnapshot.id ?? depPath,\n                name,\n                version,\n                depPath,\n                snapshot: pkgSnapshot,\n                registries: options.registries,\n              },\n              {\n                storeDir: options.storeDir,\n                virtualStoreDir: options.virtualStoreDir,\n                virtualStoreDirMaxLength: options.virtualStoreDirMaxLength,\n                dir: options.dir,\n                modulesDir:\n                  options.modulesDir ?? ('node_modules' as ModulesDir),\n              }\n            );\n\n            const subdeps = await lockfileToLicenseNode(next(), options);\n\n            const dep: LicenseNode = {\n              name,\n              dev: options.depTypes[depPath] === DepType.DevOnly,\n              integrity: (pkgSnapshot.resolution as TarballResolution)\n                .integrity,\n              version,\n              license: packageInfo.license,\n              licenseContents: packageInfo.licenseContents,\n              author: packageInfo.author,\n              homepage: packageInfo.homepage,\n              description: packageInfo.description,\n              repository: packageInfo.repository,\n              dir: packageInfo.path as string,\n            };\n\n            if (Object.keys(subdeps).length > 0) {\n              dep.dependencies = subdeps;\n              dep.requires = toRequires(subdeps);\n            }\n\n            // If the package details could be fetched, we consider it part of the tree\n            return [name, dep];\n          }\n        )\n      )\n    ).filter(Boolean) as Array<[string, LicenseNode]>\n  );\n\n  return dependencies;\n}\n\n/**\n * Reads the lockfile and converts it in a node tree of information necessary\n * to generate the licenses summary\n * @param lockfile the lockfile to process\n * @param opts     parsing instructions\n * @returns\n */\nexport async function lockfileToLicenseNodeTree(\n  lockfile: LockfileObject,\n  opts: {\n    include?: { [dependenciesField in DependenciesField]: boolean } | undefined;\n    includedImporterIds?: ProjectId[] | undefined;\n  } & LicenseExtractOptions\n): Promise<LicenseNodeTree> {\n  const importerWalkers = lockfileWalkerGroupImporterSteps(\n    lockfile,\n    opts.includedImporterIds ??\n      (Object.keys(lockfile.importers ?? {}) as ProjectId[]),\n    { include: opts.include }\n  );\n\n  const depTypes = detectDepTypes(lockfile);\n\n  const dependencies = Object.fromEntries(\n    await Promise.all(\n      importerWalkers.map(async (importerWalker) => {\n        const importerDeps = await lockfileToLicenseNode(importerWalker.step, {\n          storeDir: opts.storeDir,\n          virtualStoreDir: opts.virtualStoreDir,\n          virtualStoreDirMaxLength: opts.virtualStoreDirMaxLength,\n          modulesDir: opts.modulesDir,\n          dir: opts.dir,\n          registries: opts.registries,\n          supportedArchitectures: opts.supportedArchitectures,\n          depTypes,\n        });\n        return [\n          importerWalker.importerId,\n          {\n            dependencies: importerDeps,\n            requires: toRequires(importerDeps),\n            version: '0.0.0',\n            license: undefined,\n          },\n        ];\n      })\n    )\n  );\n\n  const licenseNodeTree: LicenseNodeTree = {\n    name: undefined,\n    version: undefined,\n    dependencies,\n    dev: false,\n    integrity: undefined,\n    requires: toRequires(dependencies),\n  };\n\n  return licenseNodeTree;\n}\n\nfunction toRequires(\n  licenseNodesByDepName: Record<string, LicenseNode>\n): Record<string, string | undefined> {\n  return mapValues.default((licenseNode: LicenseNode): string | undefined => {\n    return licenseNode.version;\n  }, licenseNodesByDepName);\n}\n"],
  "mappings": "AACA,SAAS,8BAA8B;AACvC,SAAS,4BAA4B;AACrC;AAAA,EACE;AAAA,OAEK;AACP;AAAA,EAEE;AAAA,EACA;AAAA,OACK;AAQP,SAAS,kBAAkB;AAC3B,OAAO,eAAe;AAmCtB,eAAsB,sBACpB,MACA,SACsC;AACtC,QAAM,eAA4C,OAAO;AAAA,KAErD,MAAM,QAAQ;AAAA,MACZ,KAAK,aAAa;AAAA,QAChB,OAAO,eAAsD;AAC3D,gB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