#!/usr/bin/env node
import'./chunk-5TAVYPOV.js';import*as o from'fs';import*as r from'path';import*as V from'os';import {exec}from'child_process';import n from'chalk';function m(t,s={}){return new Promise((a,c)=>{exec(t,{timeout:s.timeout||5e3,cwd:s.cwd},(i,e)=>{if(i)return c(i);a(e.trim());});})}function j(t){try{return o.existsSync(t)?JSON.parse(o.readFileSync(t,"utf8")):null}catch{return null}}function R(){let t=[],s=r.join(V.homedir(),".claude","plugins"),a=r.join(s,"marketplaces"),c=r.join(s,"cache");if(!o.existsSync(a))return t;let i;try{i=o.readdirSync(a).filter(e=>{let l=r.join(a,e);return o.statSync(l).isDirectory()&&o.existsSync(r.join(l,".git"))});}catch{return t}for(let e of i){let l=r.join(a,e),u=r.join(l,"plugins");if(!o.existsSync(u))continue;let y;try{y=o.readdirSync(u).filter(d=>o.statSync(r.join(u,d)).isDirectory());}catch{continue}for(let d of y){let b=r.join(u,d,".claude-plugin","plugin.json"),k=j(b);if(!k)continue;let D=k.version||"unknown",h=r.join(c,e,d),S="unknown";if(o.existsSync(h))try{let f=o.readdirSync(h).filter(g=>o.statSync(r.join(h,g)).isDirectory()).sort((g,v)=>{let P=g.split(".").map(Number),C=v.split(".").map(Number);for(let p=0;p<3;p++)if((P[p]||0)!==(C[p]||0))return (P[p]||0)-(C[p]||0);return 0}).reverse();f.length>0&&(S=f[0]);}catch{}let w=e;try{let g=o.readFileSync(r.join(l,".git","config"),"utf8").match(/github\.com[:/]([^/]+\/[^/\s.]+)/);g&&(w=g[1].replace(/\.git$/,""));}catch{}t.push({repo:w,plugin:d,installedVersion:S,marketplacePath:l,localVersion:D});}}return t}async function U(t){let s=null,a="unknown";try{a=await m("git rev-parse HEAD",{cwd:t.marketplacePath});}catch{}try{let l=(await m("git ls-remote origin HEAD",{cwd:t.marketplacePath,timeout:5e3})).match(/^([a-f0-9]+)/);l&&(s=l[1]);}catch{}let c=s!==null&&s!==a,i=t.localVersion!==t.installedVersion&&t.installedVersion!=="unknown";return {repo:t.repo,plugin:t.plugin,installedVersion:t.installedVersion,localVersion:t.localVersion,remoteSha:s,localSha:a,marketplacePath:t.marketplacePath,hasRemoteUpdate:c,hasCacheStale:i}}async function N(t={}){console.log(n.blue(`
Plugin Update Check
`));let s=R();if(s.length===0){console.log(n.gray("No Claude Code plugins found in ~/.claude/plugins/marketplaces/")),console.log(n.gray(`Install plugins with: /plugin marketplace add <owner>/<repo>
`));return}console.log(n.gray(`Checking ${s.length} plugin(s)...
`));let a=await Promise.all(s.map(e=>U(e))),c=a.filter(e=>e.hasRemoteUpdate||e.hasCacheStale);for(let e of a){let l=e.hasRemoteUpdate?n.yellow("update available"):e.hasCacheStale?n.cyan("restart needed"):n.green("up to date"),u=e.installedVersion!=="unknown"?`${e.installedVersion} \u2192 ${e.localVersion}`:e.localVersion;console.log(`  ${n.bold(e.plugin)} ${n.gray(`(${e.repo})`)}`),console.log(`    Version: ${u}  Status: ${l}`),e.hasRemoteUpdate&&(console.log(n.gray(`    Local:  ${e.localSha?.slice(0,8)||"?"}`)),console.log(n.gray(`    Remote: ${e.remoteSha?.slice(0,8)||"?"}`))),e.remoteSha===null&&console.log(n.gray("    (remote check skipped \u2014 network unavailable)")),console.log();}if(c.length===0){console.log(n.green(`All plugins are up to date.
`));return}let i=c.filter(e=>e.hasRemoteUpdate);if(t.update&&i.length>0){console.log(n.blue(`Pulling updates...
`));for(let e of i){process.stdout.write(`  ${e.plugin}: `);try{await m("git pull origin main",{cwd:e.marketplacePath,timeout:15e3}),console.log(n.green("updated"));}catch(l){console.log(n.red(`failed \u2014 ${l instanceof Error?l.message:"unknown error"}`));}}console.log(n.yellow(`
Restart your Claude Code session to apply updates.
`));}else if(i.length>0){console.log(n.yellow("To pull updates, run:"));for(let e of i)console.log(n.gray(`  git -C ${e.marketplacePath} pull origin main`));console.log(n.gray(`
Or run: paradigm plugin check --update`)),console.log(n.yellow(`
After updating, restart your Claude Code session.
`));}else console.log(n.yellow(`Restart your Claude Code session to apply cached updates.
`));}export{N as pluginCheckCommand};