UNPKG

@lunora/cli

Version:

The Lunora CLI: init, dev, deploy, codegen, migrate, seed, doctor, insights, logs, registry, and the rest of the project commands

5 lines (4 loc) 10.4 kB
import{jsx as t,jsxs as c}from"react/jsx-runtime";import{isInteractive as h,ACCENT as b,LUNA_ART as ie,LUNA_NAME as oe,LUNA_SIGNOFF as se,BADGE_COLUMN_WIDTH as R,badgeWidth as z,badgeLead as ce,padBadge as de}from"@lunora/config";import{render as V}from"@visulima/tui";import{Box as d}from"@visulima/tui/components/box";import{CommandPalette as ue}from"@visulima/tui/components/command-palette";import{ConfirmInput as fe}from"@visulima/tui/components/confirm-input";import{SelectInput as me}from"@visulima/tui/components/select-input";import{Spinner as he}from"@visulima/tui/components/spinner";import{Text as o}from"@visulima/tui/components/text";import{TextInput as ge}from"@visulima/tui/components/text-input";import{useApp as y}from"@visulima/tui/hooks/use-app";import{useInput as j}from"@visulima/tui/hooks/use-input";import{useInterval as pe}from"@visulima/tui/hooks/use-interval";import{useEffect as L,useState as x}from"react";import{P as Q}from"./prompt-cancelled-C8Sj7OqI.mjs";let G=!1;const F=()=>{G||(G=!0,process.getMaxListeners()<64&&process.setMaxListeners(64))},be=({children:e,onCancel:r})=>{const{exit:n}=y();return j((l,a)=>{a.ctrl&&l==="c"&&(r(),n())}),e},Z=e=>e instanceof Error?e:new Error(typeof e=="string"?e:"task failed"),K=10,ve=8,ee=({spec:e})=>c(o,{children:[t(o,{children:ce(e.text)}),t(o,{backgroundColor:e.bg,bold:!0,color:e.fg,children:de(e.text)})]}),I=({badge:e,message:r})=>e===void 0?t(o,{bold:!0,children:r}):c(d,{children:[t(ee,{spec:e}),t(o,{bold:!0,children:` ${r}`})]}),re=2,$=" ".repeat(R-1),ne=e=>" ".repeat(z(e)+1),we=e=>e===void 0?re:z(e)+1,N=({badge:e,children:r,message:n})=>c(d,{flexDirection:"column",children:[t(I,{badge:e,message:n}),t(d,{flexDirection:"column",marginLeft:we(e),children:r})]}),A=({answer:e,badge:r,message:n})=>c(d,{flexDirection:"column",children:[t(I,{badge:r,message:n}),e===""?null:t(o,{dimColor:!0,children:`${r===void 0?" ".repeat(re):ne(r)}${e}`})]}),T=e=>{const{exit:r}=y(),[n,l]=x(void 0);return L(()=>{n!==void 0&&r()},[n,r]),{answer:n,submit:(a,i)=>{e(a),l(i)}}},xe=({children:e})=>{const{exit:r}=y();return L(()=>{r()},[r]),e},C=async e=>{F(),process.stdout.write(` `);const r=V(t(xe,{children:e}));try{await r.waitUntilExit()}catch{}finally{r.unmount()}},Ke=async(e,r,n)=>{if(!h())return;const l=n===void 0||n===""?[]:n.split(` `);await C(c(d,{flexDirection:"column",children:[t(I,{badge:e,message:r}),l.map(a=>t(o,{dimColor:!0,children:`${ne(e)}${a}`},a))]}))},O=async e=>{F(),process.stdout.write(` `);const r={value:!1},n=V(t(be,{onCancel:()=>{r.value=!0},children:e}),{exitOnCtrlC:!1});try{await n.waitUntilExit()}finally{n.unmount()}if(r.value)throw new Q},k=async(e,r)=>{let n=r;return await O(e(l=>{n=l})),n},te=e=>e.description===void 0?e.label:`${e.label} — ${e.description}`,X=()=>{const{exit:e}=y();j((r,n)=>{n.escape&&e()})},ye=({badge:e,finish:r,initialIndex:n,message:l,options:a})=>{const{answer:i,submit:u}=T(r);return X(),i!==void 0?t(A,{answer:i,badge:e,message:l}):t(N,{badge:e,message:l,children:t(me,{accentColor:b,initialIndex:n,items:a.map(s=>({key:s.value,label:te(s),value:s.value})),limit:K,onSelect:s=>{u(s.value,a.find(f=>f.value===s.value)?.label??s.value)}})})},$e=({badge:e,finish:r,message:n,options:l})=>{const{exit:a}=y(),{answer:i,submit:u}=T(r);return i!==void 0?t(A,{answer:i,badge:e,message:n}):t(N,{badge:e,message:n,children:t(ue,{accentColor:b,autoFocus:!0,commands:l.map(s=>({description:s.description,id:s.value,label:s.label})),limit:K,onCancel:()=>{a()},onSelect:s=>{u(s,l.find(f=>f.value===s)?.label??s)},placeholder:"Type to filter…"})})},er=async(e,r,n)=>{if(!h()||r.length===0)return n?.default;const{badge:l}=n??{};if(r.length>ve)return k(i=>t($e,{badge:l,finish:i,message:e,options:r}),n?.default);const a=n?.default===void 0?-1:r.findIndex(i=>i.value===n.default);return k(i=>t(ye,{badge:l,finish:i,initialIndex:a>=0?a:void 0,message:e,options:r}),n?.default)},Ce=({badge:e,defaultValue:r,finish:n,message:l,placeholder:a})=>{const{answer:i,submit:u}=T(n);return X(),i!==void 0?t(A,{answer:i,badge:e,message:l}):t(N,{badge:e,message:l,children:t(ge,{defaultValue:r,onSubmit:s=>{const f=s.trim()===""?r:s.trim();u(f,f)},placeholder:a})})},rr=async(e,r)=>{const n=r?.default??"";if(!h())return n;const{badge:l}=r??{};return k(a=>t(Ce,{badge:l,defaultValue:n,finish:a,message:e,placeholder:r?.placeholder}),n)},De=({badge:e,defaults:r,finish:n,message:l,options:a})=>{const{answer:i,submit:u}=T(n),[s,f]=x(0),[g,D]=x(()=>new Set(r));return X(),j((p,v)=>{if(!(i!==void 0||a.length===0)){if(v.upArrow)f(m=>(m-1+a.length)%a.length);else if(v.downArrow)f(m=>(m+1)%a.length);else if(p===" "){const m=a[s];m!==void 0&&D(w=>{const E=new Set(w);return E.has(m.value)?E.delete(m.value):E.add(m.value),E})}else if(v.return){const m=a.filter(w=>g.has(w.value));u(m.map(w=>w.value),m.length>0?m.map(w=>w.label).join(", "):"none")}}}),i!==void 0?t(A,{answer:i,badge:e,message:l}):t(N,{badge:e,message:l,children:c(d,{flexDirection:"column",children:[t(o,{dimColor:!0,children:"space toggles · enter confirms · esc cancels"}),a.map((p,v)=>{const m=v===s;return t(o,{color:m?b:void 0,children:`${m?"▸ ":" "}${g.has(p.value)?"◼":"◻"} ${te(p)}`},p.value)})]})})},nr=async(e,r,n)=>{const l=n?.defaults??[];if(!h()||r.length===0)return[...l];const{badge:a}=n??{};return k(i=>t(De,{badge:a,defaults:l,finish:i,message:e,options:r}),[...l])},Se=({badge:e,defaultYes:r,finish:n,message:l})=>{const{answer:a,submit:i}=T(n);return a!==void 0?t(A,{answer:a,badge:e,message:l}):c(d,{children:[e===void 0?null:t(ee,{spec:e}),t(o,{bold:!0,children:e===void 0?`${l} `:` ${l} `}),t(fe,{defaultChoice:r?"confirm":"cancel",onCancel:()=>{i(!1,"No")},onConfirm:()=>{i(!0,"Yes")}})]})},ke=async(e,r)=>{const n=r?.defaultYes===!0;if(!h())return n;const{badge:l}=r??{};return k(a=>t(Se,{badge:l,defaultYes:n,finish:a,message:e}),!1)},tr=()=>h()?e=>ke(e,{defaultYes:!0}):()=>Promise.resolve(!1),H="#c8a8ff",M="#d9c8ff",Ie="#6b5b9a",U=({moon:e,prefix:r,suffix:n})=>c(d,{children:[t(o,{color:H,dimColor:!0,children:r}),typeof e=="string"?t(o,{color:M,children:e}):e,n===void 0?null:t(o,{color:H,children:n})]}),W=({left:e,right:r})=>c(o,{color:M,children:[e,t(o,{color:Ie,children:"●●"}),r]}),Ae=()=>c(d,{flexDirection:"column",paddingX:1,children:[t(U,{moon:".-‐-.",prefix:"· ✦ · ",suffix:" ✦"}),t(U,{moon:t(W,{left:"/",right:"\\"}),prefix:" ✦ · ",suffix:" ·"}),t(U,{moon:t(W,{left:"\\",right:"/"}),prefix:" . ✦ · "}),c(d,{children:[t(o,{bold:!0,color:b,children:" lunora init "}),t(o,{dimColor:!0,children:"· moonrise sequence"}),t(o,{color:M})]})]}),ar=async e=>{h()&&await C(c(d,{flexDirection:"column",children:[t(Ae,{}),t(d,{paddingX:1,children:t(o,{dimColor:!0,children:e})})]}))},Te=({label:e})=>c(d,{children:[t(o,{color:b,children:t(he,{type:"dots"})}),c(o,{children:[" ",e]})]}),lr=async(e,r)=>{if(!h())return r();F();const n=V(t(Te,{label:e}));try{return await r()}finally{n.unmount()}},ir=async()=>{h()&&await C(c(d,{paddingX:1,children:[t(o,{color:M,children:ie}),c(d,{flexDirection:"column",marginLeft:2,children:[t(o,{bold:!0,color:b,children:oe}),t(o,{children:se})]})]}))},Ee="#06b6d4",Y=" ".repeat(9),or=async e=>{h()&&await C(t(o,{bold:!0,children:`${Y}${e}`}))},sr=async e=>{h()&&await C(c(d,{children:[t(o,{color:Ee,children:`${$}◼`}),t(o,{children:` ${e}`})]}))},cr=async(e,r,n,l)=>{h()&&await C(c(d,{flexDirection:"column",children:[t(I,{badge:e,message:r}),t(d,{flexDirection:"column",marginTop:1,children:n.map(a=>c(o,{children:[`${Y}${a.lead} `,t(o,{color:b,children:a.code}),a.tail??""]},a.lead))}),t(d,{flexDirection:"column",marginTop:1,children:l.map(a=>c(o,{children:[`${Y}${a.lead} `,t(o,{color:b,children:a.code}),a.tail??""]},a.lead))})]}))},q="#a855f7",ae="#06b6d4",S=[q,"#9b51f6","#8a5cf6","#6f86f6","#4aa6ef","#2bb6dd","#12bcd0",ae],B=S.length-2,P=(e,r)=>Array.from({length:Math.max(0,e)},()=>r),J=[...P(S.length-1,q),...S,...P(S.length-1,ae),...S.toReversed()],_=J.map((e,r)=>{const n=J.slice(r,r+B);return[...n,...P(B-n.length,q)]}),Le=" ".repeat(Math.max(0,R-B)),le=({label:e})=>{const[r,n]=x(0);pe(()=>{n(a=>(a+1)%_.length)},90);const l=_[r%_.length]??[];return c(o,{children:[Le,l.map((a,i)=>t(o,{color:a,children:"█"},i)),` ${e}`]})},Ne=({label:e,status:r})=>r==="running"?t(o,{color:b,children:`${$}▶ ${e}`}):r==="failed"?t(o,{color:"red",children:`${$}✖ ${e}`}):r==="done"?t(o,{dimColor:!0,children:`${$}■ ${e}`}):t(o,{dimColor:!0,children:`${$}□ ${e}`}),Me=async(e,r)=>{const n=[];let l;for(const[a,i]of e.entries()){r(a,"running");try{n.push(await i.run()),r(a,"done")}catch(u){r(a,"failed"),l=u;break}}return{failure:l,results:n}},Ue=(e,r,n,l)=>{let a=!0;return Me(e,(i,u)=>{a&&r(s=>s.map((f,g)=>g===i?u:f))}).then(({failure:i,results:u})=>{setTimeout(()=>{a&&(n(u,i),l())},400)}).catch(()=>{}),()=>{a=!1}},_e=({end:e,onSettle:r,start:n,tasks:l})=>{const{exit:a}=y(),[i,u]=x(()=>l.map(()=>"pending"));L(()=>Ue(l,u,r,a),[a,r,l]);const s=i.length>0&&i.every(f=>f==="done");return c(d,{flexDirection:"column",children:[s?c(d,{children:[t(o,{bold:!0,color:"green",children:`${$}✔`}),t(o,{bold:!0,color:"green",children:` ${e}`})]}):t(le,{label:n}),l.map((f,g)=>t(Ne,{label:f.label,status:i[g]??"pending"},f.label))]})},dr=async(e,r)=>{if(!h()){const a=[];for(const i of e)a.push(await i.run());return a}let n=[],l;if(await O(t(_e,{end:r.end,onSettle:(a,i)=>{n=a,l=i},start:r.start,tasks:e})),l!==void 0)throw Z(l);return n},Ye=async(e,r)=>{const n=[];let l;for(const[a,i]of e.entries()){r(a);try{n.push(await i.task())}catch(u){l=u;break}}return{failure:l,values:n}},Be=({badge:e,done:r,onSettle:n,steps:l})=>{const{exit:a}=y(),[i,u]=x(0),[s,f]=x(!1);return L(()=>{let g=!0,D;return Ye(l,p=>{g&&u(p)}).then(({failure:p,values:v})=>{if(g){if(p!==void 0){n({error:p,ok:!1}),a();return}f(!0),D=setTimeout(()=>{g&&(n({ok:!0,values:v}),a())},400)}}).catch(()=>{}),()=>{g=!1,D!==void 0&&clearTimeout(D)}},[a,n,l]),s?t(I,{badge:e,message:r}):t(le,{label:l[i]?.running??""})},ur=async(e,r,n)=>{if(!h()){const a=[];for(const i of r)a.push(await i.task());return a}let l;if(await O(t(Be,{badge:e,done:n,onSettle:a=>{l=a},steps:r})),l===void 0)throw new Q;if(!l.ok)throw Z(l.error);return l.values};export{rr as B,nr as C,ar as D,sr as F,cr as H,tr as I,ur as J,or as M,Ke as R,lr as V,ir as _,ke as o,dr as q,er as v};