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