{
  "version": 3,
  "sources": ["../../../../src/packages/local-resolver/parsePref.ts"],
  "sourcesContent": ["import os from 'node:os';\nimport path from 'node:path';\nimport { PnpmError } from '../error/index.ts';\nimport normalize from 'normalize-path';\n\nimport process from 'node:process';\nimport type { PkgResolutionId } from '../types/index.ts';\n\n// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-ignore\nconst isWindows = process.platform === 'win32' || global['FAKE_WINDOWS'];\n\nconst isFilespec =\n  isWindows === true ? /^(?:[./\\\\]|~\\/|[a-z]:)/i : /^(?:[./]|~\\/|[a-z]:)/i;\nconst isFilename = /\\.(?:tgz|tar.gz|tar)$/i;\n// eslint-disable-next-line optimize-regex/optimize-regex\nconst isAbsolutePath = /^\\/|^[A-Z]:/i;\n\nexport type LocalPackageSpec = {\n  dependencyPath: string;\n  fetchSpec: string;\n  id: PkgResolutionId;\n  type: 'directory' | 'file';\n  normalizedPref: string;\n};\n\nexport type WantedLocalDependency = {\n  pref: string;\n  injected?: boolean | undefined;\n};\n\nexport function parsePref(\n  wd: WantedLocalDependency,\n  projectDir: string,\n  lockfileDir: string\n): LocalPackageSpec | null {\n  if (wd.pref.startsWith('link:') || wd.pref.startsWith('workspace:')) {\n    return fromLocal(wd, projectDir, lockfileDir, 'directory');\n  }\n\n  if (\n    wd.pref.endsWith('.tgz') ||\n    wd.pref.endsWith('.tar.gz') ||\n    wd.pref.endsWith('.tar') ||\n    wd.pref.includes(path.sep) ||\n    wd.pref.startsWith('file:') ||\n    isFilespec.test(wd.pref)\n  ) {\n    const type = isFilename.test(wd.pref) ? 'file' : 'directory';\n    return fromLocal(wd, projectDir, lockfileDir, type);\n  }\n\n  if (wd.pref.startsWith('path:')) {\n    const err = new PnpmError(\n      'PATH_IS_UNSUPPORTED_PROTOCOL',\n      'Local dependencies via `path:` protocol are not supported. ' +\n        'Use the `link:` protocol for folder dependencies and `file:` for local tarballs'\n    );\n    // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n    // @ts-expect-error\n    err.pref = wd.pref;\n    // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n    // @ts-expect-error\n    err.protocol = 'path:';\n\n    throw err;\n  }\n  return null;\n}\n\nfunction fromLocal(\n  { pref, injected }: WantedLocalDependency,\n  projectDir: string,\n  lockfileDir: string,\n  type: 'file' | 'directory'\n): LocalPackageSpec {\n  const spec = pref\n    .replace(/\\\\/g, '/')\n    // eslint-disable-next-line optimize-regex/optimize-regex\n    .replace(/^(?:file|link|workspace):\\/*([A-Z]:)/i, '$1') // drive name paths on windows\n    // eslint-disable-next-line optimize-regex/optimize-regex\n    .replace(/^(?:file|link|workspace):(?:\\/*([~./]))?/, '$1');\n\n  let protocol!: string;\n\n  if (pref.startsWith('file:')) {\n    protocol = 'file:';\n  } else if (pref.startsWith('link:')) {\n    protocol = 'link:';\n  } else {\n    protocol = type === 'directory' && injected !== true ? 'link:' : 'file:';\n  }\n\n  let fetchSpec!: string;\n\n  let normalizedPref!: string;\n\n  if (/^~\\//.test(spec)) {\n    // this is needed for windows and for file:~/foo/bar\n    fetchSpec = resolvePath(os.homedir(), spec.slice(2));\n\n    normalizedPref = `${protocol}${spec}`;\n  } else {\n    fetchSpec = resolvePath(projectDir, spec);\n\n    if (isAbsolute(spec)) {\n      normalizedPref = `${protocol}${spec}`;\n    } else {\n      normalizedPref = `${protocol}${path.relative(projectDir, fetchSpec)}`;\n    }\n  }\n\n  injected = protocol === 'file:';\n\n  const dependencyPath = injected\n    ? normalize(path.relative(lockfileDir, fetchSpec))\n    : normalize(path.resolve(fetchSpec));\n\n  const id = (\n    !injected && (type === 'directory' || projectDir === lockfileDir)\n      ? `${protocol}${normalize(path.relative(projectDir, fetchSpec))}`\n      : `${protocol}${normalize(path.relative(lockfileDir, fetchSpec))}`\n  ) as PkgResolutionId;\n\n  return {\n    dependencyPath,\n    fetchSpec,\n    id,\n    normalizedPref,\n    type,\n  };\n}\n\nfunction resolvePath(where: string, spec: string): string {\n  if (isAbsolutePath.test(spec)) return spec;\n  return path.resolve(where, spec);\n}\n\nfunction isAbsolute(dir: string): boolean {\n  if (dir[0] === '/') return true;\n  if (/^[a-z]:/i.test(dir)) return true;\n  return false;\n}\n"],
  "mappings": "AAAA,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB,SAAS,iBAAiB;AAC1B,OAAO,eAAe;AAEtB,OAAO,aAAa;AAKpB,MAAM,YAAY,QAAQ,aAAa,WAAW,OAAO,cAAc;AAEvE,MAAM,aACJ,cAAc,OAAO,4BAA4B;AACnD,MAAM,aAAa;AAEnB,MAAM,iBAAiB;AAehB,SAAS,UACd,IACA,YACA,aACyB;AACzB,MAAI,GAAG,KAAK,WAAW,OAAO,KAAK,GAAG,KAAK,WAAW,YAAY,GAAG;AACnE,WAAO,UAAU,IAAI,YAAY,aAAa,WAAW;AAAA,EAC3D;AAEA,MACE,GAAG,KAAK,SAAS,MAAM,KACvB,GAAG,KAAK,SAAS,SAAS,KAC1B,GAAG,KAAK,SAAS,MAAM,KACvB,GAAG,KAAK,SAAS,KAAK,GAAG,KACzB,GAAG,KAAK,WAAW,OAAO,KAC1B,WAAW,KAAK,GAAG,IAAI,GACvB;AACA,UAAM,OAAO,WAAW,KAAK,GAAG,IAAI,IAAI,SAAS;AACjD,WAAO,UAAU,IAAI,YAAY,aAAa,IAAI;AAAA,EACpD;AAEA,MAAI,GAAG,KAAK,WAAW,OAAO,GAAG;AAC/B,UAAM,MAAM,IAAI;AAAA,MACd;AAAA,MACA;AAAA,IAEF;AAGA,QAAI,OAAO,GAAG;AAGd,QAAI,WAAW;AAEf,UAAM;AAAA,EACR;AACA,SAAO;AACT;AAEA,SAAS,UACP,EAAE,MAAM,SAAS,GACjB,YACA,aACA,MACkB;AAClB,QAAM,OAAO,KACV,QAAQ,OAAO,GAAG,EAElB,QAAQ,yCAAyC,IAAI,EAErD,QAAQ,4CAA4C,IAAI;AAE3D,MAAI;AAEJ,MAAI,KAAK,WAAW,OAAO,GAAG;AAC5B,eAAW;AAAA,EACb,WAAW,KA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