import{yb as ZQ,zb as KQ}from"./chunk-jkfbbcs7.js";import{Pb as l,Qb as r,Rb as p}from"./chunk-py3ymxjf.js";import{jc as d,kc as YQ}from"./chunk-dnw4x7bh.js";import{oc as qQ}from"./chunk-ghzc37v7.js";import H from"node:fs";import F from"node:path";import h from"node:process";YQ();var WQ=async(...Q)=>null;function c(Q,K){let G=Q.replace(/^v/,"").split("."),$=K.replace(/^v/,"").split(".");for(let q=0;q<Math.max(G.length,$.length);q++){let Z=Number.parseInt(G[q]||"0",10),X=Number.parseInt($[q]||"0",10);if(!Number.isNaN(Z)&&!Number.isNaN(X)){if(Z!==X)return X-Z}else{let Y=($[q]||"").localeCompare(G[q]||"");if(Y!==0)return Y}}return 0}function QQ(Q){if(Q.includes("/")){let[K,...G]=Q.split("/"),$=G.join("/"),q=K.replace(/\./g,"");if(K.includes("github.com")){let X=$.split("/");return X[X.length-1].replace(/[.-]/g,"").toLowerCase()}let Z=$.replace(/[/.-]/g,"");return`${q}${Z}`.toLowerCase()}return Q.replace(/\./g,"").toLowerCase()}function zQ(Q,K){switch(Q){case"versions":if(Array.isArray(K)&&K.length>0)return`  /**
  * Available versions from newest to oldest.
  * @see https://ts-pantry.netlify.app/usage for installation instructions
  */`;break;case"programs":if(Array.isArray(K)&&K.length>0)return`  /**
  * Executable programs provided by this package.
  * These can be run after installation.
  */`;break;case"dependencies":if(Array.isArray(K)&&K.length>0)return`  /**
  * Runtime dependencies for this package.
  * These are required when running the package.${K.some((q)=>typeof q==="string"&&q.includes(":"))?"\n  * OS-specific dependencies are prefixed with `os:` (e.g., `linux:freetype.org`).":""}
  */`;break;case"buildDependencies":if(Array.isArray(K)&&K.length>0)return`  /**
  * Build dependencies for this package.
  * These are only required when building the package from source.${K.some((q)=>typeof q==="string"&&q.includes(":"))?"\n  * OS-specific dependencies are prefixed with `os:` (e.g., `linux:gnu.org/gcc`).":""}
  */`;break;case"companions":if(Array.isArray(K)&&K.length>0)return`  /**
  * Related packages that work well with this package.
  * Consider installing these for enhanced functionality.
  */`;break;case"aliases":if(Array.isArray(K)&&K.length>0)return`  /**
  * Alternative names for this package.
  * You can use any of these names to access the package.
  */`;break;case"installCommand":return`  /**
  * Command to install this package using pantry.
  * @example pantry install package-name
  */`;case"description":return`  /**
  * Brief description of what this package does.
  */`;case"domain":return`  /**
  * The canonical domain name for this package.
  */`;case"name":return`  /**
  * The display name of this package.
  */`;default:return null}return null}function XQ(Q){let K=[];K.push("{");for(let[G,$]of Object.entries(Q)){if(G==="fetchedAt")continue;let q=zQ(G,$);if(q)K.push(q);if($===void 0){K.push(`  ${G}: undefined,`);continue}if(Array.isArray($))if($.length===0)K.push(`  ${G}: [] as const,`);else if(typeof $[0]==="string"){let Z=$;if(G==="aliases"){let X=Q.name;Z=$.filter((Y)=>{if(Y.includes("--")||Y.includes("$SHELL")||Y.includes("+")||Y.includes("(")||Y.includes(")"))return!1;if(Y===X)return!1;return!0})}if(Z.length===0)K.push(`  ${G}: [] as const,`);else{K.push(`  ${G}: [`);for(let X of Z)K.push(`    '${String(X).replace(/'/g,"\\'")}',`);K.push("  ] as const,")}}else{let Z=JSON.stringify($).replace(/"/g,"'");K.push(`  ${G}: ${Z} as const,`)}else if(typeof $==="string")K.push(`  ${G}: '${String($).replace(/'/g,"\\'")}' as const,`);else if(typeof $==="number"||typeof $==="boolean")K.push(`  ${G}: ${$} as const,`);else if($===null)K.push(`  ${G}: null,`);else if(typeof $==="object")K.push(`  ${G}: ${XQ($)},`);else{let Z=JSON.stringify($).replace(/"/g,"'");K.push(`  ${G}: ${Z},`)}}return K.push("}"),K.join(`
`)}function _Q(Q,K,G){let $=[];if($.push("/**"),Q.description)$.push(` * **${G}** - ${Q.description.trim()}`);else $.push(` * **${G}** - pkgx package`);if($.push(" *"),$.push(` * @domain \`${Q.domain||K}\``),Q.programs&&Q.programs.length>0){let q=Q.programs.slice(0,5).join("`, `"),Z=Q.programs.length>5?`, ... (+${Q.programs.length-5} more)`:"";$.push(` * @programs \`${q}\`${Z}`)}if(Q.versions&&Q.versions.length>0)$.push(` * @version \`${Q.versions[0]}\` (${Q.versions.length} versions available)`),$.push(" * @versions From newest version to oldest.");if(Q.installCommand)$.push(" *"),$.push(` * @install \`${Q.installCommand}\``);if(Q.aliases&&Q.aliases.length>0){let q=Q.aliases.filter((Z)=>{return!Z.includes("--")&&!Z.includes("$SHELL")&&!Z.includes("+")&&!Z.includes("(")&&!Z.includes(")")});if(q.length>0){let Z=Q.name,X=q.filter((W)=>W!==Z),Y=q.find((W)=>W===Z);if(Y)$.push(` * @name \`${Y}\``);if(X.length>0)$.push(` * @aliases ${X.map((W)=>`\`${W}\``).join(", ")}`)}}if(Q.homepageUrl)$.push(` * @homepage ${Q.homepageUrl}`);if(Q.dependencies&&Q.dependencies.length>0){let q=Q.dependencies.slice(0,3).join("`, `"),Z=Q.dependencies.length>3?`, ... (+${Q.dependencies.length-3} more)`:"",Y=Q.dependencies.some((W)=>W.includes(":"))?" (includes OS-specific dependencies with `os:package` format)":"";$.push(` * @dependencies \`${q}\`${Z}${Y}`)}if(Q.buildDependencies&&Q.buildDependencies.length>0){let q=Q.buildDependencies.slice(0,3).join("`, `"),Z=Q.buildDependencies.length>3?`, ... (+${Q.buildDependencies.length-3} more)`:"",Y=Q.buildDependencies.some((W)=>W.includes(":"))?" (includes OS-specific dependencies with `os:package` format)":"";$.push(` * @buildDependencies \`${q}\`${Z}${Y} - required only when building from source`)}if(Q.companions&&Q.companions.length>0){let q=Q.companions.slice(0,3).join("`, `"),Z=Q.companions.length>3?`, ... (+${Q.companions.length-3} more)`:"";$.push(` * @companions \`${q}\`${Z}`)}if($.push(" *"),$.push(" * @example"),$.push(" * ```typescript"),$.push(" * import { pantry } from 'ts-pantry'"),$.push(" *"),Q.aliases&&Q.aliases.length>0){let q=Q.aliases.find((Z)=>{return!Z.includes("--")&&!Z.includes("$SHELL")&&!Z.includes("+")&&!Z.includes("(")&&!Z.includes(")")});if(q){let Z=q.replace(/[^a-z0-9]/gi,""),X=K.replace(/[.-]/g,"").replace(/\//g,"").toLowerCase();if(q===Q.name)$.push(" * // Access the package"),$.push(` * const pkg = pantry.${Z}`),$.push(" * // Or access via domain"),$.push(` * const samePkg = pantry.${X}`),$.push(" * console.log(pkg === samePkg) // true");else $.push(" * // Access via alias (recommended)"),$.push(` * const pkg = pantry.${Z}`),$.push(" * // Or access via domain"),$.push(` * const samePkg = pantry.${X}`),$.push(" * console.log(pkg === samePkg) // true")}else{let Z=K.replace(/[.-]/g,"").replace(/\//g,"").toLowerCase();$.push(` * const pkg = pantry.${Z}`)}}else{let q=K.replace(/[.-]/g,"").replace(/\//g,"").toLowerCase();$.push(` * const pkg = pantry.${q}`)}if($.push(` * console.log(pkg.name)        // "${Q.name||G}"`),Q.description){let q=Q.description.length>50?`${Q.description.substring(0,47)}...`:Q.description;$.push(` * console.log(pkg.description) // "${q}"`)}if(Q.programs&&Q.programs.length>0)$.push(` * console.log(pkg.programs)    // [${Q.programs.slice(0,2).map((q)=>`"${q}"`).join(", ")}${Q.programs.length>2?", ...":""}]`);if(Q.versions&&Q.versions.length>0)$.push(` * console.log(pkg.versions[0]) // "${Q.versions[0]}" (latest)`);return $.push(" * ```"),$.push(" *"),$.push(` * @see https://ts-pantry.netlify.app/packages/${K.replace(/\./g,"-")}.md`),$.push(" * @see https://ts-pantry.netlify.app/usage"),$.push(" */"),$.join(`
`)}function jQ(Q,K){function G(W){if(!W||W.length<2)return!1;if(W.includes("--")||W.includes("$SHELL")||W.includes("+")||W.includes("(")||W.includes(")"))return!1;if(["cli","app","tool","server","client","api","lib","core"].includes(W.toLowerCase()))return!1;if(/^[\d.]+$/.test(W)&&/^\d/.test(W))return!1;return!0}let $,q;if(K==="classic.yarnpkg.com")$="classicYarnPackage",q="yarn";else if(K==="yarnpkg.com")$="yarnPackage",q="yarn";else if(Q.aliases&&Q.aliases.length>0){let W=Q.aliases.find((J)=>G(J));if(W){let J=W.replace(/[^a-z0-9]/gi,"");$=`${J.charAt(0).toLowerCase()}${J.slice(1)}Package`,q=W}else $=`${QQ(K)}Package`,q=Q.name||K}else $=`${QQ(K)}Package`,q=Q.name||K;let Z=$.replace(/[^\w$]/g,""),X=_Q(Q,K,q),Y=XQ(Q);return`${X}
export const ${Z} = ${Y}

export type ${Z.charAt(0).toUpperCase()+Z.slice(1)} = typeof ${Z}
`}function s(Q){let K=Q.replace(/\{\{[^}]*\}\}/g,"").replace(/-{2,}/g,"-").replace(/^-+|-+$/g,"");if(!K||K.length===0)return"package";let G=K.replace(/[^\w.-]/g,"-");if(G.length>200)G=`${G.substring(0,150)}...${G.substring(G.length-45)}`;return G}function f(Q,K,G){if(!H.existsSync(Q))H.mkdirSync(Q,{recursive:!0});let $;if(K.includes("/")){let X=K.split("/");if(X.some((B)=>!B||B===".."||B==="."||B.includes("\x00")||B.includes("\\")))throw Error(`Invalid domain name (contains path-traversal): ${K}`);let Y=X[0],W=X.slice(1),J=F.join(Q,Y,...W.slice(0,-1)),E=F.resolve(Q);if(!F.resolve(J).startsWith(`${E}${F.sep}`)&&F.resolve(J)!==E)throw Error(`Path traversal detected for ${K}`);if(!H.existsSync(J))H.mkdirSync(J,{recursive:!0});let T=s(W[W.length-1]);$=F.join(J,`${T}.ts`)}else{let X=K.replace(/\./g,"");$=F.join(Q,`${X}.ts`)}let q={...G};delete q.fetchedAt;let Z=jQ(q,K);return H.writeFileSync($,Z),$}function SQ(Q,K){if(!H.existsSync(Q))return[];let G=new Set;for(let X of K)if(X.includes("/")){let Y=X.split("/"),W=Y[0],J=Y.slice(1),E=s(J[J.length-1]);G.add(F.resolve(Q,W,...J.slice(0,-1),`${E}.ts`))}else{let Y=X.replace(/\./g,"");G.add(F.resolve(Q,`${Y}.ts`))}let $=new Set(["index.ts","aliases.ts","types.ts"]),q=[];function Z(X){if(!H.existsSync(X))return;for(let Y of H.readdirSync(X,{withFileTypes:!0})){let W=F.join(X,Y.name);if(Y.isDirectory()){if(Z(W),H.existsSync(W)&&H.readdirSync(W).length===0)H.rmdirSync(W)}else if(Y.name.endsWith(".ts")&&!$.has(Y.name)){if(!G.has(F.resolve(W)))H.unlinkSync(W),q.push(W)}}}return Z(Q),q}function HQ(Q,K={}){try{let G=K.cacheDir||l;if(K.cache===!1)return console.log(`Cache disabled for ${Q}, skipping cache check`),null;let q=(d[Q]||Q).replace(/\//g,"-"),Z=F.join(G,`${q}.json`);if(console.log(`Checking for cache file: ${Z}`),!H.existsSync(Z))return null;let X;try{X=JSON.parse(H.readFileSync(Z,"utf-8"))}catch{try{H.unlinkSync(Z)}catch{}return null}if(!X.fetchedAt)return console.log(`Cache for ${Q} missing fetchedAt timestamp, invalidating`),null;if(["Crafters of fine Open Source products","Go home.","Package information for","pkgx package","Loading...","Please wait..."].some((B)=>X.description&&X.description.includes(B)))return console.log(`Cache for ${Q} has generic description "${X.description}", invalidating`),null;if(!X.versions||X.versions.length===0)return console.log(`Cache for ${Q} has no versions, invalidating`),null;let E=(K.cacheExpirationMinutes||r)*60*1000,T=Date.now();if(T-X.fetchedAt>E)return console.log(`Cache for ${Q} has expired (age: ${Math.round((T-X.fetchedAt)/60000)} minutes)`),null;return console.log(`Using cached data for ${Q} (age: ${Math.round((T-X.fetchedAt)/60000)} minutes)`),{packageInfo:X,filePath:Z}}catch(G){return console.error(`Error reading cache for ${Q}:`,G),null}}function LQ(Q,K,G={}){let $={...K,fetchedAt:Date.now()},q=G.cacheDir||l;if(!H.existsSync(q))H.mkdirSync(q,{recursive:!0});let Z=Q.replace(/\//g,"-"),X=F.join(q,`${Z}.json`);H.writeFileSync(X,JSON.stringify($,null,2));let Y=G.outputDir||"packages";if(!H.existsSync(Y))H.mkdirSync(Y,{recursive:!0});let W=f(Y,Q,K);return{cachePath:X,outputPath:W}}function GQ(){return Promise.resolve()}async function yQ(Q={}){let K=Q.apiUrl||"https://api.github.com/repos/pkgxdev/pantry/contents/projects";try{console.log(`Fetching projects from GitHub API: ${K}`);let G=await fetch(K,{headers:{Accept:"application/vnd.github.v3+json"}});if(!G.ok)throw Error(`Failed to fetch from GitHub API: ${G.statusText}`);let q=(await G.json()).filter((Z)=>Z.type==="dir").map((Z)=>({name:Z.name,url:Z.html_url}));return console.log(`Retrieved ${q.length} projects from GitHub API`),q.sort((Z,X)=>Z.name.localeCompare(X.name)),q}catch(G){throw console.error("Error fetching projects from GitHub API:",G),G}}async function t(Q,K={}){let G=Q,$=Q;if(Q.includes("/")){let[q,Z]=Q.split("/");console.log(`Identified nested package: domain=${q}, name=${Z}`)}if(d[Q])console.log(`'${Q}' is an alias for '${d[Q]}', redirecting...`),$=d[Q],Q=d[Q];try{let q=K.timeout||p,Z=await WQ(Q,{timeout:q,useClientSideScraper:!0});if(!Z||typeof Z!=="object")return $Q(Q,G,$);let X={name:Z.displayName||Z.name||Q.split("/").pop()||Q,domain:Z.domain||Q,description:Z.description||Z.brief||"",packageYmlUrl:`https://github.com/pkgxdev/pantry/tree/main/projects/${Q}/package.yml`,homepageUrl:Z.homepage||"",githubUrl:Z.github||"",installCommand:`pantry install ${Q}`,pantryInstallCommand:`pantry install ${Q}`,programs:Z.provides||[],companions:Array.isArray(Z.companions)?Z.companions:[],dependencies:Array.isArray(Z.dependencies)?Z.dependencies:[],buildDependencies:Z.buildDependencies||[],versions:Z.versions||[],aliases:[]};if(!X.versions||X.versions.length===0){let Y=X.domain||Q;if(!["apache.org","breakfastquay.com","catb.org","bloomreach.com","cairographics.org","certifi.io","charm.sh","aws.amazon.com","google.com","microsoft.com","mozilla.org","freedesktop.org","gnome.org","kde.org","sourceforge.net","sf.net"].some((J)=>Y.startsWith(J)))try{let J=X.domain||Q,E=await UQ(J);X.versions=E}catch(J){console.warn(`Failed to fetch versions for ${Q} from GitHub API:`,J),X.versions=[]}}if(X.versions&&X.versions.length>0)try{X.versions.sort((Y,W)=>{try{return-1*Bun.semver.order(Y,W)}catch{return c(Y,W)}})}catch(Y){console.warn(`Warning: Failed to sort versions for ${Q} using semver:`,Y)}return X.name=X.name||G,X.domain=X.domain||$,X.programs=X.programs||[],X.companions=X.companions||[],X.dependencies=X.dependencies||[],X.versions=X.versions||[],{packageInfo:X,originalName:G,fullDomainName:$}}catch(q){return console.error(`Error in fetchPantryPackage for ${Q}:`,q),$Q(Q,G,$)}}async function UQ(Q){try{let{shouldProceedWithGitHubRequest:K,saveRateLimitInfo:G}=await import("./chunk-wcn78wgj.js");if(!K())return console.warn("Skipping GitHub API request for versions due to rate limiting"),[];let $=await fetch(`https://api.github.com/repos/pkgxdev/pantry/contents/projects/${Q}`,{headers:{Accept:"application/vnd.github.v3+json","User-Agent":"ts-pantry"},signal:AbortSignal.timeout(15000)});if(G($.headers),$.status===403){let Z=$.headers.get("X-RateLimit-Remaining"),X=$.headers.get("X-RateLimit-Reset"),Y=X?new Date(Number(X)*1000).toLocaleString():"unknown";return console.warn(`GitHub API rate limit exceeded. Remaining: ${Z||0}, Reset: ${Y}`),[]}if(!$.ok)throw Error(`GitHub API responded with ${$.status}: ${$.statusText}`);return(await $.json()).filter((Z)=>Z.name.match(/^v\d+/)).map((Z)=>Z.name)}catch(K){return console.error("Error fetching versions from GitHub:",K),[]}}var RQ=300000,N=null;function EQ(){let Q=async(K)=>{if(console.log(`
Received ${K} signal. Cleaning up resources...`),N)clearInterval(N);await GQ(),console.log("Resources cleaned up. Exiting."),setTimeout(()=>{console.log("Forcing process exit..."),h.exit(0)},5000)};h.on("SIGINT",()=>Q("SIGINT")),h.on("SIGTERM",()=>Q("SIGTERM")),h.on("uncaughtException",(K)=>{console.error("Uncaught exception:",K),Q("uncaughtException")})}if(typeof Bun<"u"&&Bun.main===import.meta.path)EQ(),h.on("uncaughtException",(Q)=>{let K=String(Q);if(console.error("Uncaught exception caught:",Q.message),Q.message.includes("No target found for targetId")||Q.message.includes("Assertion error")||Q.message.includes("Target page, context or browser has been closed")||Q.message.includes("Failed to connect")||Q.message.includes("Connection closed")||Q.message.includes("Protocol error")||K.includes("WebSocket connection closed")||Q.message.includes("Browser has been closed")){console.error("Browser/network connection error detected, continuing execution...");return}console.error("Non-browser error, exiting..."),h.exit(1)}),h.on("unhandledRejection",(Q,K)=>{let G=String(Q);if(console.error("Unhandled rejection caught:",G),G.includes("No target found for targetId")||G.includes("Assertion error")||G.includes("Target page, context or browser has been closed")||G.includes("Failed to connect")||G.includes("Connection closed")||G.includes("Protocol error")||G.includes("WebSocket connection closed")||G.includes("Browser has been closed")){console.error("Browser/network connection error in promise, continuing execution...");return}console.error("Non-browser promise rejection, exiting..."),h.exit(1)});function vQ(){if(N)clearInterval(N);N=setInterval(()=>{console.log("Running periodic browser resource cleanup..."),GQ().then(()=>console.log("Periodic cleanup completed")).catch((Q)=>console.error("Error during periodic cleanup:",Q))},RQ)}async function bQ(Q={}){let K=Q.timeout||p,G=Q.outputDir||"packages",$=Q.cacheDir||l,q=Q.cache!==!1,Z=Q.cacheExpirationMinutes||r,X=Math.min(Q.concurrency||6,8),Y=Q.pantryDir||"src/pantry";if(!Q.outputJson)console.log(`Starting bulk fetch with concurrency: ${X}, timeout: ${K}ms`);let W=Date.now(),J=[];try{if(!Q.outputJson)console.log("Scanning local pantry for packages...");try{if(J=await OQ(Y),!Q.outputJson)console.log(`Found ${J.length} packages in local pantry`)}catch(j){if(!Q.outputJson)console.warn("Failed to scan local pantry, falling back to consts:",j);try{let{ALL_KNOWN_PACKAGES:_}=await import("./chunk-0xfx7txg.js");if(J=[..._].filter((L)=>L&&L!=="undefined"&&L!=="aliases"&&L!=="index"),!Q.outputJson)console.log(`Using comprehensive package list: ${J.length} packages`)}catch(_){if(!Q.outputJson)console.warn("Failed to load consts, falling back to GitHub API:",_);try{let L=await fetch("https://api.github.com/repos/pkgxdev/pantry/contents/projects",{headers:{"User-Agent":"ts-pantry-fetcher",Accept:"application/vnd.github.v3+json"}});if(L.ok){let x=(await L.json()).filter((U)=>U.type==="dir").map((U)=>U.name).sort();if(!Q.outputJson)console.log(`Retrieved ${x.length} projects from GitHub API`);J=x}else throw Error(`GitHub API returned ${L.status}`)}catch(L){if(!Q.outputJson)console.warn("Failed to fetch from GitHub API, falling back to local discovery:",L);if(H.existsSync(G))J=H.readdirSync(G).filter((x)=>x.endsWith(".ts")).map((x)=>F.basename(x,".ts")),console.log(`Found ${J.length} existing packages`)}}}if(J.length===0)return console.error("No packages found to process"),[];let E=["android.com","apache.org","akuity.io","alsa-project.org","anchore.com","aomedia.googlesource.com","apollographql.com","facebook.com","google.com","microsoft.com","mozilla.org","sourceforge.net","x.org","xiph.org","yui.github.io","crates.io","gitlab.com","github.com"],T=new Set,B=J.filter((j)=>j.startsWith("github.com/"));for(let j of B){let _=j.split("/");if(_.length>2){let L=_.slice(0,2).join("/");T.add(L)}}if(J=J.filter((j)=>{if(j.endsWith("/.pkgroot"))return console.log(`Filtering out .pkgroot marker: ${j}`),!1;if(j.endsWith("/README.rst"))return console.log(`Filtering out README.rst documentation: ${j}`),!1;if(j==="agwa.name")return console.log("Filtering out standalone agwa.name - only agwa.name/git-crypt is valid"),!1;if(E.includes(j))return console.log(`Filtering out parent domain ${j} - only has nested packages`),!1;if(T.has(j))return console.log(`Filtering out GitHub parent path ${j} - only has nested packages`),!1;if(j==="undefined"||j==="aliases"||j==="index")return!1;return!0}),console.log(`Found ${J.length} packages to process`),Q.limit&&Q.limit>0)J=J.slice(0,Q.limit),console.log(`Limited to ${J.length} packages`);let O=[],M=[],C=0,w=Math.min(X,6),z=[];for(let j=0;j<J.length;j+=w)z.push(J.slice(j,j+w));if(!Q.outputJson)console.log(`Processing ${J.length} packages in ${z.length} batches...`);for(let j=0;j<z.length;j++){let _=z[j];if(!Q.outputJson)console.log(`Processing batch ${j+1}/${z.length} (${_.length} packages)`);let L=_.map(async(R)=>{try{let P=F.join($,`${R.replace(/\//g,"-")}.json`);if(q&&H.existsSync(P)){let v=H.statSync(P),k=(Date.now()-v.mtime.getTime())/60000;if(k<Z){if(!Q.outputJson)console.log(`Using cached data for ${R} (age: ${Math.round(k)} minutes)`);try{let u=JSON.parse(H.readFileSync(P,"utf-8"));delete u.fetchedAt;let g=f(G,R,u);if(!Q.outputJson)console.log(`Using cached data for ${R} (saved to ${g})`);return{success:!0,packageName:R}}catch{console.warn(`Cache file corrupted for ${R}, will refetch`)}}}let S=await FQ(R,{...Q,pantryDir:Y,timeout:K,cache:q,cacheDir:$,cacheExpirationMinutes:Z});if(S){let{outputPath:v}=LQ(R,S.packageInfo,{cacheDir:$,outputDir:G,cache:q});if(!Q.outputJson)console.log(`Successfully processed ${R} (saved to ${v})`);return{success:!0,packageName:R}}else return console.warn(`Failed to fetch ${R}`),{success:!1,packageName:R}}catch(P){return console.error(`Error processing ${R}:`,P),{success:!1,packageName:R}}}),V=await Promise.allSettled(L);for(let R of V)if(C++,R.status==="fulfilled")if(R.value.success)O.push(R.value.packageName);else M.push(R.value.packageName);else console.error("Batch promise rejected:",R.reason),M.push("unknown");let x=Math.round((Date.now()-W)/1000),U=Math.round(C/x*10)/10,y=Math.round((J.length-C)/U);if(!Q.outputJson)console.log(`Batch ${j+1}/${z.length} complete. Progress: ${C}/${J.length} (${Math.round(C/J.length*100)}%) | Success: ${O.length} | Failed: ${M.length} | Rate: ${U}/s | ETA: ${y}s`);if(j<z.length-1)await new Promise((R)=>setTimeout(R,1000))}let A=Math.round((Date.now()-W)/1000);if(!Q.outputJson){if(console.log(`
Completed in ${A}s. Successfully processed: ${O.length}, Failed: ${M.length}`),M.length>0)console.log(`Failed packages: ${M.slice(0,10).join(", ")}${M.length>10?` ... and ${M.length-10} more`:""}`)}return O}catch(E){return console.error("Error in fetchAndSaveAllPackages:",E),[]}}function $Q(Q,K,G){let $=Q.split("/"),q=$[0];return{packageInfo:{name:($.length>1?$.slice(1).join("/"):null)||Q,domain:q,description:`Package information for ${Q}`,packageYmlUrl:"",homepageUrl:"",githubUrl:"",installCommand:`pantry install ${Q}`,pantryInstallCommand:`pantry install ${Q}`,programs:[],companions:[],dependencies:[],versions:[]},originalName:K,fullDomainName:G}}async function BQ(Q,K,G,$=!1,q=1,Z=3,X=!1,Y={}){try{let W=Y.cache!==!1,J=Y.cacheDir||l;if(!W)console.log(`Cache disabled for ${Q}, will fetch directly`);else{let C=HQ(Q,{cacheDir:J,cacheExpirationMinutes:Y.cacheExpirationMinutes,cache:Y.cache});if(C){let{packageInfo:w}=C,z=w.domain||Q,A=w.aliases||[],j=f(K,Q,w);return console.log(`Using cached data for ${Q} (saved to ${j})`),{success:!0,fullDomainName:z,aliases:A,filePath:j}}}let T=G+(q-1)*1e4;console.log(`Using timeout for ${Q} (attempt ${q}): ${T}ms`);let B=T,O,M=new Promise((C,w)=>{O=setTimeout(()=>{console.error(`Operation timeout for ${Q} after ${B}ms`),w(Error(`Operation timeout after ${B}ms`))},B)});if(Q.includes("/")){let C=Q.split("/"),w=C[0],z=C.slice(1).join("/");console.log(`Processing nested package: domain=${w}, subPath=${z}`);try{let A=t(`${w}/${z}`,{timeout:T,browser:Y.browser}),j=await Promise.race([A,M]);clearTimeout(O);let{packageInfo:_,originalName:L,fullDomainName:V}=j;if(_.versions&&_.versions.length>0)try{_.versions.sort((S,v)=>{try{return-1*Bun.semver.order(S,v)}catch{return c(S,v)}})}catch(S){console.warn(`Warning: Failed to sort versions for ${Q} using semver:`,S)}let x=V.replace(/\//g,"-"),U=[];if(_.aliases&&_.aliases.length>0)U.push(..._.aliases);for(let[S,v]of Object.entries(d))if(v===V&&!U.includes(S))U.push(S);if(L!==V&&!U.includes(L))U.push(L);let y=["cli","app","tool","server","client","api","lib","core"];if(!U.includes(z)&&!y.includes(z.toLowerCase()))U.push(z);let R={..._,fullPath:Q,aliases:U.length>0?U:[]};if(W){let S=F.join(J,`${x}.json`),v={...R,fetchedAt:Date.now()};if(!H.existsSync(J))H.mkdirSync(J,{recursive:!0});H.writeFileSync(S,JSON.stringify(v,null,2)),console.log(`Saved package data to cache: ${S}`)}let P=f(K,V,R);return console.log(`Successfully saved nested package ${Q} to ${P} with aliases: ${U.join(", ")||"none"}`),{success:!0,fullDomainName:V,aliases:U,filePath:P}}catch(A){if(clearTimeout(O),A.toString().includes("404")||A.toString().includes("Not Found"))return console.error(`Package ${Q} returned 404 Not Found.`),console.error(`Skipping ${Q} to avoid overwriting existing files.`),{success:!1,fullDomainName:Q};throw A}}else try{let C=t(Q,{timeout:T,browser:Y.browser}),w=await Promise.race([C,M]);clearTimeout(O);let{packageInfo:z,originalName:A,fullDomainName:j}=w,_=z.description&&z.description.trim()!=="",L=z.versions&&z.versions.length>0,V=z.programs&&z.programs.length>0,x=z.packageYmlUrl&&z.packageYmlUrl.length>0,U=z.dependencies&&z.dependencies.length>0,y=z.companions&&z.companions.length>0,R=_&&(z.description.toLowerCase().trim()==="go home."||z.description.toLowerCase().trim()==="page not found"||z.description.toLowerCase().trim()==="not found"||z.description.toLowerCase().trim()==="404"||z.description.toLowerCase().trim()==="error"),P=_&&!R,S=L||V||x||U||y,v=z.domain&&z.domain.includes(".");if(!(P||S||v))return console.error(`Package ${Q} appears to have no meaningful data. Skipping to avoid creating empty files.`),{success:!1,fullDomainName:Q};let u=z.name&&z.name!==j&&z.name!==j.split("/")[0]&&z.name.length>1&&!z.name.includes("/")?s(z.name):null,g=j.replace(/\//g,"-"),m=u?F.join(K,`${u}.ts`):F.join(K,`${g}.ts`);if(H.existsSync(m)){let b=H.readFileSync(m,"utf-8"),I=b.includes("dependencies: [")&&!b.includes("dependencies: [] as const"),n=z.dependencies.length===0,e=!b.includes("description: 'Package information for")&&!b.includes("description: '' as const"),a=z.description.includes("Package information for")||z.description.trim()==="";if(I&&n||e&&a)return console.warn(`⚠️  Existing file ${m} has better data than new fetch. Skipping to prevent data loss.`),console.warn(`   Existing deps: ${I}, New deps: ${!n}`),console.warn(`   Existing desc: ${e}, New desc: ${!a}`),{success:!1,fullDomainName:Q}}if(z.versions&&z.versions.length>0)try{z.versions.sort((b,I)=>{try{return-1*Bun.semver.order(b,I)}catch{return c(b,I)}})}catch(b){console.warn(`Warning: Failed to sort versions for ${Q} using semver:`,b)}let JQ=j.replace(/\//g,"-"),D=[];if(z.aliases&&z.aliases.length>0)D.push(...z.aliases);for(let[b,I]of Object.entries(d))if(I===j&&!D.includes(b))D.push(b);if(A!==j&&!D.includes(A))D.push(A);let o={...z,fullPath:Q,aliases:D.length>0?D:[]};if(W){let b=F.join(J,`${JQ}.json`),I={...o,fetchedAt:Date.now()};if(!H.existsSync(J))H.mkdirSync(J,{recursive:!0});H.writeFileSync(b,JSON.stringify(I,null,2)),console.log(`Saved package data to cache: ${b}`)}let i=f(K,j,o);return console.log(`Successfully saved ${Q} to ${i} with aliases: ${D.join(", ")||"none"}`),{success:!0,fullDomainName:j,aliases:D,filePath:i}}catch(C){if(clearTimeout(O),C.toString().includes("404")||C.toString().includes("Not Found"))return console.error(`Package ${Q} returned 404 Not Found. Skipping to avoid overwriting existing files.`),{success:!1,fullDomainName:Q};throw C}}catch(W){let J=String(W),E=J.includes("Operation timed out after")||J.includes("Operation timeout after")||J.includes("timeout")||J.includes("Timeout")||J.includes("Navigation timeout"),T=J.includes("No target found")||J.includes("targetId")||J.includes("Target page, context or browser has been closed")||J.includes("Protocol error")||J.includes("Connection closed")||J.includes("Assertion error")||J.includes("Browser has been closed")||J.includes("Target closed")||J.includes("Session closed")||J.includes("WebSocket connection closed");if(E)console.error(`Timeout error for ${Q}:`,J.substring(0,200)),await new Promise((O)=>setTimeout(O,1000*q));else if(T){if(console.error(`Browser connection error for ${Q}:`,J.substring(0,200)),Y.browser)try{await Y.browser.close().catch(()=>{})}catch{}await new Promise((O)=>setTimeout(O,3000*q))}if(q>=Z){if(console.error(`Failed to fetch package ${Q} after ${q} attempts:`,W),X){let O=F.join(h.cwd(),`${Q.replace(/\//g,"-")}-error.txt`);H.writeFileSync(O,`Error fetching ${Q}:
${W.toString()}

Stack:
${W.stack||"No stack trace available"}`),console.error(`Saved error details to ${O}`)}return console.error(`Skipping ${Q} after ${q} failed attempts to avoid overwriting existing files.`),{success:!1,fullDomainName:Q}}console.error(`Attempt ${q} failed for ${Q}, retrying...`,W);let B=500*q;if(T)B=2000*q;else if(E)B=1000*q;return await new Promise((O)=>setTimeout(O,B)),BQ(Q,K,G,$,q+1,Z,X,Y)}}async function OQ(Q="src/pantry"){if(!H.existsSync(Q))throw Error(`Pantry directory not found: ${Q}. Please run 'bun ./bin/cli.ts update-pantry' first.`);let K=[];function G($,q=""){let Z=H.readdirSync($,{withFileTypes:!0}),X=F.join($,"package.yml");if(q&&H.existsSync(X))K.push(q);for(let Y of Z)if(Y.isDirectory()&&!Y.name.startsWith(".")){let W=q?`${q}/${Y.name}`:Y.name,J=F.join($,Y.name);G(J,W)}}return G(Q),console.log(`Found ${K.length} packages in pantry`),K.sort()}async function VQ(Q,K="src/pantry"){let G=F.join(K,Q,"package.yml");if(!H.existsSync(G))return console.warn(`Package.yml not found for ${Q}`),null;try{let $=H.readFileSync(G,"utf-8"),q=[],Z=[],X=$.split(`
`),Y=!1,W=!1,J=!1,E="";for(let _ of X){if(_.match(/^dependencies:\s*$/)){Y=!0,W=!1,J=!1;continue}if(_.match(/^\s{2}dependencies:\s*$/)){W=!0,Y=!1,J=!1;continue}if(Y||W){if(_.match(/^\s*$/)||Y&&_.match(/^\S/)||W&&!_.match(/^\s{4,}/)){Y=!1,W=!1,J=!1;continue}let L=_.match(/^\s{2}(linux|darwin|windows):\s*$/);if(L){J=!0,E=L[1];continue}if(J){let V=_.match(/^\s{4,}([^\s:]+):\s*['"]?([^'"]+)['"]?/);if(V){let x=V[1],U=V[2].trim(),y=`${E}:`;if(U&&U!=="*"){let R=/^[~^>=<@]/.test(U),P=/^[\d.]+$/.test(U)&&/^\d/.test(U),v=`${y}${x}${R?"":P?"@":"^"}${U}`;if(W)Z.push(v);else q.push(v)}else{let R=`${y}${x}`;if(W)Z.push(R);else q.push(R)}}else if(!_.match(/^\s{4,}/)&&!_.match(/^\s*$/))J=!1,E=""}else{let V=_.match(/^\s{2,3}([^\s:]+):\s*['"]?([^'"]+)['"]?/);if(V){let x=V[1],U=V[2].trim();if(["linux","darwin","windows"].includes(x))continue;if(U&&U!=="*"){let y=/^[~^>=<@]/.test(U),R=/^[\d.]+$/.test(U)&&/^\d/.test(U),S=`${x}${y?"":R?"@":"^"}${U}`;if(W)Z.push(S);else q.push(S)}else{let y=x;if(W)Z.push(y);else q.push(y)}}}}}let T=[],B=!1;for(let _ of X){if(_.match(/^runtime:\s*$/)){B=!0;continue}if(B){if(_.match(/^\s*$/)||_.match(/^\S/)){B=!1;continue}if(_.match(/^\s+env:\s*$/))continue;if(_.match(/^\s+[A-Z_]+:\s*\$\{\{/))continue;let L=_.match(/^\s+([^\s:]+):\s*['"]?([^'"]+)['"]?/);if(L){let V=L[1],x=L[2].trim();if(/^[A-Z_]+$/.test(V))continue;if(x&&x!=="*"){let U=/^[~^>=<@]/.test(x),y=/^[\d.]+$/.test(x)&&/^\d/.test(x),R=U?"":y?"@":"^";T.push(`${V}${R}${x}`)}else T.push(V)}}}let O="",M="",C=$.match(/distributable:[\t\v\f\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*\n\s*url:\s*(.+)/);if(C){let _=C[1].trim();if(_.includes("github.com")){let L=_.match(/git\+https:\/\/github\.com\/([^/]+\/[^.]+)/);if(L){if(M=`https://github.com/${L[1]}`,L[1]==="python-attrs/attrs")O="https://www.attrs.org/"}}}let w=[],z=!1,A=!1;for(let _ of X){if(_.match(/^provides:\s*$/)){z=!0,A=!1;continue}if(z){if(_.match(/^\S/)&&!_.match(/^\s*$/)){z=!1;continue}if(_.match(/^\s+(darwin|linux|windows):\s*$/)){A=!0;continue}let L=_.match(/^\s+-\s+(?:bin|sbin)\/(.+)/);if(L){let V=L[1].trim();if(!w.includes(V))w.push(V)}}}return{name:Q.split("/").pop()||Q,domain:Q,dependencies:q,buildDependencies:Z,companions:T,programs:w,homepageUrl:O,githubUrl:M}}catch($){return console.warn(`Error reading package.yml for ${Q}:`,$),null}}async function FQ(Q,K={}){let G=K.pantryDir||"src/pantry";try{let $=await VQ(Q,G);try{let q=await t(Q,K),Z=$?.domain||q.packageInfo.domain,X=ZQ(Z),Y=q.packageInfo.aliases||[];if(X.length>0)if(KQ(Z))Y=X;else Y=[...new Set([...Y,...X])];let W=[],J=[],E="",T="",B=q.packageInfo.versions||[],O=q.packageInfo.programs||[],M=$?.programs||[];if(B.length===0||O.length===0&&M.length===0){let w=K.outputDir||"src/packages",z=Q.split("/"),A=z.length>1?F.join(w,...z.slice(0,-1),`${z[z.length-1]}.ts`):F.join(w,`${Q.replace(/\./g,"")}.ts`);if(H.existsSync(A))try{let j=H.readFileSync(A,"utf-8");if(B.length===0){let _=j.match(/versions:\s*\[([\s\S]*?)\]\s*as\s*const/);if(_&&_[1].trim())W=_[1].match(/'([^']+)'/g)?.map((L)=>L.replace(/'/g,""))||[]}if(O.length===0&&M.length===0){let _=j.match(/programs:\s*\[([\s\S]*?)\]\s*as\s*const/);if(_&&_[1].trim())J=_[1].match(/'([^']+)'/g)?.map((L)=>L.replace(/'/g,""))||[]}if(!q.packageInfo.description){let _=j.match(/description:\s*'([^']*)'\s*as\s*const/);if(_&&_[1])E=_[1]}if(!q.packageInfo.homepageUrl&&!$?.homepageUrl){let _=j.match(/homepageUrl:\s*'([^']*)'\s*as\s*const/);if(_&&_[1])T=_[1]}}catch{}}return{packageInfo:{...q.packageInfo,dependencies:$?.dependencies&&$.dependencies.length>0?$.dependencies:q.packageInfo.dependencies,buildDependencies:$?.buildDependencies&&$.buildDependencies.length>0?$.buildDependencies:q.packageInfo.buildDependencies,companions:$?.companions&&$.companions.length>0?$.companions:q.packageInfo.companions,programs:O.length>0?O:M.length>0?M:J.length>0?J:[],versions:B.length>0?B:W,domain:$?.domain||q.packageInfo.domain,description:q.packageInfo.description||E||"",name:q.packageInfo.name||$?.name||Q.split("/").pop()||Q,homepageUrl:$?.homepageUrl||q.packageInfo.homepageUrl||T,githubUrl:$?.githubUrl||q.packageInfo.githubUrl,aliases:Y},originalName:q.originalName,fullDomainName:q.fullDomainName}}catch(q){if(!$)return console.warn(`No pantry or web data available for ${Q}:`,q),null;console.warn(`Failed to fetch web metadata for ${Q}, using pantry data only:`,q);let Z=[],X=[],Y="",W="",J=K.outputDir||"src/packages",E=Q.split("/"),T=E.length>1?F.join(J,...E.slice(0,-1),`${E[E.length-1]}.ts`):F.join(J,`${Q.replace(/\./g,"")}.ts`);if(H.existsSync(T))try{let M=H.readFileSync(T,"utf-8"),C=M.match(/versions:\s*\[([\s\S]*?)\]\s*as\s*const/);if(C&&C[1].trim())Z=C[1].match(/'([^']+)'/g)?.map((j)=>j.replace(/'/g,""))||[];let w=M.match(/programs:\s*\[([\s\S]*?)\]\s*as\s*const/);if(w&&w[1].trim())X=w[1].match(/'([^']+)'/g)?.map((j)=>j.replace(/'/g,""))||[];let z=M.match(/description:\s*'([^']*)'\s*as\s*const/);if(z&&z[1])Y=z[1];let A=M.match(/homepageUrl:\s*'([^']*)'\s*as\s*const/);if(A&&A[1])W=A[1]}catch{}let B=$.programs&&$.programs.length>0?$.programs:X;return{packageInfo:{name:$.name||Q.split("/").pop()||Q,domain:$.domain||Q,description:Y||"",installCommand:`pantry install ${Q}`,pantryInstallCommand:`pantry install ${Q}`,programs:B,companions:$.companions||[],dependencies:$.dependencies||[],versions:Z,buildDependencies:$.buildDependencies||[],packageYmlUrl:`https://github.com/pkgxdev/pantry/tree/main/projects/${Q}/package.yml`,homepageUrl:W||$.homepageUrl||"",githubUrl:$.githubUrl||""},originalName:Q,fullDomainName:Q}}}catch($){return console.error(`Error processing pantry package ${Q}:`,$),null}}
export{f as kb,SQ as lb,HQ as mb,LQ as nb,GQ as ob,yQ as pb,t as qb,EQ as rb,vQ as sb,bQ as tb,BQ as ub,OQ as vb,VQ as wb,FQ as xb};
