{
  "version": 3,
  "sources": ["../../../../src/packages/modules-yaml/index.ts"],
  "sourcesContent": ["import path from 'node:path';\nimport type {\n  DepPath,\n  DependenciesField,\n  HoistedDependencies,\n  ModulesDir,\n  Registries,\n} from '../types/index.ts';\nimport readYamlFile from 'read-yaml-file';\nimport mapValues from 'ramda/src/map';\nimport isWindows from 'is-windows';\nimport writeYamlFile from 'write-yaml-file';\n\n// The dot prefix is needed because otherwise `npm shrinkwrap`\n// thinks that it is an extraneous package.\nconst MODULES_FILENAME = '.modules.yaml';\n\nexport type IncludedDependencies = {\n  [dependenciesField in DependenciesField]: boolean;\n};\n\nexport interface Modules {\n  hoistedAliases?: { [depPath: DepPath]: string[] } | undefined; // for backward compatibility\n  hoistedDependencies: HoistedDependencies;\n  hoistPattern?: string[] | undefined;\n  included?: IncludedDependencies | undefined;\n  layoutVersion: number;\n  nodeLinker?: 'hoisted' | 'isolated' | 'pnp' | undefined;\n  packageManager: string;\n  pendingBuilds: string[];\n  ignoredBuilds?: string[] | undefined;\n  prunedAt: string;\n  registries?: Registries | undefined; // nullable for backward compatibility\n  shamefullyHoist?: boolean | undefined; // for backward compatibility\n  publicHoistPattern?: string[] | undefined;\n  skipped: string[];\n  storeDir: string;\n  virtualStoreDir: string;\n  virtualStoreDirMaxLength?: number | undefined;\n  injectedDeps?: Record<string, string[]> | undefined;\n  hoistedLocations?: Record<string, string[]> | undefined;\n}\n\nexport async function readModulesManifest(\n  modulesDir: ModulesDir\n): Promise<Modules | null> {\n  const modulesYamlPath = path.join(modulesDir, MODULES_FILENAME);\n\n  let modules!: Modules;\n\n  try {\n    modules = await readYamlFile.default<Modules>(modulesYamlPath);\n\n    if (typeof modules === 'undefined') {\n      return modules;\n    }\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  } catch (err: any) {\n    if ((err as NodeJS.ErrnoException).code !== 'ENOENT') {\n      throw err;\n    }\n\n    return null;\n  }\n\n  if (!modules.virtualStoreDir) {\n    modules.virtualStoreDir = path.join(modulesDir, '.pnpm');\n  } else if (!path.isAbsolute(modules.virtualStoreDir)) {\n    modules.virtualStoreDir = path.join(modulesDir, modules.virtualStoreDir);\n  }\n\n  switch (modules.shamefullyHoist) {\n    case true: {\n      if (modules.publicHoistPattern == null) {\n        modules.publicHoistPattern = ['*'];\n      }\n\n      // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions\n      if (modules.hoistedAliases != null && !modules.hoistedDependencies) {\n        // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n        // @ts-ignore\n        // Type 'Record<DepPath, { [k: string]: string; }>' is not assignable to type 'HoistedDependencies'.\n        // 'string & { __brand: \"DepPath\"; }' index signatures are incompatible.\n        // Type '{ [k: string]: string; }' is not assignable to type 'Record<string, \"public\" | \"private\">'.\n        // 'string' index signatures are incompatible.\n        // Type 'string' is not assignable to type '\"public\" | \"private\"'.ts(2322)\n        modules.hoistedDependencies = mapValues.default(\n          (\n            aliases: string[]\n          ): {\n            [k: string]: string;\n          } => {\n            return Object.fromEntries(\n              aliases.map((alias) => [alias, 'public'])\n            );\n          },\n          modules.hoistedAliases\n        );\n      }\n\n      break;\n    }\n\n    case false: {\n      if (modules.publicHoistPattern == null) {\n        modules.publicHoistPattern = [];\n      }\n\n      if (\n        typeof modules.hoistedAliases !== 'undefined' &&\n        typeof modules.hoistedDependencies === 'undefined'\n      ) {\n        modules.hoistedDependencies = {};\n\n        for (const depPath of Object.keys(modules.hoistedAliases)) {\n          modules.hoistedDependencies[depPath as DepPath] = {};\n\n          for (const alias of modules.hoistedAliases[depPath as DepPath] ??\n            []) {\n            const dep = modules.hoistedDependencies[depPath as DepPath];\n\n            if (typeof dep !== 'undefined') {\n              dep[alias] = 'private';\n            }\n          }\n        }\n      }\n\n      break;\n    }\n  }\n\n  if (!modules.prunedAt) {\n    modules.prunedAt = new Date().toUTCString();\n  }\n\n  if (typeof modules.virtualStoreDirMaxLength === 'undefined') {\n    modules.virtualStoreDirMaxLength = 120;\n  }\n\n  return modules;\n}\n\nconst YAML_OPTS = {\n  lineWidth: 1000,\n  noCompatMode: true,\n  noRefs: true,\n  sortKeys: true,\n};\n\nexport async function writeModulesManifest(\n  modulesDir: ModulesDir,\n  modules: Modules & { registries: Registries },\n  opts?:\n    | {\n        makeModulesDir?: boolean | undefined;\n      }\n    | undefined\n): Promise<void> {\n  const modulesYamlPath = path.join(modulesDir, MODULES_FILENAME);\n\n  const saveModules = { ...modules };\n\n  if (Array.isArray(saveModules.skipped)) {\n    saveModules.skipped.sort();\n  }\n\n  if (\n    saveModules.hoistPattern == null ||\n    (saveModules.hoistPattern as unknown) === ''\n  ) {\n    // Because the YAML writer fails on undefined fields\n    // biome-ignore lint/performance/noDelete: <explanation>\n    delete saveModules.hoistPattern;\n  }\n  if (saveModules.publicHoistPattern == null) {\n    // biome-ignore lint/performance/noDelete: <explanation>\n    delete saveModules.publicHoistPattern;\n  }\n  if (\n    saveModules.hoistedAliases == null ||\n    (saveModules.hoistPattern == null && saveModules.publicHoistPattern == null)\n  ) {\n    // biome-ignore lint/performance/noDelete: <explanation>\n    delete saveModules.hoistedAliases;\n  }\n\n  // We should store the absolute virtual store directory path on Windows\n  // because junctions are used on Windows. Junctions will break even if\n  // the relative path to the virtual store remains the same after moving\n  // a project.\n  if (!isWindows()) {\n    saveModules.virtualStoreDir = path.relative(\n      modulesDir,\n      saveModules.virtualStoreDir\n    );\n  }\n  try {\n    await writeYamlFile(modulesYamlPath, saveModules, {\n      ...YAML_OPTS,\n      makeDir: opts?.makeModulesDir ?? false,\n    });\n\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  } catch (err: any) {\n    if ((err as NodeJS.ErrnoException).code !== 'ENOENT') {\n      throw err;\n    }\n  }\n}\n"],
  "mappings": "AAAA,OAAO,UAAU;AAQjB,OAAO,kBAAkB;AACzB,OAAO,eAAe;AACtB,OAAO,eAAe;AACtB,OAAO,mBAAmB;AAI1B,MAAM,mBAAmB;AA4BzB,eAAsB,oBACpB,YAC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