kaven-cli 0.11.1 → 0.11.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ import{g as a}from"./chunk-XY5BXJEH.js";export{a as MarketplaceClient};
@@ -0,0 +1 @@
1
+ import v from"fs-extra";import q from"path";import x from"os";var c=class extends Error{constructor(e){super(e),this.name="MarketplaceError"}},T=class extends c{constructor(e){super(e),this.name="AuthenticationError"}},E=class extends c{constructor(t,s){super(s);this.requiredTier=t;this.name="LicenseRequiredError"}requiredTier},p=class extends c{constructor(e){super(e),this.name="NotFoundError"}},R=class extends c{constructor(t){super(`Rate limited. Try again in ${t}s`);this.retryAfter=t;this.name="RateLimitError"}retryAfter},m=class extends c{constructor(e){super(e),this.name="ServerError"}},y=class extends c{constructor(e){super(e),this.name="NetworkError"}},A=class extends c{constructor(e){super(e),this.name="SignatureVerificationError"}};function S(r){if(r instanceof Error)return r;if(typeof r=="string")return new Error(r);try{return new Error(JSON.stringify(r))}catch{return new Error(String(r))}}var O="https://marketplace.kaven.site",_=3e4,P=3,C=1e3;function b(r){process.env.KAVEN_DEBUG==="1"&&console.debug(`[kaven:debug] ${r}`)}async function D(){try{let r=q.join(x.homedir(),".kaven","config.json");if(await v.pathExists(r)){let e=await v.readJson(r);if(typeof e.apiUrl=="string"&&e.apiUrl)return e.apiUrl}}catch{}return null}async function I(){if(process.env.KAVEN_SERVICE_TOKEN)return process.env.KAVEN_SERVICE_TOKEN;try{let r=q.join(x.homedir(),".kaven","config.json");if(await v.pathExists(r)){let e=await v.readJson(r);if(typeof e.serviceToken=="string"&&e.serviceToken)return e.serviceToken}}catch{}return null}async function j(){if(process.env.KAVEN_API_URL)return process.env.KAVEN_API_URL.replace(/\/$/,"");let r=await D();return r?r.replace(/\/$/,""):O}function K(r){return new Promise(e=>setTimeout(e,r))}function G(r){return r>=500}var $=class{baseURLPromise;authService;constructor(e){this.authService=e??null,this.baseURLPromise=j()}async resolveUrl(e){return`${await this.baseURLPromise}${e}`}async request(e,t,s={}){let k=`${await this.baseURLPromise}${t}`,{body:d,authenticated:U=!1}=s,g=null;for(let u=0;u<=P;u++){if(u>0){let l=C*Math.pow(2,u-1);b(`Retry attempt ${u} for ${e} ${t} (delay ${l}ms)`),await K(l)}try{let l={"Content-Type":"application/json",Accept:"application/json"};if(U&&this.authService){let h=await this.authService.getValidToken();l.Authorization=`Bearer ${h}`}let n=await I();n&&(l["X-Service-Token"]=n),b(`${e} ${k}`);let w=new AbortController,M=setTimeout(()=>w.abort(),_),o;try{o=await fetch(k,{method:e,headers:l,body:d!==void 0?JSON.stringify(d):void 0,signal:w.signal})}finally{clearTimeout(M)}if(b(`Response: ${o.status} ${o.statusText}`),!o.ok){let h=await o.text().catch(()=>""),f=h;try{let a=JSON.parse(h);f=a.message||a.error||h}catch{}switch(o.status){case 401:throw new T(f||"Authentication required");case 403:{let a="pro";try{a=JSON.parse(h).requiredTier||a}catch{}throw new E(a,f||"License required")}case 404:throw new p(f||"Resource not found");case 429:{let a=parseInt(o.headers.get("retry-after")??"60",10);throw new R(isNaN(a)?60:a)}default:if(G(o.status)){g=new m(f||`Server error: ${o.status}`);continue}throw new m(f||`Server error: ${o.status}`)}}return(o.headers.get("content-type")??"").includes("application/json")?await o.json():{}}catch(l){let n=S(l);if(n instanceof T||n instanceof E||n instanceof p||n instanceof R)throw n;if(n instanceof TypeError||n.name==="AbortError"){let w=new y(n.name==="AbortError"?"Request timed out after 30s":`Network error: ${n.message}`);if(g=w,u<P)continue;throw w}if(n instanceof m){if(g=n,u<P)continue;throw n}throw n}}throw g??new y("Request failed after retries")}async requestDeviceCode(){return this.request("POST","/auth/device-code",{body:{client_id:"kaven-cli"},authenticated:!1})}async pollDeviceToken(e){try{let s=`${await this.baseURLPromise}/auth/token`,i=new AbortController,k=setTimeout(()=>i.abort(),_),d;try{d=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({device_code:e,grant_type:"urn:ietf:params:oauth:grant-type:device_code"}),signal:i.signal})}finally{clearTimeout(k)}if(d.ok)return{status:"success",tokens:await d.json()};let g=(await d.json().catch(()=>({}))).error??"unknown_error";switch(g){case"authorization_pending":return{status:"authorization_pending"};case"slow_down":return{status:"slow_down"};case"access_denied":return{status:"access_denied"};case"expired_token":return{status:"expired_token"};default:throw new Error(`Unexpected error: ${g}`)}}catch(t){let s=S(t),i=s;throw i.code==="ECONNREFUSED"||i.code==="ENOTFOUND"?new y("Network error. Check your connection and try again."):s}}async refreshToken(e){return this.request("POST","/auth/refresh",{body:{refresh_token:e},authenticated:!1})}async listModules(e){let t=new URLSearchParams;e?.category&&t.set("category",e.category),e?.tier&&t.set("tier",e.tier),e?.q&&t.set("q",e.q),e?.page&&t.set("page",String(e.page)),e?.pageSize&&t.set("pageSize",String(e.pageSize));let s=t.toString(),i=`/modules${s?`?${s}`:""}`;return this.request("GET",i)}async getModule(e){return this.request("GET",`/modules/${e}`)}async getManifest(e,t){return this.request("GET",`/modules/${e}/versions/${t}/manifest`)}async createDownloadToken(e,t){return this.request("POST","/download-tokens",{body:{moduleSlug:e,version:t},authenticated:!0})}async validateLicense(e,t){return await this.request("POST","/licenses/validate",{body:{licenseKey:e,requiredTier:t}})}async getLicenseStatus(e){return this.request("GET",`/licenses/status?key=${encodeURIComponent(e)}`)}async getModuleManifest(e){try{return await this.getManifest(e,"latest")}catch(t){if(t instanceof p)return null;throw t}}async getReleaseInfo(e,t){return this.request("GET",`/modules/${e}/versions/${t}`)}async getUploadUrl(e,t,s){return this.request("POST","/releases/upload-url",{body:{moduleSlug:e,version:t,size:s},authenticated:!0})}async createRelease(e){return this.request("POST","/releases",{body:e,authenticated:!0})}async createCheckoutSession(e,t){return this.request("POST","/checkout/session",{body:{tier:e,licenseKey:t},authenticated:!0})}async getCheckoutStatus(e){return this.request("GET",`/checkout/session/${e}/status`,{authenticated:!0})}async getCategories(){try{let e=await this.request("GET","/categories",{authenticated:!1});return Array.isArray(e)?e:e.categories??[]}catch(e){if(e instanceof p){let s=(await this.request("GET","/search?q=",{authenticated:!1})).facets?.categories?.map(i=>i.category).filter(Boolean)??[];return[...new Set(s)].sort()}throw e}}};export{T as a,E as b,p as c,y as d,A as e,S as f,$ as g};
package/dist/index.js CHANGED
@@ -1,110 +1,110 @@
1
1
  #!/usr/bin/env node
