@lunora/cli
Version:
The Lunora CLI: init, dev, deploy, codegen, migrate, seed, doctor, insights, logs, registry, and the rest of the project commands
2 lines (1 loc) • 3.83 kB
JavaScript
import{r as x}from"../packem_shared/admin-token-VdUnvnKW.mjs";import{r as E,a as D}from"../packem_shared/admin-url-DJB8P6Jh.mjs";import{d as U}from"../packem_shared/command-BpMv2n0o.mjs";import{EXIT_CODE as i,exitCodeForStatus as T}from"../packem_shared/EXIT_CODE-DQVhSE-1.mjs";import{a as N}from"../packem_shared/resolve-target-C_ZloTvd.mjs";const S=["migrations"],p=e=>S.some(r=>r===e),l="/_lunora/admin/tokens",O=864e5,v=["create","list","revoke"],a=(e,r,t)=>(e.error(r),{code:t,error:r}),f=(e,r)=>e===void 0?r:new Date(e).toISOString(),$=e=>{const r=(e??"").split(",").map(n=>n.trim()).filter(n=>n!==""),t=r.filter(n=>!p(n));return t.length>0?{error:`unknown scope ${t.join(", ")} — expected ${S.join(", ")}`}:{scopes:[...new Set(r.filter(p))]}},g=(e,r,t)=>{if(e==="list")return{method:"GET",path:l};if(e==="revoke")return{method:"DELETE",path:`${l}?name=${encodeURIComponent(r.name??"")}`};const n=r.expiresDays===void 0?void 0:(r.now??Date.now)()+r.expiresDays*O;return{body:JSON.stringify({expiresAt:n,name:r.name,...t.length===0?{}:{scopes:t}}),method:"POST",path:l}},_=(e,r,t,n)=>{if(r==="create"){e.success(`created admin token "${String(t.name)}" — copy it now, it is not shown again:`),e.info(String(t.token)),Array.isArray(t.scopes)&&t.scopes.length>0&&e.info(`scopes: ${t.scopes.map(String).join(", ")}`);return}if(r==="revoke"){e.success(`revoked admin token "${String(n)}"`);return}const s=t.tokens??[];s.length===0&&e.info("no named admin tokens");for(const o of s){const c=o.scopes===void 0||o.scopes.length===0?"":` scopes ${o.scopes.join(",")}`;e.info(`${o.name} created ${f(o.createdAt,"?")} last used ${f(o.lastUsedAt,"never")} expires ${f(o.expiresAt,"never")}${c}${o.revoked===!0?" revoked (its row still holds the name: revoke it again to free it)":""}`)}},b=(e,r)=>{const{logger:t}=e;if(r!=="list"&&(e.name===void 0||e.name===""))return a(t,`admin-token ${r}: a token name is required`,i.USAGE);if(e.expiresDays!==void 0&&(!Number.isFinite(e.expiresDays)||e.expiresDays<=0))return a(t,"--expires-days must be a positive number",i.USAGE);if(e.scope!==void 0&&r!=="create")return a(t,"--scope applies to `admin-token create` only",i.USAGE);const n=$(e.scope);if("error"in n)return a(t,`admin-token create: ${n.error}`,i.USAGE);if(e.prod&&e.url===void 0)return a(t,"--prod requires an explicit --url",i.USAGE)},G=async e=>{const{logger:r}=e,t=v.find(y=>y===e.subcommand);if(t===void 0)return a(r,`admin-token: unknown subcommand "${e.subcommand??""}" — expected ${v.join(" | ")}`,i.USAGE);const n=b(e,t);if(n!==void 0)return n;const s=$(e.scope),o="scopes"in s?s.scopes:[],c=E(e.url,r,e.cwd);if(c===void 0)return{code:i.USAGE,error:"could not resolve a usable worker URL"};const{token:k}=x({cwd:e.cwd??process.cwd(),token:e.token,url:c});if(!k)return a(r,"root admin token required — pass --token, set LUNORA_ADMIN_TOKEN, or add it to .dev.vars (local targets only)",i.AUTH);const{body:A,method:h,path:w}=g(t,e,o),u=await(e.fetchImpl??D)(`${c}${w}`,{body:A,headers:{authorization:`Bearer ${k}`,"content-type":"application/json"},method:h}),m=await u.text();if(!u.ok)return a(r,`admin-token ${t} failed: HTTP ${String(u.status)}: ${m}`,T(u.status));let d;try{d=JSON.parse(m)}catch{return a(r,`admin-token ${t} failed: worker returned non-JSON: ${m}`,1)}return t==="create"&&(typeof d.token!="string"||d.token.length===0)?a(r,"admin-token create failed: the worker's answer did not include the created token",1):(e.format!=="json"&&_(r,t,d,e.name),{code:0,data:d})},q=U(async({argument:e,cwd:r,format:t,logger:n,options:s})=>{const o=await G({cwd:r,expiresDays:s.expiresDays,format:t,logger:n,name:e[1],prod:s.prod,scope:s.scope,subcommand:e[0],token:s.token,url:N({cwd:r,prod:s.prod===!0,url:s.url})});return{code:o.code,data:o.data,error:o.error}});export{q as execute,G as runAdminTokenCommand};