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