UNPKG

anl

Version:
2 lines (1 loc) 4.38 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("node:readline"),t=require("../../../../run-async/index.js"),r=require("mute-stream"),s=require("ansi-escapes"),o=require("../../../../rxjs/dist/esm5/internal/observable/empty.js"),n=require("../../../../rxjs/dist/esm5/internal/observable/from.js"),i=require("../../../../rxjs/dist/esm5/internal/util/isObservable.js"),a=require("../../../../rxjs/dist/esm5/internal/operators/concatMap.js"),c=require("../../../../rxjs/dist/esm5/internal/lastValueFrom.js"),u=require("../../../../rxjs/dist/esm5/internal/operators/reduce.js"),p=require("../../../../rxjs/dist/esm5/internal/observable/defer.js"),l=require("../../../../rxjs/dist/esm5/internal/observable/of.js"),f=require("../../../../rxjs/dist/esm5/internal/operators/filter.js");function h(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var m=h(e),d=h(r),y=h(s);const j={set:(e,t="",r)=>{let s=e;t.split(".").forEach(((e,t,o)=>{"__proto__"!==e&&"constructor"!==e&&(t===o.length-1?s[e]=r:e in s&&"object"==typeof s[e]||(s[e]={}),s=s[e])}))},get:(e,t="",r)=>{const s=r=>String.prototype.split.call(t,r).filter(Boolean).reduce(((e,t)=>null!=e?e[t]:e),e),o=s(/[,[\]]+?/)||s(/[,.[\]]+?/);return void 0===o||o===e?r:o}};function b(e,r,s){if(r in e){const o=e[r];if("function"==typeof o)return n.from(t.default(o)(s).then((t=>Object.assign(e,{[r]:t}))))}return l.of(e)}class w extends Error{name="TTYError";isTtyError=!0}exports._=j,exports.default=class{prompts;answers={};process=o.EMPTY;onClose;opt;rl;constructor(e,t){this.opt=t,this.prompts=e}run(e,t){let r;this.answers="object"==typeof t?{...t}:{},r=function(e){return Array.isArray(e)}(e)?n.from(e):i.isObservable(e)?e:function(e){return Object.values(e).every((e=>"object"==typeof e&&!Array.isArray(e)&&null!=e))}(e)?n.from(Object.entries(e).map((([e,t])=>Object.assign({},t,{name:e})))):n.from([e]),this.process=r.pipe(a.concatMap((e=>this.processQuestion(e))));const s=c.lastValueFrom(this.process.pipe(u.reduce(((e,t)=>(j.set(e,t.name,t.answer),e)),this.answers))).then((()=>this.onCompletion()),(e=>this.onError(e)));return Object.assign(s,{ui:this})}onCompletion(){return this.close(),this.answers}onError(e){return this.close(),Promise.reject(e)}processQuestion(e){return e={...e},p.defer((()=>l.of(e).pipe(a.concatMap(this.setDefaultType),a.concatMap(this.filterIfRunnable),a.concatMap((e=>b(e,"message",this.answers))),a.concatMap((e=>b(e,"default",this.answers))),a.concatMap((e=>b(e,"choices",this.answers))),a.concatMap((e=>{const{choices:t}=e;return Array.isArray(t)&&(e.choices=t.map((e=>"string"==typeof e||"number"==typeof e?{name:e,value:e}:"value"in e?e:{...e,value:e.name}))),l.of(e)})),a.concatMap((e=>this.fetchAnswer(e))))))}fetchAnswer(e){const t=this.prompts[e.type];if(null==t)throw new Error(`Prompt for type ${e.type} not found`);return function(e){return Boolean(e.prototype&&"run"in e.prototype&&"function"==typeof e.prototype.run)}(t)?p.defer((()=>{const r=m.default.createInterface(function(e={}){e.skipTTYChecks=void 0===e.skipTTYChecks||e.skipTTYChecks;const t=e.input||process.stdin;if(!e.skipTTYChecks&&!t.isTTY)throw new w("Prompts can not be meaningfully rendered in non-TTY environments");const r=new d.default;r.pipe(e.output||process.stdout);const s=r;return{terminal:!0,...e,input:t,output:s}}(this.opt));r.resume();const s=()=>{r.removeListener("SIGINT",this.onForceClose),r.setPrompt(""),r.output.unmute(),r.output.write(y.default.cursorShow),r.output.end(),r.close()};this.onClose=s,this.rl=r,process.on("exit",this.onForceClose),r.on("SIGINT",this.onForceClose);const o=new t(e,r,this.answers);return n.from(o.run().then((t=>(s(),this.onClose=void 0,this.rl=void 0,{name:e.name,answer:t}))))})):p.defer((()=>n.from(t(e,this.opt).then((t=>({name:e.name,answer:t}))))))}onForceClose=()=>{this.close(),process.kill(process.pid,"SIGINT"),console.log("")};close=()=>{process.removeListener("exit",this.onForceClose),"function"==typeof this.onClose&&this.onClose()};setDefaultType=e=>(this.prompts[e.type]||(e=Object.assign({},e,{type:"input"})),p.defer((()=>l.of(e))));filterIfRunnable=e=>{if(!0!==e.askAnswered&&void 0!==j.get(this.answers,e.name))return o.EMPTY;const{when:r}=e;return!1===r?o.EMPTY:"function"!=typeof r?l.of(e):p.defer((()=>n.from(t.default(r)(this.answers).then((t=>{if(t)return e}))).pipe(f.filter((e=>null!=e)))))}};