2
- import{a as wo,b as yo,c as vo,d as gt,e as Ze,f as fe,g as J}from"./chunk-JHLQ46NG.js";import{Command as Kr}from"commander";import S from"chalk";import{execSync as xo}from"child_process";import Eo from"path";import wt from"fs-extra";import C from"fs-extra";import G from"path";import Un from"os";var ht=class{constructor(e,t,n){this.projectRoot=e;this.markerService=t;this.manifestParser=n}async checkAll(){let e=[];return e.push(...await this.checkAnchors()),e.push(...await this.checkMarkers()),e.push(...await this.checkDependencies()),e.push(...await this.checkSchemaMerge()),e.push(...await this.checkEnvCompleteness()),e.push(...await this.checkLicense()),e.push(...await this.checkFrameworkVersion()),e.push(...await this.checkPrismaClientSync()),e}async checkAnchors(){let e=[],t=[{file:"apps/api/src/app.ts",anchor:"// [KAVEN_MODULE_IMPORTS]"},{file:"apps/api/src/app.ts",anchor:"// [KAVEN_MODULE_HOOKS]"},{file:"apps/api/src/app.ts",anchor:"// [KAVEN_MODULE_REGISTRATION]"}];for(let{file:n,anchor:a}of t){let r=G.join(this.projectRoot,n);if(!await C.pathExists(r)){e.push({type:"anchor",severity:"warning",message:`File not found: ${n}`,file:n,fixable:!1});continue}(await C.readFile(r,"utf-8")).includes(a)||e.push({type:"anchor",severity:"error",message:`Missing anchor: ${a}`,file:n,fixable:!1})}return e}async checkMarkers(){let e=[],n=(await this.readKavenConfig()).modules.filter(a=>a.installed);for(let a of n){let r=G.join(this.projectRoot,".kaven/modules",a.name,"module.json");if(!await C.pathExists(r)){e.push({type:"marker",severity:"error",message:`Manifest not found for installed module: ${a.name}`,fixable:!1});continue}try{let i=await this.manifestParser.parse(r);for(let s of i.injections){let c=G.join(this.projectRoot,s.file);if(!await C.pathExists(c)){e.push({type:"marker",severity:"error",message:`Injection target not found: ${s.file}`,file:s.file,fixable:!1});continue}let l=await C.readFile(c,"utf-8"),d=s.moduleName||a.name;this.markerService.detectMarkers(l,d).found||e.push({type:"marker",severity:"error",message:`Module ${a.name} not injected in ${s.file}`,file:s.file,fixable:!0})}}catch(i){e.push({type:"marker",severity:"error",message:`Invalid manifest for module ${a.name}: ${i instanceof Error?i.message:String(i)}`,fixable:!1})}}return e}async checkDependencies(){let e=[],n=(await this.readKavenConfig()).modules.filter(i=>i.installed),a=G.join(this.projectRoot,"package.json");if(!await C.pathExists(a))return e.push({type:"dependency",severity:"error",message:"package.json not found",fixable:!1}),e;let r=await C.readJSON(a);for(let i of n){let s=G.join(this.projectRoot,".kaven/modules",i.name,"module.json");if(await C.pathExists(s))try{let c=await this.manifestParser.parse(s);for(let l of c.dependencies.npm){let[d]=l.split("@"),u=r.dependencies?.[d],m=r.devDependencies?.[d];!u&&!m&&e.push({type:"dependency",severity:"warning",message:`Missing npm dependency: ${l}`,fixable:!0})}}catch{}}return e}async checkSchemaMerge(){let e=[],t=G.join(this.projectRoot,"packages/database/prisma/schema.base.prisma");if(!await C.pathExists(t))return e.push({type:"dependency",severity:"warning",message:"Prisma base schema not found: packages/database/prisma/schema.base.prisma",file:"packages/database/prisma/schema.base.prisma",fixable:!1}),e;let n=G.dirname(t);try{let a=await C.readdir(n);for(let r of a){if(!r.endsWith(".prisma"))continue;let i=G.join(n,r);(await C.readFile(i,"utf-8")).includes("<<<<<<<")&&e.push({type:"marker",severity:"error",message:`Merge conflict detected in schema file: ${r}`,file:G.join("packages/database/prisma",r),fixable:!1})}}catch{}return e.length===0&&e.push({type:"dependency",severity:"info",message:"Prisma schema integrity OK",fixable:!1}),e}async checkEnvCompleteness(){let e=[],t=G.join(this.projectRoot,".env.example"),n=G.join(this.projectRoot,".env");if(!await C.pathExists(t))return e.push({type:"dependency",severity:"info",message:".env.example not found \u2014 skipping env completeness check",fixable:!1}),e;if(!await C.pathExists(n))return e.push({type:"dependency",severity:"warning",message:".env file not found. Copy .env.example to .env and fill in values",file:".env",fixable:!1}),e;let a=await C.readFile(t,"utf-8"),r=await C.readFile(n,"utf-8"),i=d=>{let u=new Set;for(let m of d.split(`
3
- `)){let p=m.trim();if(p.startsWith("#")||!p.includes("="))continue;let f=p.split("=")[0].trim();f&&u.add(f)}return u},s=i(a),c=i(r),l=[];for(let d of s)c.has(d)||l.push(d);return l.length>0?e.push({type:"dependency",severity:"warning",message:`Missing env vars in .env: ${l.join(", ")}`,file:".env",fixable:!0}):e.push({type:"dependency",severity:"info",message:"Env vars completeness OK",fixable:!1}),e}async checkLicense(){let e=[],t=G.join(Un.homedir(),".kaven","license.json");if(!await C.pathExists(t))return e.push({type:"dependency",severity:"warning",message:"No license found at ~/.kaven/license.json. Run 'kaven license status' to set up.",fixable:!1}),e;try{let n=await C.readJson(t);if(n.expiresAt){let a=new Date(n.expiresAt).getTime();if(Date.now()>a)return e.push({type:"dependency",severity:"error",message:`License expired on ${n.expiresAt}. Run 'kaven upgrade' to renew.`,fixable:!1}),e}e.push({type:"dependency",severity:"info",message:`License valid (tier: ${n.tier||"unknown"})`,fixable:!1})}catch{e.push({type:"dependency",severity:"warning",message:"Could not read license file. Try 'kaven license status'.",fixable:!1})}return e}async checkFrameworkVersion(){let e=[],t=G.join(this.projectRoot,"package.json");if(!await C.pathExists(t))return e.push({type:"dependency",severity:"info",message:"package.json not found \u2014 skipping framework version check",fixable:!1}),e;try{let n=await C.readJSON(t),a=n.dependencies?.["@kaven/core"]||n.devDependencies?.["@kaven/core"];if(!a)return e.push({type:"dependency",severity:"info",message:"@kaven/core not found in dependencies \u2014 not a Kaven framework project",fixable:!1}),e;let r="1.0.0",s=a.replace(/[\^~>=<]/,"").split(" ")[0].split(".").map(Number),c=r.split(".").map(Number),l=!0;for(let d=0;d<3&&!((s[d]||0)>(c[d]||0));d++)if((s[d]||0)<(c[d]||0)){l=!1;break}l?e.push({type:"dependency",severity:"info",message:`Framework version OK (${a})`,fixable:!1}):e.push({type:"dependency",severity:"warning",message:`@kaven/core version ${a} may be outdated. Minimum: ^${r}`,fixable:!1})}catch{e.push({type:"dependency",severity:"info",message:"Could not determine framework version",fixable:!1})}return e}async checkPrismaClientSync(){let e=[],t=G.join(this.projectRoot,"node_modules/@prisma/client"),n=G.join(this.projectRoot,"prisma/schema.prisma");if(!await C.pathExists(t))return e.push({type:"dependency",severity:"warning",message:"@prisma/client not found. Run 'npx prisma generate' to generate the client.",fixable:!0}),e;if(!await C.pathExists(n))return e.push({type:"dependency",severity:"info",message:"prisma/schema.prisma not found \u2014 skipping Prisma sync check",fixable:!1}),e;try{let a=await C.stat(n),r=await C.stat(t);a.mtime>r.mtime?e.push({type:"dependency",severity:"warning",message:"Prisma schema was modified after client generation. Run 'npx prisma generate'.",fixable:!0}):e.push({type:"dependency",severity:"info",message:"Prisma client is up to date",fixable:!1})}catch{e.push({type:"dependency",severity:"info",message:"Could not compare Prisma schema and client timestamps",fixable:!1})}return e}async readKavenConfig(){let e=G.join(this.projectRoot,"kaven.json");if(!await C.pathExists(e))return{modules:[]};try{return await C.readJSON(e)}catch{return{modules:[]}}}};function Qe(o){return{moduleName:o,beginMarker:`// [KAVEN_MODULE:${o} BEGIN]`,endMarker:`// [KAVEN_MODULE:${o} END]`}}var le=class{hasModule(e,t){let n=Qe(t);return e.includes(n.beginMarker)&&e.includes(n.endMarker)}detectMarkers(e,t){let n=Qe(t),a=e.split(`
2
+ import{a as wo,b as yo,c as vo,d as gt,e as Ze,f as fe,g as J}from"./chunk-XY5BXJEH.js";import{Command as Br}from"commander";var ko={name:"kaven-cli",version:"0.11.3",type:"module",description:"Kaven CLI - The official command line tool for Kaven",main:"dist/index.js",bin:{kaven:"./dist/index.js"},files:["dist","README.md"],scripts:{build:"tsup src/index.ts --format esm --clean --minify --shims --dts",dev:"tsup src/index.ts --format esm --watch --silent",prepublishOnly:"pnpm run build",lint:"eslint 'src/**/*.{ts,tsx}' --max-warnings 0","lint:fix":"eslint 'src/**/*.{ts,tsx}' --fix",test:"node scripts/run-tests.mjs","test:watch":"node --import tsx --test --watch 'src/**/*.test.ts' 'tests/**/*.test.ts'","test:coverage":"node --import tsx --test --experimental-test-coverage 'src/**/*.test.ts' 'tests/**/*.test.ts'",typecheck:"tsc --noEmit",quality:"pnpm run lint && pnpm run typecheck && pnpm run test","quality:gate":"./.agent/scripts/quality-gate.sh",bootstrap:"./.agent/scripts/bootstrap.sh",evidence:"./.agent/scripts/evidence-bundle.sh","validate:evidence":"./.agent/scripts/validate-evidence.sh","create:pr":"./.agent/scripts/create-pr.sh","telemetry:view":"tail -f ~/.kaven/telemetry.log","telemetry:clear":"rm -f ~/.kaven/telemetry.log && touch ~/.kaven/telemetry.log"},keywords:["kaven","cli","developer-tools","saas","boilerplate"],author:"Kaven",license:"Apache-2.0",repository:{type:"git",url:"https://github.com/kaven-co/kaven-cli"},devDependencies:{"@semantic-release/changelog":"^6.0.3","@semantic-release/commit-analyzer":"^13.0.1","@semantic-release/git":"^10.0.1","@semantic-release/github":"^12.0.6","@semantic-release/npm":"^13.1.5","@semantic-release/release-notes-generator":"^14.1.0","@types/fs-extra":"^11.0.4","@types/js-yaml":"^4.0.9","@types/node":"^25.6.0","@types/tar":"^6.1.13","@typescript-eslint/eslint-plugin":"^8.53.1","@typescript-eslint/parser":"^8.53.1",eslint:"^8.0.0",msw:"^2.12.10","semantic-release":"^25.0.3",tsup:"^8.5.1",tsx:"^4.21.0",typescript:"^5.0.0"},engines:{node:">=20"},dependencies:{"@clack/prompts":"^1.2.0","@inquirer/prompts":"^7.5.2",chalk:"^5.6.2","cli-table3":"^0.6.5",commander:"^14.0.2","fs-extra":"^11.3.3",glob:"^13.0.0",i18next:"^26.0.6","js-yaml":"^4.2.0",open:"^10.1.0",ora:"^9.1.0",picocolors:"^1.1.1",tar:"^7.5.16",zod:"^4.3.6"},pnpm:{overrides:{"minimatch@<10.2.3":">=10.2.3","@isaacs/brace-expansion@<=5.0.0":">=5.0.1","brace-expansion@>=5.0.0 <5.0.6":">=5.0.6","esbuild@<=0.24.2":">=0.25.0","esbuild@>=0.27.0 <0.28.1":">=0.28.1","handlebars@<=4.7.8":">=4.7.9","tar@<=7.5.15":">=7.5.16","lodash@<=4.17.23":">=4.18.0","lodash-es@<=4.17.23":">=4.18.0","rollup@<4.59.0":">=4.59.0","vite@<=6.4.1":">=6.4.2","flatted@<=3.4.1":">=3.4.2","picomatch@<2.3.2":">=2.3.2","picomatch@>=4.0.0 <4.0.4":">=4.0.4",undici:">=7.28.0"}}};import S from"chalk";import{execSync as Eo}from"child_process";import So from"path";import wt from"fs-extra";import C from"fs-extra";import z from"path";import Kn from"os";var ht=class{constructor(e,t,n){this.projectRoot=e;this.markerService=t;this.manifestParser=n}projectRoot;markerService;manifestParser;async checkAll(){let e=[];return e.push(...await this.checkAnchors()),e.push(...await this.checkMarkers()),e.push(...await this.checkDependencies()),e.push(...await this.checkSchemaMerge()),e.push(...await this.checkEnvCompleteness()),e.push(...await this.checkLicense()),e.push(...await this.checkFrameworkVersion()),e.push(...await this.checkPrismaClientSync()),e}async checkAnchors(){let e=[],t=[{file:"apps/api/src/app.ts",anchor:"// [KAVEN_MODULE_IMPORTS]"},{file:"apps/api/src/app.ts",anchor:"// [KAVEN_MODULE_HOOKS]"},{file:"apps/api/src/app.ts",anchor:"// [KAVEN_MODULE_REGISTRATION]"}];for(let{file:n,anchor:a}of t){let r=z.join(this.projectRoot,n);if(!await C.pathExists(r)){e.push({type:"anchor",severity:"warning",message:`File not found: ${n}`,file:n,fixable:!1});continue}(await C.readFile(r,"utf-8")).includes(a)||e.push({type:"anchor",severity:"error",message:`Missing anchor: ${a}`,file:n,fixable:!1})}return e}async checkMarkers(){let e=[],n=(await this.readKavenConfig()).modules.filter(a=>a.installed);for(let a of n){let r=z.join(this.projectRoot,".kaven/modules",a.name,"module.json");if(!await C.pathExists(r)){e.push({type:"marker",severity:"error",message:`Manifest not found for installed module: ${a.name}`,fixable:!1});continue}try{let i=await this.manifestParser.parse(r);for(let s of i.injections){let c=z.join(this.projectRoot,s.file);if(!await C.pathExists(c)){e.push({type:"marker",severity:"error",message:`Injection target not found: ${s.file}`,file:s.file,fixable:!1});continue}let l=await C.readFile(c,"utf-8"),d=s.moduleName||a.name;this.markerService.detectMarkers(l,d).found||e.push({type:"marker",severity:"error",message:`Module ${a.name} not injected in ${s.file}`,file:s.file,fixable:!0})}}catch(i){e.push({type:"marker",severity:"error",message:`Invalid manifest for module ${a.name}: ${i instanceof Error?i.message:String(i)}`,fixable:!1})}}return e}async checkDependencies(){let e=[],n=(await this.readKavenConfig()).modules.filter(i=>i.installed),a=z.join(this.projectRoot,"package.json");if(!await C.pathExists(a))return e.push({type:"dependency",severity:"error",message:"package.json not found",fixable:!1}),e;let r=await C.readJSON(a);for(let i of n){let s=z.join(this.projectRoot,".kaven/modules",i.name,"module.json");if(await C.pathExists(s))try{let c=await this.manifestParser.parse(s);for(let l of c.dependencies.npm){let[d]=l.split("@"),u=r.dependencies?.[d],m=r.devDependencies?.[d];!u&&!m&&e.push({type:"dependency",severity:"warning",message:`Missing npm dependency: ${l}`,fixable:!0})}}catch{}}return e}async checkSchemaMerge(){let e=[],t=z.join(this.projectRoot,"packages/database/prisma/schema.base.prisma");if(!await C.pathExists(t))return e.push({type:"dependency",severity:"warning",message:"Prisma base schema not found: packages/database/prisma/schema.base.prisma",file:"packages/database/prisma/schema.base.prisma",fixable:!1}),e;let n=z.dirname(t);try{let a=await C.readdir(n);for(let r of a){if(!r.endsWith(".prisma"))continue;let i=z.join(n,r);(await C.readFile(i,"utf-8")).includes("<<<<<<<")&&e.push({type:"marker",severity:"error",message:`Merge conflict detected in schema file: ${r}`,file:z.join("packages/database/prisma",r),fixable:!1})}}catch{}return e.length===0&&e.push({type:"dependency",severity:"info",message:"Prisma schema integrity OK",fixable:!1}),e}async checkEnvCompleteness(){let e=[],t=z.join(this.projectRoot,".env.example"),n=z.join(this.projectRoot,".env");if(!await C.pathExists(t))return e.push({type:"dependency",severity:"info",message:".env.example not found \u2014 skipping env completeness check",fixable:!1}),e;if(!await C.pathExists(n))return e.push({type:"dependency",severity:"warning",message:".env file not found. Copy .env.example to .env and fill in values",file:".env",fixable:!1}),e;let a=await C.readFile(t,"utf-8"),r=await C.readFile(n,"utf-8"),i=d=>{let u=new Set;for(let m of d.split(`
3
+ `)){let p=m.trim();if(p.startsWith("#")||!p.includes("="))continue;let f=p.split("=")[0].trim();f&&u.add(f)}return u},s=i(a),c=i(r),l=[];for(let d of s)c.has(d)||l.push(d);return l.length>0?e.push({type:"dependency",severity:"warning",message:`Missing env vars in .env: ${l.join(", ")}`,file:".env",fixable:!0}):e.push({type:"dependency",severity:"info",message:"Env vars completeness OK",fixable:!1}),e}async checkLicense(){let e=[],t=z.join(Kn.homedir(),".kaven","license.json");if(!await C.pathExists(t))return e.push({type:"dependency",severity:"warning",message:"No license found at ~/.kaven/license.json. Run 'kaven license status' to set up.",fixable:!1}),e;try{let n=await C.readJson(t);if(n.expiresAt){let a=new Date(n.expiresAt).getTime();if(Date.now()>a)return e.push({type:"dependency",severity:"error",message:`License expired on ${n.expiresAt}. Run 'kaven upgrade' to renew.`,fixable:!1}),e}e.push({type:"dependency",severity:"info",message:`License valid (tier: ${n.tier||"unknown"})`,fixable:!1})}catch{e.push({type:"dependency",severity:"warning",message:"Could not read license file. Try 'kaven license status'.",fixable:!1})}return e}async checkFrameworkVersion(){let e=[],t=z.join(this.projectRoot,"package.json");if(!await C.pathExists(t))return e.push({type:"dependency",severity:"info",message:"package.json not found \u2014 skipping framework version check",fixable:!1}),e;try{let n=await C.readJSON(t),a=n.dependencies?.["@kaven/core"]||n.devDependencies?.["@kaven/core"];if(!a)return e.push({type:"dependency",severity:"info",message:"@kaven/core not found in dependencies \u2014 not a Kaven framework project",fixable:!1}),e;let r="1.0.0",s=a.replace(/[\^~>=<]/,"").split(" ")[0].split(".").map(Number),c=r.split(".").map(Number),l=!0;for(let d=0;d<3&&!((s[d]||0)>(c[d]||0));d++)if((s[d]||0)<(c[d]||0)){l=!1;break}l?e.push({type:"dependency",severity:"info",message:`Framework version OK (${a})`,fixable:!1}):e.push({type:"dependency",severity:"warning",message:`@kaven/core version ${a} may be outdated. Minimum: ^${r}`,fixable:!1})}catch{e.push({type:"dependency",severity:"info",message:"Could not determine framework version",fixable:!1})}return e}async checkPrismaClientSync(){let e=[],t=z.join(this.projectRoot,"node_modules/@prisma/client"),n=z.join(this.projectRoot,"prisma/schema.prisma");if(!await C.pathExists(t))return e.push({type:"dependency",severity:"warning",message:"@prisma/client not found. Run 'npx prisma generate' to generate the client.",fixable:!0}),e;if(!await C.pathExists(n))return e.push({type:"dependency",severity:"info",message:"prisma/schema.prisma not found \u2014 skipping Prisma sync check",fixable:!1}),e;try{let a=await C.stat(n),r=await C.stat(t);a.mtime>r.mtime?e.push({type:"dependency",severity:"warning",message:"Prisma schema was modified after client generation. Run 'npx prisma generate'.",fixable:!0}):e.push({type:"dependency",severity:"info",message:"Prisma client is up to date",fixable:!1})}catch{e.push({type:"dependency",severity:"info",message:"Could not compare Prisma schema and client timestamps",fixable:!1})}return e}async readKavenConfig(){let e=z.join(this.projectRoot,"kaven.json");if(!await C.pathExists(e))return{modules:[]};try{return await C.readJSON(e)}catch{return{modules:[]}}}};function Qe(o){return{moduleName:o,beginMarker:`// [KAVEN_MODULE:${o} BEGIN]`,endMarker:`// [KAVEN_MODULE:${o} END]`}}var le=class{hasModule(e,t){let n=Qe(t);return e.includes(n.beginMarker)&&e.includes(n.endMarker)}detectMarkers(e,t){let n=Qe(t),a=e.split(`
4
4
  `),r,i;for(let s=0;s<a.length;s++)if(a[s].includes(n.beginMarker)&&(r=s),a[s].includes(n.endMarker)){i=s;break}if(r!==void 0&&i!==void 0){let s=a.slice(r+1,i).join(`
5
5
  `);return{found:!0,beginLine:r,endLine:i,content:s}}return{found:!1}}injectModule(e,t,n,a){if(this.hasModule(e,n))throw new Error(`Module ${n} already injected`);if(!e.includes(t))throw new Error(`Anchor not found: ${t}`);let r=Qe(n),i=`
6
6
  ${r.beginMarker}
7
7
  ${a}
8
8
  ${r.endMarker}
9
- `;return e.replace(t,`${t}${i}`)}removeModule(e,t){let n=Qe(t),a=this.escapeRegex(n.beginMarker),r=this.escapeRegex(n.endMarker),i=new RegExp(`\\n?${a}[\\s\\S]*?${r}\\n?`,"g"),s=e.replace(i,"");if(s===e)throw new Error(`Module ${t} not found in file`);return s}escapeRegex(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}};import bo from"fs-extra";import{z as k}from"zod";var Nn=k.object({npm:k.array(k.string()).default([]),peerModules:k.array(k.string()).default([]),kavenVersion:k.string().default(">=0.1.0")}),Lt=k.object({source:k.string(),dest:k.string()}),Kn=k.object({backend:k.array(Lt).default([]),frontend:k.array(Lt).default([]),database:k.array(Lt).default([])}),Vn=k.object({file:k.string(),anchor:k.string(),code:k.string(),moduleName:k.string().optional()}),Bn=k.object({postInstall:k.string().nullable().default(null),preRemove:k.string().nullable().default(null)}),qn=k.object({key:k.string(),required:k.boolean().default(!1),example:k.string().optional()}),ko=k.object({name:k.string().min(1),version:k.string().regex(/^\d+\.\d+\.\d+$/),description:k.string().optional(),author:k.string().default("Kaven"),license:k.string().default("Proprietary"),dependencies:Nn,files:Kn,injections:k.array(Vn),scripts:Bn,env:k.array(qn).default([])});import{ZodError as Jn}from"zod";var Oe=class{async parse(e){if(!await bo.pathExists(e))throw new Error(`Manifest not found: ${e}`);let t=await bo.readFile(e,"utf-8"),n;try{n=JSON.parse(t)}catch{throw new Error(`Failed to parse manifest JSON: ${e}`)}try{return ko.parse(n)}catch(a){throw a instanceof Jn?new Error(`Invalid manifest:
9
+ `;return e.replace(t,`${t}${i}`)}removeModule(e,t){let n=Qe(t),a=this.escapeRegex(n.beginMarker),r=this.escapeRegex(n.endMarker),i=new RegExp(`\\n?${a}[\\s\\S]*?${r}\\n?`,"g"),s=e.replace(i,"");if(s===e)throw new Error(`Module ${t} not found in file`);return s}escapeRegex(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}};import xo from"fs-extra";import{z as k}from"zod";var Vn=k.object({npm:k.array(k.string()).default([]),peerModules:k.array(k.string()).default([]),kavenVersion:k.string().default(">=0.1.0")}),Lt=k.object({source:k.string(),dest:k.string()}),Bn=k.object({backend:k.array(Lt).default([]),frontend:k.array(Lt).default([]),database:k.array(Lt).default([])}),qn=k.object({file:k.string(),anchor:k.string(),code:k.string(),moduleName:k.string().optional()}),Jn=k.object({postInstall:k.string().nullable().default(null),preRemove:k.string().nullable().default(null)}),zn=k.object({key:k.string(),required:k.boolean().default(!1),example:k.string().optional()}),bo=k.object({name:k.string().min(1),version:k.string().regex(/^\d+\.\d+\.\d+$/),description:k.string().optional(),author:k.string().default("Kaven"),license:k.string().default("Proprietary"),dependencies:Vn,files:Bn,injections:k.array(qn),scripts:Jn,env:k.array(zn).default([])});import{ZodError as Gn}from"zod";var Oe=class{async parse(e){if(!await xo.pathExists(e))throw new Error(`Manifest not found: ${e}`);let t=await xo.readFile(e,"utf-8"),n;try{n=JSON.parse(t)}catch{throw new Error(`Failed to parse manifest JSON: ${e}`)}try{return bo.parse(n)}catch(a){throw a instanceof Gn?new Error(`Invalid manifest:
10
10
  ${this.formatZodError(a)}`):a}}async validate(e){try{return await this.parse(e),{valid:!0,errors:[]}}catch(t){return{valid:!1,errors:[t instanceof Error?t.message:String(t)]}}}formatZodError(e){return e.issues.map(t=>` - ${t.path.map(String).join(".")}: ${t.message}`).join(`
11
- `)}};function Gn(o){switch(o){case"error":return S.red("[ERROR]");case"warning":return S.yellow("[WARN] ");case"info":return S.cyan("[INFO] ");default:return S.green("[OK] ")}}async function zn(o,e){let t=o.filter(n=>n.fixable);if(t.length===0){console.log(S.gray(" No automatically fixable issues found."));return}for(let n of t){let a=n.message.toLowerCase();if(a.includes("missing npm dependency")||a.includes("pnpm install")){console.log(S.blue(` Fixing: ${n.message}`));try{xo("pnpm install",{cwd:e,stdio:"inherit"}),console.log(S.green(" \u2713 pnpm install completed"))}catch{console.log(S.red(" \u2717 pnpm install failed"))}continue}if(a.includes("prisma")){console.log(S.blue(` Fixing: ${n.message}`));try{xo("npx prisma generate",{cwd:e,stdio:"inherit"}),console.log(S.green(" \u2713 npx prisma generate completed"))}catch{console.log(S.red(" \u2717 npx prisma generate failed"))}continue}if(a.includes("missing env vars")&&n.file===".env"){let r=Eo.join(e,".env"),i=Eo.join(e,".env.example");console.log(S.blue(` Fixing: ${n.message}`));try{let s=await wt.readFile(i,"utf-8"),c=await wt.pathExists(r)?await wt.readFile(r,"utf-8"):"",l=p=>{let f=new Set;for(let y of p.split(`
11
+ `)}};function Hn(o){switch(o){case"error":return S.red("[ERROR]");case"warning":return S.yellow("[WARN] ");case"info":return S.cyan("[INFO] ");default:return S.green("[OK] ")}}async function Wn(o,e){let t=o.filter(n=>n.fixable);if(t.length===0){console.log(S.gray(" No automatically fixable issues found."));return}for(let n of t){let a=n.message.toLowerCase();if(a.includes("missing npm dependency")||a.includes("pnpm install")){console.log(S.blue(` Fixing: ${n.message}`));try{Eo("pnpm install",{cwd:e,stdio:"inherit"}),console.log(S.green(" \u2713 pnpm install completed"))}catch{console.log(S.red(" \u2717 pnpm install failed"))}continue}if(a.includes("prisma")){console.log(S.blue(` Fixing: ${n.message}`));try{Eo("npx prisma generate",{cwd:e,stdio:"inherit"}),console.log(S.green(" \u2713 npx prisma generate completed"))}catch{console.log(S.red(" \u2717 npx prisma generate failed"))}continue}if(a.includes("missing env vars")&&n.file===".env"){let r=So.join(e,".env"),i=So.join(e,".env.example");console.log(S.blue(` Fixing: ${n.message}`));try{let s=await wt.readFile(i,"utf-8"),c=await wt.pathExists(r)?await wt.readFile(r,"utf-8"):"",l=p=>{let f=new Set;for(let y of p.split(`
12
12
  `)){let P=y.trim();if(P.startsWith("#")||!P.includes("="))continue;let R=P.split("=")[0].trim();R&&f.add(R)}return f},d=l(s),u=l(c),m=`
13
13
  # Added by kaven doctor --fix
14
14
  `;for(let p of d)u.has(p)||(m+=`${p}=PLACEHOLDER
15
- `);await wt.appendFile(r,m),console.log(S.green(" \u2713 Placeholder env vars appended to .env"))}catch{console.log(S.red(" \u2717 Could not append env vars"))}continue}console.log(S.yellow(` Manual action required: ${n.message}`))}}async function So(o){o.json||console.log(S.blue(`Running module doctor...
16
- `));let e=new le,t=new Oe,n=new ht(process.cwd(),e,t),a=[];try{a=await n.checkAll()}catch(s){o.json?console.log(JSON.stringify({success:!1,error:s instanceof Error?s.message:String(s),results:[]})):console.error(S.red(`[ERROR] Heavy failure during doctor audit: ${s instanceof Error?s.message:String(s)}`)),process.exit(1);return}if(o.json){let s=a.filter(l=>l.severity==="error"),c=a.filter(l=>l.severity==="warning");console.log(JSON.stringify({success:s.length===0,errors:s.length,warnings:c.length,results:a},null,2)),process.exit(s.length>0?1:c.length>0?2:0);return}for(let s of a){let c=Gn(s.severity);console.log(`${c} ${s.message}`),s.file&&console.log(S.gray(` file: ${s.file}`)),s.fixable&&!o.fix&&console.log(S.gray(" (fixable: run with --fix)"))}let r=a.filter(s=>s.severity==="error"),i=a.filter(s=>s.severity==="warning");console.log(),r.length===0&&i.length===0?console.log(S.green("[OK] All checks passed! Your project is healthy.")):(r.length>0&&console.log(S.red(`[ERROR] Found ${r.length} error(s)`)),i.length>0&&console.log(S.yellow(`[WARN] Found ${i.length} warning(s)`)),o.fix||(console.log(S.gray(`
15
+ `);await wt.appendFile(r,m),console.log(S.green(" \u2713 Placeholder env vars appended to .env"))}catch{console.log(S.red(" \u2717 Could not append env vars"))}continue}console.log(S.yellow(` Manual action required: ${n.message}`))}}async function Co(o){o.json||console.log(S.blue(`Running module doctor...
16
+ `));let e=new le,t=new Oe,n=new ht(process.cwd(),e,t),a=[];try{a=await n.checkAll()}catch(s){o.json?console.log(JSON.stringify({success:!1,error:s instanceof Error?s.message:String(s),results:[]})):console.error(S.red(`[ERROR] Heavy failure during doctor audit: ${s instanceof Error?s.message:String(s)}`)),process.exit(1);return}if(o.json){let s=a.filter(l=>l.severity==="error"),c=a.filter(l=>l.severity==="warning");console.log(JSON.stringify({success:s.length===0,errors:s.length,warnings:c.length,results:a},null,2)),process.exit(s.length>0?1:c.length>0?2:0);return}for(let s of a){let c=Hn(s.severity);console.log(`${c} ${s.message}`),s.file&&console.log(S.gray(` file: ${s.file}`)),s.fixable&&!o.fix&&console.log(S.gray(" (fixable: run with --fix)"))}let r=a.filter(s=>s.severity==="error"),i=a.filter(s=>s.severity==="warning");console.log(),r.length===0&&i.length===0?console.log(S.green("[OK] All checks passed! Your project is healthy.")):(r.length>0&&console.log(S.red(`[ERROR] Found ${r.length} error(s)`)),i.length>0&&console.log(S.yellow(`[WARN] Found ${i.length} warning(s)`)),o.fix||(console.log(S.gray(`
17
17
  Tip: Run with --fix to attempt automatic repairs`)),console.log(S.gray("Try: kaven module doctor --fix")))),o.fix&&(console.log(),console.log(S.blue(`Applying auto-fixes...
18
- `)),await zn(a,process.cwd()),console.log(S.green(`
19
- Auto-fix completed.`))),r.length>0&&process.exit(1),i.length>0&&process.exit(2)}import Po from"chalk";import Zn from"ora";import ot from"path";import Fe from"fs-extra";import re from"fs-extra";import ie from"path";import{glob as Hn}from"glob";var et=class{constructor(e,t=".agent/backups"){this.projectRoot=e;this.backupDir=ie.join(e,t),this.backupId=`backup_${Date.now()}`}backupDir;backupId;filesToBackup=[];async backup(e){let t=ie.join(this.backupDir,this.backupId);await re.ensureDir(t);for(let n of e){let a=ie.resolve(this.projectRoot,n);if(!await re.pathExists(a))throw new Error(`File not found for backup: ${n}`);let r=ie.relative(this.projectRoot,a),i=ie.join(t,r);await re.ensureDir(ie.dirname(i)),await re.copy(a,i),this.filesToBackup.push(a)}console.log(`\u{1F4E6} Backup created: ${this.backupId}`)}async rollback(){let e=ie.join(this.backupDir,this.backupId);if(!await re.pathExists(e))throw new Error(`Backup not found: ${this.backupId}`);let t=await Hn(`${e}/**/*`,{nodir:!0});for(let n of t){let a=ie.relative(e,n),r=ie.join(this.projectRoot,a);await re.ensureDir(ie.dirname(r)),await re.copy(n,r,{overwrite:!0})}console.log(`\u267B\uFE0F Rollback complete: ${this.backupId}`)}async commit(){let e=ie.join(this.backupDir,this.backupId);await re.pathExists(e)&&await re.remove(e),console.log("\u2705 Transaction committed")}getBackupId(){return this.backupId}async cleanup(){if(!await re.pathExists(this.backupDir))return;let e=await re.readdir(this.backupDir),t=Date.now(),n=10080*60*1e3;for(let a of e){let r=a.match(/backup_(\d+)/);if(r){let i=parseInt(r[1]);t-i>n&&await re.remove(ie.join(this.backupDir,a))}}}};import Wn from"child_process";import Xn from"readline";import yt from"chalk";var tt=class{timeoutMs;constructor(e=6e4){this.timeoutMs=e}async runScript(e,t,n=!1){if(!n&&!await this.confirm(`Run ${t} script: ${e.command} ${(e.args??[]).join(" ")}?`)){console.log(yt.dim(` Skipping ${t} script.`));return}return new Promise((a,r)=>{let i=Wn.spawn(e.command,e.args??[],{cwd:e.cwd,stdio:["ignore","pipe","pipe"],shell:!0}),s=yt.dim(`[${t}] `);i.stdout?.on("data",l=>{process.stdout.write(s+l.toString())}),i.stderr?.on("data",l=>{process.stderr.write(s+yt.yellow(l.toString()))});let c=setTimeout(()=>{console.warn(yt.yellow(`
20
- \u26A0 ${t} script timed out after ${this.timeoutMs/1e3}s, sending SIGTERM...`)),i.kill("SIGTERM"),setTimeout(()=>{i.kill("SIGKILL")},5e3)},this.timeoutMs);i.on("close",l=>{clearTimeout(c),l===0||l===null?a():r(new Error(`${t} script exited with code ${l}`))}),i.on("error",l=>{clearTimeout(c),r(l)})})}async runScripts(e,t,n=!1){for(let a of e)await this.runScript(a,t,n)}confirm(e){return new Promise(t=>{let n=Xn.createInterface({input:process.stdin,output:process.stdout});n.question(`
21
- ${e} [y/N] `,a=>{n.close(),t(a.toLowerCase()==="y"||a.toLowerCase()==="yes")})})}};import Le from"fs-extra";import Ft from"path";import Ut from"chalk";var be=class{constructor(e,t){this.projectRoot=e;this.markerService=t}async isModuleInstalled(e){try{let t=await this.findProjectFiles();for(let n of t)try{let a=await Le.readFile(n,"utf-8");if(this.markerService.hasModule(a,e))return!0}catch{}return!1}catch{return!1}}async findProjectFiles(){let{glob:e}=await import("glob"),t=["**/*.ts","**/*.tsx","**/*.js","**/*.jsx"],n=["**/node_modules/**","**/.next/**","**/dist/**","**/build/**"],a=[];for(let r of t){let i=await e(r,{cwd:this.projectRoot,absolute:!0,ignore:n});a.push(...i)}return[...new Set(a)]}async install(e,t){let n=new et(this.projectRoot);try{let a=Array.from(new Set(e.injections.map(i=>i.file)));await n.backup(a);for(let i of e.injections)await this.injectCode(i);await n.commit();let r=await this.readManifest(this.projectRoot);if(r?.scripts?.postInstall?.length){let i=new tt;try{await i.runScripts(r.scripts.postInstall.map(s=>({...s,cwd:this.projectRoot})),"postInstall",!1)}catch(s){let c=s instanceof Error?s.message:String(s);console.warn(Ut.yellow(`
18
+ `)),await Wn(a,process.cwd()),console.log(S.green(`
19
+ Auto-fix completed.`))),r.length>0&&process.exit(1),i.length>0&&process.exit(2)}import $o from"chalk";import ea from"ora";import ot from"path";import Fe from"fs-extra";import re from"fs-extra";import ie from"path";import{glob as Xn}from"glob";var et=class{constructor(e,t=".agent/backups"){this.projectRoot=e;this.backupDir=ie.join(e,t),this.backupId=`backup_${Date.now()}`}projectRoot;backupDir;backupId;filesToBackup=[];async backup(e){let t=ie.join(this.backupDir,this.backupId);await re.ensureDir(t);for(let n of e){let a=ie.resolve(this.projectRoot,n);if(!await re.pathExists(a))throw new Error(`File not found for backup: ${n}`);let r=ie.relative(this.projectRoot,a),i=ie.join(t,r);await re.ensureDir(ie.dirname(i)),await re.copy(a,i),this.filesToBackup.push(a)}console.log(`\u{1F4E6} Backup created: ${this.backupId}`)}async rollback(){let e=ie.join(this.backupDir,this.backupId);if(!await re.pathExists(e))throw new Error(`Backup not found: ${this.backupId}`);let t=await Xn(`${e}/**/*`,{nodir:!0});for(let n of t){let a=ie.relative(e,n),r=ie.join(this.projectRoot,a);await re.ensureDir(ie.dirname(r)),await re.copy(n,r,{overwrite:!0})}console.log(`\u267B\uFE0F Rollback complete: ${this.backupId}`)}async commit(){let e=ie.join(this.backupDir,this.backupId);await re.pathExists(e)&&await re.remove(e),console.log("\u2705 Transaction committed")}getBackupId(){return this.backupId}async cleanup(){if(!await re.pathExists(this.backupDir))return;let e=await re.readdir(this.backupDir),t=Date.now(),n=10080*60*1e3;for(let a of e){let r=a.match(/backup_(\d+)/);if(r){let i=parseInt(r[1]);t-i>n&&await re.remove(ie.join(this.backupDir,a))}}}};import Yn from"child_process";import Zn from"readline";import yt from"chalk";var tt=class{timeoutMs;constructor(e=6e4){this.timeoutMs=e}async runScript(e,t,n=!1){if(!n&&!await this.confirm(`Run ${t} script: ${e.command} ${(e.args??[]).join(" ")}?`)){console.log(yt.dim(` Skipping ${t} script.`));return}return new Promise((a,r)=>{let i=Yn.spawn(e.command,e.args??[],{cwd:e.cwd,stdio:["ignore","pipe","pipe"],shell:!0}),s=yt.dim(`[${t}] `);i.stdout?.on("data",l=>{process.stdout.write(s+l.toString())}),i.stderr?.on("data",l=>{process.stderr.write(s+yt.yellow(l.toString()))});let c=setTimeout(()=>{console.warn(yt.yellow(`
20
+ \u26A0 ${t} script timed out after ${this.timeoutMs/1e3}s, sending SIGTERM...`)),i.kill("SIGTERM"),setTimeout(()=>{i.kill("SIGKILL")},5e3)},this.timeoutMs);i.on("close",l=>{clearTimeout(c),l===0||l===null?a():r(new Error(`${t} script exited with code ${l}`))}),i.on("error",l=>{clearTimeout(c),r(l)})})}async runScripts(e,t,n=!1){for(let a of e)await this.runScript(a,t,n)}confirm(e){return new Promise(t=>{let n=Zn.createInterface({input:process.stdin,output:process.stdout});n.question(`
21
+ ${e} [y/N] `,a=>{n.close(),t(a.toLowerCase()==="y"||a.toLowerCase()==="yes")})})}};import Le from"fs-extra";import Ft from"path";import Ut from"chalk";var be=class{constructor(e,t){this.projectRoot=e;this.markerService=t}projectRoot;markerService;async isModuleInstalled(e){try{let t=await this.findProjectFiles();for(let n of t)try{let a=await Le.readFile(n,"utf-8");if(this.markerService.hasModule(a,e))return!0}catch{}return!1}catch{return!1}}async findProjectFiles(){let{glob:e}=await import("glob"),t=["**/*.ts","**/*.tsx","**/*.js","**/*.jsx"],n=["**/node_modules/**","**/.next/**","**/dist/**","**/build/**"],a=[];for(let r of t){let i=await e(r,{cwd:this.projectRoot,absolute:!0,ignore:n});a.push(...i)}return[...new Set(a)]}async install(e,t){let n=new et(this.projectRoot);try{let a=Array.from(new Set(e.injections.map(i=>i.file)));await n.backup(a);for(let i of e.injections)await this.injectCode(i);await n.commit();let r=await this.readManifest(this.projectRoot);if(r?.scripts?.postInstall?.length){let i=new tt;try{await i.runScripts(r.scripts.postInstall.map(s=>({...s,cwd:this.projectRoot})),"postInstall",!1)}catch(s){let c=s instanceof Error?s.message:String(s);console.warn(Ut.yellow(`
22
22
  \u26A0 PostInstall script failed: ${c}`)),console.warn(Ut.dim(" The module is installed. Run the script manually if needed."))}}if(!t?.skipEnv&&r?.env?.length){let{EnvManager:i}=await import("./EnvManager-NMS3NMIE.js"),s=new i,c=r.env.map(l=>({name:l.key,description:l.example??l.key,required:l.required??!1}));await s.injectEnvVars(e.name,c,{projectDir:this.projectRoot,envFile:t?.envFile,skipEnv:t?.skipEnv,skipConfirmation:t?.yes})}}catch(a){let r=a instanceof Error?a.message:String(a);throw console.error(`\u274C Installation failed: ${r}`),console.log("\u{1F504} Rolling back..."),await n.rollback(),a}}async uninstall(e,t){let n=new et(this.projectRoot);try{let a=Array.from(new Set(e.injections.map(c=>c.file)));await n.backup(a);let{EnvManager:r}=await import("./EnvManager-NMS3NMIE.js");new r().removeEnvVars(e.name,{projectDir:this.projectRoot,skipEnv:t?.skipEnv});let s=await this.readManifest(this.projectRoot);if(s?.scripts?.preRemove?.length){let c=new tt;try{await c.runScripts(s.scripts.preRemove.map(l=>({...l,cwd:this.projectRoot})),"preRemove",!1)}catch(l){let d=l instanceof Error?l.message:String(l);console.warn(Ut.yellow(`
23
- \u26A0 PreRemove script failed: ${d}`))}}for(let c of a)await this.removeCode(c,e.name);await n.commit()}catch(a){let r=a instanceof Error?a.message:String(a);throw console.error(`\u274C Removal failed: ${r}`),console.log("\u{1F504} Rolling back..."),await n.rollback(),a}}async injectCode(e){let t=Ft.join(this.projectRoot,e.file),n=await Le.readFile(t,"utf-8"),a=this.markerService.injectModule(n,e.anchor,e.moduleName||"unnamed",e.code);await Le.writeFile(t,a)}async removeCode(e,t){let n=Ft.join(this.projectRoot,e),a=await Le.readFile(n,"utf-8"),r=this.markerService.removeModule(a,t);await Le.writeFile(n,r)}async readManifest(e){try{let t=await Le.readFile(Ft.join(e,"module.json"),"utf-8");return JSON.parse(t)}catch{return null}}};import vt from"fs-extra";import Co from"path";import Yn from"os";var j=class o{static instance;logPath;buffer=[];constructor(){this.logPath=Co.join(Yn.homedir(),".kaven","telemetry.log")}static getInstance(){return o.instance||(o.instance=new o),o.instance}capture(e,t,n){let a={event:e,timestamp:new Date().toISOString(),metadata:t,duration:n};this.buffer.push(a)}async flush(){if(this.buffer.length!==0)try{let e=Co.dirname(this.logPath);await vt.ensureDir(e);let t=this.buffer.map(n=>JSON.stringify(n)).join(`
23
+ \u26A0 PreRemove script failed: ${d}`))}}for(let c of a)await this.removeCode(c,e.name);await n.commit()}catch(a){let r=a instanceof Error?a.message:String(a);throw console.error(`\u274C Removal failed: ${r}`),console.log("\u{1F504} Rolling back..."),await n.rollback(),a}}async injectCode(e){let t=Ft.join(this.projectRoot,e.file),n=await Le.readFile(t,"utf-8"),a=this.markerService.injectModule(n,e.anchor,e.moduleName||"unnamed",e.code);await Le.writeFile(t,a)}async removeCode(e,t){let n=Ft.join(this.projectRoot,e),a=await Le.readFile(n,"utf-8"),r=this.markerService.removeModule(a,t);await Le.writeFile(n,r)}async readManifest(e){try{let t=await Le.readFile(Ft.join(e,"module.json"),"utf-8");return JSON.parse(t)}catch{return null}}};import vt from"fs-extra";import Po from"path";import Qn from"os";var j=class o{static instance;logPath;buffer=[];constructor(){this.logPath=Po.join(Qn.homedir(),".kaven","telemetry.log")}static getInstance(){return o.instance||(o.instance=new o),o.instance}capture(e,t,n){let a={event:e,timestamp:new Date().toISOString(),metadata:t,duration:n};this.buffer.push(a)}async flush(){if(this.buffer.length!==0)try{let e=Po.dirname(this.logPath);await vt.ensureDir(e);let t=this.buffer.map(n=>JSON.stringify(n)).join(`
24
24
  `)+`
25
25
  `;await vt.appendFile(this.logPath,t,"utf8"),this.buffer=[]}catch(e){console.debug("Erro ao gravar telemetria:",e)}}async getRecentEvents(e=20){if(!await vt.pathExists(this.logPath))return[];try{return(await vt.readFile(this.logPath,"utf8")).trim().split(`
26
- `).reverse().slice(0,e).map(n=>JSON.parse(n))}catch{return[]}}};async function $o(o,e){let t=j.getInstance(),n=Date.now();t.capture("cli.module.add.start",{manifestPath:o});let a=e||process.cwd(),r=Zn("Preparando instala\xE7\xE3o do m\xF3dulo...").start();try{let i=new le,s=new Oe,c=new be(a,i),l=ot.isAbsolute(o)?o:ot.join(a,o);if(!await Fe.pathExists(l))throw new Error(`Arquivo de manifest n\xE3o encontrado: ${o}`);r.text="Validando manifest...";let d=await s.parse(l);r.text=`Instalando ${d.name}@${d.version}...`,await c.install(d),r.text="Atualizando configura\xE7\xE3o do projeto...",await Qn(a,d.name,d.version),r.text="Salvando cache do manifest...";let u=ot.join(a,".kaven","modules",d.name);await Fe.ensureDir(u),await Fe.writeJson(ot.join(u,"module.json"),d,{spaces:2}),r.succeed(Po.green(`M\xF3dulo ${d.name} instalado com sucesso!`)),t.capture("cli.module.add.success",{name:d.name},Date.now()-n),await t.flush()}catch(i){t.capture("cli.module.add.error",{error:i.message},Date.now()-n),await t.flush(),r.fail(Po.red(`Falha na instala\xE7\xE3o: ${i instanceof Error?i.message:String(i)}`)),process.exit(1)}}async function Qn(o,e,t){let n=ot.join(o,"kaven.json"),a={modules:{}};await Fe.pathExists(n)&&(a=await Fe.readJson(n)),a.modules||(a.modules={}),a.modules[e]=t,await Fe.writeJson(n,a,{spaces:2})}import Nt from"chalk";import Kt from"ora";import Vt from"path";import Ue from"fs-extra";async function Io(o,e){let t=j.getInstance(),n=Date.now();t.capture("cli.module.remove.start",{moduleName:o});let a=e||process.cwd(),r=Kt(`Removendo m\xF3dulo ${o}...`).start();try{let i=new le,s=new be(a,i),c=Vt.join(a,"kaven.json");if(!await Ue.pathExists(c))throw new Error("Arquivo kaven.json n\xE3o encontrado. Este \xE9 um projeto Kaven?");let l=await Ue.readJson(c);if(!l.modules||!l.modules[o])throw new Error(`O m\xF3dulo ${o} n\xE3o est\xE1 instalado.`);let d=Vt.join(a,".kaven","modules",o,"module.json");if(!await Ue.pathExists(d))throw new Error(`Cache do manifest para ${o} n\xE3o encontrado em ${d}. A remo\xE7\xE3o precisa do manifest original.`);let u=await Ue.readJson(d);r.text=`Removendo inje\xE7\xF5es de ${o}...`,await s.uninstall(u),r.text="Atualizando configura\xE7\xE3o do projeto...",delete l.modules[o],await Ue.writeJson(c,l,{spaces:2}),await Ue.remove(Vt.dirname(d)),Kt().succeed(Nt.green(`M\xF3dulo ${o} removido com sucesso!`)),t.capture("cli.module.remove.success",{moduleName:o},Date.now()-n),await t.flush()}catch(i){t.capture("cli.module.remove.error",{error:i.message},Date.now()-n),await t.flush(),Kt().fail(Nt.red(`Falha ao remover m\xF3dulo ${o}:`)),r.fail(Nt.red(`${i instanceof Error?i.message:String(i)}`)),process.exit(1)}}import _ from"fs-extra";import N from"path";import la from"os";import*as To from"tar";import*as Z from"@clack/prompts";import V from"picocolors";import se from"fs-extra";import Bt from"path";import ea from"os";function Ao(o){try{let e=o.split(".");if(e.length!==3)return null;let t=e[1].replace(/-/g,"+").replace(/_/g,"/"),n=t+"=".repeat((4-t.length%4)%4),a=Buffer.from(n,"base64").toString("utf8");return JSON.parse(a)}catch{return null}}function ta(o){let e=Date.now(),t=new Date(o).getTime(),n=Math.max(0,Math.floor((t-e)/1e3));if(n===0)return"expired";let a=Math.floor(n/3600),r=Math.floor(n%3600/60);return a>0?`expires in ${a}h ${r}m`:`expires in ${r}m`}var M=class{configPath;constructor(){this.configPath=Bt.join(ea.homedir(),".kaven","auth.json")}async saveTokens(e){let t=Bt.dirname(this.configPath);await se.ensureDir(t),await se.writeJson(this.configPath,e,{spaces:2}),process.platform!=="win32"&&await se.chmod(this.configPath,384)}async storeToken(e){let t=Bt.dirname(this.configPath);await se.ensureDir(t),await se.writeJson(this.configPath,{token:e},{spaces:2}),process.platform!=="win32"&&await se.chmod(this.configPath,384)}async getAuth(){if(!await se.pathExists(this.configPath))return null;try{let e=await se.readJson(this.configPath);return e.access_token?e:null}catch{return null}}async getToken(){if(!await se.pathExists(this.configPath))return null;try{let e=await se.readJson(this.configPath);return e.access_token?e.access_token:e.token||null}catch{return null}}async getValidToken(){let e=await this.getAuth();if(!e)throw new Error("Not authenticated. Run 'kaven auth login' to authenticate.");let t=new Date(e.expires_at).getTime(),n=Date.now(),a=300*1e3,r=t-n<a,i=n>=t;if(!r)return e.access_token;try{let{MarketplaceClient:s}=await import("./MarketplaceClient-YCFH2VU4.js"),l=await new s(this).refreshToken(e.refresh_token),d={access_token:l.access_token,refresh_token:l.refresh_token,expires_at:l.expires_at,user:e.user};return await this.saveTokens(d),l.access_token}catch{if(!i)return console.warn("[kaven] Warning: Failed to refresh token. Using existing token."),e.access_token;throw new Error("Session expired. Run 'kaven auth login' to re-authenticate.")}}async isAuthenticated(){try{return!!await this.getToken()}catch{return!1}}async logout(){await se.pathExists(this.configPath)&&await se.remove(this.configPath)}async clearToken(){return this.logout()}async getUserInfo(){let e=await this.getAuth();if(!e)return null;let t=Ao(e.access_token);return t?{id:t.sub,email:t.email,name:t.githubId}:{id:e.user.githubId,email:e.user.email,name:void 0}}async getDecodedToken(){let e=await this.getAuth();return e?Ao(e.access_token):null}async getWhoamiInfo(){let e=await this.getAuth();return e?{email:e.user.email,githubId:e.user.githubId,tier:e.user.tier.charAt(0).toUpperCase()+e.user.tier.slice(1),sessionExpiry:ta(e.expires_at)}:null}};import{spawn as oa}from"child_process";import*as nt from"fs-extra";var kt=class{async performMerge(e,t,n){if(!await nt.pathExists(t))throw new Error("Baseline cache file missing: "+t+". Cannot perform safe merge.");return await nt.pathExists(e)?new Promise((a,r)=>{let i=oa("git",["merge-file","-L","Your Modifications (OURS)","-L","Original Version (BASE)","-L","Kaven Update (THEIRS)",e,t,n]),s="";i.stdout.on("data",c=>{s+=c.toString()}),i.stderr.on("data",c=>{s+=c.toString()}),i.on("close",c=>{c===0?a({success:!0,conflicts:!1,output:s}):c!==null&&c>0?a({success:!0,conflicts:!0,output:s}):r(new Error("Git merge-file failed critically: "+s))})}):(await nt.copy(n,e),{success:!0,conflicts:!1,output:"New file copied"})}};import bt from"crypto";import na from"fs-extra";async function aa(o){let e=await na.readFile(o);return bt.createHash("sha256").update(e).digest("hex")}var ra=/^[0-9a-fA-F]+$/;function ia(o){return ra.test(o)&&o.length===128?Buffer.from(o,"hex"):Buffer.from(o,"base64")}function sa(o,e,t){try{let n=bt.createPublicKey({key:Buffer.from(t,"base64"),type:"spki",format:"der"}),a=ia(e);return bt.verify(null,Buffer.from(o),n,a)?!0:bt.verify(null,Buffer.from(o+`
27
- `),n,a)}catch{return!1}}async function xt(o){let e=await aa(o.filePath);if(e!==o.expectedChecksum)throw new Ze(`Checksum mismatch: expected ${o.expectedChecksum.substring(0,16)}..., got ${e.substring(0,16)}...`);if(!sa(o.expectedChecksum,o.signature,o.publicKeyBase64))throw new Ze("Ed25519 signature verification failed. The package may have been tampered with.")}import Ne from"fs-extra";import qt from"path";var Jt=".kaven/cache/modules";function ca(o,e){let t=o.split(".").map(Number),n=e.split(".").map(Number);for(let a=0;a<3;a++)if((t[a]??0)!==(n[a]??0))return(t[a]??0)-(n[a]??0);return 0}async function Et(o,e,t,n){let a=qt.join(n,Jt,`${o}-${e}`);await Ne.ensureDir(a),await Ne.copy(t,a)}async function St(o,e){let t=qt.join(e,Jt);if(!await Ne.pathExists(t))return null;let a=(await Ne.readdir(t)).filter(i=>{let s=i.lastIndexOf("-");return s>0&&i.slice(0,s)===o});if(a.length===0)return null;a.sort((i,s)=>{let c=i.slice(i.lastIndexOf("-")+1),l=s.slice(s.lastIndexOf("-")+1);return ca(c,l)});let r=a[0];return r.slice(r.lastIndexOf("-")+1)}function Gt(o,e,t){return qt.join(t,Jt,`${o}-${e}`)}async function Mo(o,e,t){let n=Gt(o,e,t);await Ne.pathExists(n)&&await Ne.remove(n)}var da={x:To.x},B={intro:Z.intro,outro:Z.outro,spinner:Z.spinner,confirm:Z.confirm,select:Z.select,isCancel:Z.isCancel,log:Z.log};function jo(o){return o.filter(e=>{let t=N.normalize(e);return!t.startsWith("..")&&!N.isAbsolute(t)})}async function ua(){let o=N.join(la.tmpdir(),`kaven-update-${Date.now()}`);return await _.ensureDir(o),o}async function pa(o,e){let t=N.join(o,".kaven","conflicts.json");if(!await _.pathExists(t))return null;try{let n=await _.readJson(t);if(n.module===e)return n}catch{}return null}async function at(o,e,t={}){let n=j.getInstance(),a=Date.now(),r=e??process.cwd();if(B.intro(V.bold(V.cyan("kaven module update"))),!o&&(o=await fa(r),!o)){B.outro(V.yellow("Nenhum m\xF3dulo selecionado."));return}n.capture("cli.module.update.start",{slug:o});let i;try{let s=await pa(r,o);if(s){let b=await B.confirm({message:V.yellow(`Conflitos pendentes encontrados do update ${s.fromVersion}\u2192${s.toVersion}. Deseja tentar novamente?`)});if(B.isCancel(b)||!b){B.outro(V.yellow("Update cancelado. Resolva os conflitos manualmente e rode novamente."));return}await _.remove(N.join(r,".kaven","conflicts.json"))}let c=await St(o,r);c||(B.outro(V.red(`M\xF3dulo '${o}' n\xE3o est\xE1 instalado. Use 'kaven marketplace install ${o}' primeiro.`)),process.exit(1));let l=B.spinner();l.start(`Verificando vers\xE3o mais recente de '${o}'...`);let d=new M,u=new J(d);try{await d.getValidToken()}catch{l.stop("Autentica\xE7\xE3o necess\xE1ria."),B.outro(V.red("Execute: kaven auth login")),process.exit(1)}let m=await u.getModule(o),p=m.latestVersion??m.releases?.[0]?.version;if(p||(l.stop(),B.outro(V.red(`Nenhuma vers\xE3o publicada encontrada para '${o}'.`)),process.exit(1)),l.stop(`Vers\xE3o instalada: ${V.bold(c)} \u2014 \xDAltima vers\xE3o: ${V.bold(p)}`),c===p){B.outro(V.green(`\u2713 '${o}' j\xE1 est\xE1 na vers\xE3o mais recente (${p}).`)),n.capture("cli.module.update.already_latest",{slug:o,version:p},Date.now()-a),await n.flush();return}l.start(`Baixando '${o}@${p}'...`);let f=await u.createDownloadToken(o,p);i=await ua();let y=N.join(i,"module.tar.gz"),P=N.join(i,"extracted");await _.ensureDir(P);let R=await u.resolveUrl(f.downloadUrl),K=await fetch(R);if(!K.ok||!K.body)throw new Error(`Download falhou: ${K.status} ${K.statusText}`);let g=_.createWriteStream(y),I=K.body.getReader();if(await new Promise((b,A)=>{(async()=>{try{let ae=!0;for(;ae;){let{done:Ie,value:Dt}=await I.read();Ie?(g.end(),ae=!1):g.write(Dt)||await new Promise(Ot=>g.once("drain",Ot))}g.once("finish",b),g.once("error",A)}catch(ae){A(ae)}})()}),l.stop(`Download conclu\xEDdo: ${o}@${p}`),!t.skipVerify){l.start(`Verificando assinatura de ${o}@${p}...`);let b=await u.getReleaseInfo(o,p);b.checksum&&b.signature&&b.publicKey?(await xt({filePath:y,expectedChecksum:b.checksum,signature:b.signature,publicKeyBase64:b.publicKey}),l.stop(`Assinatura verificada: ${o}@${p}`)):l.stop(`Sem dados de assinatura para ${o}@${p} \u2014 verifica\xE7\xE3o ignorada`)}l.start("Extraindo arquivos..."),await da.x({file:y,cwd:P}),l.stop("Extra\xE7\xE3o completa.");let me=N.join(P,"module.json");if(!await _.pathExists(me))throw new Error(`module.json n\xE3o encontrado no pacote de '${o}@${p}'`);let Ye=await _.readJson(me),Pe=jo(Ye.mergeable??[]),_t=jo(Ye.copyOnly??[]);l.start(`Cacheando baseline de ${o}@${p}...`),await Et(o,p,P,r),l.stop("Baseline cacheado.");let ho=Gt(o,c,r),ft=new kt,$e=[];for(let b of Pe){let A=N.join(r,b),ne=N.join(ho,b),ae=N.join(P,b);if(!await _.pathExists(ae))continue;l.start(`Mergeando ${b}...`),(await ft.performMerge(A,ne,ae)).conflicts?($e.push(b),l.stop(V.yellow(` Conflito em ${b}`))):l.stop(V.green(` \u2713 ${b}`))}for(let b of _t){let A=N.join(P,b),ne=N.join(r,b);await _.pathExists(A)&&(l.start(`Copiando ${b}...`),await _.ensureDir(N.dirname(ne)),await _.copy(A,ne,{overwrite:!0}),l.stop(V.green(` \u2713 ${b} (sobrescrito)`)))}if($e.length>0){let b={module:o,fromVersion:c,toVersion:p,timestamp:new Date().toISOString(),conflicts:$e};await _.ensureDir(N.join(r,".kaven")),await _.writeJson(N.join(r,".kaven","conflicts.json"),b,{spaces:2}),B.outro(V.red(`Update parcial \u2014 ${$e.length} conflito(s) detectado(s):
26
+ `).reverse().slice(0,e).map(n=>JSON.parse(n))}catch{return[]}}};async function Io(o,e){let t=j.getInstance(),n=Date.now();t.capture("cli.module.add.start",{manifestPath:o});let a=e||process.cwd(),r=ea("Preparando instala\xE7\xE3o do m\xF3dulo...").start();try{let i=new le,s=new Oe,c=new be(a,i),l=ot.isAbsolute(o)?o:ot.join(a,o);if(!await Fe.pathExists(l))throw new Error(`Arquivo de manifest n\xE3o encontrado: ${o}`);r.text="Validando manifest...";let d=await s.parse(l);r.text=`Instalando ${d.name}@${d.version}...`,await c.install(d),r.text="Atualizando configura\xE7\xE3o do projeto...",await ta(a,d.name,d.version),r.text="Salvando cache do manifest...";let u=ot.join(a,".kaven","modules",d.name);await Fe.ensureDir(u),await Fe.writeJson(ot.join(u,"module.json"),d,{spaces:2}),r.succeed($o.green(`M\xF3dulo ${d.name} instalado com sucesso!`)),t.capture("cli.module.add.success",{name:d.name},Date.now()-n),await t.flush()}catch(i){t.capture("cli.module.add.error",{error:i.message},Date.now()-n),await t.flush(),r.fail($o.red(`Falha na instala\xE7\xE3o: ${i instanceof Error?i.message:String(i)}`)),process.exit(1)}}async function ta(o,e,t){let n=ot.join(o,"kaven.json"),a={modules:{}};await Fe.pathExists(n)&&(a=await Fe.readJson(n)),a.modules||(a.modules={}),a.modules[e]=t,await Fe.writeJson(n,a,{spaces:2})}import Nt from"chalk";import Kt from"ora";import Vt from"path";import Ue from"fs-extra";async function Ao(o,e){let t=j.getInstance(),n=Date.now();t.capture("cli.module.remove.start",{moduleName:o});let a=e||process.cwd(),r=Kt(`Removendo m\xF3dulo ${o}...`).start();try{let i=new le,s=new be(a,i),c=Vt.join(a,"kaven.json");if(!await Ue.pathExists(c))throw new Error("Arquivo kaven.json n\xE3o encontrado. Este \xE9 um projeto Kaven?");let l=await Ue.readJson(c);if(!l.modules||!l.modules[o])throw new Error(`O m\xF3dulo ${o} n\xE3o est\xE1 instalado.`);let d=Vt.join(a,".kaven","modules",o,"module.json");if(!await Ue.pathExists(d))throw new Error(`Cache do manifest para ${o} n\xE3o encontrado em ${d}. A remo\xE7\xE3o precisa do manifest original.`);let u=await Ue.readJson(d);r.text=`Removendo inje\xE7\xF5es de ${o}...`,await s.uninstall(u),r.text="Atualizando configura\xE7\xE3o do projeto...",delete l.modules[o],await Ue.writeJson(c,l,{spaces:2}),await Ue.remove(Vt.dirname(d)),Kt().succeed(Nt.green(`M\xF3dulo ${o} removido com sucesso!`)),t.capture("cli.module.remove.success",{moduleName:o},Date.now()-n),await t.flush()}catch(i){t.capture("cli.module.remove.error",{error:i.message},Date.now()-n),await t.flush(),Kt().fail(Nt.red(`Falha ao remover m\xF3dulo ${o}:`)),r.fail(Nt.red(`${i instanceof Error?i.message:String(i)}`)),process.exit(1)}}import _ from"fs-extra";import N from"path";import ua from"os";import*as Ro from"tar";import*as Z from"@clack/prompts";import V from"picocolors";import se from"fs-extra";import Bt from"path";import oa from"os";function Mo(o){try{let e=o.split(".");if(e.length!==3)return null;let t=e[1].replace(/-/g,"+").replace(/_/g,"/"),n=t+"=".repeat((4-t.length%4)%4),a=Buffer.from(n,"base64").toString("utf8");return JSON.parse(a)}catch{return null}}function na(o){let e=Date.now(),t=new Date(o).getTime(),n=Math.max(0,Math.floor((t-e)/1e3));if(n===0)return"expired";let a=Math.floor(n/3600),r=Math.floor(n%3600/60);return a>0?`expires in ${a}h ${r}m`:`expires in ${r}m`}var M=class{configPath;constructor(){this.configPath=Bt.join(oa.homedir(),".kaven","auth.json")}async saveTokens(e){let t=Bt.dirname(this.configPath);await se.ensureDir(t),await se.writeJson(this.configPath,e,{spaces:2}),process.platform!=="win32"&&await se.chmod(this.configPath,384)}async storeToken(e){let t=Bt.dirname(this.configPath);await se.ensureDir(t),await se.writeJson(this.configPath,{token:e},{spaces:2}),process.platform!=="win32"&&await se.chmod(this.configPath,384)}async getAuth(){if(!await se.pathExists(this.configPath))return null;try{let e=await se.readJson(this.configPath);return e.access_token?e:null}catch{return null}}async getToken(){if(!await se.pathExists(this.configPath))return null;try{let e=await se.readJson(this.configPath);return e.access_token?e.access_token:e.token||null}catch{return null}}async getValidToken(){let e=await this.getAuth();if(!e)throw new Error("Not authenticated. Run 'kaven auth login' to authenticate.");let t=new Date(e.expires_at).getTime(),n=Date.now(),a=300*1e3,r=t-n<a,i=n>=t;if(!r)return e.access_token;try{let{MarketplaceClient:s}=await import("./MarketplaceClient-XMLIXDWN.js"),l=await new s(this).refreshToken(e.refresh_token),d={access_token:l.access_token,refresh_token:l.refresh_token,expires_at:l.expires_at,user:e.user};return await this.saveTokens(d),l.access_token}catch{if(!i)return console.warn("[kaven] Warning: Failed to refresh token. Using existing token."),e.access_token;throw new Error("Session expired. Run 'kaven auth login' to re-authenticate.")}}async isAuthenticated(){try{return!!await this.getToken()}catch{return!1}}async logout(){await se.pathExists(this.configPath)&&await se.remove(this.configPath)}async clearToken(){return this.logout()}async getUserInfo(){let e=await this.getAuth();if(!e)return null;let t=Mo(e.access_token);return t?{id:t.sub,email:t.email,name:t.githubId}:{id:e.user.githubId,email:e.user.email,name:void 0}}async getDecodedToken(){let e=await this.getAuth();return e?Mo(e.access_token):null}async getWhoamiInfo(){let e=await this.getAuth();return e?{email:e.user.email,githubId:e.user.githubId,tier:e.user.tier.charAt(0).toUpperCase()+e.user.tier.slice(1),sessionExpiry:na(e.expires_at)}:null}};import{spawn as aa}from"child_process";import*as nt from"fs-extra";var kt=class{async performMerge(e,t,n){if(!await nt.pathExists(t))throw new Error("Baseline cache file missing: "+t+". Cannot perform safe merge.");return await nt.pathExists(e)?new Promise((a,r)=>{let i=aa("git",["merge-file","-L","Your Modifications (OURS)","-L","Original Version (BASE)","-L","Kaven Update (THEIRS)",e,t,n]),s="";i.stdout.on("data",c=>{s+=c.toString()}),i.stderr.on("data",c=>{s+=c.toString()}),i.on("close",c=>{c===0?a({success:!0,conflicts:!1,output:s}):c!==null&&c>0?a({success:!0,conflicts:!0,output:s}):r(new Error("Git merge-file failed critically: "+s))})}):(await nt.copy(n,e),{success:!0,conflicts:!1,output:"New file copied"})}};import bt from"crypto";import ra from"fs-extra";async function ia(o){let e=await ra.readFile(o);return bt.createHash("sha256").update(e).digest("hex")}var sa=/^[0-9a-fA-F]+$/;function ca(o){return sa.test(o)&&o.length===128?Buffer.from(o,"hex"):Buffer.from(o,"base64")}function la(o,e,t){try{let n=bt.createPublicKey({key:Buffer.from(t,"base64"),type:"spki",format:"der"}),a=ca(e);return bt.verify(null,Buffer.from(o),n,a)?!0:bt.verify(null,Buffer.from(o+`
27
+ `),n,a)}catch{return!1}}async function xt(o){let e=await ia(o.filePath);if(e!==o.expectedChecksum)throw new Ze(`Checksum mismatch: expected ${o.expectedChecksum.substring(0,16)}..., got ${e.substring(0,16)}...`);if(!la(o.expectedChecksum,o.signature,o.publicKeyBase64))throw new Ze("Ed25519 signature verification failed. The package may have been tampered with.")}import Ne from"fs-extra";import qt from"path";var Jt=".kaven/cache/modules";function da(o,e){let t=o.split(".").map(Number),n=e.split(".").map(Number);for(let a=0;a<3;a++)if((t[a]??0)!==(n[a]??0))return(t[a]??0)-(n[a]??0);return 0}async function Et(o,e,t,n){let a=qt.join(n,Jt,`${o}-${e}`);await Ne.ensureDir(a),await Ne.copy(t,a)}async function St(o,e){let t=qt.join(e,Jt);if(!await Ne.pathExists(t))return null;let a=(await Ne.readdir(t)).filter(i=>{let s=i.lastIndexOf("-");return s>0&&i.slice(0,s)===o});if(a.length===0)return null;a.sort((i,s)=>{let c=i.slice(i.lastIndexOf("-")+1),l=s.slice(s.lastIndexOf("-")+1);return da(c,l)});let r=a[0];return r.slice(r.lastIndexOf("-")+1)}function zt(o,e,t){return qt.join(t,Jt,`${o}-${e}`)}async function jo(o,e,t){let n=zt(o,e,t);await Ne.pathExists(n)&&await Ne.remove(n)}var pa={x:Ro.x},B={intro:Z.intro,outro:Z.outro,spinner:Z.spinner,confirm:Z.confirm,select:Z.select,isCancel:Z.isCancel,log:Z.log};function To(o){return o.filter(e=>{let t=N.normalize(e);return!t.startsWith("..")&&!N.isAbsolute(t)})}async function ma(){let o=N.join(ua.tmpdir(),`kaven-update-${Date.now()}`);return await _.ensureDir(o),o}async function fa(o,e){let t=N.join(o,".kaven","conflicts.json");if(!await _.pathExists(t))return null;try{let n=await _.readJson(t);if(n.module===e)return n}catch{}return null}async function at(o,e,t={}){let n=j.getInstance(),a=Date.now(),r=e??process.cwd();if(B.intro(V.bold(V.cyan("kaven module update"))),!o&&(o=await ha(r),!o)){B.outro(V.yellow("Nenhum m\xF3dulo selecionado."));return}n.capture("cli.module.update.start",{slug:o});let i;try{let s=await fa(r,o);if(s){let b=await B.confirm({message:V.yellow(`Conflitos pendentes encontrados do update ${s.fromVersion}\u2192${s.toVersion}. Deseja tentar novamente?`)});if(B.isCancel(b)||!b){B.outro(V.yellow("Update cancelado. Resolva os conflitos manualmente e rode novamente."));return}await _.remove(N.join(r,".kaven","conflicts.json"))}let c=await St(o,r);c||(B.outro(V.red(`M\xF3dulo '${o}' n\xE3o est\xE1 instalado. Use 'kaven marketplace install ${o}' primeiro.`)),process.exit(1));let l=B.spinner();l.start(`Verificando vers\xE3o mais recente de '${o}'...`);let d=new M,u=new J(d);try{await d.getValidToken()}catch{l.stop("Autentica\xE7\xE3o necess\xE1ria."),B.outro(V.red("Execute: kaven auth login")),process.exit(1)}let m=await u.getModule(o),p=m.latestVersion??m.releases?.[0]?.version;if(p||(l.stop(),B.outro(V.red(`Nenhuma vers\xE3o publicada encontrada para '${o}'.`)),process.exit(1)),l.stop(`Vers\xE3o instalada: ${V.bold(c)} \u2014 \xDAltima vers\xE3o: ${V.bold(p)}`),c===p){B.outro(V.green(`\u2713 '${o}' j\xE1 est\xE1 na vers\xE3o mais recente (${p}).`)),n.capture("cli.module.update.already_latest",{slug:o,version:p},Date.now()-a),await n.flush();return}l.start(`Baixando '${o}@${p}'...`);let f=await u.createDownloadToken(o,p);i=await ma();let y=N.join(i,"module.tar.gz"),P=N.join(i,"extracted");await _.ensureDir(P);let R=await u.resolveUrl(f.downloadUrl),K=await fetch(R);if(!K.ok||!K.body)throw new Error(`Download falhou: ${K.status} ${K.statusText}`);let g=_.createWriteStream(y),I=K.body.getReader();if(await new Promise((b,A)=>{(async()=>{try{let ae=!0;for(;ae;){let{done:Ie,value:Dt}=await I.read();Ie?(g.end(),ae=!1):g.write(Dt)||await new Promise(Ot=>g.once("drain",Ot))}g.once("finish",b),g.once("error",A)}catch(ae){A(ae)}})()}),l.stop(`Download conclu\xEDdo: ${o}@${p}`),!t.skipVerify){l.start(`Verificando assinatura de ${o}@${p}...`);let b=await u.getReleaseInfo(o,p);b.checksum&&b.signature&&b.publicKey?(await xt({filePath:y,expectedChecksum:b.checksum,signature:b.signature,publicKeyBase64:b.publicKey}),l.stop(`Assinatura verificada: ${o}@${p}`)):l.stop(`Sem dados de assinatura para ${o}@${p} \u2014 verifica\xE7\xE3o ignorada`)}l.start("Extraindo arquivos..."),await pa.x({file:y,cwd:P}),l.stop("Extra\xE7\xE3o completa.");let me=N.join(P,"module.json");if(!await _.pathExists(me))throw new Error(`module.json n\xE3o encontrado no pacote de '${o}@${p}'`);let Ye=await _.readJson(me),Pe=To(Ye.mergeable??[]),_t=To(Ye.copyOnly??[]);l.start(`Cacheando baseline de ${o}@${p}...`),await Et(o,p,P,r),l.stop("Baseline cacheado.");let ho=zt(o,c,r),ft=new kt,$e=[];for(let b of Pe){let A=N.join(r,b),ne=N.join(ho,b),ae=N.join(P,b);if(!await _.pathExists(ae))continue;l.start(`Mergeando ${b}...`),(await ft.performMerge(A,ne,ae)).conflicts?($e.push(b),l.stop(V.yellow(` Conflito em ${b}`))):l.stop(V.green(` \u2713 ${b}`))}for(let b of _t){let A=N.join(P,b),ne=N.join(r,b);await _.pathExists(A)&&(l.start(`Copiando ${b}...`),await _.ensureDir(N.dirname(ne)),await _.copy(A,ne,{overwrite:!0}),l.stop(V.green(` \u2713 ${b} (sobrescrito)`)))}if($e.length>0){let b={module:o,fromVersion:c,toVersion:p,timestamp:new Date().toISOString(),conflicts:$e};await _.ensureDir(N.join(r,".kaven")),await _.writeJson(N.join(r,".kaven","conflicts.json"),b,{spaces:2}),B.outro(V.red(`Update parcial \u2014 ${$e.length} conflito(s) detectado(s):
28
28
  `+$e.map(A=>` \u2022 ${A}`).join(`
29
29
  `)+`
30
30
 
31
- Resolva os conflitos (marcadores git <<<<<<< / ======= / >>>>>>>), depois rode 'kaven module update ${o}' novamente.`)),n.capture("cli.module.update.conflicts",{slug:o,fromVersion:c,toVersion:p,conflictCount:$e.length},Date.now()-a),await n.flush();return}await ma(r,o,p),await Mo(o,c,r),B.outro(V.green(`\u2713 '${o}' atualizado de ${c} \u2192 ${p}`)),n.capture("cli.module.update.success",{slug:o,fromVersion:c,toVersion:p},Date.now()-a),await n.flush()}catch(s){let c=fe(s);B.outro(V.red(`Falha ao atualizar '${o}': ${c.message}`)),n.capture("cli.module.update.error",{slug:o,error:c.message},Date.now()-a),await n.flush(),process.exit(1)}finally{i&&await _.remove(i).catch(()=>{})}}async function ma(o,e,t){let n=N.join(o,"kaven.json"),a={};await _.pathExists(n)&&(a=await _.readJson(n)),(!a.modules||typeof a.modules!="object"||Array.isArray(a.modules))&&(a.modules={}),a.modules[e]=t,await _.writeJson(n,a,{spaces:2})}async function fa(o){let e=N.join(o,".kaven","cache","modules");if(!await _.pathExists(e)){B.log.warn("Nenhum m\xF3dulo instalado encontrado.");return}let t=await _.readdir(e);if(t.length===0){B.log.warn("Nenhum m\xF3dulo instalado encontrado.");return}let n=t.map(r=>{let i=r.lastIndexOf("-"),s=r.slice(0,i),c=r.slice(i+1);return{value:s,label:`${s} (${c})`}}),a=await B.select({message:"Selecione o m\xF3dulo para atualizar:",options:n});if(!B.isCancel(a))return a}import D from"chalk";import Ke from"ora";import Ct from"path";import O from"fs-extra";import it from"crypto";import Ro from"os";import{z as xe}from"zod";var ga=xe.object({name:xe.string().min(1),slug:xe.string().min(1).regex(/^[a-z0-9-]+$/),version:xe.string().regex(/^\d+\.\d+\.\d+$/),description:xe.string().min(1),author:xe.string().optional(),license:xe.string().optional(),tier:xe.enum(["free","starter","complete","pro"])}),rt=Ct.join(Ro.homedir(),".kaven","signing-key.json");async function ha(){if(await O.pathExists(rt))try{let a=await O.readJson(rt),r=it.createPrivateKey({key:Buffer.from(a.privateKey,"base64"),type:"pkcs8",format:"der"}),i=it.createPublicKey(r);return{privateKey:r,publicKey:i}}catch{}let{privateKey:o,publicKey:e}=it.generateKeyPairSync("ed25519"),t=o.export({type:"pkcs8",format:"der"}),n=e.export({type:"spki",format:"der"});return await O.ensureDir(Ct.dirname(rt)),await O.writeJson(rt,{privateKey:t.toString("base64"),publicKey:n.toString("base64")},{spaces:2}),process.platform!=="win32"&&await O.chmod(rt,384),{privateKey:o,publicKey:e}}async function wa(o){let e=await O.readFile(o);return it.createHash("sha256").update(e).digest("hex")}async function ya(o,e){await(await import("tar")).create({gzip:!0,file:e,cwd:o,filter:n=>{let a=n.replace(/\\/g,"/"),r=["node_modules",".git","dist",".env"];for(let i of r)if(a.startsWith(i+"/")||a===i||a.endsWith(".log"))return!1;return!0}},["."])}async function _o(o){let e=process.cwd(),t=Ct.join(e,"module.json");await O.pathExists(t)||(console.error(D.red("Error: module.json not found in current directory.")),console.error(D.gray("Try: run this command from inside a module directory")),process.exit(1));let n;try{let g=await O.readJson(t),I=ga.safeParse(g);if(!I.success){console.error(D.red("Error: Invalid module.json:"));for(let me of I.error.issues)console.error(D.red(` - ${me.path.join(".")}: ${me.message}`));process.exit(1)}n=I.data}catch(g){console.error(D.red(`Error: Failed to parse module.json: ${g instanceof Error?g.message:String(g)}`)),process.exit(1)}console.log(),console.log(D.bold(`Publishing module: ${n.name} v${n.version}`)),console.log(D.gray(`Slug: ${n.slug} | Tier: ${n.tier}`)),console.log();let a=Ct.join(Ro.tmpdir(),`kaven-${n.slug}-${n.version}.tar.gz`),r=Ke("Creating module package...").start();try{await ya(e,a);let g=await O.stat(a);r.succeed(`Package created (${(g.size/1024).toFixed(1)} KB)`)}catch(g){r.fail("Failed to create package"),console.error(D.red(g instanceof Error?g.message:String(g))),await O.remove(a).catch(()=>{}),process.exit(1)}let i=Ke("Computing SHA-256 checksum...").start(),s;try{s=await wa(a),i.succeed(`Checksum: ${s.substring(0,16)}...`)}catch{i.fail("Failed to compute checksum"),await O.remove(a).catch(()=>{}),process.exit(1);return}let c=Ke("Signing package...").start(),l,d;try{let{privateKey:g,publicKey:I}=await ha();l=it.sign(null,Buffer.from(s),g).toString("base64"),d=I.export({type:"spki",format:"der"}).toString("base64"),c.succeed(`Package signed (${l.substring(0,16)}...)`)}catch{c.fail("Failed to sign package"),await O.remove(a).catch(()=>{}),process.exit(1);return}if(o.dryRun){console.log(),console.log(D.yellow("Dry-run mode: skipping upload and release creation.")),console.log(D.green("\u2705 Package validated successfully.")),await O.remove(a).catch(()=>{});return}let u=new M;try{await u.getValidToken()}catch{console.error(D.red("Error: Not authenticated. Run 'kaven auth login' first.")),await O.remove(a).catch(()=>{}),process.exit(1);return}let m=new J(u),p=await O.stat(a),f=Ke("Getting upload URL...").start(),y,P;try{let g=await m.getUploadUrl(n.slug,n.version,p.size);y=g.uploadUrl,P=g.s3Key,f.succeed("Upload URL received")}catch(g){f.fail("Failed to get upload URL"),console.error(D.red(g instanceof Error?g.message:String(g))),await O.remove(a).catch(()=>{}),process.exit(1);return}let R=Ke(`Uploading package (${(p.size/1024).toFixed(1)} KB)...`).start();try{let g=await O.readFile(a),I=await fetch(y,{method:"PUT",headers:{"Content-Type":"application/gzip","Content-Length":String(p.size)},body:g});if(!I.ok)throw new Error(`Upload failed: ${I.status} ${I.statusText}`);R.succeed("Package uploaded successfully")}catch(g){R.fail("Upload failed"),console.error(D.red(g instanceof Error?g.message:String(g))),await O.remove(a).catch(()=>{}),process.exit(1);return}let K=Ke("Creating release record...").start();try{let g=await m.createRelease({moduleSlug:n.slug,version:n.version,s3Key:P,checksum:s,signature:l,publicKey:d,changelog:o.changelog});K.succeed(`Release created: ${n.slug}@${g.version} (ID: ${g.id})`)}catch(g){K.fail("Failed to create release"),console.error(D.red(g instanceof Error?g.message:String(g))),await O.remove(a).catch(()=>{}),process.exit(1);return}await O.remove(a).catch(()=>{}),console.log(),console.log(D.green(`\u2705 Published ${n.name} v${n.version} to the Kaven Marketplace!`)),console.log(D.gray(`View your module at: https://marketplace.kaven.site/modules/${n.slug}`)),console.log(),console.log(D.bold("Next steps:")),console.log(D.gray(" 1. Share your module with the community")),console.log(D.gray(" 2. Monitor installation metrics")),console.log(D.gray(" 3. Update module with 'kaven module publish' when ready"))}import v from"chalk";import Do from"ora";import ba from"path";import*as Oo from"@inquirer/prompts";import zt from"fs-extra";import va from"path";var Ae=[{id:"auth",label:"Auth & Identity",description:"Gest\xE3o de usu\xE1rios, permiss\xF5es e sess\xF5es",models:["User","Role","Capability","AuthSession","AuditLog"],enums:["UserRole"],dependsOn:[]},{id:"billing",label:"Billing",description:"Faturamento, assinaturas e pagamentos",models:["Invoice","Order","Subscription","Plan","Payment","Product"],enums:[],dependsOn:["auth"]},{id:"projects",label:"Projects",description:"Gest\xE3o de projetos e tasks",models:["Project","Task"],enums:["ProjectStatus","TaskStatus","TaskPriority"],dependsOn:["auth"]},{id:"notifications",label:"Notifications",description:"Notifica\xE7\xF5es e prefer\xEAncias de usu\xE1rio",models:["Notification","UserPreference"],enums:[],dependsOn:["auth"]},{id:"marketing-tracking",label:"Marketing Tracking",description:"Observabilidade de an\xFAncios, GTM, GA4 e Meta CAPI",models:["TrackingEvent"],enums:["TrackingSource"],dependsOn:["auth"]}],st=o=>`// [KAVEN_MODULE:${o.toUpperCase()} BEGIN]`,ct=o=>`// [KAVEN_MODULE:${o.toUpperCase()} END]`,ka=/^(\s*)\/\/\s*(.*)$/,Ve=class{schemaPath;constructor(e){this.schemaPath=va.join(e,"packages","database","prisma","schema.extended.prisma")}async exists(){return zt.pathExists(this.schemaPath)}get path(){return this.schemaPath}async readSchema(){return zt.readFile(this.schemaPath,"utf-8")}async writeSchema(e){await zt.writeFile(this.schemaPath,e,"utf-8")}validateMarkers(e,t){let n=st(t),a=ct(t),r=e.includes(n),i=e.includes(a);if(r&&!i)throw new Error(`Marcador \xF3rf\xE3o detectado: Faltando END para o m\xF3dulo "${t}".`);if(!r&&i)throw new Error(`Marcador \xF3rf\xE3o detectado: Faltando BEGIN para o m\xF3dulo "${t}".`);if(!r&&!i)throw new Error(`O m\xF3dulo "${t}" n\xE3o possui uma se\xE7\xE3o marcada no schema.`);if(e.indexOf(n)>e.indexOf(a))throw new Error(`Marcadores invertidos para o m\xF3dulo "${t}".`)}async getModuleStatus(e){let t=await this.readSchema(),n=st(e.id),a=ct(e.id),r=t.includes(n)&&t.includes(a),i=!1;if(r){let s=this.extractBlock(t,e.id);i=s!==null&&this.isBlockActive(s)}else i=e.models.some(s=>this.isModelActive(t,s));return{id:e.id,label:e.label,description:e.description,models:e.models,dependsOn:e.dependsOn,active:i,hasMarkers:r}}async activateModule(e){let t=await this.readSchema();this.validateMarkers(t,e.id);let n=this.uncommentBlock(t,e.id);await this.writeSchema(n)}async deactivateModule(e){let t=await this.readSchema();this.validateMarkers(t,e.id);let n=this.commentBlock(t,e.id);await this.writeSchema(n)}extractBlock(e,t){let n=st(t),a=ct(t),r=e.split(`
31
+ Resolva os conflitos (marcadores git <<<<<<< / ======= / >>>>>>>), depois rode 'kaven module update ${o}' novamente.`)),n.capture("cli.module.update.conflicts",{slug:o,fromVersion:c,toVersion:p,conflictCount:$e.length},Date.now()-a),await n.flush();return}await ga(r,o,p),await jo(o,c,r),B.outro(V.green(`\u2713 '${o}' atualizado de ${c} \u2192 ${p}`)),n.capture("cli.module.update.success",{slug:o,fromVersion:c,toVersion:p},Date.now()-a),await n.flush()}catch(s){let c=fe(s);B.outro(V.red(`Falha ao atualizar '${o}': ${c.message}`)),n.capture("cli.module.update.error",{slug:o,error:c.message},Date.now()-a),await n.flush(),process.exit(1)}finally{i&&await _.remove(i).catch(()=>{})}}async function ga(o,e,t){let n=N.join(o,"kaven.json"),a={};await _.pathExists(n)&&(a=await _.readJson(n)),(!a.modules||typeof a.modules!="object"||Array.isArray(a.modules))&&(a.modules={}),a.modules[e]=t,await _.writeJson(n,a,{spaces:2})}async function ha(o){let e=N.join(o,".kaven","cache","modules");if(!await _.pathExists(e)){B.log.warn("Nenhum m\xF3dulo instalado encontrado.");return}let t=await _.readdir(e);if(t.length===0){B.log.warn("Nenhum m\xF3dulo instalado encontrado.");return}let n=t.map(r=>{let i=r.lastIndexOf("-"),s=r.slice(0,i),c=r.slice(i+1);return{value:s,label:`${s} (${c})`}}),a=await B.select({message:"Selecione o m\xF3dulo para atualizar:",options:n});if(!B.isCancel(a))return a}import D from"chalk";import Ke from"ora";import Ct from"path";import O from"fs-extra";import it from"crypto";import _o from"os";import{z as xe}from"zod";var wa=xe.object({name:xe.string().min(1),slug:xe.string().min(1).regex(/^[a-z0-9-]+$/),version:xe.string().regex(/^\d+\.\d+\.\d+$/),description:xe.string().min(1),author:xe.string().optional(),license:xe.string().optional(),tier:xe.enum(["free","starter","complete","pro"])}),rt=Ct.join(_o.homedir(),".kaven","signing-key.json");async function ya(){if(await O.pathExists(rt))try{let a=await O.readJson(rt),r=it.createPrivateKey({key:Buffer.from(a.privateKey,"base64"),type:"pkcs8",format:"der"}),i=it.createPublicKey(r);return{privateKey:r,publicKey:i}}catch{}let{privateKey:o,publicKey:e}=it.generateKeyPairSync("ed25519"),t=o.export({type:"pkcs8",format:"der"}),n=e.export({type:"spki",format:"der"});return await O.ensureDir(Ct.dirname(rt)),await O.writeJson(rt,{privateKey:t.toString("base64"),publicKey:n.toString("base64")},{spaces:2}),process.platform!=="win32"&&await O.chmod(rt,384),{privateKey:o,publicKey:e}}async function va(o){let e=await O.readFile(o);return it.createHash("sha256").update(e).digest("hex")}async function ka(o,e){await(await import("tar")).create({gzip:!0,file:e,cwd:o,filter:n=>{let a=n.replace(/\\/g,"/"),r=["node_modules",".git","dist",".env"];for(let i of r)if(a.startsWith(i+"/")||a===i||a.endsWith(".log"))return!1;return!0}},["."])}async function Do(o){let e=process.cwd(),t=Ct.join(e,"module.json");await O.pathExists(t)||(console.error(D.red("Error: module.json not found in current directory.")),console.error(D.gray("Try: run this command from inside a module directory")),process.exit(1));let n;try{let g=await O.readJson(t),I=wa.safeParse(g);if(!I.success){console.error(D.red("Error: Invalid module.json:"));for(let me of I.error.issues)console.error(D.red(` - ${me.path.join(".")}: ${me.message}`));process.exit(1)}n=I.data}catch(g){console.error(D.red(`Error: Failed to parse module.json: ${g instanceof Error?g.message:String(g)}`)),process.exit(1)}console.log(),console.log(D.bold(`Publishing module: ${n.name} v${n.version}`)),console.log(D.gray(`Slug: ${n.slug} | Tier: ${n.tier}`)),console.log();let a=Ct.join(_o.tmpdir(),`kaven-${n.slug}-${n.version}.tar.gz`),r=Ke("Creating module package...").start();try{await ka(e,a);let g=await O.stat(a);r.succeed(`Package created (${(g.size/1024).toFixed(1)} KB)`)}catch(g){r.fail("Failed to create package"),console.error(D.red(g instanceof Error?g.message:String(g))),await O.remove(a).catch(()=>{}),process.exit(1)}let i=Ke("Computing SHA-256 checksum...").start(),s;try{s=await va(a),i.succeed(`Checksum: ${s.substring(0,16)}...`)}catch{i.fail("Failed to compute checksum"),await O.remove(a).catch(()=>{}),process.exit(1);return}let c=Ke("Signing package...").start(),l,d;try{let{privateKey:g,publicKey:I}=await ya();l=it.sign(null,Buffer.from(s),g).toString("base64"),d=I.export({type:"spki",format:"der"}).toString("base64"),c.succeed(`Package signed (${l.substring(0,16)}...)`)}catch{c.fail("Failed to sign package"),await O.remove(a).catch(()=>{}),process.exit(1);return}if(o.dryRun){console.log(),console.log(D.yellow("Dry-run mode: skipping upload and release creation.")),console.log(D.green("\u2705 Package validated successfully.")),await O.remove(a).catch(()=>{});return}let u=new M;try{await u.getValidToken()}catch{console.error(D.red("Error: Not authenticated. Run 'kaven auth login' first.")),await O.remove(a).catch(()=>{}),process.exit(1);return}let m=new J(u),p=await O.stat(a),f=Ke("Getting upload URL...").start(),y,P;try{let g=await m.getUploadUrl(n.slug,n.version,p.size);y=g.uploadUrl,P=g.s3Key,f.succeed("Upload URL received")}catch(g){f.fail("Failed to get upload URL"),console.error(D.red(g instanceof Error?g.message:String(g))),await O.remove(a).catch(()=>{}),process.exit(1);return}let R=Ke(`Uploading package (${(p.size/1024).toFixed(1)} KB)...`).start();try{let g=await O.readFile(a),I=await fetch(y,{method:"PUT",headers:{"Content-Type":"application/gzip","Content-Length":String(p.size)},body:g});if(!I.ok)throw new Error(`Upload failed: ${I.status} ${I.statusText}`);R.succeed("Package uploaded successfully")}catch(g){R.fail("Upload failed"),console.error(D.red(g instanceof Error?g.message:String(g))),await O.remove(a).catch(()=>{}),process.exit(1);return}let K=Ke("Creating release record...").start();try{let g=await m.createRelease({moduleSlug:n.slug,version:n.version,s3Key:P,checksum:s,signature:l,publicKey:d,changelog:o.changelog});K.succeed(`Release created: ${n.slug}@${g.version} (ID: ${g.id})`)}catch(g){K.fail("Failed to create release"),console.error(D.red(g instanceof Error?g.message:String(g))),await O.remove(a).catch(()=>{}),process.exit(1);return}await O.remove(a).catch(()=>{}),console.log(),console.log(D.green(`\u2705 Published ${n.name} v${n.version} to the Kaven Marketplace!`)),console.log(D.gray(`View your module at: https://marketplace.kaven.site/modules/${n.slug}`)),console.log(),console.log(D.bold("Next steps:")),console.log(D.gray(" 1. Share your module with the community")),console.log(D.gray(" 2. Monitor installation metrics")),console.log(D.gray(" 3. Update module with 'kaven module publish' when ready"))}import v from"chalk";import Oo from"ora";import Ea from"path";import*as Lo from"@inquirer/prompts";import Gt from"fs-extra";import ba from"path";var Ae=[{id:"auth",label:"Auth & Identity",description:"Gest\xE3o de usu\xE1rios, permiss\xF5es e sess\xF5es",models:["User","Role","Capability","AuthSession","AuditLog"],enums:["UserRole"],dependsOn:[]},{id:"billing",label:"Billing",description:"Faturamento, assinaturas e pagamentos",models:["Invoice","Order","Subscription","Plan","Payment","Product"],enums:[],dependsOn:["auth"]},{id:"projects",label:"Projects",description:"Gest\xE3o de projetos e tasks",models:["Project","Task"],enums:["ProjectStatus","TaskStatus","TaskPriority"],dependsOn:["auth"]},{id:"notifications",label:"Notifications",description:"Notifica\xE7\xF5es e prefer\xEAncias de usu\xE1rio",models:["Notification","UserPreference"],enums:[],dependsOn:["auth"]},{id:"marketing-tracking",label:"Marketing Tracking",description:"Observabilidade de an\xFAncios, GTM, GA4 e Meta CAPI",models:["TrackingEvent"],enums:["TrackingSource"],dependsOn:["auth"]}],st=o=>`// [KAVEN_MODULE:${o.toUpperCase()} BEGIN]`,ct=o=>`// [KAVEN_MODULE:${o.toUpperCase()} END]`,xa=/^(\s*)\/\/\s*(.*)$/,Ve=class{schemaPath;constructor(e){this.schemaPath=ba.join(e,"packages","database","prisma","schema.extended.prisma")}async exists(){return Gt.pathExists(this.schemaPath)}get path(){return this.schemaPath}async readSchema(){return Gt.readFile(this.schemaPath,"utf-8")}async writeSchema(e){await Gt.writeFile(this.schemaPath,e,"utf-8")}validateMarkers(e,t){let n=st(t),a=ct(t),r=e.includes(n),i=e.includes(a);if(r&&!i)throw new Error(`Marcador \xF3rf\xE3o detectado: Faltando END para o m\xF3dulo "${t}".`);if(!r&&i)throw new Error(`Marcador \xF3rf\xE3o detectado: Faltando BEGIN para o m\xF3dulo "${t}".`);if(!r&&!i)throw new Error(`O m\xF3dulo "${t}" n\xE3o possui uma se\xE7\xE3o marcada no schema.`);if(e.indexOf(n)>e.indexOf(a))throw new Error(`Marcadores invertidos para o m\xF3dulo "${t}".`)}async getModuleStatus(e){let t=await this.readSchema(),n=st(e.id),a=ct(e.id),r=t.includes(n)&&t.includes(a),i=!1;if(r){let s=this.extractBlock(t,e.id);i=s!==null&&this.isBlockActive(s)}else i=e.models.some(s=>this.isModelActive(t,s));return{id:e.id,label:e.label,description:e.description,models:e.models,dependsOn:e.dependsOn,active:i,hasMarkers:r}}async activateModule(e){let t=await this.readSchema();this.validateMarkers(t,e.id);let n=this.uncommentBlock(t,e.id);await this.writeSchema(n)}async deactivateModule(e){let t=await this.readSchema();this.validateMarkers(t,e.id);let n=this.commentBlock(t,e.id);await this.writeSchema(n)}extractBlock(e,t){let n=st(t),a=ct(t),r=e.split(`
32
32
  `),i=-1,s=-1;for(let c=0;c<r.length;c++)if(r[c].includes(n)&&(i=c),r[c].includes(a)&&i!==-1){s=c;break}return i===-1||s===-1?null:r.slice(i+1,s).join(`
33
33
  `)}isBlockActive(e){return e.split(`
34
34
  `).some(t=>{let n=t.trim();return n.length>0&&!n.startsWith("//")&&!n.startsWith("/*")&&!n.startsWith("*")})}isModelActive(e,t){let n=e.split(`
35
35
  `);for(let a of n)if(a.trim().startsWith(`model ${t}`)&&!a.trimStart().startsWith("//"))return!0;return!1}commentBlock(e,t){let n=st(t),a=ct(t),r=e.split(`
36
36
  `),i=!1,s=[];for(let c of r){if(c.includes(n)){i=!0,s.push(c);continue}if(c.includes(a)){i=!1,s.push(c);continue}if(i){let l=c.trim();l.length===0||l.startsWith("//")?s.push(c):s.push(`// ${c}`)}else s.push(c)}return s.join(`
37
37
  `)}uncommentBlock(e,t){let n=st(t),a=ct(t),r=e.split(`
38
- `),i=!1,s=[];for(let c of r){if(c.includes(n)){i=!0,s.push(c);continue}if(c.includes(a)){i=!1,s.push(c);continue}if(i){let l=c.match(ka);l?s.push(l[1]+l[2]):s.push(c)}else s.push(c)}return s.join(`
39
- `)}};var xa={confirm:Oo.confirm};function Ht(o){return Ae.find(e=>e.id===o.toLowerCase())}function Lo(o,e){if(!o){let t=ba.join(e,"packages","database","prisma","schema.extended.prisma");console.error(v.red(`
40
- Error: Schema not found at: ${t}`)),console.error(v.gray("Make sure you are in the root of a valid Kaven project.")),process.exit(1)}}async function Wt(o,e,t={}){let n=j.getInstance(),a=Date.now();n.capture("cli.module.activate.start",{moduleName:o});let r=e??process.cwd(),i=new Ve(r),s=Do(`Activating module ${o}...`).start();try{let c=await i.exists();s.stop(),Lo(c,r);let l=Ht(o);if(l||(console.error(v.red(`
38
+ `),i=!1,s=[];for(let c of r){if(c.includes(n)){i=!0,s.push(c);continue}if(c.includes(a)){i=!1,s.push(c);continue}if(i){let l=c.match(xa);l?s.push(l[1]+l[2]):s.push(c)}else s.push(c)}return s.join(`
39
+ `)}};var Sa={confirm:Lo.confirm};function Ht(o){return Ae.find(e=>e.id===o.toLowerCase())}function Fo(o,e){if(!o){let t=Ea.join(e,"packages","database","prisma","schema.extended.prisma");console.error(v.red(`
40
+ Error: Schema not found at: ${t}`)),console.error(v.gray("Make sure you are in the root of a valid Kaven project.")),process.exit(1)}}async function Wt(o,e,t={}){let n=j.getInstance(),a=Date.now();n.capture("cli.module.activate.start",{moduleName:o});let r=e??process.cwd(),i=new Ve(r),s=Oo(`Activating module ${o}...`).start();try{let c=await i.exists();s.stop(),Fo(c,r);let l=Ht(o);if(l||(console.error(v.red(`
41
41
  Unknown module: "${o}".`)),console.error(v.gray(`Available modules: ${Ae.map(u=>u.id).join(", ")}`)),process.exit(1)),l.dependsOn.length>0){s.start("Checking dependencies...");let u=[];for(let p of l.dependsOn){let f=Ht(p);if(!f)continue;let y=await i.getModuleStatus(f);u.push(y)}let m=u.filter(p=>!p.active);if(m.length>0)if(s.stop(),t.withDeps)for(let p of m)await Wt(p.id,e,{...t,withDeps:!0});else console.error(v.red(`
42
42
  Inactive dependencies for "${o}": ${m.map(p=>p.id).join(", ")}`)),console.error(v.gray(`Activate them first:
43
43
  ${m.map(p=>` kaven module activate ${p.id}`).join(`
44
44
  `)}`)),console.log(v.gray(`
45
45
  Or use --with-deps to activate them automatically.`)),process.exit(1);s.stop()}if((await i.getModuleStatus(l)).active){console.log(v.yellow(`
46
- Module "${l.label}" is already active.`));return}if(!t.yes&&!await xa.confirm({message:`Activate module "${l.label}"? This will uncomment ${l.models.length} model(s) in the schema.`,default:!0})){console.log(v.gray(`
46
+ Module "${l.label}" is already active.`));return}if(!t.yes&&!await Sa.confirm({message:`Activate module "${l.label}"? This will uncomment ${l.models.length} model(s) in the schema.`,default:!0})){console.log(v.gray(`
47
47
  Activation aborted.`));return}if(t.dryRun){console.log(v.bold(`
48
48
  --- DRY RUN: Activating ${l.label} ---`)),console.log(`Models to uncomment: ${l.models.join(", ")}`),l.enums.length>0&&console.log(`Enums to uncomment: ${l.enums.join(", ")}`);return}s.start(`Uncommenting ${l.label} models in schema...`),await i.activateModule(l),s.succeed(v.green(`
49
- Module ${l.label} activated. ${l.models.length} models added: ${l.models.join(", ")}`)),t.skipMigrate||console.log(v.cyan("\nNext step: Run `pnpm db:generate && pnpm db:migrate` to apply changes.")),n.capture("cli.module.activate.success",{moduleName:l.id,models:l.models.length},Date.now()-a),await n.flush()}catch(c){let l=fe(c);s.fail(v.red(`Failed to activate module: ${l.message}`)),n.capture("cli.module.activate.error",{moduleName:o,error:l.message},Date.now()-a),await n.flush(),process.exit(1)}}async function Fo(o,e,t={}){let n=j.getInstance(),a=Date.now();n.capture("cli.module.deactivate.start",{moduleName:o});let r=e??process.cwd(),i=new Ve(r),s=Do(`Deactivating module ${o}...`).start();try{let c=await i.exists();s.stop(),Lo(c,r);let l=Ht(o);l||(console.error(v.red(`
49
+ Module ${l.label} activated. ${l.models.length} models added: ${l.models.join(", ")}`)),t.skipMigrate||console.log(v.cyan("\nNext step: Run `pnpm db:generate && pnpm db:migrate` to apply changes.")),n.capture("cli.module.activate.success",{moduleName:l.id,models:l.models.length},Date.now()-a),await n.flush()}catch(c){let l=fe(c);s.fail(v.red(`Failed to activate module: ${l.message}`)),n.capture("cli.module.activate.error",{moduleName:o,error:l.message},Date.now()-a),await n.flush(),process.exit(1)}}async function Uo(o,e,t={}){let n=j.getInstance(),a=Date.now();n.capture("cli.module.deactivate.start",{moduleName:o});let r=e??process.cwd(),i=new Ve(r),s=Oo(`Deactivating module ${o}...`).start();try{let c=await i.exists();s.stop(),Fo(c,r);let l=Ht(o);l||(console.error(v.red(`
50
50
  Unknown module: "${o}".`)),console.error(v.gray(`Available modules: ${Ae.map(m=>m.id).join(", ")}`)),process.exit(1)),s.start("Checking reverse dependencies...");let d=[];for(let m of Ae){if(m.id===l.id||!m.dependsOn.includes(l.id))continue;(await i.getModuleStatus(m)).active&&d.push(m.id)}if(d.length>0&&(s.stop(),console.error(v.red(`
51
51
  Cannot deactivate "${o}": the following active modules depend on it:`)),console.error(v.gray(` ${d.join(", ")}`)),console.error(v.gray(`Deactivate them first:
52
52
  ${d.map(m=>` kaven module deactivate ${m}`).join(`
53
53
  `)}`)),process.exit(1)),!(await i.getModuleStatus(l)).active){s.stop(),console.log(v.yellow(`
54
54
  Module "${l.label}" is already inactive.`));return}if(t.dryRun){s.stop(),console.log(v.bold(`
55
55
  --- DRY RUN: Deactivating ${l.label} ---`)),console.log(`Models to comment: ${l.models.join(", ")}`);return}s.start(`Commenting ${l.label} models in schema...`),await i.deactivateModule(l),s.succeed(v.green(`
56
- Module ${l.label} deactivated successfully.`)),t.skipMigrate||console.log(v.cyan("\nNext step: Run `pnpm db:generate && pnpm db:migrate` to apply changes.")),n.capture("cli.module.deactivate.success",{moduleName:l.id},Date.now()-a),await n.flush()}catch(c){let l=fe(c);s.fail(v.red(`Failed to deactivate module: ${l.message}`)),n.capture("cli.module.deactivate.error",{moduleName:o,error:l.message},Date.now()-a),await n.flush(),process.exit(1)}}async function Uo(o){let e=o??process.cwd(),t=new Ve(e),n=await t.exists(),a={module:18,status:10,models:44,deps:20},r=v.bold("Module".padEnd(a.module))+v.bold("Status".padEnd(a.status))+v.bold("Models".padEnd(a.models))+v.bold("Depends on"),i="\u2500".repeat(a.module+a.status+a.models+a.deps);if(console.log(),console.log(v.blue(`Kaven Schema Modules
57
- `)),console.log(r),console.log(v.gray(i)),!n){for(let s of Ae){let c=s.id.padEnd(a.module),l=v.gray("unknown".padEnd(a.status)),d=s.models.join(", ").padEnd(a.models),u=s.dependsOn.length>0?s.dependsOn.join(", "):"\u2014";console.log(`${c}${l}${d}${u}`)}console.log(),console.log(v.yellow("Warning: schema.extended.prisma not found. Status cannot be determined.")),console.log(v.gray("Run this command in the root of a Kaven project to see real status."));return}for(let s of Ae){let c=await t.getModuleStatus(s),l=s.id.padEnd(a.module),d=c.active?"active":"inactive",u=c.active?v.green(d.padEnd(a.status)):v.gray(d.padEnd(a.status)),m=s.models.join(", ").padEnd(a.models),p=s.dependsOn.length>0?s.dependsOn.join(", "):"\u2014";console.log(`${l}${u}${m}${p}`)}console.log(),console.log(v.gray("To activate: kaven module activate <name> | To deactivate: kaven module deactivate <name>"))}import te from"chalk";import No from"ora";import Ea from"open";function Sa(o){return new Promise(e=>setTimeout(e,o))}async function Ca(o,e,t,n,a){let r=Date.now()+t*1e3,i=[5,10,15,20],s=0,c=n;for(;Date.now()<r;){let l=Math.ceil((r-Date.now())/1e3),d=Math.floor(l/60),u=String(l%60).padStart(2,"0");a.text=`Waiting for authorization... (expires in ${d}:${u})`,await Sa(c*1e3);try{let m=await o.pollDeviceToken(e);if(m.status==="success"&&m.tokens)return m.tokens;switch(m.status){case"slow_down":c+=5;break;case"access_denied":throw new Error("Authorization denied by user. Try again with 'kaven auth login'.");case"expired_token":throw new Error("Device code expired. Run 'kaven auth login' again.");case"authorization_pending":s<i.length-1&&(s++,c=i[s]);break}}catch(m){if(m.message.includes("denied")||m.message.includes("expired"))throw m;let p=m;throw p.code==="ECONNREFUSED"||p.code==="ENOTFOUND"?new Error("Network error. Check your connection and try again."):m}}throw new Error("Device code expired. Run 'kaven auth login' again.")}async function Ko(){let o=j.getInstance(),e=Date.now();o.capture("cli.auth.login.start");let t=new J,n=new M;console.log(te.blue(`\u{1F510} Starting authentication flow...
58
- `));let a=No("Requesting device code from marketplace...").start();try{let{device_code:r,user_code:i,verification_uri:s,expires_in:c,interval:l}=await t.requestDeviceCode();a.stop(),console.log(te.yellow(`To complete login, follow these steps:
56
+ Module ${l.label} deactivated successfully.`)),t.skipMigrate||console.log(v.cyan("\nNext step: Run `pnpm db:generate && pnpm db:migrate` to apply changes.")),n.capture("cli.module.deactivate.success",{moduleName:l.id},Date.now()-a),await n.flush()}catch(c){let l=fe(c);s.fail(v.red(`Failed to deactivate module: ${l.message}`)),n.capture("cli.module.deactivate.error",{moduleName:o,error:l.message},Date.now()-a),await n.flush(),process.exit(1)}}async function No(o){let e=o??process.cwd(),t=new Ve(e),n=await t.exists(),a={module:18,status:10,models:44,deps:20},r=v.bold("Module".padEnd(a.module))+v.bold("Status".padEnd(a.status))+v.bold("Models".padEnd(a.models))+v.bold("Depends on"),i="\u2500".repeat(a.module+a.status+a.models+a.deps);if(console.log(),console.log(v.blue(`Kaven Schema Modules
57
+ `)),console.log(r),console.log(v.gray(i)),!n){for(let s of Ae){let c=s.id.padEnd(a.module),l=v.gray("unknown".padEnd(a.status)),d=s.models.join(", ").padEnd(a.models),u=s.dependsOn.length>0?s.dependsOn.join(", "):"\u2014";console.log(`${c}${l}${d}${u}`)}console.log(),console.log(v.yellow("Warning: schema.extended.prisma not found. Status cannot be determined.")),console.log(v.gray("Run this command in the root of a Kaven project to see real status."));return}for(let s of Ae){let c=await t.getModuleStatus(s),l=s.id.padEnd(a.module),d=c.active?"active":"inactive",u=c.active?v.green(d.padEnd(a.status)):v.gray(d.padEnd(a.status)),m=s.models.join(", ").padEnd(a.models),p=s.dependsOn.length>0?s.dependsOn.join(", "):"\u2014";console.log(`${l}${u}${m}${p}`)}console.log(),console.log(v.gray("To activate: kaven module activate <name> | To deactivate: kaven module deactivate <name>"))}import te from"chalk";import Ko from"ora";import Ca from"open";function Pa(o){return new Promise(e=>setTimeout(e,o))}async function $a(o,e,t,n,a){let r=Date.now()+t*1e3,i=[5,10,15,20],s=0,c=n;for(;Date.now()<r;){let l=Math.ceil((r-Date.now())/1e3),d=Math.floor(l/60),u=String(l%60).padStart(2,"0");a.text=`Waiting for authorization... (expires in ${d}:${u})`,await Pa(c*1e3);try{let m=await o.pollDeviceToken(e);if(m.status==="success"&&m.tokens)return m.tokens;switch(m.status){case"slow_down":c+=5;break;case"access_denied":throw new Error("Authorization denied by user. Try again with 'kaven auth login'.");case"expired_token":throw new Error("Device code expired. Run 'kaven auth login' again.");case"authorization_pending":s<i.length-1&&(s++,c=i[s]);break}}catch(m){if(m.message.includes("denied")||m.message.includes("expired"))throw m;let p=m;throw p.code==="ECONNREFUSED"||p.code==="ENOTFOUND"?new Error("Network error. Check your connection and try again."):m}}throw new Error("Device code expired. Run 'kaven auth login' again.")}async function Vo(){let o=j.getInstance(),e=Date.now();o.capture("cli.auth.login.start");let t=new J,n=new M;console.log(te.blue(`\u{1F510} Starting authentication flow...
58
+ `));let a=Ko("Requesting device code from marketplace...").start();try{let{device_code:r,user_code:i,verification_uri:s,expires_in:c,interval:l}=await t.requestDeviceCode();a.stop(),console.log(te.yellow(`To complete login, follow these steps:
59
59
  `)),console.log(te.bold(` Your verification code: ${te.cyan(i)}
60
- `));try{await Ea(s),console.log(te.dim(" \u2713 Browser opened automatically"))}catch{console.log(te.yellow(" Open this URL in your browser:")),console.log(te.underline(` ${s}
61
- `))}let d=No("Waiting for authorization...").start(),u=await Ca(t,r,c,l,d);await n.saveTokens(u),d.succeed(te.green(`Logged in as ${te.bold(u.user.email)}`)),console.log(te.dim(` Tier: ${u.user.tier}`)),console.log(te.gray(`
60
+ `));try{await Ca(s),console.log(te.dim(" \u2713 Browser opened automatically"))}catch{console.log(te.yellow(" Open this URL in your browser:")),console.log(te.underline(` ${s}
61
+ `))}let d=Ko("Waiting for authorization...").start(),u=await $a(t,r,c,l,d);await n.saveTokens(u),d.succeed(te.green(`Logged in as ${te.bold(u.user.email)}`)),console.log(te.dim(` Tier: ${u.user.tier}`)),console.log(te.gray(`
62
62
  Your credentials were saved securely in ~/.kaven/auth.json
63
63
  `)),o.capture("cli.auth.login.success",{tier:u.user.tier},Date.now()-e),await o.flush()}catch(r){o.capture("cli.auth.login.error",{error:r.message},Date.now()-e),await o.flush(),a.fail(te.red("Authentication failed")),console.error(te.red(`
64
64
  Error: ${r.message}
65
- `)),process.exit(1)}}import Xt from"chalk";async function Vo(){let o=new M;try{if(!await o.isAuthenticated()){console.log(Xt.yellow("You are not authenticated."));return}await o.logout(),console.log(Xt.green("Logged out successfully."))}catch{console.error(Xt.red("Error during logout.")),process.exit(1)}}import Me from"chalk";async function Bo(){let o=j.getInstance();o.capture("cli.auth.whoami.start");let e=new M;try{let t=await e.getWhoamiInfo();if(!t){console.log(Me.yellow("You are not authenticated.")),console.log(Me.gray("Use 'kaven auth login' to sign in.")),o.capture("cli.auth.whoami.not_authenticated"),await o.flush();return}o.capture("cli.auth.whoami.authenticated"),console.log(),console.log(` ${Me.bold("Email:")} ${t.email}`),console.log(` ${Me.bold("GitHub:")} ${t.githubId}`),console.log(` ${Me.bold("Tier:")} ${t.tier}`),console.log(` ${Me.bold("Session:")} ${t.sessionExpiry}`),console.log()}catch{console.error(Me.red("Error checking authentication status.")),process.exit(1)}await o.flush()}import L from"chalk";import Pa from"ora";import $a from"cli-table3";function Ia(o){switch(o.toLowerCase()){case"starter":return L.green(o.toLowerCase());case"complete":return L.yellow(o.toLowerCase());case"pro":return L.magenta(o.toLowerCase());case"enterprise":return L.blue(o.toLowerCase());default:return o}}async function qo(o={}){let e=j.getInstance(),t=Date.now();e.capture("cli.marketplace.list.start");let n=new M,a=new J(n),r=o.page??1,i=Math.min(o.limit??20,100),s=o.sort??"newest";await n.isAuthenticated()||console.log(L.yellow("Warning: Not authenticated. Only public modules will be shown. Run: kaven auth login"));let l=Pa("Fetching modules from Marketplace...").start();try{let d=await a.listModules({category:o.category,page:r,pageSize:i,q:void 0});l.stop();let u=d.data,m=d.meta,p=d.total??m?.total??u.length;if(o.json){console.log(JSON.stringify(d,null,2)),e.capture("cli.marketplace.list.success",{count:u.length,json:!0},Date.now()-t),await e.flush();return}if(u.length===0){console.log(L.yellow("No modules found matching your criteria.")),e.capture("cli.marketplace.list.success",{count:0},Date.now()-t),await e.flush();return}let f=new $a({head:[L.white.bold("Slug"),L.white.bold("Name"),L.white.bold("Version"),L.white.bold("Tier"),L.white.bold("Installs")],style:{head:[],border:["gray"]}}),y=[...u];s==="popular"?y.sort((g,I)=>I.installCount-g.installCount):s==="name"&&y.sort((g,I)=>g.name.localeCompare(I.name));for(let g of y)f.push([L.cyan(g.slug),L.white(g.name),L.green(g.latestVersion??"\u2014"),Ia(g.requiredTier??g.tier??""),L.gray(String(g.installCount))]);console.log(L.blue.bold(`
65
+ `)),process.exit(1)}}import Xt from"chalk";async function Bo(){let o=new M;try{if(!await o.isAuthenticated()){console.log(Xt.yellow("You are not authenticated."));return}await o.logout(),console.log(Xt.green("Logged out successfully."))}catch{console.error(Xt.red("Error during logout.")),process.exit(1)}}import Me from"chalk";async function qo(){let o=j.getInstance();o.capture("cli.auth.whoami.start");let e=new M;try{let t=await e.getWhoamiInfo();if(!t){console.log(Me.yellow("You are not authenticated.")),console.log(Me.gray("Use 'kaven auth login' to sign in.")),o.capture("cli.auth.whoami.not_authenticated"),await o.flush();return}o.capture("cli.auth.whoami.authenticated"),console.log(),console.log(` ${Me.bold("Email:")} ${t.email}`),console.log(` ${Me.bold("GitHub:")} ${t.githubId}`),console.log(` ${Me.bold("Tier:")} ${t.tier}`),console.log(` ${Me.bold("Session:")} ${t.sessionExpiry}`),console.log()}catch{console.error(Me.red("Error checking authentication status.")),process.exit(1)}await o.flush()}import L from"chalk";import Ia from"ora";import Aa from"cli-table3";function Ma(o){switch(o.toLowerCase()){case"starter":return L.green(o.toLowerCase());case"complete":return L.yellow(o.toLowerCase());case"pro":return L.magenta(o.toLowerCase());case"enterprise":return L.blue(o.toLowerCase());default:return o}}async function Jo(o={}){let e=j.getInstance(),t=Date.now();e.capture("cli.marketplace.list.start");let n=new M,a=new J(n),r=o.page??1,i=Math.min(o.limit??20,100),s=o.sort??"newest";await n.isAuthenticated()||console.log(L.yellow("Warning: Not authenticated. Only public modules will be shown. Run: kaven auth login"));let l=Ia("Fetching modules from Marketplace...").start();try{let d=await a.listModules({category:o.category,page:r,pageSize:i,q:void 0});l.stop();let u=d.data,m=d.meta,p=d.total??m?.total??u.length;if(o.json){console.log(JSON.stringify(d,null,2)),e.capture("cli.marketplace.list.success",{count:u.length,json:!0},Date.now()-t),await e.flush();return}if(u.length===0){console.log(L.yellow("No modules found matching your criteria.")),e.capture("cli.marketplace.list.success",{count:0},Date.now()-t),await e.flush();return}let f=new Aa({head:[L.white.bold("Slug"),L.white.bold("Name"),L.white.bold("Version"),L.white.bold("Tier"),L.white.bold("Installs")],style:{head:[],border:["gray"]}}),y=[...u];s==="popular"?y.sort((g,I)=>I.installCount-g.installCount):s==="name"&&y.sort((g,I)=>g.name.localeCompare(I.name));for(let g of y)f.push([L.cyan(g.slug),L.white(g.name),L.green(g.latestVersion??"\u2014"),Ma(g.requiredTier??g.tier??""),L.gray(String(g.installCount))]);console.log(L.blue.bold(`
66
66
  Available Marketplace Modules:
67
67
  `)),console.log(f.toString());let P=Math.ceil(p/i),R=(r-1)*i+1,K=Math.min(r*i,p);console.log(L.gray(`
68
- Showing ${R}-${K} of ${p} modules (page ${r}/${P})`)),console.log(L.gray("Use 'kaven marketplace install <slug>' to install a module.")),e.capture("cli.marketplace.list.success",{count:u.length,total:p},Date.now()-t),await e.flush()}catch(d){l.stop(),d instanceof gt&&(console.error(L.red("Could not reach marketplace. Check your connection.")),e.capture("cli.marketplace.list.error",{error:"network_error"},Date.now()-t),await e.flush(),process.exit(1)),e.capture("cli.marketplace.list.error",{error:d.message},Date.now()-t),await e.flush(),console.error(L.red("Error fetching modules from Marketplace.")),console.error(d),process.exit(1)}}import z from"chalk";import Aa from"ora";import je from"fs-extra";import Pt from"path";import ja from"os";import*as Go from"tar";var Jo=["starter","complete","pro","enterprise"];function Yt(o){return(o??"starter").toLowerCase()}function lt(o,e){let t=Yt(o),n=Yt(e),a=Jo.indexOf(t),r=Jo.indexOf(n),i=a>=0?a:0,s=r>=0?r:0;return i>=s}function Zt(o){let e=Yt(o);return{starter:"Starter",complete:"Complete",pro:"Pro",enterprise:"Enterprise"}[e]??o??"Starter"}var Ma={ora:Aa},Ta={x:Go.x};async function Ra(){let o=Pt.join(ja.tmpdir(),"kaven-install-");return je.mkdtemp(o)}function _a(o){return o<1024?`${o} B`:o<1024*1024?`${Math.round(o/1024)} KB`:`${(o/(1024*1024)).toFixed(1)} MB`}async function $t(o,e={}){let t=j.getInstance(),n=Date.now();t.capture("cli.marketplace.install.start",{slug:o});let a=new M,r=new J(a),i=process.cwd(),s;try{s=await a.getValidToken()}catch{console.error(z.red("Authentication required. Run: kaven auth login")),t.capture("cli.marketplace.install.error",{slug:o,error:"not_authenticated"},Date.now()-n),await t.flush(),process.exit(1);return}let c=Ma.ora(`Preparing installation of '${o}'...`).start(),l=null;try{c.text=`Fetching module '${o}' from Marketplace...`;let d;try{d=await r.getModule(o)}catch(A){if(A instanceof vo){c.fail(z.red(`Module '${o}' not found in Marketplace.`)),process.exit(1);return}throw A}let u=d.latestVersion??d.releases?.[0]?.version,m=e.version??u;if(!m){c.fail(z.red(`No published version found for '${o}'.`)),process.exit(1);return}if(!e.skipTierCheck){let ne=(await a.getAuth())?.user?.tier??"starter",ae=d.requiredTier??d.tier;if(!lt(ne,ae)){c.stop(),console.error(z.red(`
69
- \u2717 This module requires a ${Zt(ae)} plan or higher.`)),console.error(z.yellow(` Your current plan: ${Zt(ne)}`)),console.error(z.dim(` Upgrade: https://kaven.site/pricing
70
- `)),t.capture("cli.marketplace.install.error",{slug:o,error:"tier_insufficient",userTier:ne,moduleTier:ae},Date.now()-n),await t.flush(),process.exit(1);return}}let p=new le,f=new be(i,p);if(await f.isModuleInstalled(o)&&!e.force){c.stop(),console.log(z.yellow(`Module '${o}' is already installed. Use --force to overwrite.`)),t.capture("cli.marketplace.install.skipped",{slug:o,reason:"already_installed"},Date.now()-n),await t.flush();return}c.text=`Creating download token for '${o}@${m}'...`;let P=await r.createDownloadToken(o,m);l=await Ra();let R=Pt.join(l,"module.tar.gz"),K=Pt.join(l,"extracted");await je.ensureDir(K),c.text=`Downloading ${o} v${m}...`;let g=await r.resolveUrl(P.downloadUrl),I=await fetch(g);if(!I.ok)throw new Error(`Download failed: ${I.status} ${I.statusText}`);let me=I.headers.get("content-length"),Ye=me?` (${_a(parseInt(me,10))})`:"";if(c.text=`Downloading ${o} v${m}${Ye}...`,!I.body)throw new Error("No response body received for download");let Pe=je.createWriteStream(R),_t=I.body.getReader();if(await new Promise((A,ne)=>{(async()=>{try{let Ie=!0;for(;Ie;){let{done:Dt,value:Ot}=await _t.read();Dt?(Pe.end(),Ie=!1):Pe.write(Ot)||await new Promise(Fn=>Pe.once("drain",Fn))}Pe.once("finish",A),Pe.once("error",ne)}catch(Ie){ne(Ie)}})()}),(await je.stat(R)).size===0)throw new Error("Downloaded file is empty");if(!e.skipVerify){c.text=`Verifying signature for ${o} v${m}...`;let A=await r.getReleaseInfo(o,m);A.checksum&&A.signature&&A.publicKey?(await xt({filePath:R,expectedChecksum:A.checksum,signature:A.signature,publicKeyBase64:A.publicKey}),c.text=`Signature verified for ${o} v${m}`):c.text=`No signature data for ${o} v${m} \u2014 skipping verification`}c.text=`Extracting ${o} v${m}...`,await Ta.x({file:R,cwd:K});let ft=Pt.join(K,"module.json");if(!await je.pathExists(ft))throw new Error(`module.json not found in extracted archive for '${o}'`);let b=await je.readJson(ft);c.text=`Caching baseline for ${o} v${m}...`,await Et(o,m,K,i),c.text=`Installing ${o} v${m}...`,await f.install(b),c.succeed(z.green(`Module '${o}@${m}' installed successfully.`)),console.log(z.gray("Use 'kaven module doctor' to verify the installation.")),t.capture("cli.marketplace.install.success",{slug:o,version:m},Date.now()-n),await t.flush()}catch(d){let u=fe(d);if(c.stop(),u instanceof Ze){console.error(z.red(`Signature verification failed for '${o}': ${u.message}`)),console.error(z.yellow("Use --skip-verify to bypass (not recommended).")),t.capture("cli.marketplace.install.error",{slug:o,error:"signature_verification_failed"},Date.now()-n),await t.flush(),process.exit(1);return}if(u instanceof yo){console.error(z.red(`License required: '${o}' requires a '${u.requiredTier}' license.`)),console.error(z.yellow(`Run: kaven upgrade ${u.requiredTier}`)),t.capture("cli.marketplace.install.error",{slug:o,error:"license_required",tier:u.requiredTier},Date.now()-n),await t.flush(),process.exit(1);return}if(u instanceof wo||u.message.includes("Not authenticated")){console.error(z.red("Authentication required. Run: kaven auth login")),t.capture("cli.marketplace.install.error",{slug:o,error:"auth_error"},Date.now()-n),await t.flush(),process.exit(1);return}if(u instanceof gt){console.error(z.red("Could not reach marketplace. Check your connection.")),t.capture("cli.marketplace.install.error",{slug:o,error:"network_error"},Date.now()-n),await t.flush(),process.exit(1);return}t.capture("cli.marketplace.install.error",{slug:o,error:u.message},Date.now()-n),await t.flush(),c.fail(z.red(`Failed to install module '${o}'.`)),console.error(u),process.exit(1)}finally{l&&await je.remove(l).catch(()=>{})}}import{select as Qt,spinner as Fa,outro as Ua,isCancel as Na,cancel as Ka}from"@clack/prompts";import $ from"picocolors";import{createInstance as Da}from"i18next";var Oa={common:{welcome:"Welcome to Kaven CLI",success:"Success",error:"Error",warning:"Warning",suggestion:"Suggestion",proceed:"Do you want to proceed?",cancelled:"Operation cancelled",exit:"Exit",back:"Back",cancel:"Cancel"},init:{intro:"Bootstrapping a new Kaven project",outro:"Project created successfully! Ready to build.",projectName:"What is the name of your project?",template:"Select a template",withSquad:"Include AIOX Squad (AI Agents)?",installing:"Installing dependencies..."},module:{activate:{title:"Module Activation: {{name}}",models:"Models to be added: {{count}}",envVars:"Env vars to inject: {{count}}",alreadyActive:"Module '{{name}}' is already active."},list:{header:"Kaven Schema Modules"}},doctor:{checking:"Running diagnostics...",allClear:"Your project is healthy!",issuesFound:"Found {{count}} issue(s).",fixSuggestion:"\u{1F4A1} Run 'kaven module doctor --fix' to resolve automatically."},marketplace:{browse:{loadingCategories:"Loading categories...",categoryLoadFailed:"Could not load categories \u2014 showing all modules.",browseByCategory:"Browse by category:",allModules:"All modules",loadingModules:"Loading modules...",errorLoadingModules:"Error loading modules: {{error}}",noModulesFound:"No modules found.",nextPage:"\u2192 Next page",prevPage:"\u2190 Previous page",backToCategories:"\u2191 Back to categories",whatToDo:"What would you like to do?",install:"Install {{name}}",backToList:"Back to module list",sessionEnded:"Browse session ended."}},config:{features:{catalogHeader:"Kaven Framework \u2014 Capability Catalog",capabilitiesTotal:"{{count}} capabilities total",tierPresets:"Tier presets:",tuiHeader:"\u{1F6E1}\uFE0F Kaven Feature Flag Configuration",selectTier:"Select a base tier:",customize:"Customize individual capabilities?",confirmOverwrite:"Seed file already exists. Overwrite?",seedWritten:"Seed file written to: {{path}}",runSeed:"Run pnpm prisma db seed to apply capabilities to your database.",dryRunHeader:"--- DRY RUN: Generated Content ---"}}},La={common:{welcome:"Bem-vindo \xE0 Kaven CLI",success:"Sucesso",error:"Erro",warning:"Aviso",suggestion:"Sugest\xE3o",proceed:"Deseja continuar?",cancelled:"Opera\xE7\xE3o cancelada",exit:"Sair",back:"Voltar",cancel:"Cancelar"},init:{intro:"Iniciando um novo projeto Kaven",outro:"Projeto criado com sucesso! Hora de construir.",projectName:"Qual o nome do seu projeto?",template:"Selecione um template",withSquad:"Incluir AIOX Squad (Agentes de IA)?",installing:"Instalando depend\xEAncias..."},module:{activate:{title:"Ativa\xE7\xE3o de M\xF3dulo: {{name}}",models:"Models a serem adicionados: {{count}}",envVars:"Vari\xE1veis de ambiente: {{count}}",alreadyActive:"O m\xF3dulo '{{name}}' j\xE1 est\xE1 ativo."},list:{header:"M\xF3dulos de Schema Kaven"}},doctor:{checking:"Executando diagn\xF3sticos...",allClear:"Seu projeto est\xE1 saud\xE1vel!",issuesFound:"Encontrados {{count}} problema(s).",fixSuggestion:"\u{1F4A1} Execute 'kaven module doctor --fix' para resolver automaticamente."},marketplace:{browse:{loadingCategories:"Carregando categorias...",categoryLoadFailed:"N\xE3o foi poss\xEDvel carregar categorias \u2014 exibindo todos os m\xF3dulos.",browseByCategory:"Navegar por categoria:",allModules:"Todos os m\xF3dulos",loadingModules:"Carregando m\xF3dulos...",errorLoadingModules:"Erro ao carregar m\xF3dulos: {{error}}",noModulesFound:"Nenhum m\xF3dulo encontrado.",nextPage:"\u2192 Pr\xF3xima p\xE1gina",prevPage:"\u2190 P\xE1gina anterior",backToCategories:"\u2191 Voltar \xE0s categorias",whatToDo:"O que deseja fazer?",install:"Instalar {{name}}",backToList:"Voltar \xE0 lista",sessionEnded:"Sess\xE3o de navega\xE7\xE3o encerrada."}},config:{features:{catalogHeader:"Kaven Framework \u2014 Cat\xE1logo de Capacidades",capabilitiesTotal:"{{count}} capacidades no total",tierPresets:"Presets de tier:",tuiHeader:"\u{1F6E1}\uFE0F Configura\xE7\xE3o de Feature Flags Kaven",selectTier:"Selecione um tier base:",customize:"Personalizar capacidades individualmente?",confirmOverwrite:"Arquivo de seed j\xE1 existe. Sobrescrever?",seedWritten:"Seed escrito em: {{path}}",runSeed:"Execute pnpm prisma db seed para aplicar as capacidades ao banco.",dryRunHeader:"--- DRY RUN: Conte\xFAdo Gerado ---"}}},de=class o{static instance;i18n=Da();constructor(){}static async getInstance(){return o.instance||(o.instance=new o,await o.instance.init()),o.instance}async init(){let e=process.env.KAVEN_LANG||"en";await this.i18n.init({lng:e,fallbackLng:"en",resources:{en:{translation:Oa},"pt-BR":{translation:La}}})}t(e,t){return this.i18n.t(e,t)}async setLanguage(e){await this.i18n.changeLanguage(e)}getI18n(){return this.i18n}};var zo=10;function Ho(o){switch(o.toUpperCase()){case"FREE":return $.dim("[FREE]");case"STARTER":return $.green("[STARTER]");case"COMPLETE":return $.yellow("[COMPLETE]");case"PRO":return $.magenta("[PRO]");case"ENTERPRISE":return $.cyan("[ENTERPRISE]");default:return $.dim(`[${o}]`)}}function eo(o){Na(o)&&(Ka("Cancelled."),process.exit(0))}async function Wo(){let o=await de.getInstance(),e=new M,t=new J(e),n=Fa(),r=(await e.getAuth())?.user?.tier??"starter",i={category:null,page:1,selectedModule:null,screen:"categories"};for(;i.screen!=="exit";){if(i.screen==="categories"){n.start(o.t("marketplace.browse.loadingCategories"));let s=[];try{s=await t.getCategories(),n.stop()}catch{n.stop($.yellow(o.t("marketplace.browse.categoryLoadFailed")))}let c=[{label:o.t("marketplace.browse.allModules"),value:"__all__"},...s.map(d=>({label:d,value:d})),{label:$.dim(o.t("common.exit")),value:"__exit__"}],l=await Qt({message:o.t("marketplace.browse.browseByCategory"),options:c});if(eo(l),l==="__exit__"){i.screen="exit";break}i.category=l==="__all__"?null:l,i.page=1,i.screen="list";continue}if(i.screen==="list"){n.start(o.t("marketplace.browse.loadingModules"));let s=[],c=1;try{let f=await t.listModules({category:i.category||void 0,page:i.page,pageSize:zo});s=f.data,c=Math.ceil(f.total/zo)||1,n.stop()}catch(f){n.stop($.red(o.t("marketplace.browse.errorLoadingModules",{error:f instanceof Error?f.message:String(f)}))),i.screen="categories";continue}if(s.length===0){console.log($.yellow(o.t("marketplace.browse.noModulesFound"))),i.screen="categories";continue}let l=i.category?` [${i.category}]`:"",d=c>1?` (${i.page}/${c})`:"",u=[...s.map(f=>{let y=f.requiredTier??f.tier??"",P=y&&!lt(r,y),R=Ho(y),K=P?$.red(" \u{1F512}"):"";return{label:`${f.name} ${R}${K}`,hint:f.description,value:f.slug}}),...i.page<c?[{label:$.cyan(o.t("marketplace.browse.nextPage")),value:"__next__"}]:[],...i.page>1?[{label:$.cyan(o.t("marketplace.browse.prevPage")),value:"__prev__"}]:[],{label:$.dim(o.t("marketplace.browse.backToCategories")),value:"__back__"},{label:$.dim(o.t("common.exit")),value:"__exit__"}],m=await Qt({message:`Modules${l}${d}:`,options:u});if(eo(m),m==="__exit__"){i.screen="exit";break}if(m==="__back__"){i.screen="categories";continue}if(m==="__next__"){i.page++;continue}if(m==="__prev__"){i.page=Math.max(1,i.page-1);continue}let p=s.find(f=>f.slug===m);p&&(i.selectedModule=p,i.screen="detail");continue}if(i.screen==="detail"&&i.selectedModule){let s=i.selectedModule,c=s.requiredTier??s.tier??"",l=c&&!lt(r,c);console.log(),console.log($.bold(s.name),Ho(c),l?$.red("\u{1F512}"):""),console.log($.dim(`Version: ${s.latestVersion||"latest"}`)),s.installCount!==void 0&&console.log($.dim(`Installs: ${s.installCount.toLocaleString()}`)),s.description&&(console.log(),console.log(s.description)),l&&(console.log(),console.log($.yellow("Requires a higher plan. Upgrade: https://kaven.site/pricing"))),console.log();let d=await Qt({message:o.t("marketplace.browse.whatToDo"),options:[{label:o.t("marketplace.browse.install",{name:s.name}),value:"install"},{label:$.dim(o.t("marketplace.browse.backToList")),value:"back"},{label:$.dim(o.t("common.exit")),value:"exit"}]});if(eo(d),d==="exit"){i.screen="exit";break}if(d==="back"){i.selectedModule=null,i.screen="list";continue}if(d==="install"){console.log(),await $t(s.slug,{force:!1,skipEnv:!1}),i.screen="exit";break}}}Ua($.dim(o.t("marketplace.browse.sessionEnded")))}import ge from"chalk";async function Xo(o=10){let t=await j.getInstance().getRecentEvents(o);if(t.length===0){console.log(ge.yellow(`
68
+ Showing ${R}-${K} of ${p} modules (page ${r}/${P})`)),console.log(L.gray("Use 'kaven marketplace install <slug>' to install a module.")),e.capture("cli.marketplace.list.success",{count:u.length,total:p},Date.now()-t),await e.flush()}catch(d){l.stop(),d instanceof gt&&(console.error(L.red("Could not reach marketplace. Check your connection.")),e.capture("cli.marketplace.list.error",{error:"network_error"},Date.now()-t),await e.flush(),process.exit(1)),e.capture("cli.marketplace.list.error",{error:d.message},Date.now()-t),await e.flush(),console.error(L.red("Error fetching modules from Marketplace.")),console.error(d),process.exit(1)}}import G from"chalk";import ja from"ora";import je from"fs-extra";import Pt from"path";import Ra from"os";import*as Go from"tar";var zo=["starter","complete","pro","enterprise"];function Yt(o){return(o??"starter").toLowerCase()}function lt(o,e){let t=Yt(o),n=Yt(e),a=zo.indexOf(t),r=zo.indexOf(n),i=a>=0?a:0,s=r>=0?r:0;return i>=s}function Zt(o){let e=Yt(o);return{starter:"Starter",complete:"Complete",pro:"Pro",enterprise:"Enterprise"}[e]??o??"Starter"}var Ta={ora:ja},_a={x:Go.x};async function Da(){let o=Pt.join(Ra.tmpdir(),"kaven-install-");return je.mkdtemp(o)}function Oa(o){return o<1024?`${o} B`:o<1024*1024?`${Math.round(o/1024)} KB`:`${(o/(1024*1024)).toFixed(1)} MB`}async function $t(o,e={}){let t=j.getInstance(),n=Date.now();t.capture("cli.marketplace.install.start",{slug:o});let a=new M,r=new J(a),i=process.cwd(),s;try{s=await a.getValidToken()}catch{console.error(G.red("Authentication required. Run: kaven auth login")),t.capture("cli.marketplace.install.error",{slug:o,error:"not_authenticated"},Date.now()-n),await t.flush(),process.exit(1);return}let c=Ta.ora(`Preparing installation of '${o}'...`).start(),l=null;try{c.text=`Fetching module '${o}' from Marketplace...`;let d;try{d=await r.getModule(o)}catch(A){if(A instanceof vo){c.fail(G.red(`Module '${o}' not found in Marketplace.`)),process.exit(1);return}throw A}let u=d.latestVersion??d.releases?.[0]?.version,m=e.version??u;if(!m){c.fail(G.red(`No published version found for '${o}'.`)),process.exit(1);return}if(!e.skipTierCheck){let ne=(await a.getAuth())?.user?.tier??"starter",ae=d.requiredTier??d.tier;if(!lt(ne,ae)){c.stop(),console.error(G.red(`
69
+ \u2717 This module requires a ${Zt(ae)} plan or higher.`)),console.error(G.yellow(` Your current plan: ${Zt(ne)}`)),console.error(G.dim(` Upgrade: https://kaven.site/pricing
70
+ `)),t.capture("cli.marketplace.install.error",{slug:o,error:"tier_insufficient",userTier:ne,moduleTier:ae},Date.now()-n),await t.flush(),process.exit(1);return}}let p=new le,f=new be(i,p);if(await f.isModuleInstalled(o)&&!e.force){c.stop(),console.log(G.yellow(`Module '${o}' is already installed. Use --force to overwrite.`)),t.capture("cli.marketplace.install.skipped",{slug:o,reason:"already_installed"},Date.now()-n),await t.flush();return}c.text=`Creating download token for '${o}@${m}'...`;let P=await r.createDownloadToken(o,m);l=await Da();let R=Pt.join(l,"module.tar.gz"),K=Pt.join(l,"extracted");await je.ensureDir(K),c.text=`Downloading ${o} v${m}...`;let g=await r.resolveUrl(P.downloadUrl),I=await fetch(g);if(!I.ok)throw new Error(`Download failed: ${I.status} ${I.statusText}`);let me=I.headers.get("content-length"),Ye=me?` (${Oa(parseInt(me,10))})`:"";if(c.text=`Downloading ${o} v${m}${Ye}...`,!I.body)throw new Error("No response body received for download");let Pe=je.createWriteStream(R),_t=I.body.getReader();if(await new Promise((A,ne)=>{(async()=>{try{let Ie=!0;for(;Ie;){let{done:Dt,value:Ot}=await _t.read();Dt?(Pe.end(),Ie=!1):Pe.write(Ot)||await new Promise(Un=>Pe.once("drain",Un))}Pe.once("finish",A),Pe.once("error",ne)}catch(Ie){ne(Ie)}})()}),(await je.stat(R)).size===0)throw new Error("Downloaded file is empty");if(!e.skipVerify){c.text=`Verifying signature for ${o} v${m}...`;let A=await r.getReleaseInfo(o,m);A.checksum&&A.signature&&A.publicKey?(await xt({filePath:R,expectedChecksum:A.checksum,signature:A.signature,publicKeyBase64:A.publicKey}),c.text=`Signature verified for ${o} v${m}`):c.text=`No signature data for ${o} v${m} \u2014 skipping verification`}c.text=`Extracting ${o} v${m}...`,await _a.x({file:R,cwd:K});let ft=Pt.join(K,"module.json");if(!await je.pathExists(ft))throw new Error(`module.json not found in extracted archive for '${o}'`);let b=await je.readJson(ft);c.text=`Caching baseline for ${o} v${m}...`,await Et(o,m,K,i),c.text=`Installing ${o} v${m}...`,await f.install(b),c.succeed(G.green(`Module '${o}@${m}' installed successfully.`)),console.log(G.gray("Use 'kaven module doctor' to verify the installation.")),t.capture("cli.marketplace.install.success",{slug:o,version:m},Date.now()-n),await t.flush()}catch(d){let u=fe(d);if(c.stop(),u instanceof Ze){console.error(G.red(`Signature verification failed for '${o}': ${u.message}`)),console.error(G.yellow("Use --skip-verify to bypass (not recommended).")),t.capture("cli.marketplace.install.error",{slug:o,error:"signature_verification_failed"},Date.now()-n),await t.flush(),process.exit(1);return}if(u instanceof yo){console.error(G.red(`License required: '${o}' requires a '${u.requiredTier}' license.`)),console.error(G.yellow(`Run: kaven upgrade ${u.requiredTier}`)),t.capture("cli.marketplace.install.error",{slug:o,error:"license_required",tier:u.requiredTier},Date.now()-n),await t.flush(),process.exit(1);return}if(u instanceof wo||u.message.includes("Not authenticated")){console.error(G.red("Authentication required. Run: kaven auth login")),t.capture("cli.marketplace.install.error",{slug:o,error:"auth_error"},Date.now()-n),await t.flush(),process.exit(1);return}if(u instanceof gt){console.error(G.red("Could not reach marketplace. Check your connection.")),t.capture("cli.marketplace.install.error",{slug:o,error:"network_error"},Date.now()-n),await t.flush(),process.exit(1);return}t.capture("cli.marketplace.install.error",{slug:o,error:u.message},Date.now()-n),await t.flush(),c.fail(G.red(`Failed to install module '${o}'.`)),console.error(u),process.exit(1)}finally{l&&await je.remove(l).catch(()=>{})}}import{select as Qt,spinner as Na,outro as Ka,isCancel as Va,cancel as Ba}from"@clack/prompts";import $ from"picocolors";import{createInstance as La}from"i18next";var Fa={common:{welcome:"Welcome to Kaven CLI",success:"Success",error:"Error",warning:"Warning",suggestion:"Suggestion",proceed:"Do you want to proceed?",cancelled:"Operation cancelled",exit:"Exit",back:"Back",cancel:"Cancel"},init:{intro:"Bootstrapping a new Kaven project",outro:"Project created successfully! Ready to build.",projectName:"What is the name of your project?",template:"Select a template",withSquad:"Include AIOX Squad (AI Agents)?",installing:"Installing dependencies..."},module:{activate:{title:"Module Activation: {{name}}",models:"Models to be added: {{count}}",envVars:"Env vars to inject: {{count}}",alreadyActive:"Module '{{name}}' is already active."},list:{header:"Kaven Schema Modules"}},doctor:{checking:"Running diagnostics...",allClear:"Your project is healthy!",issuesFound:"Found {{count}} issue(s).",fixSuggestion:"\u{1F4A1} Run 'kaven module doctor --fix' to resolve automatically."},marketplace:{browse:{loadingCategories:"Loading categories...",categoryLoadFailed:"Could not load categories \u2014 showing all modules.",browseByCategory:"Browse by category:",allModules:"All modules",loadingModules:"Loading modules...",errorLoadingModules:"Error loading modules: {{error}}",noModulesFound:"No modules found.",nextPage:"\u2192 Next page",prevPage:"\u2190 Previous page",backToCategories:"\u2191 Back to categories",whatToDo:"What would you like to do?",install:"Install {{name}}",backToList:"Back to module list",sessionEnded:"Browse session ended."}},config:{features:{catalogHeader:"Kaven Framework \u2014 Capability Catalog",capabilitiesTotal:"{{count}} capabilities total",tierPresets:"Tier presets:",tuiHeader:"\u{1F6E1}\uFE0F Kaven Feature Flag Configuration",selectTier:"Select a base tier:",customize:"Customize individual capabilities?",confirmOverwrite:"Seed file already exists. Overwrite?",seedWritten:"Seed file written to: {{path}}",runSeed:"Run pnpm prisma db seed to apply capabilities to your database.",dryRunHeader:"--- DRY RUN: Generated Content ---"}}},Ua={common:{welcome:"Bem-vindo \xE0 Kaven CLI",success:"Sucesso",error:"Erro",warning:"Aviso",suggestion:"Sugest\xE3o",proceed:"Deseja continuar?",cancelled:"Opera\xE7\xE3o cancelada",exit:"Sair",back:"Voltar",cancel:"Cancelar"},init:{intro:"Iniciando um novo projeto Kaven",outro:"Projeto criado com sucesso! Hora de construir.",projectName:"Qual o nome do seu projeto?",template:"Selecione um template",withSquad:"Incluir AIOX Squad (Agentes de IA)?",installing:"Instalando depend\xEAncias..."},module:{activate:{title:"Ativa\xE7\xE3o de M\xF3dulo: {{name}}",models:"Models a serem adicionados: {{count}}",envVars:"Vari\xE1veis de ambiente: {{count}}",alreadyActive:"O m\xF3dulo '{{name}}' j\xE1 est\xE1 ativo."},list:{header:"M\xF3dulos de Schema Kaven"}},doctor:{checking:"Executando diagn\xF3sticos...",allClear:"Seu projeto est\xE1 saud\xE1vel!",issuesFound:"Encontrados {{count}} problema(s).",fixSuggestion:"\u{1F4A1} Execute 'kaven module doctor --fix' para resolver automaticamente."},marketplace:{browse:{loadingCategories:"Carregando categorias...",categoryLoadFailed:"N\xE3o foi poss\xEDvel carregar categorias \u2014 exibindo todos os m\xF3dulos.",browseByCategory:"Navegar por categoria:",allModules:"Todos os m\xF3dulos",loadingModules:"Carregando m\xF3dulos...",errorLoadingModules:"Erro ao carregar m\xF3dulos: {{error}}",noModulesFound:"Nenhum m\xF3dulo encontrado.",nextPage:"\u2192 Pr\xF3xima p\xE1gina",prevPage:"\u2190 P\xE1gina anterior",backToCategories:"\u2191 Voltar \xE0s categorias",whatToDo:"O que deseja fazer?",install:"Instalar {{name}}",backToList:"Voltar \xE0 lista",sessionEnded:"Sess\xE3o de navega\xE7\xE3o encerrada."}},config:{features:{catalogHeader:"Kaven Framework \u2014 Cat\xE1logo de Capacidades",capabilitiesTotal:"{{count}} capacidades no total",tierPresets:"Presets de tier:",tuiHeader:"\u{1F6E1}\uFE0F Configura\xE7\xE3o de Feature Flags Kaven",selectTier:"Selecione um tier base:",customize:"Personalizar capacidades individualmente?",confirmOverwrite:"Arquivo de seed j\xE1 existe. Sobrescrever?",seedWritten:"Seed escrito em: {{path}}",runSeed:"Execute pnpm prisma db seed para aplicar as capacidades ao banco.",dryRunHeader:"--- DRY RUN: Conte\xFAdo Gerado ---"}}},de=class o{static instance;i18n=La();constructor(){}static async getInstance(){return o.instance||(o.instance=new o,await o.instance.init()),o.instance}async init(){let e=process.env.KAVEN_LANG||"en";await this.i18n.init({lng:e,fallbackLng:"en",resources:{en:{translation:Fa},"pt-BR":{translation:Ua}}})}t(e,t){return this.i18n.t(e,t)}async setLanguage(e){await this.i18n.changeLanguage(e)}getI18n(){return this.i18n}};var Ho=10;function Wo(o){switch(o.toUpperCase()){case"FREE":return $.dim("[FREE]");case"STARTER":return $.green("[STARTER]");case"COMPLETE":return $.yellow("[COMPLETE]");case"PRO":return $.magenta("[PRO]");case"ENTERPRISE":return $.cyan("[ENTERPRISE]");default:return $.dim(`[${o}]`)}}function eo(o){Va(o)&&(Ba("Cancelled."),process.exit(0))}async function Xo(){let o=await de.getInstance(),e=new M,t=new J(e),n=Na(),r=(await e.getAuth())?.user?.tier??"starter",i={category:null,page:1,selectedModule:null,screen:"categories"};for(;i.screen!=="exit";){if(i.screen==="categories"){n.start(o.t("marketplace.browse.loadingCategories"));let s=[];try{s=await t.getCategories(),n.stop()}catch{n.stop($.yellow(o.t("marketplace.browse.categoryLoadFailed")))}let c=[{label:o.t("marketplace.browse.allModules"),value:"__all__"},...s.map(d=>({label:d,value:d})),{label:$.dim(o.t("common.exit")),value:"__exit__"}],l=await Qt({message:o.t("marketplace.browse.browseByCategory"),options:c});if(eo(l),l==="__exit__"){i.screen="exit";break}i.category=l==="__all__"?null:l,i.page=1,i.screen="list";continue}if(i.screen==="list"){n.start(o.t("marketplace.browse.loadingModules"));let s=[],c=1;try{let f=await t.listModules({category:i.category||void 0,page:i.page,pageSize:Ho});s=f.data,c=Math.ceil(f.total/Ho)||1,n.stop()}catch(f){n.stop($.red(o.t("marketplace.browse.errorLoadingModules",{error:f instanceof Error?f.message:String(f)}))),i.screen="categories";continue}if(s.length===0){console.log($.yellow(o.t("marketplace.browse.noModulesFound"))),i.screen="categories";continue}let l=i.category?` [${i.category}]`:"",d=c>1?` (${i.page}/${c})`:"",u=[...s.map(f=>{let y=f.requiredTier??f.tier??"",P=y&&!lt(r,y),R=Wo(y),K=P?$.red(" \u{1F512}"):"";return{label:`${f.name} ${R}${K}`,hint:f.description,value:f.slug}}),...i.page<c?[{label:$.cyan(o.t("marketplace.browse.nextPage")),value:"__next__"}]:[],...i.page>1?[{label:$.cyan(o.t("marketplace.browse.prevPage")),value:"__prev__"}]:[],{label:$.dim(o.t("marketplace.browse.backToCategories")),value:"__back__"},{label:$.dim(o.t("common.exit")),value:"__exit__"}],m=await Qt({message:`Modules${l}${d}:`,options:u});if(eo(m),m==="__exit__"){i.screen="exit";break}if(m==="__back__"){i.screen="categories";continue}if(m==="__next__"){i.page++;continue}if(m==="__prev__"){i.page=Math.max(1,i.page-1);continue}let p=s.find(f=>f.slug===m);p&&(i.selectedModule=p,i.screen="detail");continue}if(i.screen==="detail"&&i.selectedModule){let s=i.selectedModule,c=s.requiredTier??s.tier??"",l=c&&!lt(r,c);console.log(),console.log($.bold(s.name),Wo(c),l?$.red("\u{1F512}"):""),console.log($.dim(`Version: ${s.latestVersion||"latest"}`)),s.installCount!==void 0&&console.log($.dim(`Installs: ${s.installCount.toLocaleString()}`)),s.description&&(console.log(),console.log(s.description)),l&&(console.log(),console.log($.yellow("Requires a higher plan. Upgrade: https://kaven.site/pricing"))),console.log();let d=await Qt({message:o.t("marketplace.browse.whatToDo"),options:[{label:o.t("marketplace.browse.install",{name:s.name}),value:"install"},{label:$.dim(o.t("marketplace.browse.backToList")),value:"back"},{label:$.dim(o.t("common.exit")),value:"exit"}]});if(eo(d),d==="exit"){i.screen="exit";break}if(d==="back"){i.selectedModule=null,i.screen="list";continue}if(d==="install"){console.log(),await $t(s.slug,{force:!1,skipEnv:!1}),i.screen="exit";break}}}Ka($.dim(o.t("marketplace.browse.sessionEnded")))}import ge from"chalk";async function Yo(o=10){let t=await j.getInstance().getRecentEvents(o);if(t.length===0){console.log(ge.yellow(`
71
71
  Nenhum evento de telemetria encontrado localmente.
72
72
  `));return}console.log(ge.blue.bold(`
73
73
  \u{1F4CA} \xDAltimos ${t.length} eventos de telemetria:
74
74
  `)),t.forEach(n=>{let a=ge.gray(`[${new Date(n.timestamp).toLocaleTimeString()}]`),r=n.event.includes("error")?ge.red("\u2716"):ge.green("\u2714"),i=n.duration?ge.yellow(` (${n.duration}ms)`):"";console.log(`${a} ${r} ${ge.cyan(n.event)}${i}`),n.metadata&&Object.keys(n.metadata).length>0&&console.log(ge.gray(` > ${JSON.stringify(n.metadata)}`))}),console.log(ge.gray(`
75
75
  Log local: ~/.kaven/telemetry.log
76
- `))}import{Command as qa}from"commander";import{Command as Ba}from"commander";import ue from"chalk";import to from"fs/promises";import Zo from"path";import Qo from"os";var Yo=Zo.join(Qo.homedir(),".kaven","cache","licenses.json"),Va=3600*1e3,It=class{cacheDir=Zo.join(Qo.homedir(),".kaven","cache");isValidFormat(e){return/^KAVEN-(STARTER|COMPLETE|PRO|ENTERPRISE)-[A-Z0-9]{8}-[A-Z0-9]{2}$/.test(e)}async readCache(){try{let e=await to.readFile(Yo,"utf-8");return JSON.parse(e)}catch{return{}}}async writeCache(e){await to.mkdir(this.cacheDir,{recursive:!0}),await to.writeFile(Yo,JSON.stringify(e,null,2),"utf-8")}isCacheValid(e){return Date.now()-e.validatedAt<Va}async getCached(e){let n=(await this.readCache())[e];return n&&this.isCacheValid(n)?n:null}async setCached(e){let t=await this.readCache();t[e.key]={...e,validatedAt:Date.now()},await this.writeCache(t)}async validateLicense(e,t){let n=await this.getCached(e);if(n)return{valid:!0,tier:n.tier,expiresAt:n.expiresAt,source:"cache"};if(!this.isValidFormat(e))throw new Error("Invalid license key format");let{MarketplaceClient:a}=await import("./MarketplaceClient-YCFH2VU4.js"),i=await new a().validateLicense(e,t);return await this.setCached({key:e,tier:i.tier,expiresAt:i.expiresAt,validatedAt:Date.now()}),{...i,source:"api"}}async getLicenseStatus(e){let{MarketplaceClient:t}=await import("./MarketplaceClient-YCFH2VU4.js");return new t().getLicenseStatus(e)}tierLevel(e){return{STARTER:1,COMPLETE:2,PRO:3,ENTERPRISE:4}[e.toUpperCase()]??0}userHasRequiredTier(e,t){return this.tierLevel(e)>=this.tierLevel(t)}};function en(){return new Ba("status").description("Show license status and tier information").argument("[license-key]","License key to check (uses stored license if omitted)").action(async o=>{let e=new It,t=o??process.env.KAVEN_LICENSE_KEY;t||(console.error(ue.red("\u2717 No license key provided. Pass as argument or set KAVEN_LICENSE_KEY.")),process.exit(1));try{console.log(ue.dim("Checking license status..."));let n=await e.getLicenseStatus(t);if(console.log(`
76
+ `))}import{Command as za}from"commander";import{Command as Ja}from"commander";import ue from"chalk";import to from"fs/promises";import Qo from"path";import en from"os";var Zo=Qo.join(en.homedir(),".kaven","cache","licenses.json"),qa=3600*1e3,It=class{cacheDir=Qo.join(en.homedir(),".kaven","cache");isValidFormat(e){return/^KAVEN-(STARTER|COMPLETE|PRO|ENTERPRISE)-[A-Z0-9]{8}-[A-Z0-9]{2}$/.test(e)}async readCache(){try{let e=await to.readFile(Zo,"utf-8");return JSON.parse(e)}catch{return{}}}async writeCache(e){await to.mkdir(this.cacheDir,{recursive:!0}),await to.writeFile(Zo,JSON.stringify(e,null,2),"utf-8")}isCacheValid(e){return Date.now()-e.validatedAt<qa}async getCached(e){let n=(await this.readCache())[e];return n&&this.isCacheValid(n)?n:null}async setCached(e){let t=await this.readCache();t[e.key]={...e,validatedAt:Date.now()},await this.writeCache(t)}async validateLicense(e,t){let n=await this.getCached(e);if(n)return{valid:!0,tier:n.tier,expiresAt:n.expiresAt,source:"cache"};if(!this.isValidFormat(e))throw new Error("Invalid license key format");let{MarketplaceClient:a}=await import("./MarketplaceClient-XMLIXDWN.js"),i=await new a().validateLicense(e,t);return await this.setCached({key:e,tier:i.tier,expiresAt:i.expiresAt,validatedAt:Date.now()}),{...i,source:"api"}}async getLicenseStatus(e){let{MarketplaceClient:t}=await import("./MarketplaceClient-XMLIXDWN.js");return new t().getLicenseStatus(e)}tierLevel(e){return{STARTER:1,COMPLETE:2,PRO:3,ENTERPRISE:4}[e.toUpperCase()]??0}userHasRequiredTier(e,t){return this.tierLevel(e)>=this.tierLevel(t)}};function tn(){return new Ja("status").description("Show license status and tier information").argument("[license-key]","License key to check (uses stored license if omitted)").action(async o=>{let e=new It,t=o??process.env.KAVEN_LICENSE_KEY;t||(console.error(ue.red("\u2717 No license key provided. Pass as argument or set KAVEN_LICENSE_KEY.")),process.exit(1));try{console.log(ue.dim("Checking license status..."));let n=await e.getLicenseStatus(t);if(console.log(`
77
77
  `+ue.bold("License Status")+`
78
- `),console.log(` Key: ${ue.dim(n.key.substring(0,16)+"...")}`),console.log(` Tier: ${ue.magenta(n.tier)}`),n.expiresAt){let a=n.daysUntilExpiry,r=a!==null&&a<30?ue.red:ue.green;console.log(` Expires: ${r(n.expiresAt)}${a!==null?ue.dim(` (${a} days)`):""}`)}else console.log(` Expires: ${ue.green("Never")}`);console.log()}catch(n){let a=n instanceof Error?n.message:"Failed to check license status";console.error(ue.red("\u2717 "+a)),process.exit(1)}})}function tn(){let o=new qa("license").description("Manage Kaven licenses");return o.addCommand(en()),o}import we from"path";import ye from"fs-extra";import{fileURLToPath as pn}from"url";import{intro as tr,outro as or,text as jt,select as dn,confirm as un,spinner as nr,note as ar,isCancel as rr,cancel as ro}from"@clack/prompts";import Y from"picocolors";import x from"fs-extra";import ce from"path";import{spawn as Ja}from"child_process";var Ga="https://github.com/kaven-co/kaven-template.git",za="https://github.com/bychrisr/kaven-squad";function Te(o,e,t,n={}){let a=typeof n=="function"?{onData:n}:n,{onData:r,timeoutMs:i,env:s}=a;return new Promise((c,l)=>{let d=Ja(o,e,{cwd:t,stdio:r?"pipe":"inherit",env:{...process.env,...s}}),u;i&&(u=setTimeout(()=>{d.kill("SIGKILL"),c(1)},i)),r&&d.stdout&&d.stdout.on("data",m=>r(m.toString())),r&&d.stderr&&d.stderr.on("data",m=>r(m.toString())),d.on("error",m=>{clearTimeout(u),l(m)}),d.on("close",m=>{clearTimeout(u),c(m??0)})})}var Be=class{validateName(e){return!e||e.trim().length===0?{valid:!1,reason:"Project name cannot be empty"}:/\s/.test(e)?{valid:!1,reason:"Project name cannot contain spaces"}:/^[a-z0-9-]+$/.test(e)?{valid:!0}:{valid:!1,reason:"Project name must only contain lowercase letters, numbers, and hyphens"}}async cloneTemplate(e,t){let n=t||Ga;if(console.log(`[INIT] Clone Source: ${n}`),console.log(`[INIT] Target Dir: ${e}`),await x.pathExists(n)&&(n.startsWith("/")||n.startsWith("./")||n.startsWith("../"))){console.log("[INIT] Local Path Detected. Copying..."),await x.copy(n,e,{filter:i=>!i.includes("node_modules")&&!i.includes(".git")&&!i.includes(".turbo")}),console.log("[INIT] Local Copy Done.");return}let a=null;if(await x.pathExists(e)){let i=await x.readdir(e);i.length===1&&i[0]===".kaven"&&(a=`${e}__kaven_state_tmp`,await x.move(ce.join(e,".kaven"),a),await x.remove(e))}let r=await Te("git",["clone","--depth","1",n,e],process.cwd());if(a&&await x.pathExists(a)&&(await x.ensureDir(e),await x.move(a,ce.join(e,".kaven"),{overwrite:!0})),r!==0)throw new Error(`git clone failed with exit code ${r}`)}async removeGitDir(e){let t=ce.join(e,".git");await x.pathExists(t)&&await x.remove(t)}async replacePlaceholders(e,t){let n={"{{PROJECT_NAME}}":t.projectName,"{{DATABASE_URL}}":t.dbUrl,"{{DEFAULT_LOCALE}}":t.locale,"{{DEFAULT_CURRENCY}}":t.currency},a=["package.json",".env.example","packages/database/prisma/schema.prisma","apps/api/package.json","apps/admin/package.json","apps/tenant/package.json","docs/architecture/tech-stack.md","docs/architecture/source-tree.md","docs/architecture/coding-standards.md"];for(let i of a){let s=ce.join(e,i);if(!await x.pathExists(s))continue;let c=await x.readFile(s,"utf-8");for(let[l,d]of Object.entries(n))c=c.split(l).join(d);await x.writeFile(s,c,"utf-8")}let r=ce.join(e,"package.json");if(await x.pathExists(r)){let i=await x.readJson(r);i.name!==t.projectName&&(i.name=t.projectName,await x.writeJson(r,i,{spaces:2}))}}async runInstall(e){let t=await Te("pnpm",["install"],e);if(t!==0)throw new Error(`pnpm install failed with exit code ${t}`)}async initGit(e){await Te("git",["init"],e),await Te("git",["add","."],e),await Te("git",["commit","-m","chore: initial kaven setup"],e)}async installSquad(e){let t=ce.join(e,"squads"),n=ce.join(t,"kaven-squad");if(await x.pathExists(n))return{installed:!1,reason:"already-exists"};await x.ensureDir(t);let a=await Te("git",["clone","--depth","1",za,n],process.cwd(),{env:{GIT_TERMINAL_PROMPT:"0"},timeoutMs:3e4});if(a!==0)return await x.pathExists(n)&&await x.remove(n),{installed:!1,reason:`git clone exited with code ${a}`};let r=ce.join(n,".git");return await x.pathExists(r)&&await x.remove(r),{installed:!0}}async installAIOXCore(e){if(parseInt(process.version.slice(1).split(".")[0],10)<18)return{installed:!1,reason:`Node >= 18 required (current: ${process.version})`};let n=ce.join(e,"squads","kaven-squad");if(!await x.pathExists(n))return{installed:!1,reason:"kaven-squad not found \u2014 run squad install first"};if((await x.readdir(n)).length===0)return{installed:!1,reason:"kaven-squad directory is empty \u2014 squad install may have failed"};let r=ce.join(e,".aiox-core");if(await x.pathExists(r))return{installed:!0,reason:"already-installed"};let i=await Te("npx",["aiox-core@latest","install","--quiet"],e,{timeoutMs:12e4});return i!==0?{installed:!1,reason:`npx aiox-core exited with code ${i}`}:{installed:!0}}async healthCheck(e){let t=[],n=["package.json",".env.example","packages/database/prisma/schema.prisma"];for(let r of n)await x.pathExists(ce.join(e,r))||t.push(`Missing required file: ${r}`);return await x.pathExists(ce.join(e,"node_modules"))||t.push("Dependencies not installed. Run: pnpm install"),{healthy:t.length===0,issues:t}}};import Re from"path";import he from"fs-extra";import Ha from"os";import on from"js-yaml";import{z as T}from"zod";var At=Re.join(Ha.homedir(),".kaven"),oo=Re.join(At,"config.json"),nn="kaven-config.yaml",an=T.object({registry:T.string().url().default("https://marketplace.kaven.site"),telemetry:T.boolean().default(!0),theme:T.enum(["light","dark"]).default("dark"),language:T.enum(["en","pt-BR"]).default("en"),customRegistry:T.string().url().optional(),projectId:T.string().uuid().optional(),activeModules:T.array(T.string()).default([]),capabilities:T.record(T.string(),T.union([T.string(),T.boolean(),T.number()])).default({}),lastLogin:T.string().datetime().optional(),projectDefaults:T.object({dbUrl:T.string().optional(),emailProvider:T.enum(["postmark","resend","ses","smtp"]).optional(),locale:T.string().optional(),currency:T.string().optional()}).optional()}),no=class{globalConfig={};projectConfig={};currentConfig;constructor(){this.currentConfig=an.parse({})}async initialize(){if(await he.ensureDir(At),await he.pathExists(oo))try{this.globalConfig=await he.readJson(oo)}catch{this.globalConfig={}}let e=this.findProjectRoot();if(e){let t=Re.join(e,nn);if(await he.pathExists(t))try{let n=await he.readFile(t,"utf-8");this.projectConfig=on.load(n)}catch{this.projectConfig={}}}this.resolve()}resolve(){this.currentConfig=an.parse({...this.globalConfig,...this.projectConfig})}findProjectRoot(){let e=process.cwd();for(;e!==Re.parse(e).root;){if(he.existsSync(Re.join(e,"package.json")))return e;e=Re.dirname(e)}return null}get(e){let t=`KAVEN_${e.toUpperCase()}`;if(process.env[t]!==void 0){let n=process.env[t];return typeof this.currentConfig[e]=="boolean"?n==="true":typeof this.currentConfig[e]=="number"?Number(n):n}return this.currentConfig[e]}async set(e,t,n="project"){n==="project"?(this.projectConfig[e]=t,await this.persistProject()):(this.globalConfig[e]=t,await this.persistGlobal()),this.resolve()}async persistGlobal(){await he.ensureDir(At),await he.writeJson(oo,this.globalConfig,{spaces:2})}async persistProject(){let e=this.findProjectRoot();if(!e)return;let t=Re.join(e,nn),n=on.dump(this.projectConfig,{lineWidth:120,noRefs:!0});await he.writeFile(t,n,"utf-8")}async reset(){this.globalConfig={},this.projectConfig={},await this.persistGlobal(),await this.persistProject(),this.resolve()}getRegistry(){return this.currentConfig.customRegistry||this.currentConfig.registry}getAll(){return{...this.currentConfig}}getProjectRoot(){return this.findProjectRoot()}getConfigDir(){return At}},q=new no;import Ee from"picocolors";var Wa=`
78
+ `),console.log(` Key: ${ue.dim(n.key.substring(0,16)+"...")}`),console.log(` Tier: ${ue.magenta(n.tier)}`),n.expiresAt){let a=n.daysUntilExpiry,r=a!==null&&a<30?ue.red:ue.green;console.log(` Expires: ${r(n.expiresAt)}${a!==null?ue.dim(` (${a} days)`):""}`)}else console.log(` Expires: ${ue.green("Never")}`);console.log()}catch(n){let a=n instanceof Error?n.message:"Failed to check license status";console.error(ue.red("\u2717 "+a)),process.exit(1)}})}function on(){let o=new za("license").description("Manage Kaven licenses");return o.addCommand(tn()),o}import we from"path";import ye from"fs-extra";import{fileURLToPath as mn}from"url";import{intro as nr,outro as ar,text as jt,select as un,confirm as pn,spinner as rr,note as ir,isCancel as sr,cancel as ro}from"@clack/prompts";import Y from"picocolors";import x from"fs-extra";import ce from"path";import{spawn as Ga}from"child_process";var Ha="https://github.com/kaven-co/kaven-template.git",Wa="https://github.com/bychrisr/kaven-squad";function Te(o,e,t,n={}){let a=typeof n=="function"?{onData:n}:n,{onData:r,timeoutMs:i,env:s}=a;return new Promise((c,l)=>{let d=Ga(o,e,{cwd:t,stdio:r?"pipe":"inherit",env:{...process.env,...s}}),u;i&&(u=setTimeout(()=>{d.kill("SIGKILL"),c(1)},i)),r&&d.stdout&&d.stdout.on("data",m=>r(m.toString())),r&&d.stderr&&d.stderr.on("data",m=>r(m.toString())),d.on("error",m=>{clearTimeout(u),l(m)}),d.on("close",m=>{clearTimeout(u),c(m??0)})})}var Be=class{validateName(e){return!e||e.trim().length===0?{valid:!1,reason:"Project name cannot be empty"}:/\s/.test(e)?{valid:!1,reason:"Project name cannot contain spaces"}:/^[a-z0-9-]+$/.test(e)?{valid:!0}:{valid:!1,reason:"Project name must only contain lowercase letters, numbers, and hyphens"}}async cloneTemplate(e,t){let n=t||Ha;if(console.log(`[INIT] Clone Source: ${n}`),console.log(`[INIT] Target Dir: ${e}`),await x.pathExists(n)&&(n.startsWith("/")||n.startsWith("./")||n.startsWith("../"))){console.log("[INIT] Local Path Detected. Copying..."),await x.copy(n,e,{filter:i=>!i.includes("node_modules")&&!i.includes(".git")&&!i.includes(".turbo")}),console.log("[INIT] Local Copy Done.");return}let a=null;if(await x.pathExists(e)){let i=await x.readdir(e);i.length===1&&i[0]===".kaven"&&(a=`${e}__kaven_state_tmp`,await x.move(ce.join(e,".kaven"),a),await x.remove(e))}let r=await Te("git",["clone","--depth","1",n,e],process.cwd());if(a&&await x.pathExists(a)&&(await x.ensureDir(e),await x.move(a,ce.join(e,".kaven"),{overwrite:!0})),r!==0)throw new Error(`git clone failed with exit code ${r}`)}async removeGitDir(e){let t=ce.join(e,".git");await x.pathExists(t)&&await x.remove(t)}async replacePlaceholders(e,t){let n={"{{PROJECT_NAME}}":t.projectName,"{{DATABASE_URL}}":t.dbUrl,"{{DEFAULT_LOCALE}}":t.locale,"{{DEFAULT_CURRENCY}}":t.currency},a=["package.json",".env.example","packages/database/prisma/schema.prisma","apps/api/package.json","apps/admin/package.json","apps/tenant/package.json","docs/architecture/tech-stack.md","docs/architecture/source-tree.md","docs/architecture/coding-standards.md"];for(let i of a){let s=ce.join(e,i);if(!await x.pathExists(s))continue;let c=await x.readFile(s,"utf-8");for(let[l,d]of Object.entries(n))c=c.split(l).join(d);await x.writeFile(s,c,"utf-8")}let r=ce.join(e,"package.json");if(await x.pathExists(r)){let i=await x.readJson(r);i.name!==t.projectName&&(i.name=t.projectName,await x.writeJson(r,i,{spaces:2}))}}async runInstall(e){let t=await Te("pnpm",["install"],e);if(t!==0)throw new Error(`pnpm install failed with exit code ${t}`)}async initGit(e){await Te("git",["init"],e),await Te("git",["add","."],e),await Te("git",["commit","-m","chore: initial kaven setup"],e)}async installSquad(e){let t=ce.join(e,"squads"),n=ce.join(t,"kaven-squad");if(await x.pathExists(n))return{installed:!1,reason:"already-exists"};await x.ensureDir(t);let a=await Te("git",["clone","--depth","1",Wa,n],process.cwd(),{env:{GIT_TERMINAL_PROMPT:"0"},timeoutMs:3e4});if(a!==0)return await x.pathExists(n)&&await x.remove(n),{installed:!1,reason:`git clone exited with code ${a}`};let r=ce.join(n,".git");return await x.pathExists(r)&&await x.remove(r),{installed:!0}}async installAIOXCore(e){if(parseInt(process.version.slice(1).split(".")[0],10)<18)return{installed:!1,reason:`Node >= 18 required (current: ${process.version})`};let n=ce.join(e,"squads","kaven-squad");if(!await x.pathExists(n))return{installed:!1,reason:"kaven-squad not found \u2014 run squad install first"};if((await x.readdir(n)).length===0)return{installed:!1,reason:"kaven-squad directory is empty \u2014 squad install may have failed"};let r=ce.join(e,".aiox-core");if(await x.pathExists(r))return{installed:!0,reason:"already-installed"};let i=await Te("npx",["aiox-core@latest","install","--quiet"],e,{timeoutMs:12e4});return i!==0?{installed:!1,reason:`npx aiox-core exited with code ${i}`}:{installed:!0}}async healthCheck(e){let t=[],n=["package.json",".env.example","packages/database/prisma/schema.prisma"];for(let r of n)await x.pathExists(ce.join(e,r))||t.push(`Missing required file: ${r}`);return await x.pathExists(ce.join(e,"node_modules"))||t.push("Dependencies not installed. Run: pnpm install"),{healthy:t.length===0,issues:t}}};import Re from"path";import he from"fs-extra";import Xa from"os";import nn from"js-yaml";import{z as T}from"zod";var At=Re.join(Xa.homedir(),".kaven"),oo=Re.join(At,"config.json"),an="kaven-config.yaml",rn=T.object({registry:T.string().url().default("https://marketplace.kaven.site"),telemetry:T.boolean().default(!0),theme:T.enum(["light","dark"]).default("dark"),language:T.enum(["en","pt-BR"]).default("en"),customRegistry:T.string().url().optional(),projectId:T.string().uuid().optional(),activeModules:T.array(T.string()).default([]),capabilities:T.record(T.string(),T.union([T.string(),T.boolean(),T.number()])).default({}),lastLogin:T.string().datetime().optional(),projectDefaults:T.object({dbUrl:T.string().optional(),emailProvider:T.enum(["postmark","resend","ses","smtp"]).optional(),locale:T.string().optional(),currency:T.string().optional()}).optional()}),no=class{globalConfig={};projectConfig={};currentConfig;constructor(){this.currentConfig=rn.parse({})}async initialize(){if(await he.ensureDir(At),await he.pathExists(oo))try{this.globalConfig=await he.readJson(oo)}catch{this.globalConfig={}}let e=this.findProjectRoot();if(e){let t=Re.join(e,an);if(await he.pathExists(t))try{let n=await he.readFile(t,"utf-8");this.projectConfig=nn.load(n)}catch{this.projectConfig={}}}this.resolve()}resolve(){this.currentConfig=rn.parse({...this.globalConfig,...this.projectConfig})}findProjectRoot(){let e=process.cwd();for(;e!==Re.parse(e).root;){if(he.existsSync(Re.join(e,"package.json")))return e;e=Re.dirname(e)}return null}get(e){let t=`KAVEN_${e.toUpperCase()}`;if(process.env[t]!==void 0){let n=process.env[t];return typeof this.currentConfig[e]=="boolean"?n==="true":typeof this.currentConfig[e]=="number"?Number(n):n}return this.currentConfig[e]}async set(e,t,n="project"){n==="project"?(this.projectConfig[e]=t,await this.persistProject()):(this.globalConfig[e]=t,await this.persistGlobal()),this.resolve()}async persistGlobal(){await he.ensureDir(At),await he.writeJson(oo,this.globalConfig,{spaces:2})}async persistProject(){let e=this.findProjectRoot();if(!e)return;let t=Re.join(e,an),n=nn.dump(this.projectConfig,{lineWidth:120,noRefs:!0});await he.writeFile(t,n,"utf-8")}async reset(){this.globalConfig={},this.projectConfig={},await this.persistGlobal(),await this.persistProject(),this.resolve()}getRegistry(){return this.currentConfig.customRegistry||this.currentConfig.registry}getAll(){return{...this.currentConfig}}getProjectRoot(){return this.findProjectRoot()}getConfigDir(){return At}},q=new no;import Ee from"picocolors";var Ya=`
79
79
  \u2588\u2588\u2557 \u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2557 \u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2588\u2557 \u2588\u2588\u2557
80
80
  \u2588\u2588\u2551 \u2588\u2588\u2554\u255D\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255D\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2551
81
81
  \u2588\u2588\u2588\u2588\u2588\u2554\u255D \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2551\u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2554\u2588\u2588\u2557 \u2588\u2588\u2551
82
82
  \u2588\u2588\u2554\u2550\u2588\u2588\u2557 \u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2551\u255A\u2588\u2588\u2557 \u2588\u2588\u2554\u255D\u2588\u2588\u2554\u2550\u2550\u255D \u2588\u2588\u2551\u255A\u2588\u2588\u2557\u2588\u2588\u2551
83
83
  \u2588\u2588\u2551 \u2588\u2588\u2557\u2588\u2588\u2551 \u2588\u2588\u2551 \u255A\u2588\u2588\u2588\u2588\u2554\u255D \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2551 \u255A\u2588\u2588\u2588\u2588\u2551
84
84
  \u255A\u2550\u255D \u255A\u2550\u255D\u255A\u2550\u255D \u255A\u2550\u255D \u255A\u2550\u2550\u2550\u255D \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u255D\u255A\u2550\u255D \u255A\u2550\u2550\u2550\u255D
85
- `;function rn(){return`
86
- ${Wa.split(`
85
+ `;function sn(){return`
86
+ ${Ya.split(`
87
87
  `).map(t=>Ee.cyan(t)).join(`
88
88
  `)}
89
89
  ${Ee.dim(" The Premium Framework Orchestrator for SaaS")}
90
- `}var Nl={primary:Ee.cyan,secondary:Ee.magenta,success:Ee.green,warning:Ee.yellow,error:Ee.red,dim:Ee.dim};import*as Xa from"child_process";import ao from"fs-extra";import*as Mt from"path";import Za from"ora";import sn from"chalk";var Ya={...Xa},Qa={ora:Za};async function dt(o,e={}){if(e.skipAiox)return;let t=Mt.join(o,".aiox-core");if(!ao.existsSync(t)&&!ao.existsSync(Mt.join(o,".aiox")))return;let n=Qa.ora("Bootstrapping AIOX environment...").start();try{let a=ao.existsSync(Mt.join(o,".aiox-core/bin/aiox.js"))?".aiox-core/bin/aiox.js":".aiox/bin/aiox.js";Ya.execSync(`node ${a} devops environment-bootstrap --quiet`,{cwd:o,stdio:"pipe",timeout:6e4}),n.succeed("AIOX environment bootstrapped")}catch(a){n.warn("AIOX environment bootstrap failed"),console.log(sn.yellow(" \u26A0 Run manually inside the project: kaven aiox bootstrap")),process.env.KAVEN_DEBUG&&console.error(sn.gray(` Error: ${a.message}`))}}import qe from"fs-extra";import cn from"path";var er=".kaven/init-state.json",ln=["clone","configure","install","git","squad","aiox-core","env-bootstrap"],ut=class o{constructor(e){this.projectDir=e;this.statePath=cn.join(e,er)}statePath;state=null;static async findIncomplete(e){let t=new o(e);return!await qe.pathExists(t.statePath)||(await t.load(),t.isComplete())?null:t}async load(){return this.state=await qe.readJson(this.statePath),this.state}async create(e,t,n){let a={clone:"pending",configure:"pending",install:"pending",git:"pending",squad:"pending","aiox-core":"pending","env-bootstrap":"pending"};n.skipInstall&&(a.install="skipped"),n.skipGit&&(a.git="skipped"),n.withSquad||(a.squad="skipped",a["aiox-core"]="skipped"),n.skipAiox&&(a["env-bootstrap"]="skipped"),this.state={version:"1",projectName:e,answers:t,options:n,steps:a,startedAt:new Date().toISOString(),updatedAt:new Date().toISOString()},await qe.ensureDir(cn.dirname(this.statePath)),await this.persist()}async markStep(e,t){if(!this.state)throw new Error("State not initialized");this.state.steps[e]=t,this.state.updatedAt=new Date().toISOString(),await this.persist()}getState(){if(!this.state)throw new Error("State not loaded");return this.state}getResumePoint(){return this.state?ln.find(e=>this.state.steps[e]==="pending"||this.state.steps[e]==="failed")??null:null}isComplete(){return this.state?ln.every(e=>this.state.steps[e]==="done"||this.state.steps[e]==="skipped"):!1}async cleanup(){await qe.pathExists(this.statePath)&&await qe.remove(this.statePath)}async persist(){await qe.writeJson(this.statePath,this.state,{spaces:2})}};function Se(o){rr(o)&&(ro("Operation cancelled."),process.exit(0))}async function X(o,e,t,n,a={}){let r=nr();r.start(t);try{return await n(),await o.markStep(e,"done"),r.stop(Y.green(`${t} \u2713`)),!0}catch(i){await o.markStep(e,"failed");let s=i instanceof Error?i.message:String(i);return r.stop(Y.red(`${t} \u2717`)),a.fatal&&(ro(s),process.exit(1)),console.log(Y.dim(` Error: ${s}`)),!1}}async function mn(o,e){let t=await de.getInstance(),n=new Be,a=await dn({message:"\u{1F310} Select Language / Selecione o Idioma:",options:[{label:"English",value:"en"},{label:"Portugu\xEAs (Brasil)",value:"pt-BR"}],initialValue:"en"});Se(a),await t.setLanguage(a),await q.initialize(),await q.set("language",a,"global"),console.log(rn()),tr(Y.cyan(t.t("init.intro")));let r=o;r||(e.defaults?r="my-kaven-app":(r=await jt({message:t.t("init.projectName"),placeholder:"my-kaven-app",validate:d=>{let u=n.validateName(d||"");return u.valid?void 0:u.reason}}),Se(r)));let i=we.resolve(process.cwd(),r),s=await ut.findIncomplete(i);if(s&&!e.force){let d=s.getResumePoint(),u=e.resume?!0:await un({message:Y.yellow(`\u26A0 Incomplete init detected (stopped at: ${Y.bold(d??"unknown")}). Resume from here?`),initialValue:!0});if(e.resume||Se(u),u){await sr(i,s,n);return}e.force=!0}else await ye.pathExists(i)&&!e.force&&(ro(Y.red(`Error: Directory "${r}" already exists. Use --force to overwrite.`)),process.exit(1));let c;if(e.defaults){let d=q.getAll().projectDefaults||{};c={dbUrl:e.dbUrl||d.dbUrl||`postgresql://user:password@localhost:5432/${r}`,emailProvider:e.emailProvider||d.emailProvider||"postmark",locale:e.locale||d.locale||"en-US",currency:e.currency||d.currency||"USD"}}else{let d=q.getAll().projectDefaults||{},u=await jt({message:"Database URL (PostgreSQL):",initialValue:d.dbUrl||`postgresql://user:password@localhost:5432/${r}`});Se(u);let m=await dn({message:"Email provider:",options:[{label:"Postmark",value:"postmark"},{label:"Resend",value:"resend"},{label:"AWS SES",value:"ses"},{label:"SMTP",value:"smtp"}],initialValue:d.emailProvider||"postmark"});Se(m);let p=await jt({message:"Default locale:",initialValue:d.locale||"en-US"});Se(p);let f=await jt({message:"Default currency:",initialValue:d.currency||"USD"});Se(f);let y=await un({message:t.t("init.withSquad"),initialValue:!0});Se(y),e.withSquad=y,c={dbUrl:u,emailProvider:m,locale:p,currency:f}}let l=new ut(i);await l.create(r,{...c,projectName:r},{withSquad:e.withSquad,skipInstall:e.skipInstall,skipGit:e.skipGit,skipAiox:e.skipAiox,template:e.template}),await ir(i,r,c,l,n,e)}async function ir(o,e,t,n,a,r){let i=pn(import.meta.url),s=we.dirname(i);await X(n,"clone","Cloning kaven-template...",async()=>{await a.cloneTemplate(o,r.template)},{fatal:!0}),await X(n,"configure","Configuring project...",async()=>{await a.removeGitDir(o),await a.replacePlaceholders(o,{...t,projectName:e});let c=we.join(o,".claude","CLAUDE.md");await ye.ensureDir(we.dirname(c));let l=we.resolve(s,"../../core/templates/CLAUDE.md.hbs");if(await ye.pathExists(l)){let d=await ye.readFile(l,"utf-8");await ye.writeFile(c,d.replace("${new Date().toLocaleDateString()}",new Date().toLocaleDateString()),"utf-8")}},{fatal:!0}),r.skipInstall?await n.markStep("install","skipped"):await X(n,"install",cr(),async()=>{await a.runInstall(o)}),r.skipGit?await n.markStep("git","skipped"):await X(n,"git","Initializing git...",async()=>{await a.initGit(o)}),r.withSquad?await X(n,"squad","Installing kaven-squad...",async()=>{let l=await a.installSquad(o);if(!l.installed)throw new Error(l.reason??"squad install failed")})?await X(n,"aiox-core","Installing AIOX Core...",async()=>{let l=await a.installAIOXCore(o);if(!l.installed)throw new Error(l.reason??"aiox-core install failed")}):(await n.markStep("aiox-core","skipped"),console.log(Y.dim(` To retry: cd ${e} && kaven init --resume`))):(await n.markStep("squad","skipped"),await n.markStep("aiox-core","skipped")),r.skipAiox?await n.markStep("env-bootstrap","skipped"):await X(n,"env-bootstrap","Bootstrapping AIOX environment...",async()=>{await dt(o,{skipAiox:!1})}),await fn(e,n)}async function sr(o,e,t){let n=e.getState(),{answers:a,options:r,projectName:i}=n;console.log(Y.cyan(`
90
+ `}var Bl={primary:Ee.cyan,secondary:Ee.magenta,success:Ee.green,warning:Ee.yellow,error:Ee.red,dim:Ee.dim};import*as Za from"child_process";import ao from"fs-extra";import*as Mt from"path";import er from"ora";import cn from"chalk";var Qa={...Za},tr={ora:er};async function dt(o,e={}){if(e.skipAiox)return;let t=Mt.join(o,".aiox-core");if(!ao.existsSync(t)&&!ao.existsSync(Mt.join(o,".aiox")))return;let n=tr.ora("Bootstrapping AIOX environment...").start();try{let a=ao.existsSync(Mt.join(o,".aiox-core/bin/aiox.js"))?".aiox-core/bin/aiox.js":".aiox/bin/aiox.js";Qa.execSync(`node ${a} devops environment-bootstrap --quiet`,{cwd:o,stdio:"pipe",timeout:6e4}),n.succeed("AIOX environment bootstrapped")}catch(a){n.warn("AIOX environment bootstrap failed"),console.log(cn.yellow(" \u26A0 Run manually inside the project: kaven aiox bootstrap")),process.env.KAVEN_DEBUG&&console.error(cn.gray(` Error: ${a.message}`))}}import qe from"fs-extra";import ln from"path";var or=".kaven/init-state.json",dn=["clone","configure","install","git","squad","aiox-core","env-bootstrap"],ut=class o{constructor(e){this.projectDir=e;this.statePath=ln.join(e,or)}projectDir;statePath;state=null;static async findIncomplete(e){let t=new o(e);return!await qe.pathExists(t.statePath)||(await t.load(),t.isComplete())?null:t}async load(){return this.state=await qe.readJson(this.statePath),this.state}async create(e,t,n){let a={clone:"pending",configure:"pending",install:"pending",git:"pending",squad:"pending","aiox-core":"pending","env-bootstrap":"pending"};n.skipInstall&&(a.install="skipped"),n.skipGit&&(a.git="skipped"),n.withSquad||(a.squad="skipped",a["aiox-core"]="skipped"),n.skipAiox&&(a["env-bootstrap"]="skipped"),this.state={version:"1",projectName:e,answers:t,options:n,steps:a,startedAt:new Date().toISOString(),updatedAt:new Date().toISOString()},await qe.ensureDir(ln.dirname(this.statePath)),await this.persist()}async markStep(e,t){if(!this.state)throw new Error("State not initialized");this.state.steps[e]=t,this.state.updatedAt=new Date().toISOString(),await this.persist()}getState(){if(!this.state)throw new Error("State not loaded");return this.state}getResumePoint(){return this.state?dn.find(e=>this.state.steps[e]==="pending"||this.state.steps[e]==="failed")??null:null}isComplete(){return this.state?dn.every(e=>this.state.steps[e]==="done"||this.state.steps[e]==="skipped"):!1}async cleanup(){await qe.pathExists(this.statePath)&&await qe.remove(this.statePath)}async persist(){await qe.writeJson(this.statePath,this.state,{spaces:2})}};function Se(o){sr(o)&&(ro("Operation cancelled."),process.exit(0))}async function X(o,e,t,n,a={}){let r=rr();r.start(t);try{return await n(),await o.markStep(e,"done"),r.stop(Y.green(`${t} \u2713`)),!0}catch(i){await o.markStep(e,"failed");let s=i instanceof Error?i.message:String(i);return r.stop(Y.red(`${t} \u2717`)),a.fatal&&(ro(s),process.exit(1)),console.log(Y.dim(` Error: ${s}`)),!1}}async function fn(o,e){let t=await de.getInstance(),n=new Be,a=await un({message:"\u{1F310} Select Language / Selecione o Idioma:",options:[{label:"English",value:"en"},{label:"Portugu\xEAs (Brasil)",value:"pt-BR"}],initialValue:"en"});Se(a),await t.setLanguage(a),await q.initialize(),await q.set("language",a,"global"),console.log(sn()),nr(Y.cyan(t.t("init.intro")));let r=o;r||(e.defaults?r="my-kaven-app":(r=await jt({message:t.t("init.projectName"),placeholder:"my-kaven-app",validate:d=>{let u=n.validateName(d||"");return u.valid?void 0:u.reason}}),Se(r)));let i=we.resolve(process.cwd(),r),s=await ut.findIncomplete(i);if(s&&!e.force){let d=s.getResumePoint(),u=e.resume?!0:await pn({message:Y.yellow(`\u26A0 Incomplete init detected (stopped at: ${Y.bold(d??"unknown")}). Resume from here?`),initialValue:!0});if(e.resume||Se(u),u){await lr(i,s,n);return}e.force=!0}else await ye.pathExists(i)&&!e.force&&(ro(Y.red(`Error: Directory "${r}" already exists. Use --force to overwrite.`)),process.exit(1));let c;if(e.defaults){let d=q.getAll().projectDefaults||{};c={dbUrl:e.dbUrl||d.dbUrl||`postgresql://user:password@localhost:5432/${r}`,emailProvider:e.emailProvider||d.emailProvider||"postmark",locale:e.locale||d.locale||"en-US",currency:e.currency||d.currency||"USD"}}else{let d=q.getAll().projectDefaults||{},u=await jt({message:"Database URL (PostgreSQL):",initialValue:d.dbUrl||`postgresql://user:password@localhost:5432/${r}`});Se(u);let m=await un({message:"Email provider:",options:[{label:"Postmark",value:"postmark"},{label:"Resend",value:"resend"},{label:"AWS SES",value:"ses"},{label:"SMTP",value:"smtp"}],initialValue:d.emailProvider||"postmark"});Se(m);let p=await jt({message:"Default locale:",initialValue:d.locale||"en-US"});Se(p);let f=await jt({message:"Default currency:",initialValue:d.currency||"USD"});Se(f);let y=await pn({message:t.t("init.withSquad"),initialValue:!0});Se(y),e.withSquad=y,c={dbUrl:u,emailProvider:m,locale:p,currency:f}}let l=new ut(i);await l.create(r,{...c,projectName:r},{withSquad:e.withSquad,skipInstall:e.skipInstall,skipGit:e.skipGit,skipAiox:e.skipAiox,template:e.template}),await cr(i,r,c,l,n,e)}async function cr(o,e,t,n,a,r){let i=mn(import.meta.url),s=we.dirname(i);await X(n,"clone","Cloning kaven-template...",async()=>{await a.cloneTemplate(o,r.template)},{fatal:!0}),await X(n,"configure","Configuring project...",async()=>{await a.removeGitDir(o),await a.replacePlaceholders(o,{...t,projectName:e});let c=we.join(o,".claude","CLAUDE.md");await ye.ensureDir(we.dirname(c));let l=we.resolve(s,"../../core/templates/CLAUDE.md.hbs");if(await ye.pathExists(l)){let d=await ye.readFile(l,"utf-8");await ye.writeFile(c,d.replace("${new Date().toLocaleDateString()}",new Date().toLocaleDateString()),"utf-8")}},{fatal:!0}),r.skipInstall?await n.markStep("install","skipped"):await X(n,"install",dr(),async()=>{await a.runInstall(o)}),r.skipGit?await n.markStep("git","skipped"):await X(n,"git","Initializing git...",async()=>{await a.initGit(o)}),r.withSquad?await X(n,"squad","Installing kaven-squad...",async()=>{let l=await a.installSquad(o);if(!l.installed)throw new Error(l.reason??"squad install failed")})?await X(n,"aiox-core","Installing AIOX Core...",async()=>{let l=await a.installAIOXCore(o);if(!l.installed)throw new Error(l.reason??"aiox-core install failed")}):(await n.markStep("aiox-core","skipped"),console.log(Y.dim(` To retry: cd ${e} && kaven init --resume`))):(await n.markStep("squad","skipped"),await n.markStep("aiox-core","skipped")),r.skipAiox?await n.markStep("env-bootstrap","skipped"):await X(n,"env-bootstrap","Bootstrapping AIOX environment...",async()=>{await dt(o,{skipAiox:!1})}),await gn(e,n)}async function lr(o,e,t){let n=e.getState(),{answers:a,options:r,projectName:i}=n;console.log(Y.cyan(`
91
91
  Resuming init for ${Y.bold(i)}...
92
- `));let s=pn(import.meta.url),c=we.dirname(s),l=d=>{let u=n.steps[d];return u==="pending"||u==="failed"};l("clone")&&await X(e,"clone","Cloning kaven-template...",async()=>{await t.cloneTemplate(o,r.template)},{fatal:!0}),l("configure")&&await X(e,"configure","Configuring project...",async()=>{await t.removeGitDir(o),await t.replacePlaceholders(o,a);let d=we.join(o,".claude","CLAUDE.md");await ye.ensureDir(we.dirname(d));let u=we.resolve(c,"../../core/templates/CLAUDE.md.hbs");if(await ye.pathExists(u)){let m=await ye.readFile(u,"utf-8");await ye.writeFile(d,m.replace("${new Date().toLocaleDateString()}",new Date().toLocaleDateString()),"utf-8")}},{fatal:!0}),l("install")&&await X(e,"install","Installing dependencies...",async()=>{await t.runInstall(o)}),l("git")&&await X(e,"git","Initializing git...",async()=>{await t.initGit(o)}),l("squad")?await X(e,"squad","Installing kaven-squad...",async()=>{let u=await t.installSquad(o);if(!u.installed)throw new Error(u.reason??"squad install failed")})&&l("aiox-core")&&await X(e,"aiox-core","Installing AIOX Core...",async()=>{let u=await t.installAIOXCore(o);if(!u.installed)throw new Error(u.reason??"aiox-core install failed")}):l("aiox-core")&&await X(e,"aiox-core","Installing AIOX Core...",async()=>{let d=await t.installAIOXCore(o);if(!d.installed)throw new Error(d.reason??"aiox-core install failed")}),l("env-bootstrap")&&await X(e,"env-bootstrap","Bootstrapping AIOX environment...",async()=>{await dt(o,{skipAiox:!1})}),await fn(i,e)}function cr(){return"Installing dependencies..."}async function fn(o,e){if(e.isComplete())await e.cleanup();else{let t=e.getResumePoint();console.log(Y.yellow(`
92
+ `));let s=mn(import.meta.url),c=we.dirname(s),l=d=>{let u=n.steps[d];return u==="pending"||u==="failed"};l("clone")&&await X(e,"clone","Cloning kaven-template...",async()=>{await t.cloneTemplate(o,r.template)},{fatal:!0}),l("configure")&&await X(e,"configure","Configuring project...",async()=>{await t.removeGitDir(o),await t.replacePlaceholders(o,a);let d=we.join(o,".claude","CLAUDE.md");await ye.ensureDir(we.dirname(d));let u=we.resolve(c,"../../core/templates/CLAUDE.md.hbs");if(await ye.pathExists(u)){let m=await ye.readFile(u,"utf-8");await ye.writeFile(d,m.replace("${new Date().toLocaleDateString()}",new Date().toLocaleDateString()),"utf-8")}},{fatal:!0}),l("install")&&await X(e,"install","Installing dependencies...",async()=>{await t.runInstall(o)}),l("git")&&await X(e,"git","Initializing git...",async()=>{await t.initGit(o)}),l("squad")?await X(e,"squad","Installing kaven-squad...",async()=>{let u=await t.installSquad(o);if(!u.installed)throw new Error(u.reason??"squad install failed")})&&l("aiox-core")&&await X(e,"aiox-core","Installing AIOX Core...",async()=>{let u=await t.installAIOXCore(o);if(!u.installed)throw new Error(u.reason??"aiox-core install failed")}):l("aiox-core")&&await X(e,"aiox-core","Installing AIOX Core...",async()=>{let d=await t.installAIOXCore(o);if(!d.installed)throw new Error(d.reason??"aiox-core install failed")}),l("env-bootstrap")&&await X(e,"env-bootstrap","Bootstrapping AIOX environment...",async()=>{await dt(o,{skipAiox:!1})}),await gn(i,e)}function dr(){return"Installing dependencies..."}async function gn(o,e){if(e.isComplete())await e.cleanup();else{let t=e.getResumePoint();console.log(Y.yellow(`
93
93
  \u26A0 Setup incomplete (stopped at: ${Y.bold(t??"unknown")})`)),console.log(Y.dim(` Resume with: kaven init ${o} --resume
94
- `))}ar(`cd ${Y.cyan(o)}
94
+ `))}ir(`cd ${Y.cyan(o)}
95
95
  cp .env.example .env
96
96
  pnpm docker:up
97
97
  pnpm db:migrate && pnpm db:seed
98
- pnpm dev`,"Next Steps"),or(Y.cyan("Project ready. Happy building \u{1F680}"));try{await q.initialize();let t=e.getState();await q.set("projectDefaults",{dbUrl:t.answers.dbUrl,emailProvider:t.answers.emailProvider,locale:t.answers.locale,currency:t.answers.currency},"global")}catch{}}import F from"chalk";import lo from"ora";import gr from"open";import kn from"path";import Ge from"fs-extra";import hr from"os";import H from"chalk";import io from"ora";import Tt from"path";import{fileURLToPath as lr}from"url";import{spawn as dr}from"child_process";import gn from"fs";var ur=lr(import.meta.url),hn=Tt.dirname(ur);function pr(){let o=[Tt.join(hn,"../../package.json"),Tt.join(hn,"../../../package.json"),Tt.join(process.cwd(),"package.json")];for(let e of o)if(gn.existsSync(e))try{return JSON.parse(gn.readFileSync(e,"utf8"))}catch{continue}return{version:"0.4.2-alpha.0"}}var mr=pr(),Je=mr.version,_e="kaven-cli";async function so(){let o=io("Checking for updates...").start();try{let e=await fetch(`https://registry.npmjs.org/${_e}`);if(!e.ok){o.fail("Could not check for updates");return}let a=(await e.json())["dist-tags"]?.latest||Je;if(o.stop(),console.log(),console.log(H.bold("Version Check:")),console.log(` Current: ${H.cyan(Je)}`),console.log(` Latest: ${H.cyan(a)}`),console.log(),a===Je){console.log(H.green("\u2705 You're on the latest version!"));return}let r=wn(Je),i=wn(a);i.major>r.major||i.major===r.major&&i.minor>r.minor||i.major===r.major&&i.minor===r.minor&&i.patch>r.patch?(console.log(H.yellow(`\u26A0 Update available: ${a}`)),console.log(),console.log(H.bold("To upgrade, run:")),console.log(H.cyan(` npm install -g ${_e}@latest`)),console.log(H.cyan(" or")),console.log(H.cyan(` pnpm add -g ${_e}@latest`)),console.log(),console.log(H.gray("Release notes: https://github.com/kaven-co/kaven-cli/releases"))):console.log(H.green("\u2705 You're on the latest version!"))}catch(e){o.fail(`Check failed: ${e instanceof Error?e.message:String(e)}`),console.log(H.gray("You can manually check at: https://www.npmjs.com/package/kaven-cli"))}}async function co(){console.log();let o=io("Fetching latest version...").start();try{let e=await fetch(`https://registry.npmjs.org/${_e}`);if(!e.ok)throw new Error("Failed to fetch package info");let a=(await e.json())["dist-tags"]?.latest||Je;if(a===Je){o.succeed("Already on latest version");return}o.text=`Installing ${_e}@${a}...`;let r=process.env.npm_config_user_agent?.includes("pnpm")?"pnpm":"npm",i=await yn(r,["install","-g",`${_e}@${a}`]);if(i!==0){o.fail(`Installation failed with exit code ${i}`),console.error(H.gray(`Try: ${r} install -g ${_e}@latest`)),process.exit(1);return}o.succeed(`Updated to ${a}`);let s=io("Running health check...").start(),c=await fr();c.ok?(s.succeed("Installation verified"),console.log(),console.log(H.green("\u2705 CLI upgraded successfully!")),console.log(H.gray("Try: kaven --version"))):(s.warn("Installation verification failed"),console.log(H.yellow(c.errors.join(`
99
- `))))}catch(e){o.fail(`Installation failed: ${e instanceof Error?e.message:String(e)}`),process.exit(1)}}async function fr(){let o=[];try{await yn("kaven",["--version"],{stdio:"pipe"})!==0&&o.push("kaven command not available in PATH")}catch{o.push("Could not execute kaven command")}return{ok:o.length===0,errors:o}}function yn(o,e,t){return new Promise((n,a)=>{let r=dr(o,e,t||{stdio:"inherit"});r.on("error",a),r.on("close",i=>n(i??0))})}function wn(o){let e=o.replace(/^v/,"").split("-")[0],[t,n,a]=e.split(".").map(Number);return{major:t||0,minor:n||0,patch:a||0}}var ze=kn.join(hr.homedir(),".kaven","license.json"),wr=["starter","complete","pro","enterprise"],pt={starter:"Starter",complete:"Complete",pro:"Pro",enterprise:"Enterprise"},yr=5e3,vn=120;async function vr(){if(!await Ge.pathExists(ze))return null;try{return(await Ge.readJson(ze)).key||null}catch{return null}}async function kr(o){let e={};if(await Ge.pathExists(ze))try{e=await Ge.readJson(ze)}catch{}await Ge.ensureDir(kn.dirname(ze)),await Ge.writeJson(ze,{...e,tier:o},{spaces:2})}function br(o){return new Promise(e=>setTimeout(e,o))}async function bn(o){let e=new M;try{await e.getValidToken()}catch{console.error(F.red("Error: Not authenticated. Run 'kaven auth login' first.")),process.exit(1);return}let t=await vr();if(!t){console.error(F.red("Error: No license found. Add your license key first with 'kaven license status'.")),process.exit(1);return}let n=new J(e),a=lo("Loading your current license...").start(),r;try{r=(await n.getLicenseStatus(t)).tier.toLowerCase(),a.succeed(`Current tier: ${F.bold(pt[r]||r)}`)}catch{a.fail("Could not load license status"),console.error(F.gray("Try: kaven license status")),process.exit(1);return}let{printTierComparisonTable:i}=await import("./tier-table-DQMPQSI2.js");i(r,"pro"),console.log();let{select:s}=await import("@inquirer/prompts"),c=wr.filter(f=>f!==r&&f!=="enterprise");if(c.length===0){console.log(F.yellow(`You're already on the highest available tier (${pt[r]||r}).`)),console.log(F.gray("For Enterprise plans, contact: enterprise@kaven.site"));return}let l=await s({message:"Select target tier:",choices:c.map(f=>({name:pt[f],value:f}))});if(l===r){console.log(F.yellow(`Already on ${pt[r]||r}.`));return}let d=lo("Creating checkout session...").start(),u,m;try{let f=await n.createCheckoutSession(l,t);u=f.sessionUrl,m=f.sessionId,d.succeed("Checkout session created")}catch(f){d.fail("Failed to create checkout session"),console.error(F.red(f instanceof Error?f.message:String(f))),process.exit(1);return}if(o.browser!==!1){console.log(F.cyan("Opening checkout in your browser...")),console.log(F.gray(`URL: ${u}`));try{await gr(u)}catch{console.log(F.yellow("Could not open browser automatically. Open this URL manually:")),console.log(F.cyan(u))}}else console.log(F.cyan("Open this URL to complete checkout:")),console.log(F.bold(u));console.log();let p=lo("Waiting for payment confirmation (checking every 5s, max 10 min)...").start();for(let f=0;f<vn;f++){await br(yr);let y=vn-f-1;p.text=`Waiting for payment confirmation... (${y*5}s remaining)`;try{let P=await n.getCheckoutStatus(m);if(P.status==="confirmed"){p.succeed("Payment confirmed!");let R=P.tier||l;await kr(R),console.log(),console.log(F.green(`\u2705 Successfully upgraded to ${pt[R]||R}!`)),console.log(F.gray("Your new features are now unlocked."));return}if(P.status==="cancelled"){p.fail("Checkout was cancelled."),console.log(F.gray("No changes were made to your subscription."));return}if(P.status==="failed"){p.fail("Payment failed."),console.log(F.gray("Please try again or contact support@kaven.site")),process.exit(1);return}}catch{}}p.warn("Timed out waiting for payment confirmation."),console.log(),console.log(F.yellow("If you completed payment, your upgrade may take a few minutes to activate.")),console.log(F.gray("Check your upgrade status at: https://dashboard.kaven.site/billing"))}import He from"path";import Rt from"fs-extra";import*as oe from"@clack/prompts";import E from"picocolors";var U={intro:oe.intro,outro:oe.outro,spinner:oe.spinner,log:{info:oe.log.info,warn:oe.log.warn,success:oe.log.success,error:oe.log.error,step:oe.log.step,message:oe.log.message}},xr="kaven-cli";function Er(o){let e=[He.join(o,"package.json"),He.join(He.dirname(new URL(import.meta.url).pathname),"../../../package.json"),He.join(He.dirname(new URL(import.meta.url).pathname),"../../package.json")];for(let t of e)try{if(Rt.existsSync(t)){let n=JSON.parse(Rt.readFileSync(t,"utf8"));if(n.version)return n.version}}catch{}return"0.4.2-alpha.0"}async function Sr(){try{let o=await fetch(`https://registry.npmjs.org/${xr}`);return o.ok?(await o.json())["dist-tags"]?.latest??null:null}catch{return null}}function xn(o,e){let t=r=>r.replace(/^v/,"").split("-")[0].split(".").map(Number),n=t(o),a=t(e);for(let r=0;r<3;r++)if((n[r]??0)!==(a[r]??0))return(n[r]??0)-(a[r]??0);return 0}async function Cr(o){let e=He.join(o,".kaven","cache","modules");if(!await Rt.pathExists(e))return[];let t=await Rt.readdir(e),n=new Set;for(let a of t){let r=a.lastIndexOf("-");r>0&&n.add(a.slice(0,r))}return[...n]}async function Pr(o,e){let t=Er(o),n=null,a=!1;e&&(n=await Sr(),n&&xn(n,t)>0&&(a=!0));let r=await Cr(o),i=[];if(r.length>0){let s=null;try{let c=new M;await c.getValidToken(),s=new J(c)}catch{}for(let c of r){let l=await St(c,o);if(!l)continue;let d=null,u;if(s)try{d=(await s.getModule(c)).latestVersion??null}catch(p){u=fe(p).message}else u="marketplace offline or not authenticated";let m=!!(d&&xn(d,l)>0);i.push({slug:c,installed:l,latest:d,outdated:m,error:u})}}return{cliCurrent:t,cliLatest:n,cliOutdated:a,modules:i}}function $r(o,e){if(e&&(o.cliLatest===null?U.log.warn(E.yellow(`kaven-cli: ${E.bold(o.cliCurrent)} (could not reach npm)`)):o.cliOutdated?U.log.warn(E.yellow(`kaven-cli: ${E.bold(o.cliCurrent)} \u2192 ${E.bold(o.cliLatest)} available`)+E.dim(`
100
- npm install -g kaven-cli@latest`)):U.log.success(E.green(`kaven-cli: ${E.bold(o.cliCurrent)} \u2713 up to date`))),o.modules.length===0){U.log.info(E.dim("No modules installed in this project."));return}for(let t of o.modules)t.error?U.log.warn(E.yellow(`${t.slug}: ${E.bold(t.installed)} (${t.error})`)):t.outdated?U.log.warn(E.yellow(`${t.slug}: ${E.bold(t.installed)} \u2192 ${E.bold(t.latest)} available`)):U.log.success(E.green(`${t.slug}: ${E.bold(t.installed)} \u2713`))}async function En(o={},e){let t=e??process.cwd(),{core:n,module:a,all:r,check:i,skipVerify:s}=o;if(a){await at(a,t,{skipVerify:s});return}let c=!!(n||r||!n&&!a);U.intro(E.bold(E.cyan("kaven update")));let l=U.spinner();l.start("Verificando atualiza\xE7\xF5es dispon\xEDveis...");let d=await Pr(t,c);if(l.stop("Status de atualiza\xE7\xF5es:"),$r(d,c),!(d.cliOutdated||d.modules.some(m=>m.outdated))){U.outro(E.green("\u2713 Tudo atualizado."));return}if(i){U.outro(E.yellow("Atualiza\xE7\xF5es dispon\xEDveis. Rode sem --check para aplicar."));return}if((r||n)&&d.cliOutdated&&d.cliLatest&&(U.log.info(E.cyan(`Atualizando kaven-cli ${d.cliCurrent} \u2192 ${d.cliLatest}...`)),U.log.info(E.dim(" Execute: npm install -g kaven-cli@latest"))),r){let m=d.modules.filter(p=>p.outdated);for(let p of m)U.log.info(E.cyan(`
98
+ pnpm dev`,"Next Steps"),ar(Y.cyan("Project ready. Happy building \u{1F680}"));try{await q.initialize();let t=e.getState();await q.set("projectDefaults",{dbUrl:t.answers.dbUrl,emailProvider:t.answers.emailProvider,locale:t.answers.locale,currency:t.answers.currency},"global")}catch{}}import F from"chalk";import lo from"ora";import wr from"open";import bn from"path";import ze from"fs-extra";import yr from"os";import H from"chalk";import io from"ora";import Tt from"path";import{fileURLToPath as ur}from"url";import{spawn as pr}from"child_process";import hn from"fs";var mr=ur(import.meta.url),wn=Tt.dirname(mr);function fr(){let o=[Tt.join(wn,"../../package.json"),Tt.join(wn,"../../../package.json"),Tt.join(process.cwd(),"package.json")];for(let e of o)if(hn.existsSync(e))try{return JSON.parse(hn.readFileSync(e,"utf8"))}catch{continue}return{version:"0.4.2-alpha.0"}}var gr=fr(),Je=gr.version,_e="kaven-cli";async function so(){let o=io("Checking for updates...").start();try{let e=await fetch(`https://registry.npmjs.org/${_e}`);if(!e.ok){o.fail("Could not check for updates");return}let a=(await e.json())["dist-tags"]?.latest||Je;if(o.stop(),console.log(),console.log(H.bold("Version Check:")),console.log(` Current: ${H.cyan(Je)}`),console.log(` Latest: ${H.cyan(a)}`),console.log(),a===Je){console.log(H.green("\u2705 You're on the latest version!"));return}let r=yn(Je),i=yn(a);i.major>r.major||i.major===r.major&&i.minor>r.minor||i.major===r.major&&i.minor===r.minor&&i.patch>r.patch?(console.log(H.yellow(`\u26A0 Update available: ${a}`)),console.log(),console.log(H.bold("To upgrade, run:")),console.log(H.cyan(` npm install -g ${_e}@latest`)),console.log(H.cyan(" or")),console.log(H.cyan(` pnpm add -g ${_e}@latest`)),console.log(),console.log(H.gray("Release notes: https://github.com/kaven-co/kaven-cli/releases"))):console.log(H.green("\u2705 You're on the latest version!"))}catch(e){o.fail(`Check failed: ${e instanceof Error?e.message:String(e)}`),console.log(H.gray("You can manually check at: https://www.npmjs.com/package/kaven-cli"))}}async function co(){console.log();let o=io("Fetching latest version...").start();try{let e=await fetch(`https://registry.npmjs.org/${_e}`);if(!e.ok)throw new Error("Failed to fetch package info");let a=(await e.json())["dist-tags"]?.latest||Je;if(a===Je){o.succeed("Already on latest version");return}o.text=`Installing ${_e}@${a}...`;let r=process.env.npm_config_user_agent?.includes("pnpm")?"pnpm":"npm",i=await vn(r,["install","-g",`${_e}@${a}`]);if(i!==0){o.fail(`Installation failed with exit code ${i}`),console.error(H.gray(`Try: ${r} install -g ${_e}@latest`)),process.exit(1);return}o.succeed(`Updated to ${a}`);let s=io("Running health check...").start(),c=await hr();c.ok?(s.succeed("Installation verified"),console.log(),console.log(H.green("\u2705 CLI upgraded successfully!")),console.log(H.gray("Try: kaven --version"))):(s.warn("Installation verification failed"),console.log(H.yellow(c.errors.join(`
99
+ `))))}catch(e){o.fail(`Installation failed: ${e instanceof Error?e.message:String(e)}`),process.exit(1)}}async function hr(){let o=[];try{await vn("kaven",["--version"],{stdio:"pipe"})!==0&&o.push("kaven command not available in PATH")}catch{o.push("Could not execute kaven command")}return{ok:o.length===0,errors:o}}function vn(o,e,t){return new Promise((n,a)=>{let r=pr(o,e,t||{stdio:"inherit"});r.on("error",a),r.on("close",i=>n(i??0))})}function yn(o){let e=o.replace(/^v/,"").split("-")[0],[t,n,a]=e.split(".").map(Number);return{major:t||0,minor:n||0,patch:a||0}}var Ge=bn.join(yr.homedir(),".kaven","license.json"),vr=["starter","complete","pro","enterprise"],pt={starter:"Starter",complete:"Complete",pro:"Pro",enterprise:"Enterprise"},kr=5e3,kn=120;async function br(){if(!await ze.pathExists(Ge))return null;try{return(await ze.readJson(Ge)).key||null}catch{return null}}async function xr(o){let e={};if(await ze.pathExists(Ge))try{e=await ze.readJson(Ge)}catch{}await ze.ensureDir(bn.dirname(Ge)),await ze.writeJson(Ge,{...e,tier:o},{spaces:2})}function Er(o){return new Promise(e=>setTimeout(e,o))}async function xn(o){let e=new M;try{await e.getValidToken()}catch{console.error(F.red("Error: Not authenticated. Run 'kaven auth login' first.")),process.exit(1);return}let t=await br();if(!t){console.error(F.red("Error: No license found. Add your license key first with 'kaven license status'.")),process.exit(1);return}let n=new J(e),a=lo("Loading your current license...").start(),r;try{r=(await n.getLicenseStatus(t)).tier.toLowerCase(),a.succeed(`Current tier: ${F.bold(pt[r]||r)}`)}catch{a.fail("Could not load license status"),console.error(F.gray("Try: kaven license status")),process.exit(1);return}let{printTierComparisonTable:i}=await import("./tier-table-DQMPQSI2.js");i(r,"pro"),console.log();let{select:s}=await import("@inquirer/prompts"),c=vr.filter(f=>f!==r&&f!=="enterprise");if(c.length===0){console.log(F.yellow(`You're already on the highest available tier (${pt[r]||r}).`)),console.log(F.gray("For Enterprise plans, contact: enterprise@kaven.site"));return}let l=await s({message:"Select target tier:",choices:c.map(f=>({name:pt[f],value:f}))});if(l===r){console.log(F.yellow(`Already on ${pt[r]||r}.`));return}let d=lo("Creating checkout session...").start(),u,m;try{let f=await n.createCheckoutSession(l,t);u=f.sessionUrl,m=f.sessionId,d.succeed("Checkout session created")}catch(f){d.fail("Failed to create checkout session"),console.error(F.red(f instanceof Error?f.message:String(f))),process.exit(1);return}if(o.browser!==!1){console.log(F.cyan("Opening checkout in your browser...")),console.log(F.gray(`URL: ${u}`));try{await wr(u)}catch{console.log(F.yellow("Could not open browser automatically. Open this URL manually:")),console.log(F.cyan(u))}}else console.log(F.cyan("Open this URL to complete checkout:")),console.log(F.bold(u));console.log();let p=lo("Waiting for payment confirmation (checking every 5s, max 10 min)...").start();for(let f=0;f<kn;f++){await Er(kr);let y=kn-f-1;p.text=`Waiting for payment confirmation... (${y*5}s remaining)`;try{let P=await n.getCheckoutStatus(m);if(P.status==="confirmed"){p.succeed("Payment confirmed!");let R=P.tier||l;await xr(R),console.log(),console.log(F.green(`\u2705 Successfully upgraded to ${pt[R]||R}!`)),console.log(F.gray("Your new features are now unlocked."));return}if(P.status==="cancelled"){p.fail("Checkout was cancelled."),console.log(F.gray("No changes were made to your subscription."));return}if(P.status==="failed"){p.fail("Payment failed."),console.log(F.gray("Please try again or contact support@kaven.site")),process.exit(1);return}}catch{}}p.warn("Timed out waiting for payment confirmation."),console.log(),console.log(F.yellow("If you completed payment, your upgrade may take a few minutes to activate.")),console.log(F.gray("Check your upgrade status at: https://dashboard.kaven.site/billing"))}import He from"path";import Rt from"fs-extra";import*as oe from"@clack/prompts";import E from"picocolors";var U={intro:oe.intro,outro:oe.outro,spinner:oe.spinner,log:{info:oe.log.info,warn:oe.log.warn,success:oe.log.success,error:oe.log.error,step:oe.log.step,message:oe.log.message}},Sr="kaven-cli";function Cr(o){let e=[He.join(o,"package.json"),He.join(He.dirname(new URL(import.meta.url).pathname),"../../../package.json"),He.join(He.dirname(new URL(import.meta.url).pathname),"../../package.json")];for(let t of e)try{if(Rt.existsSync(t)){let n=JSON.parse(Rt.readFileSync(t,"utf8"));if(n.version)return n.version}}catch{}return"0.4.2-alpha.0"}async function Pr(){try{let o=await fetch(`https://registry.npmjs.org/${Sr}`);return o.ok?(await o.json())["dist-tags"]?.latest??null:null}catch{return null}}function En(o,e){let t=r=>r.replace(/^v/,"").split("-")[0].split(".").map(Number),n=t(o),a=t(e);for(let r=0;r<3;r++)if((n[r]??0)!==(a[r]??0))return(n[r]??0)-(a[r]??0);return 0}async function $r(o){let e=He.join(o,".kaven","cache","modules");if(!await Rt.pathExists(e))return[];let t=await Rt.readdir(e),n=new Set;for(let a of t){let r=a.lastIndexOf("-");r>0&&n.add(a.slice(0,r))}return[...n]}async function Ir(o,e){let t=Cr(o),n=null,a=!1;e&&(n=await Pr(),n&&En(n,t)>0&&(a=!0));let r=await $r(o),i=[];if(r.length>0){let s=null;try{let c=new M;await c.getValidToken(),s=new J(c)}catch{}for(let c of r){let l=await St(c,o);if(!l)continue;let d=null,u;if(s)try{d=(await s.getModule(c)).latestVersion??null}catch(p){u=fe(p).message}else u="marketplace offline or not authenticated";let m=!!(d&&En(d,l)>0);i.push({slug:c,installed:l,latest:d,outdated:m,error:u})}}return{cliCurrent:t,cliLatest:n,cliOutdated:a,modules:i}}function Ar(o,e){if(e&&(o.cliLatest===null?U.log.warn(E.yellow(`kaven-cli: ${E.bold(o.cliCurrent)} (could not reach npm)`)):o.cliOutdated?U.log.warn(E.yellow(`kaven-cli: ${E.bold(o.cliCurrent)} \u2192 ${E.bold(o.cliLatest)} available`)+E.dim(`
100
+ npm install -g kaven-cli@latest`)):U.log.success(E.green(`kaven-cli: ${E.bold(o.cliCurrent)} \u2713 up to date`))),o.modules.length===0){U.log.info(E.dim("No modules installed in this project."));return}for(let t of o.modules)t.error?U.log.warn(E.yellow(`${t.slug}: ${E.bold(t.installed)} (${t.error})`)):t.outdated?U.log.warn(E.yellow(`${t.slug}: ${E.bold(t.installed)} \u2192 ${E.bold(t.latest)} available`)):U.log.success(E.green(`${t.slug}: ${E.bold(t.installed)} \u2713`))}async function Sn(o={},e){let t=e??process.cwd(),{core:n,module:a,all:r,check:i,skipVerify:s}=o;if(a){await at(a,t,{skipVerify:s});return}let c=!!(n||r||!n&&!a);U.intro(E.bold(E.cyan("kaven update")));let l=U.spinner();l.start("Verificando atualiza\xE7\xF5es dispon\xEDveis...");let d=await Ir(t,c);if(l.stop("Status de atualiza\xE7\xF5es:"),Ar(d,c),!(d.cliOutdated||d.modules.some(m=>m.outdated))){U.outro(E.green("\u2713 Tudo atualizado."));return}if(i){U.outro(E.yellow("Atualiza\xE7\xF5es dispon\xEDveis. Rode sem --check para aplicar."));return}if((r||n)&&d.cliOutdated&&d.cliLatest&&(U.log.info(E.cyan(`Atualizando kaven-cli ${d.cliCurrent} \u2192 ${d.cliLatest}...`)),U.log.info(E.dim(" Execute: npm install -g kaven-cli@latest"))),r){let m=d.modules.filter(p=>p.outdated);for(let p of m)U.log.info(E.cyan(`
101
101
  Atualizando m\xF3dulo ${p.slug} ${p.installed} \u2192 ${p.latest}...`)),await at(p.slug,t,{skipVerify:s});U.outro(E.green("\u2713 Todos os m\xF3dulos atualizados."));return}if(!n){let m=d.modules.filter(p=>p.outdated);if(m.length>0){U.log.info(E.dim(`
102
102
  Para atualizar m\xF3dulos:`));for(let p of m)U.log.info(E.dim(` kaven update --module ${p.slug}`));U.log.info(E.dim(`
103
- Para atualizar tudo: kaven update --all`))}U.outro("")}}import ve from"chalk";import pe from"fs-extra";import We from"path";import Ir from"os";var po=class{cacheDir;maxSizeBytes;indexPath;constructor(e,t){this.cacheDir=e??We.join(Ir.homedir(),".kaven","cache"),this.maxSizeBytes=t??50*1024*1024,this.indexPath=We.join(this.cacheDir,"_index.json")}async ensureDir(){await pe.ensureDir(this.cacheDir)}keyToFileName(e){return e.replace(/[^a-zA-Z0-9-_:.]/g,"_")+".json"}async readIndex(){try{if(await pe.pathExists(this.indexPath))return await pe.readJson(this.indexPath)}catch{}return{}}async writeIndex(e){await this.ensureDir(),await pe.writeJson(this.indexPath,e,{spaces:2})}async get(e){let n=(await this.readIndex())[e];if(!n||Date.now()>n.expiresAt)return null;try{let a=We.join(this.cacheDir,n.file);return(await pe.readJson(a)).data}catch{return null}}async getStale(e){let n=(await this.readIndex())[e];if(!n)return null;try{let a=We.join(this.cacheDir,n.file);return(await pe.readJson(a)).data}catch{return null}}async set(e,t,n){await this.ensureDir();let a=JSON.stringify(t),r=Buffer.byteLength(a,"utf8"),i=Date.now(),s=i+n,c=this.keyToFileName(e),l=We.join(this.cacheDir,c),d={data:t,expiresAt:s,size:r,createdAt:i};await pe.writeJson(l,d,{spaces:2});let u=await this.readIndex();u[e]={file:c,expiresAt:s,size:r,createdAt:i},await this.writeIndex(u),await this.evict()}async evict(){let e=await this.readIndex(),t=Object.entries(e),n=t.reduce((r,[,i])=>r+i.size,0);if(n<=this.maxSizeBytes)return;t.sort(([,r],[,i])=>r.createdAt-i.createdAt);let a={...e};for(let[r,i]of t){if(n<=this.maxSizeBytes)break;try{let s=We.join(this.cacheDir,i.file);await pe.remove(s),delete a[r],n-=i.size}catch{}}await this.writeIndex(a)}async stats(){let e=await this.readIndex(),t=Object.values(e);if(t.length===0)return{totalSize:0,entries:0};let n=t.reduce((s,c)=>s+c.size,0),a=t.map(s=>s.createdAt),r=new Date(Math.min(...a)),i=new Date(Math.max(...a));return{totalSize:n,entries:t.length,oldest:r,newest:i}}async clear(){await pe.pathExists(this.cacheDir)&&await pe.remove(this.cacheDir)}},uo=null;function mo(){return uo||(uo=new po),uo}function Sn(o){if(o===0)return"0 B";let e=1024,t=["B","KB","MB","GB"],n=Math.floor(Math.log(o)/Math.log(e));return`${parseFloat((o/Math.pow(e,n)).toFixed(1))} ${t[n]}`}async function Cn(){let o=mo(),e=await o.stats();console.log(ve.bold(`
103
+ Para atualizar tudo: kaven update --all`))}U.outro("")}}import ve from"chalk";import pe from"fs-extra";import We from"path";import Mr from"os";var po=class{cacheDir;maxSizeBytes;indexPath;constructor(e,t){this.cacheDir=e??We.join(Mr.homedir(),".kaven","cache"),this.maxSizeBytes=t??50*1024*1024,this.indexPath=We.join(this.cacheDir,"_index.json")}async ensureDir(){await pe.ensureDir(this.cacheDir)}keyToFileName(e){return e.replace(/[^a-zA-Z0-9-_:.]/g,"_")+".json"}async readIndex(){try{if(await pe.pathExists(this.indexPath))return await pe.readJson(this.indexPath)}catch{}return{}}async writeIndex(e){await this.ensureDir(),await pe.writeJson(this.indexPath,e,{spaces:2})}async get(e){let n=(await this.readIndex())[e];if(!n||Date.now()>n.expiresAt)return null;try{let a=We.join(this.cacheDir,n.file);return(await pe.readJson(a)).data}catch{return null}}async getStale(e){let n=(await this.readIndex())[e];if(!n)return null;try{let a=We.join(this.cacheDir,n.file);return(await pe.readJson(a)).data}catch{return null}}async set(e,t,n){await this.ensureDir();let a=JSON.stringify(t),r=Buffer.byteLength(a,"utf8"),i=Date.now(),s=i+n,c=this.keyToFileName(e),l=We.join(this.cacheDir,c),d={data:t,expiresAt:s,size:r,createdAt:i};await pe.writeJson(l,d,{spaces:2});let u=await this.readIndex();u[e]={file:c,expiresAt:s,size:r,createdAt:i},await this.writeIndex(u),await this.evict()}async evict(){let e=await this.readIndex(),t=Object.entries(e),n=t.reduce((r,[,i])=>r+i.size,0);if(n<=this.maxSizeBytes)return;t.sort(([,r],[,i])=>r.createdAt-i.createdAt);let a={...e};for(let[r,i]of t){if(n<=this.maxSizeBytes)break;try{let s=We.join(this.cacheDir,i.file);await pe.remove(s),delete a[r],n-=i.size}catch{}}await this.writeIndex(a)}async stats(){let e=await this.readIndex(),t=Object.values(e);if(t.length===0)return{totalSize:0,entries:0};let n=t.reduce((s,c)=>s+c.size,0),a=t.map(s=>s.createdAt),r=new Date(Math.min(...a)),i=new Date(Math.max(...a));return{totalSize:n,entries:t.length,oldest:r,newest:i}}async clear(){await pe.pathExists(this.cacheDir)&&await pe.remove(this.cacheDir)}},uo=null;function mo(){return uo||(uo=new po),uo}function Cn(o){if(o===0)return"0 B";let e=1024,t=["B","KB","MB","GB"],n=Math.floor(Math.log(o)/Math.log(e));return`${parseFloat((o/Math.pow(e,n)).toFixed(1))} ${t[n]}`}async function Pn(){let o=mo(),e=await o.stats();console.log(ve.bold(`
104
104
  Kaven CLI Cache Status
105
- `)),console.log(` Cache directory: ${ve.cyan(o.cacheDir)}`),console.log(` Total size: ${ve.cyan(Sn(e.totalSize))}`),console.log(` Cached entries: ${ve.cyan(e.entries.toString())}`),e.oldest&&console.log(` Oldest entry: ${ve.gray(e.oldest.toLocaleString())}`),e.newest&&console.log(` Newest entry: ${ve.gray(e.newest.toLocaleString())}`),console.log(),console.log(ve.gray("Run 'kaven cache clear' to remove all cached data."))}async function Pn(){let o=mo(),e=await o.stats();if(e.entries===0){console.log(ve.gray("Cache is already empty."));return}await o.clear(),console.log(ve.green(`Cache cleared: ${e.entries} entries (${Sn(e.totalSize)}) removed.`))}import Q from"chalk";async function $n(o,e){(!o||!e)&&(console.error(Q.red("Error: Both key and value are required")),console.error(Q.gray("Usage: kaven config set KEY VALUE")),process.exit(1)),await q.initialize();try{await q.set(o,e),console.log(Q.green(`\u2705 Set ${Q.bold(o)} = ${Q.bold(e)}`))}catch(t){console.error(Q.red(`Error: ${t instanceof Error?t.message:String(t)}`)),process.exit(1)}}async function In(o,e){o||(console.error(Q.red("Error: Key is required")),console.error(Q.gray("Usage: kaven config get KEY")),process.exit(1)),await q.initialize();try{let t=q.get(o);e.json?console.log(JSON.stringify({[o]:t},null,2)):console.log(t)}catch(t){console.error(Q.red(`Error: ${t instanceof Error?t.message:String(t)}`)),process.exit(1)}}async function An(o){await q.initialize();let e=q.getAll();if(o.json)console.log(JSON.stringify(e,null,2));else{console.log(Q.bold("Kaven Configuration:")),console.log(Q.gray(`Location: ${q.getConfigDir()}/config.json`)),console.log();let t=Object.entries(e),n=Math.max(...t.map(([a])=>a.length));for(let[a,r]of t){let i=" ".repeat(n-a.length),s=typeof r=="object"?JSON.stringify(r,null,2).replace(/\n/g,`
106
- `+" ".repeat(n+3)):r;console.log(` ${a}${i} ${Q.cyan(s)}`)}}}async function Mn(){let{confirm:o}=await import("@inquirer/prompts");if(!await o({message:"Are you sure you want to reset config to defaults?",default:!1})){console.log(Q.yellow("Cancelled."));return}await q.initialize(),await q.reset(),console.log(Q.green("\u2705 Config reset to defaults"))}import{select as Ar,confirm as jn,text as Mr,multiselect as jr,isCancel as Tr,cancel as Rr}from"@clack/prompts";import W from"picocolors";import go from"fs-extra";import Tn from"path";var ke=[{key:"FEATURE_EMAIL_VERIFICATION",type:"boolean",description:"Email verification on signup",category:"Auth",defaultValue:"true"},{key:"FEATURE_2FA_TOTP",type:"boolean",description:"Two-factor authentication via TOTP",category:"Auth",defaultValue:"false"},{key:"FEATURE_SSO_SAML",type:"boolean",description:"Single Sign-On via SAML/OIDC",category:"Auth",defaultValue:"false"},{key:"FEATURE_SOCIAL_LOGIN",type:"boolean",description:"Login via Google, GitHub, etc.",category:"Auth",defaultValue:"true"},{key:"FEATURE_MAGIC_LINK",type:"boolean",description:"Passwordless login via email",category:"Auth",defaultValue:"false"},{key:"FEATURE_CUSTOM_DOMAIN",type:"boolean",description:"Custom domain per tenant",category:"Tenancy",defaultValue:"false"},{key:"FEATURE_WHITE_LABEL",type:"boolean",description:"Remove Kaven branding",category:"Tenancy",defaultValue:"false"},{key:"FEATURE_MULTI_BUSINESS",type:"boolean",description:"Multiple businesses per user",category:"Tenancy",defaultValue:"false"},{key:"FEATURE_AGENCY_HUB",type:"boolean",description:"Agency management dashboard",category:"Tenancy",defaultValue:"false"},{key:"FEATURE_TENANT_THEMES",type:"boolean",description:"Custom themes for tenants",category:"Tenancy",defaultValue:"true"},{key:"FEATURE_SUBSCRIPTIONS",type:"boolean",description:"Subscription management",category:"Billing",defaultValue:"true"},{key:"FEATURE_INVOICING",type:"boolean",description:"Automatic invoicing",category:"Billing",defaultValue:"true"},{key:"FEATURE_USAGE_BILLING",type:"boolean",description:"Metered usage billing",category:"Billing",defaultValue:"false"},{key:"FEATURE_PADDLE_CHECKOUT",type:"boolean",description:"Paddle payment integration",category:"Billing",defaultValue:"true"},{key:"FEATURE_PAGUBIT_PIX",type:"boolean",description:"Pix payment support",category:"Billing",defaultValue:"false"},{key:"FEATURE_API_ACCESS",type:"boolean",description:"External API access",category:"API",defaultValue:"false"},{key:"FEATURE_WEBHOOKS",type:"boolean",description:"Outgoing webhooks",category:"API",defaultValue:"false"},{key:"FEATURE_MARKETPLACE_ACCESS",type:"boolean",description:"Kaven Marketplace access",category:"API",defaultValue:"true"},{key:"MAX_API_CALLS_MONTH",type:"numeric",description:"Maximum API calls per month",category:"API",defaultValue:"10000"},{key:"MAX_AGENT_API_CALLS_HOUR",type:"numeric",description:"Maximum agent calls per hour",category:"API",defaultValue:"100"},{key:"MAX_TEAM_MEMBERS",type:"numeric",description:"Maximum team members per tenant",category:"Limits",defaultValue:"5"},{key:"MAX_PROJECTS",type:"numeric",description:"Maximum projects per tenant",category:"Limits",defaultValue:"3"},{key:"MAX_STORAGE_GB",type:"numeric",description:"Maximum storage in GB",category:"Limits",defaultValue:"1"},{key:"MAX_TENANTS",type:"numeric",description:"Maximum sub-tenants",category:"Limits",defaultValue:"1"},{key:"FEATURE_PRIORITY_SUPPORT",type:"boolean",description:"Priority support queue",category:"Support",defaultValue:"false"},{key:"FEATURE_AUDIT_COMPLIANCE",type:"boolean",description:"Audit logs and compliance",category:"Support",defaultValue:"false"},{key:"FEATURE_DATA_EXPORT",type:"boolean",description:"Customer data export",category:"Support",defaultValue:"true"}],fo={starter:{FEATURE_EMAIL_VERIFICATION:!0,FEATURE_SOCIAL_LOGIN:!0,FEATURE_MARKETPLACE_ACCESS:!0,MAX_TEAM_MEMBERS:"5",MAX_PROJECTS:"3",MAX_API_CALLS_MONTH:"10000",MAX_AGENT_API_CALLS_HOUR:"100"},complete:{FEATURE_EMAIL_VERIFICATION:!0,FEATURE_SOCIAL_LOGIN:!0,FEATURE_CUSTOM_DOMAIN:!0,FEATURE_API_ACCESS:!0,FEATURE_MARKETPLACE_ACCESS:!0,MAX_TEAM_MEMBERS:"25",MAX_PROJECTS:"20",MAX_API_CALLS_MONTH:"100000",MAX_AGENT_API_CALLS_HOUR:"500"},pro:{FEATURE_EMAIL_VERIFICATION:!0,FEATURE_SOCIAL_LOGIN:!0,FEATURE_CUSTOM_DOMAIN:!0,FEATURE_WHITE_LABEL:!0,FEATURE_API_ACCESS:!0,FEATURE_MARKETPLACE_ACCESS:!0,MAX_TEAM_MEMBERS:"100",MAX_PROJECTS:"100",MAX_API_CALLS_MONTH:"1000000",MAX_AGENT_API_CALLS_HOUR:"2000"},enterprise:{}};function mt(o){Tr(o)&&(Rr("Cancelled."),process.exit(0))}async function Rn(o){let e=o.outputPath??Tn.join(process.cwd(),"packages","database","prisma","seeds","capabilities.seed.ts");if(o.list){let t=await de.getInstance();_r(t);return}if(o.tier){await _n(o.tier,e,o);return}await Dr(e,o)}function _r(o){console.log(),console.log(W.bold(W.underline(o.t("config.features.catalogHeader")))),console.log(W.dim(o.t("config.features.capabilitiesTotal",{count:ke.length})+`
107
- `));let e=[...new Set(ke.map(t=>t.category))];for(let t of e){let n=ke.filter(a=>a.category===t);console.log(W.bold(W.cyan(` ${t} (${n.length})`)));for(let a of n)console.log(` ${W.white(a.key.padEnd(30))} ${W.dim(`[${a.type}]`)}`),console.log(` ${W.dim(a.description)}`);console.log()}console.log(W.bold(o.t("config.features.tierPresets")));for(let t of["starter","complete","pro","enterprise"])console.log(` ${W.white(t.padEnd(12))}`);console.log()}async function _n(o,e,t){let n=fo[o]||{},a={};for(let r of ke)o==="enterprise"?a[r.key]=r.type==="boolean"?!0:"-1":a[r.key]=n[r.key]??(r.type==="boolean"?!1:r.defaultValue);await Dn(a,e,t,o)}async function Dr(o,e){let t=await de.getInstance();console.log(),console.log(W.bold(W.underline(t.t("config.features.tuiHeader"))));let n=await Ar({message:t.t("config.features.selectTier"),options:[{label:"Starter \u2014 Essential SaaS features",value:"starter"},{label:"Complete \u2014 White-label + Custom Domains",value:"complete"},{label:"Pro \u2014 Extended API + Limits",value:"pro"},{label:"Enterprise \u2014 Unlimited everything",value:"enterprise"},{label:W.dim(t.t("common.cancel")),value:"cancel"}]});if(mt(n),n==="cancel")return;let a=n,r=fo[a]||{},i={},s=await jn({message:t.t("config.features.customize"),initialValue:!1});if(mt(s),!s)return _n(a,o,e);let c=[...new Set(ke.map(l=>l.category))];for(let l of c){let d=ke.filter(p=>p.category===l),u=d.filter(p=>p.type==="boolean"),m=d.filter(p=>p.type==="numeric");if(u.length>0){let p=u.filter(y=>a==="enterprise"||r[y.key]===!0).map(y=>y.key),f=await jr({message:`${l} features:`,options:u.map(y=>({label:`${y.key.padEnd(30)} \u2014 ${y.description}`,value:y.key})),initialValues:p,required:!1});mt(f);for(let y of u)i[y.key]=f.includes(y.key)}for(let p of m){let f=a==="enterprise"?"-1":r[p.key]||p.defaultValue,y=await Mr({message:`${p.key} (${p.description}):`,placeholder:f,defaultValue:f});mt(y),i[p.key]=y}}await Dn(i,o,e,a)}async function Dn(o,e,t,n){let a=await de.getInstance(),r=ke.map(s=>{let c=o[s.key];return` { key: "${s.key}", type: "${s.type}", defaultValue: "${c}", description: "${s.description}" },`}).join(`
105
+ `)),console.log(` Cache directory: ${ve.cyan(o.cacheDir)}`),console.log(` Total size: ${ve.cyan(Cn(e.totalSize))}`),console.log(` Cached entries: ${ve.cyan(e.entries.toString())}`),e.oldest&&console.log(` Oldest entry: ${ve.gray(e.oldest.toLocaleString())}`),e.newest&&console.log(` Newest entry: ${ve.gray(e.newest.toLocaleString())}`),console.log(),console.log(ve.gray("Run 'kaven cache clear' to remove all cached data."))}async function $n(){let o=mo(),e=await o.stats();if(e.entries===0){console.log(ve.gray("Cache is already empty."));return}await o.clear(),console.log(ve.green(`Cache cleared: ${e.entries} entries (${Cn(e.totalSize)}) removed.`))}import Q from"chalk";async function In(o,e){(!o||!e)&&(console.error(Q.red("Error: Both key and value are required")),console.error(Q.gray("Usage: kaven config set KEY VALUE")),process.exit(1)),await q.initialize();try{await q.set(o,e),console.log(Q.green(`\u2705 Set ${Q.bold(o)} = ${Q.bold(e)}`))}catch(t){console.error(Q.red(`Error: ${t instanceof Error?t.message:String(t)}`)),process.exit(1)}}async function An(o,e){o||(console.error(Q.red("Error: Key is required")),console.error(Q.gray("Usage: kaven config get KEY")),process.exit(1)),await q.initialize();try{let t=q.get(o);e.json?console.log(JSON.stringify({[o]:t},null,2)):console.log(t)}catch(t){console.error(Q.red(`Error: ${t instanceof Error?t.message:String(t)}`)),process.exit(1)}}async function Mn(o){await q.initialize();let e=q.getAll();if(o.json)console.log(JSON.stringify(e,null,2));else{console.log(Q.bold("Kaven Configuration:")),console.log(Q.gray(`Location: ${q.getConfigDir()}/config.json`)),console.log();let t=Object.entries(e),n=Math.max(...t.map(([a])=>a.length));for(let[a,r]of t){let i=" ".repeat(n-a.length),s=typeof r=="object"?JSON.stringify(r,null,2).replace(/\n/g,`
106
+ `+" ".repeat(n+3)):r;console.log(` ${a}${i} ${Q.cyan(s)}`)}}}async function jn(){let{confirm:o}=await import("@inquirer/prompts");if(!await o({message:"Are you sure you want to reset config to defaults?",default:!1})){console.log(Q.yellow("Cancelled."));return}await q.initialize(),await q.reset(),console.log(Q.green("\u2705 Config reset to defaults"))}import{select as jr,confirm as Tn,text as Tr,multiselect as Rr,isCancel as _r,cancel as Dr}from"@clack/prompts";import W from"picocolors";import go from"fs-extra";import Rn from"path";var ke=[{key:"FEATURE_EMAIL_VERIFICATION",type:"boolean",description:"Email verification on signup",category:"Auth",defaultValue:"true"},{key:"FEATURE_2FA_TOTP",type:"boolean",description:"Two-factor authentication via TOTP",category:"Auth",defaultValue:"false"},{key:"FEATURE_SSO_SAML",type:"boolean",description:"Single Sign-On via SAML/OIDC",category:"Auth",defaultValue:"false"},{key:"FEATURE_SOCIAL_LOGIN",type:"boolean",description:"Login via Google, GitHub, etc.",category:"Auth",defaultValue:"true"},{key:"FEATURE_MAGIC_LINK",type:"boolean",description:"Passwordless login via email",category:"Auth",defaultValue:"false"},{key:"FEATURE_CUSTOM_DOMAIN",type:"boolean",description:"Custom domain per tenant",category:"Tenancy",defaultValue:"false"},{key:"FEATURE_WHITE_LABEL",type:"boolean",description:"Remove Kaven branding",category:"Tenancy",defaultValue:"false"},{key:"FEATURE_MULTI_BUSINESS",type:"boolean",description:"Multiple businesses per user",category:"Tenancy",defaultValue:"false"},{key:"FEATURE_AGENCY_HUB",type:"boolean",description:"Agency management dashboard",category:"Tenancy",defaultValue:"false"},{key:"FEATURE_TENANT_THEMES",type:"boolean",description:"Custom themes for tenants",category:"Tenancy",defaultValue:"true"},{key:"FEATURE_SUBSCRIPTIONS",type:"boolean",description:"Subscription management",category:"Billing",defaultValue:"true"},{key:"FEATURE_INVOICING",type:"boolean",description:"Automatic invoicing",category:"Billing",defaultValue:"true"},{key:"FEATURE_USAGE_BILLING",type:"boolean",description:"Metered usage billing",category:"Billing",defaultValue:"false"},{key:"FEATURE_PADDLE_CHECKOUT",type:"boolean",description:"Paddle payment integration",category:"Billing",defaultValue:"true"},{key:"FEATURE_PAGUBIT_PIX",type:"boolean",description:"Pix payment support",category:"Billing",defaultValue:"false"},{key:"FEATURE_API_ACCESS",type:"boolean",description:"External API access",category:"API",defaultValue:"false"},{key:"FEATURE_WEBHOOKS",type:"boolean",description:"Outgoing webhooks",category:"API",defaultValue:"false"},{key:"FEATURE_MARKETPLACE_ACCESS",type:"boolean",description:"Kaven Marketplace access",category:"API",defaultValue:"true"},{key:"MAX_API_CALLS_MONTH",type:"numeric",description:"Maximum API calls per month",category:"API",defaultValue:"10000"},{key:"MAX_AGENT_API_CALLS_HOUR",type:"numeric",description:"Maximum agent calls per hour",category:"API",defaultValue:"100"},{key:"MAX_TEAM_MEMBERS",type:"numeric",description:"Maximum team members per tenant",category:"Limits",defaultValue:"5"},{key:"MAX_PROJECTS",type:"numeric",description:"Maximum projects per tenant",category:"Limits",defaultValue:"3"},{key:"MAX_STORAGE_GB",type:"numeric",description:"Maximum storage in GB",category:"Limits",defaultValue:"1"},{key:"MAX_TENANTS",type:"numeric",description:"Maximum sub-tenants",category:"Limits",defaultValue:"1"},{key:"FEATURE_PRIORITY_SUPPORT",type:"boolean",description:"Priority support queue",category:"Support",defaultValue:"false"},{key:"FEATURE_AUDIT_COMPLIANCE",type:"boolean",description:"Audit logs and compliance",category:"Support",defaultValue:"false"},{key:"FEATURE_DATA_EXPORT",type:"boolean",description:"Customer data export",category:"Support",defaultValue:"true"}],fo={starter:{FEATURE_EMAIL_VERIFICATION:!0,FEATURE_SOCIAL_LOGIN:!0,FEATURE_MARKETPLACE_ACCESS:!0,MAX_TEAM_MEMBERS:"5",MAX_PROJECTS:"3",MAX_API_CALLS_MONTH:"10000",MAX_AGENT_API_CALLS_HOUR:"100"},complete:{FEATURE_EMAIL_VERIFICATION:!0,FEATURE_SOCIAL_LOGIN:!0,FEATURE_CUSTOM_DOMAIN:!0,FEATURE_API_ACCESS:!0,FEATURE_MARKETPLACE_ACCESS:!0,MAX_TEAM_MEMBERS:"25",MAX_PROJECTS:"20",MAX_API_CALLS_MONTH:"100000",MAX_AGENT_API_CALLS_HOUR:"500"},pro:{FEATURE_EMAIL_VERIFICATION:!0,FEATURE_SOCIAL_LOGIN:!0,FEATURE_CUSTOM_DOMAIN:!0,FEATURE_WHITE_LABEL:!0,FEATURE_API_ACCESS:!0,FEATURE_MARKETPLACE_ACCESS:!0,MAX_TEAM_MEMBERS:"100",MAX_PROJECTS:"100",MAX_API_CALLS_MONTH:"1000000",MAX_AGENT_API_CALLS_HOUR:"2000"},enterprise:{}};function mt(o){_r(o)&&(Dr("Cancelled."),process.exit(0))}async function _n(o){let e=o.outputPath??Rn.join(process.cwd(),"packages","database","prisma","seeds","capabilities.seed.ts");if(o.list){let t=await de.getInstance();Or(t);return}if(o.tier){await Dn(o.tier,e,o);return}await Lr(e,o)}function Or(o){console.log(),console.log(W.bold(W.underline(o.t("config.features.catalogHeader")))),console.log(W.dim(o.t("config.features.capabilitiesTotal",{count:ke.length})+`
107
+ `));let e=[...new Set(ke.map(t=>t.category))];for(let t of e){let n=ke.filter(a=>a.category===t);console.log(W.bold(W.cyan(` ${t} (${n.length})`)));for(let a of n)console.log(` ${W.white(a.key.padEnd(30))} ${W.dim(`[${a.type}]`)}`),console.log(` ${W.dim(a.description)}`);console.log()}console.log(W.bold(o.t("config.features.tierPresets")));for(let t of["starter","complete","pro","enterprise"])console.log(` ${W.white(t.padEnd(12))}`);console.log()}async function Dn(o,e,t){let n=fo[o]||{},a={};for(let r of ke)o==="enterprise"?a[r.key]=r.type==="boolean"?!0:"-1":a[r.key]=n[r.key]??(r.type==="boolean"?!1:r.defaultValue);await On(a,e,t,o)}async function Lr(o,e){let t=await de.getInstance();console.log(),console.log(W.bold(W.underline(t.t("config.features.tuiHeader"))));let n=await jr({message:t.t("config.features.selectTier"),options:[{label:"Starter \u2014 Essential SaaS features",value:"starter"},{label:"Complete \u2014 White-label + Custom Domains",value:"complete"},{label:"Pro \u2014 Extended API + Limits",value:"pro"},{label:"Enterprise \u2014 Unlimited everything",value:"enterprise"},{label:W.dim(t.t("common.cancel")),value:"cancel"}]});if(mt(n),n==="cancel")return;let a=n,r=fo[a]||{},i={},s=await Tn({message:t.t("config.features.customize"),initialValue:!1});if(mt(s),!s)return Dn(a,o,e);let c=[...new Set(ke.map(l=>l.category))];for(let l of c){let d=ke.filter(p=>p.category===l),u=d.filter(p=>p.type==="boolean"),m=d.filter(p=>p.type==="numeric");if(u.length>0){let p=u.filter(y=>a==="enterprise"||r[y.key]===!0).map(y=>y.key),f=await Rr({message:`${l} features:`,options:u.map(y=>({label:`${y.key.padEnd(30)} \u2014 ${y.description}`,value:y.key})),initialValues:p,required:!1});mt(f);for(let y of u)i[y.key]=f.includes(y.key)}for(let p of m){let f=a==="enterprise"?"-1":r[p.key]||p.defaultValue,y=await Tr({message:`${p.key} (${p.description}):`,placeholder:f,defaultValue:f});mt(y),i[p.key]=y}}await On(i,o,e,a)}async function On(o,e,t,n){let a=await de.getInstance(),r=ke.map(s=>{let c=o[s.key];return` { key: "${s.key}", type: "${s.type}", defaultValue: "${c}", description: "${s.description}" },`}).join(`
108
108
  `),i=`// packages/database/prisma/seeds/capabilities.seed.ts
109
109
  // Generated by kaven config features \u2014 ${new Date().toISOString()}
110
110
  // Tier: ${n}
@@ -127,8 +127,8 @@ ${r}
127
127
  }
128
128
  }
129
129
  `;if(t.dryRun){console.log(W.bold(`
130
- `+a.t("config.features.dryRunHeader"))),console.log(i);return}if(go.existsSync(e)&&!t.force){let s=await jn({message:a.t("config.features.confirmOverwrite"),initialValue:!1});if(mt(s),!s)return}await go.ensureDir(Tn.dirname(e)),await go.writeFile(e,i,"utf-8"),console.log(W.green(`
131
- \u2705 ${a.t("config.features.seedWritten",{path:e})}`)),console.log(W.dim(a.t("config.features.runSeed")))}import ee from"chalk";import Or from"ora";import Xe from"path";import De from"fs-extra";var Lr=`name: Tests
130
+ `+a.t("config.features.dryRunHeader"))),console.log(i);return}if(go.existsSync(e)&&!t.force){let s=await Tn({message:a.t("config.features.confirmOverwrite"),initialValue:!1});if(mt(s),!s)return}await go.ensureDir(Rn.dirname(e)),await go.writeFile(e,i,"utf-8"),console.log(W.green(`
131
+ \u2705 ${a.t("config.features.seedWritten",{path:e})}`)),console.log(W.dim(a.t("config.features.runSeed")))}import ee from"chalk";import Fr from"ora";import Xe from"path";import De from"fs-extra";var Ur=`name: Tests
132
132
  on:
133
133
  push:
134
134
  branches: [main, develop]
@@ -164,7 +164,7 @@ jobs:
164
164
  - run: pnpm lint
165
165
  - run: pnpm test
166
166
  - run: pnpm build
167
- `,Fr=`name: Publish Module
167
+ `,Nr=`name: Publish Module
168
168
  on:
169
169
  push:
170
170
  tags:
@@ -188,7 +188,7 @@ jobs:
188
188
  env:
189
189
  KAVEN_LICENSE_KEY: \${{ secrets.KAVEN_LICENSE_KEY }}
190
190
  run: kaven module publish
191
- `,Ur=`#!/bin/bash
191
+ `,Kr=`#!/bin/bash
192
192
  # Pre-commit hook for Kaven projects
193
193
  set -e
194
194
 
@@ -209,7 +209,7 @@ if command -v pnpm format &> /dev/null; then
209
209
  fi
210
210
 
211
211
  echo "\u2705 All pre-commit checks passed"
212
- `;async function On(o){let e=process.cwd(),t=Xe.join(e,"package.json");await De.pathExists(t)||(console.error(ee.red("Error: package.json not found. Run this in a Kaven project root.")),process.exit(1));let n=Or("Setting up CI/CD configuration...").start();try{let a=Xe.join(e,".github","workflows");await De.ensureDir(a);let r=Xe.join(a,"test.yml");o.dryRun||await De.writeFile(r,Lr),n.text=`Creating ${ee.cyan(".github/workflows/test.yml")}...`;let i=Xe.join(a,"publish.yml");o.dryRun||await De.writeFile(i,Fr),n.text=`Creating ${ee.cyan(".github/workflows/publish.yml")}...`;let s=Xe.join(e,".husky");await De.ensureDir(s);let c=Xe.join(s,"pre-commit");o.dryRun||(await De.writeFile(c,Ur),await De.chmod(c,493)),n.text=`Creating ${ee.cyan(".husky/pre-commit")}...`,n.succeed("CI/CD configuration created"),console.log(),console.log(ee.bold("Files created:")),console.log(` ${ee.cyan(".github/workflows/test.yml")} - Run tests on push/PR`),console.log(` ${ee.cyan(".github/workflows/publish.yml")} - Publish on git tags`),console.log(` ${ee.cyan(".husky/pre-commit")} - Local pre-commit validation`),console.log(),console.log(ee.bold("Next steps:")),console.log(ee.gray(" 1. Install husky: pnpm husky install")),console.log(ee.gray(" 2. Add GitHub secrets: KAVEN_LICENSE_KEY")),console.log(ee.gray(" 3. Push to GitHub and watch workflows run")),o.dryRun&&(console.log(),console.log(ee.yellow("(Dry-run: No files were actually created)")))}catch(a){n.fail("Failed to create CI/CD configuration"),console.error(ee.red(a instanceof Error?a.message:String(a))),process.exit(1)}}import Ce from"picocolors";import{spinner as Nr}from"@clack/prompts";function Ln(o){let e=o.command("aiox").description("AIOX integration utilities");e.command("bootstrap").description("Run AIOX environment bootstrap in current project").option("--skip-aiox","Skip AIOX logic (for testing)").action(async t=>{let n=process.cwd();await dt(n,t)}),e.command("install-squad").description("Install or repair kaven-squad in current project (resumes incomplete kaven init)").action(async()=>{let t=process.cwd(),n=new Be,a=Nr();a.start("Installing kaven-squad...");let r=await n.installSquad(t);if(r.installed){a.stop(Ce.green("kaven-squad installed \u2713")),a.start("Installing AIOX Core runtime...");let i=await n.installAIOXCore(t);i.installed?a.stop(Ce.green("AIOX Core installed \u2713")):(a.stop(Ce.yellow(`AIOX Core skipped: ${i.reason}`)),console.log(Ce.dim(" Run manually: npx aiox-core@latest install --quiet")))}else r.reason==="already-exists"?(a.stop(Ce.yellow("kaven-squad already installed \u2014 nothing to do")),console.log(Ce.dim(" To reinstall: remove squads/kaven-squad/ and re-run"))):(a.stop(Ce.red(`Squad install failed: ${r.reason}`)),console.log(Ce.dim(" Manual fallback: git clone https://github.com/bychrisr/kaven-squad squads/kaven-squad")),process.exit(1))})}var Vr=()=>{let o=new Kr;o.name("kaven").description("The official CLI for the Kaven SaaS boilerplate ecosystem").version("0.4.2-alpha.0").addHelpText("after",`
212
+ `;async function Ln(o){let e=process.cwd(),t=Xe.join(e,"package.json");await De.pathExists(t)||(console.error(ee.red("Error: package.json not found. Run this in a Kaven project root.")),process.exit(1));let n=Fr("Setting up CI/CD configuration...").start();try{let a=Xe.join(e,".github","workflows");await De.ensureDir(a);let r=Xe.join(a,"test.yml");o.dryRun||await De.writeFile(r,Ur),n.text=`Creating ${ee.cyan(".github/workflows/test.yml")}...`;let i=Xe.join(a,"publish.yml");o.dryRun||await De.writeFile(i,Nr),n.text=`Creating ${ee.cyan(".github/workflows/publish.yml")}...`;let s=Xe.join(e,".husky");await De.ensureDir(s);let c=Xe.join(s,"pre-commit");o.dryRun||(await De.writeFile(c,Kr),await De.chmod(c,493)),n.text=`Creating ${ee.cyan(".husky/pre-commit")}...`,n.succeed("CI/CD configuration created"),console.log(),console.log(ee.bold("Files created:")),console.log(` ${ee.cyan(".github/workflows/test.yml")} - Run tests on push/PR`),console.log(` ${ee.cyan(".github/workflows/publish.yml")} - Publish on git tags`),console.log(` ${ee.cyan(".husky/pre-commit")} - Local pre-commit validation`),console.log(),console.log(ee.bold("Next steps:")),console.log(ee.gray(" 1. Install husky: pnpm husky install")),console.log(ee.gray(" 2. Add GitHub secrets: KAVEN_LICENSE_KEY")),console.log(ee.gray(" 3. Push to GitHub and watch workflows run")),o.dryRun&&(console.log(),console.log(ee.yellow("(Dry-run: No files were actually created)")))}catch(a){n.fail("Failed to create CI/CD configuration"),console.error(ee.red(a instanceof Error?a.message:String(a))),process.exit(1)}}import Ce from"picocolors";import{spinner as Vr}from"@clack/prompts";function Fn(o){let e=o.command("aiox").description("AIOX integration utilities");e.command("bootstrap").description("Run AIOX environment bootstrap in current project").option("--skip-aiox","Skip AIOX logic (for testing)").action(async t=>{let n=process.cwd();await dt(n,t)}),e.command("install-squad").description("Install or repair kaven-squad in current project (resumes incomplete kaven init)").action(async()=>{let t=process.cwd(),n=new Be,a=Vr();a.start("Installing kaven-squad...");let r=await n.installSquad(t);if(r.installed){a.stop(Ce.green("kaven-squad installed \u2713")),a.start("Installing AIOX Core runtime...");let i=await n.installAIOXCore(t);i.installed?a.stop(Ce.green("AIOX Core installed \u2713")):(a.stop(Ce.yellow(`AIOX Core skipped: ${i.reason}`)),console.log(Ce.dim(" Run manually: npx aiox-core@latest install --quiet")))}else r.reason==="already-exists"?(a.stop(Ce.yellow("kaven-squad already installed \u2014 nothing to do")),console.log(Ce.dim(" To reinstall: remove squads/kaven-squad/ and re-run"))):(a.stop(Ce.red(`Squad install failed: ${r.reason}`)),console.log(Ce.dim(" Manual fallback: git clone https://github.com/bychrisr/kaven-squad squads/kaven-squad")),process.exit(1))})}var qr=()=>{let o=new Br;o.name("kaven").description("The official CLI for the Kaven SaaS boilerplate ecosystem").version(ko.version).addHelpText("after",`
213
213
  Examples:
214
214
  $ kaven init my-saas-app Bootstrap a new Kaven project
215
215
  $ kaven auth login Authenticate with Kaven Marketplace
@@ -227,7 +227,7 @@ Examples:
227
227
  $ kaven init my-app --skip-git Skip git initialization
228
228
  $ kaven init my-app --with-squad Install kaven-squad for AIOX integration
229
229
  $ kaven init my-app --resume Resume incomplete setup from last step
230
- `).action((c,l)=>mn(c,{defaults:l.defaults,skipInstall:l.skipInstall,skipGit:l.skipGit,force:l.force,template:l.template,withSquad:l.withSquad,skipAiox:l.skipAiox,resume:l.resume}));let e=o.command("module").alias("m").description("Manage Kaven modules: install, remove, publish, activate, and diagnose").addHelpText("after",`
230
+ `).action((c,l)=>fn(c,{defaults:l.defaults,skipInstall:l.skipInstall,skipGit:l.skipGit,force:l.force,template:l.template,withSquad:l.withSquad,skipAiox:l.skipAiox,resume:l.resume}));let e=o.command("module").alias("m").description("Manage Kaven modules: install, remove, publish, activate, and diagnose").addHelpText("after",`
231
231
  Examples:
232
232
  $ kaven module doctor Check module integrity
233
233
  $ kaven module doctor --fix Auto-fix detected issues
@@ -247,15 +247,15 @@ Examples:
247
247
  $ kaven module doctor
248
248
  $ kaven module doctor --fix
249
249
  $ kaven module doctor --json
250
- `).action(c=>So({fix:c.fix,json:c.json})),e.command("add <path>").description("Install a module from a local manifest file").addHelpText("after",`
250
+ `).action(c=>Co({fix:c.fix,json:c.json})),e.command("add <path>").description("Install a module from a local manifest file").addHelpText("after",`
251
251
  Examples:
252
252
  $ kaven module add ./modules/payments/module.json
253
253
  $ kaven module add /absolute/path/to/module.json
254
- `).action(c=>$o(c)),e.command("remove <name>").description("Remove an installed module and clean up injected code").addHelpText("after",`
254
+ `).action(c=>Io(c)),e.command("remove <name>").description("Remove an installed module and clean up injected code").addHelpText("after",`
255
255
  Examples:
256
256
  $ kaven module remove payments
257
257
  $ kaven module remove notifications
258
- `).action(c=>Io(c)),e.command("publish").description("Publish the current directory as a module to Kaven Marketplace").option("--dry-run","Validate and package the module without uploading").option("--changelog <text>","Release notes for this version").addHelpText("after",`
258
+ `).action(c=>Ao(c)),e.command("publish").description("Publish the current directory as a module to Kaven Marketplace").option("--dry-run","Validate and package the module without uploading").option("--changelog <text>","Release notes for this version").addHelpText("after",`
259
259
  Requirements:
260
260
  - module.json must exist in the current directory
261
261
  - Must be authenticated: run 'kaven auth login' first
@@ -264,7 +264,7 @@ Examples:
264
264
  $ kaven module publish
265
265
  $ kaven module publish --dry-run
266
266
  $ kaven module publish --changelog "Added dark mode support"
267
- `).action(c=>_o({dryRun:c.dryRun,changelog:c.changelog})),e.command("activate <name> [root]").description("Activate a Kaven schema module by uncommenting its models in schema.extended.prisma").option("--with-deps","Automatically activate required dependencies").option("--skip-migrate","Skip db:generate and db:migrate after activation").option("--dry-run","Show affected models without modifying schema").option("--yes","Skip confirmation prompt").addHelpText("after",`
267
+ `).action(c=>Do({dryRun:c.dryRun,changelog:c.changelog})),e.command("activate <name> [root]").description("Activate a Kaven schema module by uncommenting its models in schema.extended.prisma").option("--with-deps","Automatically activate required dependencies").option("--skip-migrate","Skip db:generate and db:migrate after activation").option("--dry-run","Show affected models without modifying schema").option("--yes","Skip confirmation prompt").addHelpText("after",`
268
268
  Modules: billing, projects, notifications
269
269
 
270
270
  Examples:
@@ -279,11 +279,11 @@ Examples:
279
279
  $ kaven module deactivate billing
280
280
  $ kaven module deactivate projects
281
281
  $ kaven module deactivate projects ./my-app
282
- `).action((c,l,d)=>Fo(c,l,d)),e.command("list [root]").description("List available Kaven schema modules with their status, models, and dependencies").addHelpText("after",`
282
+ `).action((c,l,d)=>Uo(c,l,d)),e.command("list [root]").description("List available Kaven schema modules with their status, models, and dependencies").addHelpText("after",`
283
283
  Examples:
284
284
  $ kaven module list
285
285
  $ kaven module list ./my-app
286
- `).action(c=>Uo(c)),e.command("update [slug]").description("Update an installed marketplace module to the latest version").option("--skip-verify","Skip Ed25519 signature verification (dev only)").addHelpText("after",`
286
+ `).action(c=>No(c)),e.command("update [slug]").description("Update an installed marketplace module to the latest version").option("--skip-verify","Skip Ed25519 signature verification (dev only)").addHelpText("after",`
287
287
  Examples:
288
288
  $ kaven module update payments Update the payments module
289
289
  $ kaven module update Interactive: select module to update
@@ -293,13 +293,13 @@ Examples:
293
293
  $ kaven auth login Start device code authentication flow
294
294
  $ kaven auth whoami Show current user info
295
295
  $ kaven auth logout End the local session
296
- `);t.command("login").description("Start the interactive device code authentication flow (RFC 8628)").action(()=>Ko()),t.command("logout").description("Clear the local authentication session").action(()=>Vo()),t.command("whoami").description("Display information about the currently authenticated user").action(()=>Bo());let n=o.command("marketplace").alias("mkt").alias("market").description("Explore, browse, and install modules from the Kaven Marketplace").addHelpText("after",`
296
+ `);t.command("login").description("Start the interactive device code authentication flow (RFC 8628)").action(()=>Vo()),t.command("logout").description("Clear the local authentication session").action(()=>Bo()),t.command("whoami").description("Display information about the currently authenticated user").action(()=>qo());let n=o.command("marketplace").alias("mkt").alias("market").description("Explore, browse, and install modules from the Kaven Marketplace").addHelpText("after",`
297
297
  Examples:
298
298
  $ kaven marketplace list
299
299
  $ kaven marketplace list --category auth --sort popular
300
300
  $ kaven marketplace install payments
301
301
  $ kaven marketplace browse
302
- `);n.command("list").description("List all modules available in the marketplace").option("--category <category>","Filter modules by category").option("--sort <field>","Sort order: newest (default), popular, name","newest").option("--page <n>","Page number (default: 1)","1").option("--limit <n>","Results per page (default: 20, max: 100)","20").option("--json","Output raw JSON instead of formatted table").action(c=>qo({category:c.category,sort:c.sort,page:parseInt(c.page,10),limit:parseInt(c.limit,10),json:c.json??!1})),n.command("install <moduleId>").description("Download and install a module from the Kaven Marketplace").option("--version <ver>","Install a specific version (default: latest)").option("--force","Skip overwrite confirmation").option("--skip-env","Skip environment variable injection").option("--skip-verify","Skip Ed25519 signature verification (dev only)").option("--env-file <path>","Target .env file (default: .env)").addHelpText("after",`
302
+ `);n.command("list").description("List all modules available in the marketplace").option("--category <category>","Filter modules by category").option("--sort <field>","Sort order: newest (default), popular, name","newest").option("--page <n>","Page number (default: 1)","1").option("--limit <n>","Results per page (default: 20, max: 100)","20").option("--json","Output raw JSON instead of formatted table").action(c=>Jo({category:c.category,sort:c.sort,page:parseInt(c.page,10),limit:parseInt(c.limit,10),json:c.json??!1})),n.command("install <moduleId>").description("Download and install a module from the Kaven Marketplace").option("--version <ver>","Install a specific version (default: latest)").option("--force","Skip overwrite confirmation").option("--skip-env","Skip environment variable injection").option("--skip-verify","Skip Ed25519 signature verification (dev only)").option("--env-file <path>","Target .env file (default: .env)").addHelpText("after",`
303
303
  Examples:
304
304
  $ kaven marketplace install payments
305
305
  $ kaven marketplace install payments --version 1.2.0
@@ -308,28 +308,28 @@ Examples:
308
308
  `).action((c,l)=>$t(c,{version:l.version,force:l.force??!1,skipEnv:l.skipEnv??!1,skipVerify:l.skipVerify??!1,envFile:l.envFile})),n.command("browse").description("Interactive TUI module browser \u2014 explore modules by category").addHelpText("after",`
309
309
  Navigate with arrow keys, press Enter to select.
310
310
  Supports category filtering and pagination.
311
- `).action(()=>Wo()),o.command("update").description("Check and apply updates for CLI and installed modules").option("--core","Update only the CLI itself").option("--module <slug>","Update a specific installed module").option("--all","Apply all available updates (CLI + modules)").option("--check","Check for updates without applying").option("--skip-verify","Skip Ed25519 signature verification (dev only)").addHelpText("after",`
311
+ `).action(()=>Xo()),o.command("update").description("Check and apply updates for CLI and installed modules").option("--core","Update only the CLI itself").option("--module <slug>","Update a specific installed module").option("--all","Apply all available updates (CLI + modules)").option("--check","Check for updates without applying").option("--skip-verify","Skip Ed25519 signature verification (dev only)").addHelpText("after",`
312
312
  Examples:
313
313
  $ kaven update Check all updates (CLI + modules)
314
314
  $ kaven update --core Check/apply CLI update only
315
315
  $ kaven update --module payments Update the payments module
316
316
  $ kaven update --all Apply all updates
317
317
  $ kaven update --check List updates without applying
318
- `).action(c=>En({core:c.core,module:c.module,all:c.all,check:c.check,skipVerify:c.skipVerify}));let a=o.command("upgrade").description("Upgrade your Kaven license plan").addHelpText("after",`
318
+ `).action(c=>Sn({core:c.core,module:c.module,all:c.all,check:c.check,skipVerify:c.skipVerify}));let a=o.command("upgrade").description("Upgrade your Kaven license plan").addHelpText("after",`
319
319
  Examples:
320
320
  $ kaven upgrade Upgrade license tier (interactive)
321
321
  $ kaven upgrade check Check current license status
322
322
  $ kaven upgrade install Force install latest CLI (manual)
323
323
 
324
324
  Note: To update the CLI or modules, use \`kaven update\` instead.
325
- `);a.command("tier").description("Upgrade your Kaven license to a higher tier (default)").option("--no-browser","Print the checkout URL instead of opening the browser").action(c=>bn({browser:c.browser!==!1})),a.command("check").description("Check current license status").action(()=>so()),a.command("install").description("Force install the latest Kaven CLI version globally").action(()=>co()),o.command("telemetry").description("View observability and command audit logs").command("view").description("Display the most recent local telemetry events").option("-l, --limit <number>","Number of events to display","10").action(c=>Xo(parseInt(c.limit))),o.addCommand(tn());let i=o.command("cache").description("Manage the local API response cache").addHelpText("after",`
325
+ `);a.command("tier").description("Upgrade your Kaven license to a higher tier (default)").option("--no-browser","Print the checkout URL instead of opening the browser").action(c=>xn({browser:c.browser!==!1})),a.command("check").description("Check current license status").action(()=>so()),a.command("install").description("Force install the latest Kaven CLI version globally").action(()=>co()),o.command("telemetry").description("View observability and command audit logs").command("view").description("Display the most recent local telemetry events").option("-l, --limit <number>","Number of events to display","10").action(c=>Yo(parseInt(c.limit))),o.addCommand(on());let i=o.command("cache").description("Manage the local API response cache").addHelpText("after",`
326
326
  Cache directory: ~/.kaven/cache (max 50 MB)
327
327
  Cached data: module listings (24h TTL), manifests (7d), license status (1h)
328
328
 
329
329
  Examples:
330
330
  $ kaven cache status
331
331
  $ kaven cache clear
332
- `);i.command("status").description("Show cache statistics (size, entry count, age)").action(()=>Cn()),i.command("clear").description("Delete all locally cached API responses").action(()=>Pn());let s=o.command("config").description("Manage Kaven CLI configuration").addHelpText("after",`
332
+ `);i.command("status").description("Show cache statistics (size, entry count, age)").action(()=>Pn()),i.command("clear").description("Delete all locally cached API responses").action(()=>$n());let s=o.command("config").description("Manage Kaven CLI configuration").addHelpText("after",`
333
333
  Config file: ~/.kaven/config.json
334
334
 
335
335
  Examples:
@@ -340,7 +340,7 @@ Examples:
340
340
  $ kaven config features
341
341
  $ kaven config features --tier complete
342
342
  $ kaven config features --list
343
- `);s.command("set <key> <value>").description("Set a configuration value").action((c,l)=>$n(c,l)),s.command("get <key>").description("Get a configuration value").option("--json","Output as JSON").action((c,l)=>In(c,{json:l.json})),s.command("view").description("Display all configuration").option("--json","Output as JSON").action(c=>An({json:c.json})),s.command("reset").description("Reset configuration to defaults").action(()=>Mn()),s.command("features").description("Interactive TUI to select and configure the 60 framework capabilities (feature flags)").option("--tier <tier>","Apply a preset tier directly without prompts: starter | complete | pro | enterprise").option("--list","List all available capabilities grouped by category, without modifying anything").addHelpText("after",`
343
+ `);s.command("set <key> <value>").description("Set a configuration value").action((c,l)=>In(c,l)),s.command("get <key>").description("Get a configuration value").option("--json","Output as JSON").action((c,l)=>An(c,{json:l.json})),s.command("view").description("Display all configuration").option("--json","Output as JSON").action(c=>Mn({json:c.json})),s.command("reset").description("Reset configuration to defaults").action(()=>jn()),s.command("features").description("Interactive TUI to select and configure the 60 framework capabilities (feature flags)").option("--tier <tier>","Apply a preset tier directly without prompts: starter | complete | pro | enterprise").option("--list","List all available capabilities grouped by category, without modifying anything").addHelpText("after",`
344
344
  Output: packages/database/prisma/seeds/capabilities.seed.ts (relative to cwd)
345
345
 
346
346
  Examples:
@@ -351,7 +351,7 @@ Examples:
351
351
 
352
352
  After generating the seed file:
353
353
  $ pnpm prisma db seed
354
- `).action(c=>Rn({tier:c.tier,list:c.list??!1})),o.command("init-ci").description("Initialize GitHub Actions CI/CD workflows").option("--dry-run","Show what would be created without writing files").addHelpText("after",`
354
+ `).action(c=>_n({tier:c.tier,list:c.list??!1})),o.command("init-ci").description("Initialize GitHub Actions CI/CD workflows").option("--dry-run","Show what would be created without writing files").addHelpText("after",`
355
355
  Creates:
356
356
  - .github/workflows/test.yml Run tests on push/PR
357
357
  - .github/workflows/publish.yml Publish modules on git tags
@@ -360,4 +360,4 @@ Creates:
360
360
  Examples:
361
361
  $ kaven init-ci Interactive setup
362
362
  $ kaven init-ci --dry-run Show what would be created
363
- `).action(c=>On({dryRun:c.dryRun})),Ln(o),o.parse(process.argv)},Br=process.argv[1]&&(process.argv[1].endsWith("/kaven")||process.argv[1].endsWith("/index.ts")||process.argv[1].endsWith("/index.js"));Br&&Vr();export{Vr as main};
363
+ `).action(c=>Ln({dryRun:c.dryRun})),Fn(o),o.parse(process.argv)},Jr=process.argv[1]&&(process.argv[1].endsWith("/kaven")||process.argv[1].endsWith("/index.ts")||process.argv[1].endsWith("/index.js"));Jr&&qr();export{qr as main};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kaven-cli",
3
- "version": "0.11.1",
3
+ "version": "0.11.3",
4
4
  "type": "module",
5
5
  "description": "Kaven CLI - The official command line tool for Kaven",
6
6
  "main": "dist/index.js",
@@ -75,27 +75,30 @@
75
75
  "fs-extra": "^11.3.3",
76
76
  "glob": "^13.0.0",
77
77
  "i18next": "^26.0.6",
78
- "js-yaml": "^4.1.1",
78
+ "js-yaml": "^4.2.0",
79
79
  "open": "^10.1.0",
80
80
  "ora": "^9.1.0",
81
81
  "picocolors": "^1.1.1",
82
- "tar": "^7.5.9",
82
+ "tar": "^7.5.16",
83
83
  "zod": "^4.3.6"
84
84
  },
85
85
  "pnpm": {
86
86
  "overrides": {
87
87
  "minimatch@<10.2.3": ">=10.2.3",
88
88
  "@isaacs/brace-expansion@<=5.0.0": ">=5.0.1",
89
+ "brace-expansion@>=5.0.0 <5.0.6": ">=5.0.6",
89
90
  "esbuild@<=0.24.2": ">=0.25.0",
91
+ "esbuild@>=0.27.0 <0.28.1": ">=0.28.1",
90
92
  "handlebars@<=4.7.8": ">=4.7.9",
91
- "tar@<=7.5.10": ">=7.5.11",
93
+ "tar@<=7.5.15": ">=7.5.16",
92
94
  "lodash@<=4.17.23": ">=4.18.0",
93
95
  "lodash-es@<=4.17.23": ">=4.18.0",
94
96
  "rollup@<4.59.0": ">=4.59.0",
95
97
  "vite@<=6.4.1": ">=6.4.2",
96
98
  "flatted@<=3.4.1": ">=3.4.2",
97
99
  "picomatch@<2.3.2": ">=2.3.2",
98
- "picomatch@>=4.0.0 <4.0.4": ">=4.0.4"
100
+ "picomatch@>=4.0.0 <4.0.4": ">=4.0.4",
101
+ "undici": ">=7.28.0"
99
102
  }
100
103
  }
101
104
  }
@@ -1 +0,0 @@
1
- import{g as a}from"./chunk-JHLQ46NG.js";export{a as MarketplaceClient};
@@ -1 +0,0 @@
1
- import v from"fs-extra";import q from"path";import x from"os";var c=class extends Error{constructor(e){super(e),this.name="MarketplaceError"}},T=class extends c{constructor(e){super(e),this.name="AuthenticationError"}},E=class extends c{constructor(t,s){super(s);this.requiredTier=t;this.name="LicenseRequiredError"}},p=class extends c{constructor(e){super(e),this.name="NotFoundError"}},R=class extends c{constructor(t){super(`Rate limited. Try again in ${t}s`);this.retryAfter=t;this.name="RateLimitError"}},m=class extends c{constructor(e){super(e),this.name="ServerError"}},y=class extends c{constructor(e){super(e),this.name="NetworkError"}},A=class extends c{constructor(e){super(e),this.name="SignatureVerificationError"}};function S(r){if(r instanceof Error)return r;if(typeof r=="string")return new Error(r);try{return new Error(JSON.stringify(r))}catch{return new Error(String(r))}}var O="https://marketplace.kaven.site",_=3e4,P=3,C=1e3;function b(r){process.env.KAVEN_DEBUG==="1"&&console.debug(`[kaven:debug] ${r}`)}async function D(){try{let r=q.join(x.homedir(),".kaven","config.json");if(await v.pathExists(r)){let e=await v.readJson(r);if(typeof e.apiUrl=="string"&&e.apiUrl)return e.apiUrl}}catch{}return null}async function I(){if(process.env.KAVEN_SERVICE_TOKEN)return process.env.KAVEN_SERVICE_TOKEN;try{let r=q.join(x.homedir(),".kaven","config.json");if(await v.pathExists(r)){let e=await v.readJson(r);if(typeof e.serviceToken=="string"&&e.serviceToken)return e.serviceToken}}catch{}return null}async function j(){if(process.env.KAVEN_API_URL)return process.env.KAVEN_API_URL.replace(/\/$/,"");let r=await D();return r?r.replace(/\/$/,""):O}function K(r){return new Promise(e=>setTimeout(e,r))}function G(r){return r>=500}var $=class{baseURLPromise;authService;constructor(e){this.authService=e??null,this.baseURLPromise=j()}async resolveUrl(e){return`${await this.baseURLPromise}${e}`}async request(e,t,s={}){let k=`${await this.baseURLPromise}${t}`,{body:d,authenticated:U=!1}=s,g=null;for(let u=0;u<=P;u++){if(u>0){let l=C*Math.pow(2,u-1);b(`Retry attempt ${u} for ${e} ${t} (delay ${l}ms)`),await K(l)}try{let l={"Content-Type":"application/json",Accept:"application/json"};if(U&&this.authService){let h=await this.authService.getValidToken();l.Authorization=`Bearer ${h}`}let n=await I();n&&(l["X-Service-Token"]=n),b(`${e} ${k}`);let w=new AbortController,M=setTimeout(()=>w.abort(),_),o;try{o=await fetch(k,{method:e,headers:l,body:d!==void 0?JSON.stringify(d):void 0,signal:w.signal})}finally{clearTimeout(M)}if(b(`Response: ${o.status} ${o.statusText}`),!o.ok){let h=await o.text().catch(()=>""),f=h;try{let a=JSON.parse(h);f=a.message||a.error||h}catch{}switch(o.status){case 401:throw new T(f||"Authentication required");case 403:{let a="pro";try{a=JSON.parse(h).requiredTier||a}catch{}throw new E(a,f||"License required")}case 404:throw new p(f||"Resource not found");case 429:{let a=parseInt(o.headers.get("retry-after")??"60",10);throw new R(isNaN(a)?60:a)}default:if(G(o.status)){g=new m(f||`Server error: ${o.status}`);continue}throw new m(f||`Server error: ${o.status}`)}}return(o.headers.get("content-type")??"").includes("application/json")?await o.json():{}}catch(l){let n=S(l);if(n instanceof T||n instanceof E||n instanceof p||n instanceof R)throw n;if(n instanceof TypeError||n.name==="AbortError"){let w=new y(n.name==="AbortError"?"Request timed out after 30s":`Network error: ${n.message}`);if(g=w,u<P)continue;throw w}if(n instanceof m){if(g=n,u<P)continue;throw n}throw n}}throw g??new y("Request failed after retries")}async requestDeviceCode(){return this.request("POST","/auth/device-code",{body:{client_id:"kaven-cli"},authenticated:!1})}async pollDeviceToken(e){try{let s=`${await this.baseURLPromise}/auth/token`,i=new AbortController,k=setTimeout(()=>i.abort(),_),d;try{d=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({device_code:e,grant_type:"urn:ietf:params:oauth:grant-type:device_code"}),signal:i.signal})}finally{clearTimeout(k)}if(d.ok)return{status:"success",tokens:await d.json()};let g=(await d.json().catch(()=>({}))).error??"unknown_error";switch(g){case"authorization_pending":return{status:"authorization_pending"};case"slow_down":return{status:"slow_down"};case"access_denied":return{status:"access_denied"};case"expired_token":return{status:"expired_token"};default:throw new Error(`Unexpected error: ${g}`)}}catch(t){let s=S(t),i=s;throw i.code==="ECONNREFUSED"||i.code==="ENOTFOUND"?new y("Network error. Check your connection and try again."):s}}async refreshToken(e){return this.request("POST","/auth/refresh",{body:{refresh_token:e},authenticated:!1})}async listModules(e){let t=new URLSearchParams;e?.category&&t.set("category",e.category),e?.tier&&t.set("tier",e.tier),e?.q&&t.set("q",e.q),e?.page&&t.set("page",String(e.page)),e?.pageSize&&t.set("pageSize",String(e.pageSize));let s=t.toString(),i=`/modules${s?`?${s}`:""}`;return this.request("GET",i)}async getModule(e){return this.request("GET",`/modules/${e}`)}async getManifest(e,t){return this.request("GET",`/modules/${e}/versions/${t}/manifest`)}async createDownloadToken(e,t){return this.request("POST","/download-tokens",{body:{moduleSlug:e,version:t},authenticated:!0})}async validateLicense(e,t){return await this.request("POST","/licenses/validate",{body:{licenseKey:e,requiredTier:t}})}async getLicenseStatus(e){return this.request("GET",`/licenses/status?key=${encodeURIComponent(e)}`)}async getModuleManifest(e){try{return await this.getManifest(e,"latest")}catch(t){if(t instanceof p)return null;throw t}}async getReleaseInfo(e,t){return this.request("GET",`/modules/${e}/versions/${t}`)}async getUploadUrl(e,t,s){return this.request("POST","/releases/upload-url",{body:{moduleSlug:e,version:t,size:s},authenticated:!0})}async createRelease(e){return this.request("POST","/releases",{body:e,authenticated:!0})}async createCheckoutSession(e,t){return this.request("POST","/checkout/session",{body:{tier:e,licenseKey:t},authenticated:!0})}async getCheckoutStatus(e){return this.request("GET",`/checkout/session/${e}/status`,{authenticated:!0})}async getCategories(){try{let e=await this.request("GET","/categories",{authenticated:!1});return Array.isArray(e)?e:e.categories??[]}catch(e){if(e instanceof p){let s=(await this.request("GET","/search?q=",{authenticated:!1})).facets?.categories?.map(i=>i.category).filter(Boolean)??[];return[...new Set(s)].sort()}throw e}}};export{T as a,E as b,p as c,y as d,A as e,S as f,$ as g};