UNPKG

@smythos/cli

Version:

SmythOS SRE Command Line Interface

7 lines (6 loc) 12.7 kB
#!/usr/bin/env node "use strict";var c=Object.defineProperty;var o=(e,t)=>c(e,"name",{value:t,configurable:!0});var process$2=require("node:process"),index=require("./index2.cjs"),index$1=require("./index6.cjs");const copyProperty=o((e,t,i,s)=>{if(i==="length"||i==="prototype"||i==="arguments"||i==="caller")return;const n=Object.getOwnPropertyDescriptor(e,i),r=Object.getOwnPropertyDescriptor(t,i);!canCopyProperty(n,r)&&s||Object.defineProperty(e,i,r)},"copyProperty"),canCopyProperty=o(function(e,t){return e===void 0||e.configurable||e.writable===t.writable&&e.enumerable===t.enumerable&&e.configurable===t.configurable&&(e.writable||e.value===t.value)},"canCopyProperty"),changePrototype=o((e,t)=>{const i=Object.getPrototypeOf(t);i!==Object.getPrototypeOf(e)&&Object.setPrototypeOf(e,i)},"changePrototype"),wrappedToString=o((e,t)=>`/* Wrapped ${e}*/ ${t}`,"wrappedToString"),toStringDescriptor=Object.getOwnPropertyDescriptor(Function.prototype,"toString"),toStringName=Object.getOwnPropertyDescriptor(Function.prototype.toString,"name"),changeToString=o((e,t,i)=>{const s=i===""?"":`with ${i.trim()}() `,n=wrappedToString.bind(null,s,t.toString());Object.defineProperty(n,"name",toStringName);const{writable:r,enumerable:h,configurable:l}=toStringDescriptor;Object.defineProperty(e,"toString",{value:n,writable:r,enumerable:h,configurable:l})},"changeToString");function mimicFunction(e,t,{ignoreNonConfigurable:i=!1}={}){const{name:s}=e;for(const n of Reflect.ownKeys(t))copyProperty(e,t,n,i);return changePrototype(e,t),changeToString(e,t,s),e}o(mimicFunction,"mimicFunction");const calledFunctions=new WeakMap,onetime=o((e,t={})=>{if(typeof e!="function")throw new TypeError("Expected a function");let i,s=0;const n=e.displayName||e.name||"<anonymous>",r=o(function(...h){if(calledFunctions.set(r,++s),s===1)i=e.apply(this,h),e=void 0;else if(t.throw===!0)throw new Error(`Function \`${n}\` can only be called once`);return i},"onetime");return mimicFunction(r,e),calledFunctions.set(r,s),r},"onetime");onetime.callCount=e=>{if(!calledFunctions.has(e))throw new Error(`The given function \`${e.name}\` is not wrapped by the \`onetime\` package`);return calledFunctions.get(e)};const signals=[];signals.push("SIGHUP","SIGINT","SIGTERM");process.platform!=="win32"&&signals.push("SIGALRM","SIGABRT","SIGVTALRM","SIGXCPU","SIGXFSZ","SIGUSR2","SIGTRAP","SIGSYS","SIGQUIT","SIGIOT");process.platform==="linux"&&signals.push("SIGIO","SIGPOLL","SIGPWR","SIGSTKFLT");const processOk=o(e=>!!e&&typeof e=="object"&&typeof e.removeListener=="function"&&typeof e.emit=="function"&&typeof e.reallyExit=="function"&&typeof e.listeners=="function"&&typeof e.kill=="function"&&typeof e.pid=="number"&&typeof e.on=="function","processOk"),kExitEmitter=Symbol.for("signal-exit emitter"),global=globalThis,ObjectDefineProperty=Object.defineProperty.bind(Object);class Emitter{static{o(this,"Emitter")}emitted={afterExit:!1,exit:!1};listeners={afterExit:[],exit:[]};count=0;id=Math.random();constructor(){if(global[kExitEmitter])return global[kExitEmitter];ObjectDefineProperty(global,kExitEmitter,{value:this,writable:!1,enumerable:!1,configurable:!1})}on(t,i){this.listeners[t].push(i)}removeListener(t,i){const s=this.listeners[t],n=s.indexOf(i);n!==-1&&(n===0&&s.length===1?s.length=0:s.splice(n,1))}emit(t,i,s){if(this.emitted[t])return!1;this.emitted[t]=!0;let n=!1;for(const r of this.listeners[t])n=r(i,s)===!0||n;return t==="exit"&&(n=this.emit("afterExit",i,s)||n),n}}class SignalExitBase{static{o(this,"SignalExitBase")}}const signalExitWrap=o(e=>({onExit(t,i){return e.onExit(t,i)},load(){return e.load()},unload(){return e.unload()}}),"signalExitWrap");class SignalExitFallback extends SignalExitBase{static{o(this,"SignalExitFallback")}onExit(){return()=>{}}load(){}unload(){}}class SignalExit extends SignalExitBase{static{o(this,"SignalExit")}#o=process$1.platform==="win32"?"SIGINT":"SIGHUP";#i=new Emitter;#t;#n;#c;#e={};#r=!1;constructor(t){super(),this.#t=t,this.#e={};for(const i of signals)this.#e[i]=()=>{const s=this.#t.listeners(i);let{count:n}=this.#i;const r=t;if(typeof r.__signal_exit_emitter__=="object"&&typeof r.__signal_exit_emitter__.count=="number"&&(n+=r.__signal_exit_emitter__.count),s.length===n){this.unload();const h=this.#i.emit("exit",null,i),l=i==="SIGHUP"?this.#o:i;h||t.kill(t.pid,l)}};this.#c=t.reallyExit,this.#n=t.emit}onExit(t,i){if(!processOk(this.#t))return()=>{};this.#r===!1&&this.load();const s=i?.alwaysLast?"afterExit":"exit";return this.#i.on(s,t),()=>{this.#i.removeListener(s,t),this.#i.listeners.exit.length===0&&this.#i.listeners.afterExit.length===0&&this.unload()}}load(){if(!this.#r){this.#r=!0,this.#i.count+=1;for(const t of signals)try{const i=this.#e[t];i&&this.#t.on(t,i)}catch{}this.#t.emit=(t,...i)=>this.#d(t,...i),this.#t.reallyExit=t=>this.#s(t)}}unload(){this.#r&&(this.#r=!1,signals.forEach(t=>{const i=this.#e[t];if(!i)throw new Error("Listener not defined for signal: "+t);try{this.#t.removeListener(t,i)}catch{}}),this.#t.emit=this.#n,this.#t.reallyExit=this.#c,this.#i.count-=1)}#s(t){return processOk(this.#t)?(this.#t.exitCode=t||0,this.#i.emit("exit",this.#t.exitCode,null),this.#c.call(this.#t,this.#t.exitCode)):0}#d(t,...i){const s=this.#n;if(t==="exit"&&processOk(this.#t)){typeof i[0]=="number"&&(this.#t.exitCode=i[0]);const n=s.call(this.#t,t,...i);return this.#i.emit("exit",this.#t.exitCode,null),n}else return s.call(this.#t,t,...i)}}const process$1=globalThis.process,{onExit}=signalExitWrap(processOk(process$1)?new SignalExit(process$1):new SignalExitFallback),terminal=process$2.stderr.isTTY?process$2.stderr:process$2.stdout.isTTY?process$2.stdout:void 0,restoreCursor=terminal?onetime(()=>{onExit(()=>{terminal.write("\x1B[?25h")},{alwaysLast:!0})}):()=>{};let isHidden=!1;const cliCursor={};cliCursor.show=(e=process$2.stderr)=>{e.isTTY&&(isHidden=!1,e.write("\x1B[?25h"))};cliCursor.hide=(e=process$2.stderr)=>{e.isTTY&&(restoreCursor(),isHidden=!0,e.write("\x1B[?25l"))};cliCursor.toggle=(e,t)=>{e!==void 0&&(isHidden=e),isHidden?cliCursor.show(t):cliCursor.hide(t)};var cliSpinnersExports=index.requireCliSpinners(),cliSpinners=index.getDefaultExportFromCjs(cliSpinnersExports);function isUnicodeSupported$1(){return process$2.platform!=="win32"?process$2.env.TERM!=="linux":!!process$2.env.CI||!!process$2.env.WT_SESSION||!!process$2.env.TERMINUS_SUBLIME||process$2.env.ConEmuTask==="{cmd::Cmder}"||process$2.env.TERM_PROGRAM==="Terminus-Sublime"||process$2.env.TERM_PROGRAM==="vscode"||process$2.env.TERM==="xterm-256color"||process$2.env.TERM==="alacritty"||process$2.env.TERMINAL_EMULATOR==="JetBrains-JediTerm"}o(isUnicodeSupported$1,"isUnicodeSupported$1");const main={info:index.chalk.blue("\u2139"),success:index.chalk.green("\u2714"),warning:index.chalk.yellow("\u26A0"),error:index.chalk.red("\u2716")},fallback={info:index.chalk.blue("i"),success:index.chalk.green("\u221A"),warning:index.chalk.yellow("\u203C"),error:index.chalk.red("\xD7")},logSymbols=isUnicodeSupported$1()?main:fallback;function isInteractive({stream:e=process.stdout}={}){return!!(e&&e.isTTY&&process.env.TERM!=="dumb"&&!("CI"in process.env))}o(isInteractive,"isInteractive");function isUnicodeSupported(){const{env:e}=process$2,{TERM:t,TERM_PROGRAM:i}=e;return process$2.platform!=="win32"?t!=="linux":!!e.WT_SESSION||!!e.TERMINUS_SUBLIME||e.ConEmuTask==="{cmd::Cmder}"||i==="Terminus-Sublime"||i==="vscode"||t==="xterm-256color"||t==="alacritty"||t==="rxvt-unicode"||t==="rxvt-unicode-256color"||e.TERMINAL_EMULATOR==="JetBrains-JediTerm"}o(isUnicodeSupported,"isUnicodeSupported");const ASCII_ETX_CODE=3;class StdinDiscarder{static{o(this,"StdinDiscarder")}#o=0;start(){this.#o++,this.#o===1&&this.#i()}stop(){if(this.#o<=0)throw new Error("`stop` called more times than `start`");this.#o--,this.#o===0&&this.#t()}#i(){process$2.platform==="win32"||!process$2.stdin.isTTY||(process$2.stdin.setRawMode(!0),process$2.stdin.on("data",this.#n),process$2.stdin.resume())}#t(){process$2.stdin.isTTY&&(process$2.stdin.off("data",this.#n),process$2.stdin.pause(),process$2.stdin.setRawMode(!1))}#n(t){t[0]===ASCII_ETX_CODE&&process$2.emit("SIGINT")}}const stdinDiscarder=new StdinDiscarder;class Ora{static{o(this,"Ora")}#o=0;#i=!1;#t=0;#n=-1;#c=0;#e;#r;#s;#d;#x;#a;#u;#f;#m;#h;#l;color;constructor(t){typeof t=="string"&&(t={text:t}),this.#e={color:"cyan",stream:process$2.stderr,discardStdin:!0,hideCursor:!0,...t},this.color=this.#e.color,this.spinner=this.#e.spinner,this.#x=this.#e.interval,this.#s=this.#e.stream,this.#a=typeof this.#e.isEnabled=="boolean"?this.#e.isEnabled:isInteractive({stream:this.#s}),this.#u=typeof this.#e.isSilent=="boolean"?this.#e.isSilent:!1,this.text=this.#e.text,this.prefixText=this.#e.prefixText,this.suffixText=this.#e.suffixText,this.indent=this.#e.indent,process$2.env.NODE_ENV==="test"&&(this._stream=this.#s,this._isEnabled=this.#a,Object.defineProperty(this,"_linesToClear",{get(){return this.#o},set(i){this.#o=i}}),Object.defineProperty(this,"_frameIndex",{get(){return this.#n}}),Object.defineProperty(this,"_lineCount",{get(){return this.#t}}))}get indent(){return this.#f}set indent(t=0){if(!(t>=0&&Number.isInteger(t)))throw new Error("The `indent` option must be an integer from 0 and up");this.#f=t,this.#p()}get interval(){return this.#x??this.#r.interval??100}get spinner(){return this.#r}set spinner(t){if(this.#n=-1,this.#x=void 0,typeof t=="object"){if(t.frames===void 0)throw new Error("The given spinner must have a `frames` property");this.#r=t}else if(!isUnicodeSupported())this.#r=cliSpinners.line;else if(t===void 0)this.#r=cliSpinners.dots;else if(t!=="default"&&cliSpinners[t])this.#r=cliSpinners[t];else throw new Error(`There is no built-in spinner named '${t}'. See https://github.com/sindresorhus/cli-spinners/blob/main/spinners.json for a full list.`)}get text(){return this.#m}set text(t=""){this.#m=t,this.#p()}get prefixText(){return this.#h}set prefixText(t=""){this.#h=t,this.#p()}get suffixText(){return this.#l}set suffixText(t=""){this.#l=t,this.#p()}get isSpinning(){return this.#d!==void 0}#T(t=this.#h,i=" "){return typeof t=="string"&&t!==""?t+i:typeof t=="function"?t()+i:""}#g(t=this.#l,i=" "){return typeof t=="string"&&t!==""?i+t:typeof t=="function"?i+t():""}#p(){const t=this.#s.columns??80,i=this.#T(this.#h,"-"),s=this.#g(this.#l,"-"),n=" ".repeat(this.#f)+i+"--"+this.#m+"--"+s;this.#t=0;for(const r of index$1.stripAnsi(n).split(` `))this.#t+=Math.max(1,Math.ceil(index$1.stringWidth(r,{countAnsiEscapeCodes:!0})/t))}get isEnabled(){return this.#a&&!this.#u}set isEnabled(t){if(typeof t!="boolean")throw new TypeError("The `isEnabled` option must be a boolean");this.#a=t}get isSilent(){return this.#u}set isSilent(t){if(typeof t!="boolean")throw new TypeError("The `isSilent` option must be a boolean");this.#u=t}frame(){const t=Date.now();(this.#n===-1||t-this.#c>=this.interval)&&(this.#n=++this.#n%this.#r.frames.length,this.#c=t);const{frames:i}=this.#r;let s=i[this.#n];this.color&&(s=index.chalk[this.color](s));const n=typeof this.#h=="string"&&this.#h!==""?this.#h+" ":"",r=typeof this.text=="string"?" "+this.text:"",h=typeof this.#l=="string"&&this.#l!==""?" "+this.#l:"";return n+s+r+h}clear(){if(!this.#a||!this.#s.isTTY)return this;this.#s.cursorTo(0);for(let t=0;t<this.#o;t++)t>0&&this.#s.moveCursor(0,-1),this.#s.clearLine(1);return(this.#f||this.lastIndent!==this.#f)&&this.#s.cursorTo(this.#f),this.lastIndent=this.#f,this.#o=0,this}render(){return this.#u?this:(this.clear(),this.#s.write(this.frame()),this.#o=this.#t,this)}start(t){return t&&(this.text=t),this.#u?this:this.#a?this.isSpinning?this:(this.#e.hideCursor&&cliCursor.hide(this.#s),this.#e.discardStdin&&process$2.stdin.isTTY&&(this.#i=!0,stdinDiscarder.start()),this.render(),this.#d=setInterval(this.render.bind(this),this.interval),this):(this.text&&this.#s.write(`- ${this.text} `),this)}stop(){return this.#a?(clearInterval(this.#d),this.#d=void 0,this.#n=0,this.clear(),this.#e.hideCursor&&cliCursor.show(this.#s),this.#e.discardStdin&&process$2.stdin.isTTY&&this.#i&&(stdinDiscarder.stop(),this.#i=!1),this):this}succeed(t){return this.stopAndPersist({symbol:logSymbols.success,text:t})}fail(t){return this.stopAndPersist({symbol:logSymbols.error,text:t})}warn(t){return this.stopAndPersist({symbol:logSymbols.warning,text:t})}info(t){return this.stopAndPersist({symbol:logSymbols.info,text:t})}stopAndPersist(t={}){if(this.#u)return this;const i=t.prefixText??this.#h,s=this.#T(i," "),n=t.symbol??" ",r=t.text??this.text,l=typeof r=="string"?(n?" ":"")+r:"",a=t.suffixText??this.#l,u=this.#g(a," "),f=s+n+l+u+` `;return this.stop(),this.#s.write(f),this}}function ora(e){return new Ora(e)}o(ora,"ora");exports.cliCursor=cliCursor;exports.ora=ora;