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.3 kB
import{jsx as n,jsxs as a}from"react/jsx-runtime";import{isInteractive as m,ACCENT as w,LUNA_ART as ce,LUNA_NAME as le,LUNA_SIGNOFF as se,BADGE_COLUMN_WIDTH as z,badgeWidth as J,badgeLead as ae,padBadge as ue}from"@lunora/config";import{render as H}from"@visulima/tui";import{Box as u}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})=>a(l,{children:[n(l,{children:ae(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}):a(u,{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})=>a(u,{flexDirection:"column",children:[n($,{badge:e,message:t}),n(u,{flexDirection:"column",marginLeft:Ce(e),children:r})]}),g=({answer:e,badge:r,message:t})=>a(u,{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},N=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(!m())return;const i=t===void 0||t===""?[]:t.split(` `);await N(a(u,{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:d}=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:w,initialIndex:t,items:o.map(s=>({key:s.value,label:ne(s),value:s.value})),limit:Z,onSelect:s=>{d(s.value,o.find(f=>f.value===s.value)?.label??s.value)}})})},be=({badge:e,finish:r,message:t,options:i})=>{const{exit:o}=I(),{answer:c,submit:d}=R(r);return c!==void 0?n(g,{answer:c,badge:e,message:t}):n(D,{badge:e,message:t,children:n(fe,{accentColor:w,autoFocus:!0,commands:i.map(s=>({description:s.description,id:s.value,label:s.label})),limit:Z,onCancel:()=>{o()},onSelect:s=>{d(s,i.find(f=>f.value===s)?.label??s)},placeholder:"Type to filter…"})})},er=async(e,r,t)=>{if(!m()||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:d}=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:s=>{const f=s.trim()===""?r:s.trim();d(f,f)},placeholder:o})})},rr=async(e,r)=>{const t=r?.default??"";if(!m())return t;const{badge:i}=r??{};return E(o=>n(Te,{badge:i,defaultValue:t,finish:o,message:e,placeholder:r?.placeholder}),t)},Ne=({badge:e,defaults:r,finish:t,message:i,options:o})=>{const{answer:c,submit:d}=R(t),[s,f]=S(0),[p,x]=S(()=>new Set(r));return U(),B((b,v)=>{if(!(c!==void 0||o.length===0)){if(v.upArrow)f(h=>(h-1+o.length)%o.length);else if(v.downArrow)f(h=>(h+1)%o.length);else if(b===" "){const h=o[s];h!==void 0&&x(C=>{const _=new Set(C);return _.has(h.value)?_.delete(h.value):_.add(h.value),_})}else if(v.return){const h=o.filter(C=>p.has(C.value));d(h.map(C=>C.value),h.length>0?h.map(C=>C.label).join(", "):"none")}}}),c!==void 0?n(g,{answer:c,badge:e,message:i}):n(D,{badge:e,message:i,children:a(u,{flexDirection:"column",children:[n(l,{dimColor:!0,children:"space toggles · enter confirms · esc cancels"}),o.map((b,v)=>{const h=v===s;return n(l,{color:h?w:void 0,children:`${h?"▸ ":" "}${p.has(b.value)?"◼":"◻"} ${ne(b)}`},b.value)})]})})},tr=async(e,r,t)=>{const i=t?.defaults??[];if(!m()||r.length===0)return[...i];const{badge:o}=t??{};return E(c=>n(Ne,{badge:o,defaults:i,finish:c,message:e,options:r}),[...i])},ye=({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}):a(u,{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(!m())return t;const{badge:i}=r??{};return E(o=>n(ye,{badge:i,defaultYes:t,finish:o,message:e}),!1)},nr=()=>m()?e=>Ee(e,{defaultYes:!0}):()=>Promise.resolve(!1),W="#c8a8ff",L="#d9c8ff",$e="#6b5b9a",P=({moon:e,prefix:r,suffix:t})=>a(u,{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})=>a(l,{color:L,children:[e,n(l,{color:$e,children:"●●"}),r]}),ge=()=>a(u,{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:" . ✦ · "}),a(u,{children:[n(l,{bold:!0,color:w,children:" lunora init "}),n(l,{dimColor:!0,children:"· moonrise sequence"}),n(l,{color:L})]})]}),or=async e=>{m()&&await N(a(u,{flexDirection:"column",children:[n(ge,{}),n(u,{paddingX:1,children:n(l,{dimColor:!0,children:e})})]}))},Re=({label:e})=>a(u,{children:[n(l,{color:w,children:n(pe,{type:"dots"})}),a(l,{children:[" ",e]})]}),ir=async(e,r)=>{if(!m())return r();V();const t=H(n(Re,{label:e}));try{return await r()}finally{t.unmount()}},cr=async()=>{m()&&await N(a(u,{paddingX:1,children:[n(l,{color:L,children:ce}),a(u,{flexDirection:"column",marginLeft:2,children:[n(l,{bold:!0,color:w,children:le}),n(l,{children:se})]})]}))},_e="#06b6d4",k=" ".repeat(9),lr=async e=>{m()&&await N(n(l,{bold:!0,children:`${k}${e}`}))},sr=async e=>{m()&&await N(a(u,{children:[n(l,{color:_e,children:`${T}◼`}),n(l,{children:` ${e}`})]}))},ar=async(e,r,t,i)=>{m()&&await N(a(u,{flexDirection:"column",children:[n($,{badge:e,message:r}),n(u,{flexDirection:"column",marginTop:1,children:t.map(o=>a(l,{children:[`${k}${o.lead} `,n(l,{color:w,children:o.code}),o.tail??""]},o.lead))}),n(u,{flexDirection:"column",marginTop:1,children:i.map(o=>a(l,{children:[`${k}${o.lead} `,n(l,{color:w,children:o.code}),o.tail??""]},o.lead))})]}))},Y="#a855f7",oe="#06b6d4",y=[Y,"#9b51f6","#8a5cf6","#6f86f6","#4aa6ef","#2bb6dd","#12bcd0",oe],O=y.length-2,F=(e,r)=>Array.from({length:Math.max(0,e)},()=>r),q=[...F(y.length-1,Y),...y,...F(y.length-1,oe),...y.toReversed()],M=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)%M.length)},90);const i=M[r%M.length]??[];return a(l,{children:[Ae,i.map((o,c)=>n(l,{color:o,children:"█"},c)),` ${e}`]})},De=({label:e,status:r})=>r==="running"?n(l,{color:w,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)=>{const t=[];let i;for(const[o,c]of e.entries()){r(o,"running");try{t.push(await c.run()),r(o,"done")}catch(d){r(o,"failed"),i=d;break}}return{failure:i,results:t}},Pe=(e,r,t,i)=>{let o=!0;return Le(e,(d,s)=>{o&&r(f=>f.map((p,x)=>x===d?s:p))}).then(({failure:d,results:s})=>{setTimeout(()=>{o&&(t(s,d),i())},400)}).catch(()=>{}),()=>{o=!1}},Me=({end:e,onSettle:r,start:t,tasks:i})=>{const{exit:o}=I(),[c,d]=S(()=>i.map(()=>"pending"));A(()=>Pe(i,d,r,o),[o,r,i]);const s=c.length>0&&c.every(f=>f==="done");return a(u,{flexDirection:"column",children:[s?a(u,{children:[n(l,{bold:!0,color:"green",children:`${T}✔`}),n(l,{bold:!0,color:"green",children:` ${e}`})]}):n(ie,{label:t}),i.map((f,p)=>n(De,{label:f.label,status:c[p]??"pending"},f.label))]})},ur=async(e,r)=>{if(!m()){const o=[];for(const c of e)o.push(await c.run());return o}let t=[],i;if(await G(n(Me,{end:r.end,onSettle:(o,c)=>{t=o,i=c},start:r.start,tasks:e})),i!==void 0)throw Q(i);return t},ke=async(e,r)=>{const t=[];let i;for(const[o,c]of e.entries()){r(o);try{t.push(await c.task())}catch(d){i=d;break}}return{failure:i,values:t}},Oe=({badge:e,done:r,onSettle:t,steps:i})=>{const{exit:o}=I(),[c,d]=S(0),[s,f]=S(!1);return A(()=>{let p=!0,x;return ke(i,v=>{p&&d(v)}).then(({failure:v,values:h})=>{if(p){if(v!==void 0){t({error:v,ok:!1}),o();return}f(!0),x=setTimeout(()=>{p&&(t({ok:!0,values:h}),o())},400)}}).catch(()=>{}),()=>{p=!1,x!==void 0&&clearTimeout(x)}},[o,t,i]),s?n($,{badge:e,message:r}):n(ie,{label:i[c]?.running??""})},dr=async(e,r,t)=>{if(!m()){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,sr as i,ar as j,ur as k,dr as l,er as t,ir as w};