{"version":3,"file":"shared.mjs","names":[],"sources":["../../src/commands/shared.ts"],"sourcesContent":["import consola from \"consola\";\nimport { createFromPURL } from \"../core/purl.ts\";\nimport { ecosystems, type Registry } from \"../core/registry.ts\";\nimport {\n  HTTPError,\n  NotFoundError,\n  RateLimitError,\n  UnknownEcosystemError,\n  InvalidPURLError,\n} from \"../core/errors.ts\";\nimport { CachedRegistry } from \"../cache/cached-registry.ts\";\n\nexport const sharedArgs = {\n  json: {\n    type: \"boolean\" as const,\n    description: \"Output as JSON\",\n    default: false,\n  },\n  cache: {\n    type: \"boolean\" as const,\n    description: \"Read and write cached results\",\n    negativeDescription: \"Bypass cache reads and writes, fetch fresh data\",\n    default: true,\n  },\n} as const;\n\n/**\n * Resolve a PURL or shorthand, using cache unless disabled.\n *\n * @param input - PURL or shorthand package identifier.\n * @param useCache - Whether to wrap the registry with caching.\n * @returns {Promise<[Registry, string, string]>} The registry, package name, and version.\n */\nexport async function resolvePURL(\n  input: string,\n  useCache = true,\n): Promise<[Registry, string, string]> {\n  const purl = input.startsWith(\"pkg:\") ? input : `pkg:${input}`;\n  const [reg, name, version] = await createFromPURL(purl);\n  return [useCache ? new CachedRegistry(reg) : reg, name, version];\n}\n\n/**\n * Run a command with standard CLI error handling.\n *\n * @param fn - Command operation to run.\n * @returns {Promise<void>} Completion after the command or handled exit.\n */\nexport async function withErrorHandling(fn: () => Promise<void>): Promise<void> {\n  try {\n    await fn();\n  } catch (error) {\n    if (error instanceof NotFoundError) {\n      consola.error(\n        `Package not found: ${error.ecosystem}/${error.packageName}${error.version ? `@${error.version}` : \"\"}`,\n      );\n      return process.exit(1);\n    }\n    if (error instanceof UnknownEcosystemError) {\n      consola.error(`Unknown ecosystem: ${error.ecosystem}`);\n      consola.info(`Supported: ${ecosystems().join(\", \")}`);\n      return process.exit(1);\n    }\n    if (error instanceof InvalidPURLError) {\n      consola.error(`Invalid PURL: ${error.purl}`);\n      consola.info(\"Examples: pkg:npm/lodash, npm/lodash@4.17.21, pkg:cargo/serde\");\n      return process.exit(1);\n    }\n    if (error instanceof RateLimitError) {\n      consola.error(`Rate limited by registry. Retry after ${error.retryAfter}s`);\n      return process.exit(1);\n    }\n    if (error instanceof HTTPError) {\n      consola.error(`Registry request failed: HTTP ${error.statusCode} (${error.url})`);\n      if (error.isServerError()) {\n        consola.info(\"The registry may be temporarily unavailable. Try again later.\");\n      }\n      return process.exit(1);\n    }\n    throw error;\n  }\n}\n"],"mappings":";;;;AAYA,MAAa,aAAa;CACxB,MAAM;EACJ,MAAM;EACN,aAAa;EACb,SAAS;CACX;CACA,OAAO;EACL,MAAM;EACN,aAAa;EACb,qBAAqB;EACrB,SAAS;CACX;AACF;;;;;;;EASA;CAIE;AACA;AACQ,eAAW,kBAAmB,IAAG;CAAS,IAAA;EAAM,MAAA,GAAA;CAAO,SAAA,OAAA;EACjE,IAAA,iBAAA,eAAA;;;;;;;GAQA,OAAA,QAAsB,KAAA,CAAA;EACpB;EACE,IAAA,iBAAS,kBAAA;GACX,QAAS,MAAO,iBAAA,MAAA,MAAA;GACd,QAAI,KAAA,+DAAgC;GAClC,OAAA,QACE,KAAA,CAAA;EAEF;EACF,IAAA,iBAAA,gBAAA;GACA,QAAI,MAAA,yCAAwC,MAAA,WAAA,EAAA;GAC1C,OAAA,QAAc,KAAA,CAAA;EACd;EACA,IAAA,iBAAoB,WAAC;GACvB,QAAA,MAAA,iCAAA,MAAA,WAAA,IAAA,MAAA,IAAA,EAAA;GACA,IAAI,MAAA,cAAiB,GAAA,QAAA,KAAkB,+DAAA;GACrC,OAAA,QAAc,KAAA,CAAA;EACd;EACA,MAAA;CACF;AACA;AAEE,SAAA,aAAe,YAAM"}