@umac-js/terminal
Version:
MacOS Terminal utils
4 lines (3 loc) • 1.94 kB
JavaScript
import{exec as h,promptSelect as m,UmacCommand as L,successStyle as i,infoStyle as o,liStyle as S}from"@umac-js/utils";import{env as p}from"node:process";const g="@umac-js/terminal",u="2.0.7",E="MacOS Terminal utils",d="https://github.com/angelespejo/umac/tree/main/packages/plugin/terminal",C={"umac-terminal":"./dist/bin.mjs"},H=Object.keys(C)[0]||g,f=d;class w{async#e(l){const{stderr:s,stdout:t}=await h(l);if(s)throw new Error(s.toString());return t.toString()}async getShellList(){return(await this.#e('grep "^[^#]" /etc/shells')).trim().split(`
`).map(l=>l.replace("/bin/","").trim())}getCurrentShell(){return p.SHELL?.replace("/bin/","")||"unknown"}async changeShell(l){if(!l){const t=await this.getShellList(),a=await this.getCurrentShell();if(t.length===0)return;l=await m("Select a shell to change:",t.filter(r=>r!==a))}const s=`/bin/${l}`;return await this.#e(`chsh -s ${s}`)}async open(){return await this.#e("open -a Terminal")}}const e={SHELL:"shell",OPEN:"open",SHELL_LIST:"list",SHELL_CHANGE:"change",SHELL_CURRENT:"current"},c=new L({description:E,version:u,name:H,helpURL:f,helpOpts:{cmds:[{value:e.OPEN,desc:"Opem Temrinal app"},{value:e.SHELL,desc:"Shell functions",cmds:[{value:e.SHELL_LIST,desc:"List all shells"},{value:e.SHELL_CHANGE,desc:"Change to another shell"},{value:e.SHELL_CURRENT,desc:"Set current shell"}]}]},fn:async({argv:n,getHelp:l})=>{const s=new w;if(n.existsCmd(e.SHELL))if(n.existsCmd(e.SHELL_CHANGE)){const t=await s.changeShell();console.log(i(["Termnial shell","Changed to "+t]))}else if(n.existsCmd(e.SHELL_CURRENT)){const t=await s.getCurrentShell();console.log(o(["Termnial shell",t]))}else if(n.existsCmd(e.SHELL_LIST)){const t=await s.getShellList();console.log(o(["Available shells:",`
`])),t.forEach(a=>console.log(S(a)))}else console.log(l());else n.existsCmd(e.OPEN)&&(await s.open(),console.log(i(["Termnial app","Succesfully opened!"])))}});export{e as CMD,c as cli,c as default};