{"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  \"no-cache\": {\n    type: \"boolean\" as const,\n    description: \"Bypass cache, fetch fresh data\",\n    default: false,\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;EACV;CACD,YAAY;EACV,MAAM;EACN,aAAa;EACb,SAAS;;CAEZ;;;;;;;;AASD,eAAsB,YACpB,OACA,WAAW,MAC0B;CAErC,MAAM,CAAC,KAAK,MAAM,WAAW,MAAM,eADtB,MAAM,WAAW,OAAO,GAAG,QAAQ,OAAO,QACA;AACvD,QAAO;EAAC,WAAW,IAAI,eAAe,IAAI,GAAG;EAAK;EAAM;EAAQ;;;;;;;;AASlE,eAAsB,kBAAkB,IAAwC;AAC9E,KAAI;AACF,QAAM,IAAI;UACH,OAAO;AACd,MAAI,iBAAiB,eAAe;AAClC,WAAQ,MACN,sBAAsB,MAAM,UAAU,GAAG,MAAM,cAAc,MAAM,UAAU,IAAI,MAAM,YAAY,KACpG;AACD,UAAO,QAAQ,KAAK,EAAE;;AAExB,MAAI,iBAAiB,uBAAuB;AAC1C,WAAQ,MAAM,sBAAsB,MAAM,YAAY;AACtD,WAAQ,KAAK,cAAc,YAAY,CAAC,KAAK,KAAK,GAAG;AACrD,UAAO,QAAQ,KAAK,EAAE;;AAExB,MAAI,iBAAiB,kBAAkB;AACrC,WAAQ,MAAM,iBAAiB,MAAM,OAAO;AAC5C,WAAQ,KAAK,gEAAgE;AAC7E,UAAO,QAAQ,KAAK,EAAE;;AAExB,MAAI,iBAAiB,gBAAgB;AACnC,WAAQ,MAAM,yCAAyC,MAAM,WAAW,GAAG;AAC3E,UAAO,QAAQ,KAAK,EAAE;;AAExB,MAAI,iBAAiB,WAAW;AAC9B,WAAQ,MAAM,iCAAiC,MAAM,WAAW,IAAI,MAAM,IAAI,GAAG;AACjF,OAAI,MAAM,eAAe,CACvB,SAAQ,KAAK,gEAAgE;AAE/E,UAAO,QAAQ,KAAK,EAAE;;AAExB,QAAM"}