viteshell
Version:
A minimalistic shell implementation written in TypeScript.
8 lines (7 loc) • 26 kB
JavaScript
/**
* viteshell - v0.8.1
* @author Henry Hale
* @license MIT
* @url https://github.com/henryhale/viteshell#readme
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).ViteShell=e()}(this,(function(){"use strict";function t(t,e,r){if("function"==typeof t?t===e:t.has(e))return arguments.length<3?e:r;throw new TypeError("Private element is not present on this object")}function e(e,r){return e.get(t(e,r))}function r(e,r,n){return e.set(t(e,r),n),n}function n(){n=function(){return e};var t,e={},r=Object.prototype,i=r.hasOwnProperty,o=Object.defineProperty||function(t,e,r){t[e]=r.value},a="function"==typeof Symbol?Symbol:{},s=a.iterator||"@@iterator",u=a.asyncIterator||"@@asyncIterator",c=a.toStringTag||"@@toStringTag";function f(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{f({},"")}catch(t){f=function(t,e,r){return t[e]=r}}function l(t,e,r,n){var i=e&&e.prototype instanceof g?e:g,a=Object.create(i.prototype),s=new N(n||[]);return o(a,"_invoke",{value:P(t,r,s)}),a}function h(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}e.wrap=l;var p="suspendedStart",v="suspendedYield",d="executing",y="completed",m={};function g(){}function w(){}function b(){}var x={};f(x,s,(function(){return this}));var k=Object.getPrototypeOf,E=k&&k(k(I([])));E&&E!==r&&i.call(E,s)&&(x=E);var S=b.prototype=g.prototype=Object.create(x);function O(t){["next","throw","return"].forEach((function(e){f(t,e,(function(t){return this._invoke(e,t)}))}))}function j(t,e){function r(n,o,a,s){var u=h(t[n],t,o);if("throw"!==u.type){var c=u.arg,f=c.value;return f&&"object"==typeof f&&i.call(f,"__await")?e.resolve(f.__await).then((function(t){r("next",t,a,s)}),(function(t){r("throw",t,a,s)})):e.resolve(f).then((function(t){c.value=t,a(c)}),(function(t){return r("throw",t,a,s)}))}s(u.arg)}var n;o(this,"_invoke",{value:function(t,i){function o(){return new e((function(e,n){r(t,i,e,n)}))}return n=n?n.then(o,o):o()}})}function P(e,r,n){var i=p;return function(o,a){if(i===d)throw Error("Generator is already running");if(i===y){if("throw"===o)throw a;return{value:t,done:!0}}for(n.method=o,n.arg=a;;){var s=n.delegate;if(s){var u=A(s,n);if(u){if(u===m)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(i===p)throw i=y,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);i=d;var c=h(e,r,n);if("normal"===c.type){if(i=n.done?y:v,c.arg===m)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(i=y,n.method="throw",n.arg=c.arg)}}}function A(e,r){var n=r.method,i=e.iterator[n];if(i===t)return r.delegate=null,"throw"===n&&e.iterator.return&&(r.method="return",r.arg=t,A(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),m;var o=h(i,e.iterator,r.arg);if("throw"===o.type)return r.method="throw",r.arg=o.arg,r.delegate=null,m;var a=o.arg;return a?a.done?(r[e.resultName]=a.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,m):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,m)}function L(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function T(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function N(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(L,this),this.reset(!0)}function I(e){if(e||""===e){var r=e[s];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,o=function r(){for(;++n<e.length;)if(i.call(e,n))return r.value=e[n],r.done=!1,r;return r.value=t,r.done=!0,r};return o.next=o}}throw new TypeError(typeof e+" is not iterable")}return w.prototype=b,o(S,"constructor",{value:b,configurable:!0}),o(b,"constructor",{value:w,configurable:!0}),w.displayName=f(b,c,"GeneratorFunction"),e.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===w||"GeneratorFunction"===(e.displayName||e.name))},e.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,b):(t.__proto__=b,f(t,c,"GeneratorFunction")),t.prototype=Object.create(S),t},e.awrap=function(t){return{__await:t}},O(j.prototype),f(j.prototype,u,(function(){return this})),e.AsyncIterator=j,e.async=function(t,r,n,i,o){void 0===o&&(o=Promise);var a=new j(l(t,r,n,i),o);return e.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},O(S),f(S,c,"Generator"),f(S,s,(function(){return this})),f(S,"toString",(function(){return"[object Generator]"})),e.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},e.values=I,N.prototype={constructor:N,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(T),!e)for(var r in this)"t"===r.charAt(0)&&i.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function n(n,i){return s.type="throw",s.arg=e,r.next=n,i&&(r.method="next",r.arg=t),!!i}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],s=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=i.call(a,"catchLoc"),c=i.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!c)throw Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&i.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var o=n;break}}o&&("break"===t||"continue"===t)&&o.tryLoc<=e&&e<=o.finallyLoc&&(o=null);var a=o?o.completion:{};return a.type=t,a.arg=e,o?(this.method="next",this.next=o.finallyLoc,m):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),m},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),T(r),m}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var i=n.arg;T(r)}return i}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:I(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),m}},e}function i(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,e||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:e+""}function o(t){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},o(t)}function a(t,e,r,n,i,o,a){try{var s=t[o](a),u=s.value}catch(t){return void r(t)}s.done?e(u):Promise.resolve(u).then(n,i)}function s(t){return function(){var e=this,r=arguments;return new Promise((function(n,i){var o=t.apply(e,r);function s(t){a(o,n,i,s,u,"next",t)}function u(t){a(o,n,i,s,u,"throw",t)}s(void 0)}))}}function u(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function c(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,i(n.key),n)}}function f(t,e,r){return e&&c(t.prototype,e),r&&c(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function l(t,e,r){return(e=i(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function h(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,i,o,a,s=[],u=!0,c=!1;try{if(o=(r=r.call(t)).next,0===e){if(Object(r)!==r)return;u=!1}else for(;!(u=(n=o.call(r)).done)&&(s.push(n.value),s.length!==e);u=!0);}catch(t){c=!0,i=t}finally{try{if(!u&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(c)throw i}}return s}}(t,e)||v(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function p(t){return function(t){if(Array.isArray(t))return d(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||v(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function v(t,e){if(t){if("string"==typeof t)return d(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?d(t,e):void 0}}function d(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function y(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!r){if(Array.isArray(t)||(r=v(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var n=0,i=function(){};return{s:i,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a=!0,s=!1;return{s:function(){r=r.call(t)},n:function(){var t=r.next();return a=t.done,t},e:function(t){s=!0,o=t},f:function(){try{a||null==r.return||r.return()}finally{if(s)throw o}}}}function m(t,e){if(e.has(t))throw new TypeError("Cannot initialize the same private elements twice on an object")}function g(t,e,r){m(t,e),e.set(t,r)}function w(t){return"function"==typeof t}function b(){return Math.floor(1024*Math.random()*50)}var x,k=function(){return f((function t(){u(this,t),l(this,"buffer",void 0),l(this,"extractor",void 0),this.buffer=[],this.extractor=void 0}),[{key:"isBusy",get:function(){return void 0!==this.extractor}},{key:"insert",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";this.buffer.push(t),w(this.extractor)&&this.extractor.call(void 0)}},{key:"extract",get:function(){var t;return(null===(t=this.buffer.shift())||void 0===t?void 0:t.trim())||""}},{key:"readline",value:function(){var t=this;return new Promise((function(e){t.buffer.length?e(t.extract):t.extractor=function(){t.extractor=void 0,e(t.extract)}}))}},{key:"reset",value:function(){this.buffer.splice(0),this.extractor=void 0}}])}(),E=function(){return f((function t(){u(this,t),l(this,"isActive",void 0),l(this,"bufferOutput",void 0),l(this,"buffer",void 0),l(this,"onoutput",void 0),l(this,"onerror",void 0),l(this,"onclear",void 0),this.isActive=!0,this.bufferOutput=!1,this.buffer=[]}),[{key:"disable",value:function(){this.isActive=!1}},{key:"enable",value:function(){this.isActive=!0}},{key:"clear",value:function(){var t;null===(t=this.onclear)||void 0===t||t.call(void 0)}},{key:"write",value:function(t){var e,r,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"data";this.isActive&&(t=null===(e=t)||void 0===e?void 0:e.toString(),this.bufferOutput?this.buffer.push({type:n,data:t}):null===(r="data"==n?this.onoutput:this.onerror)||void 0===r||r.call(void 0,t))}},{key:"error",value:function(t){this.write(t,"error")}},{key:"extract",get:function(){return this.buffer.splice(0).map((function(t){return t.data+""}))}},{key:"flush",value:function(){var t=this;this.bufferOutput=!1,this.buffer.length&&this.buffer.splice(0).forEach((function(e){t.write(e.data,e.type)}))}},{key:"reset",value:function(){this.flush(),this.enable()}}])}(),S="v0.8.1",O="vsh",j="?",P=O+": process aborted!",A=O+": process timed out!",L=O+": process terminated!",T="command not found",N="RANDOM",I=O+": inactive, use shell.reset() to activate",_={SHELL:O,USERNAME:"user",HOSTNAME:"web",CWD:"/",PS1:"$USERNAME@$HOSTNAME: $CWD $ ",PS2:"> ","?":"0",RANDOM:""+b()};function M(t){var e;return(""+(arguments.length>1&&void 0!==arguments[1]?arguments[1]:"")).replace(/(?:\$([a-z_][a-z0-9_]+|\?))/gi,(function(r){return void 0===(e=t[r.slice(1)])?"":e.toString()}))}function D(t){return"object"===o(e=t)&&null!==e&&w(t.action)&&!!t.description&&!!t.synopsis;var e}function W(t){return t.match(/^([a-zA-Z0-9_]+)=(.*)$/)}function C(t,e){if(t)return e&&t.AND?t.AND:!e&&t.OR?t.OR:C(t.AND,e)||C(t.OR,e)}var R=[";","|","&&","||"];function $(t){var e={cmd:"",args:"",argv:[],PIPE:void 0,AND:void 0,OR:void 0};e.cmd=t.shift()||"";var r=t.findIndex((function(t){return R.slice(1).includes(t)}));return-1===r?(e.argv=t.splice(0),e.args=(e.cmd+" "+e.argv.join(" ")).trim(),e):(e.argv=t.splice(0,r),e.args=(e.cmd+" "+e.argv.join(" ")).trim(),t[0]===R[1]&&(e.PIPE=$(t.splice(1))),t[0]===R[2]&&(e.AND=$(t.splice(1))),t[0]===R[3]&&(e.OR=$(t.splice(1))),e)}function G(t){return function(t){var e=[];if(!t||"string"!=typeof t)return e;var r,n=[],i=!1,o=!1,a=!1,s=!1,u="";if(R.some((function(e){return t.startsWith(r=e)||t.endsWith(e)})))throw new SyntaxError("unexpected token '"+r+"'");if(t.split("").forEach((function(t,r,c){if(i&&"'"===t)return i=!1,void(s=!0);if(!i&&!o&&!a){if("'"===t)return void(i=!0);if('"'===t)return void(o=!0);if("\\"===t)return void(a=!0);if(["\b","\f","\n","\r","\t"," ",";"].includes(t))return(u.length>0||s)&&(n.push(u),s=!1),";"===t&&n.length&&e.push(n.splice(0)),void(u="")}if(!i&&o&&!a&&'"'===t)return o=!1,void(s=!0);!i&&o&&!a&&"\\"===t&&(a=!0,['"',"`","$","\\"].includes(c[r+1]))||(a&&(a=!1),u+=t)})),(u.length>0||s)&&(n.push(u),s=!1),n.length&&e.push(n),o)throw new SyntaxError("unexpected end of string while looking for matching double quote");if(i)throw new SyntaxError("unexpected end of string while looking for matching single quote");if(a)throw new SyntaxError("unexpected end of string right after slash");return e}(t).map((function(t){return $(t)}))}function F(t,e,r,n){var i=!1,o=n.signal;o.addEventListener("abort",(function(){return i=!0}));var a={readline:function(){return i?Promise.resolve(""):e.readline()}},s={clear:function(){return!i&&r.clear()},write:function(e){i||r.write(M(t.env,e))},writeln:function(t){return s.write(t+"\n")}},u={write:function(e){i||r.error(M(t.env,e))},writeln:function(t){return u.write(t+"\n")}};return{cmd:"",args:"",argv:[],get env(){return t.env},get stderr(){return u},get stdin(){return a},get stdout(){return s},get history(){return t.history},get version(){return S},get exitCode(){return function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,r=parseInt(""+t);return isNaN(r)?e:r}(t.env[j])},exit:function(t){n.abort(t||L)},onExit:function(t){o.addEventListener("abort",(function(){t.call(void 0,o.reason.toString())}))}}}function H(){return{env:Object.assign(Object.create(null),_),alias:{},history:[]}}function z(t){return JSON.parse("string"==typeof t?t:JSON.stringify(t))}function J(t,e,r){var n,i=t.signal;return r&&r>0&&(n=setTimeout((function(){t.abort(A)}),r)),new Promise((function(t,r){i.addEventListener("abort",(function(){clearTimeout(n),r(i.reason.toString()||P)})),e(i,r).then(t).catch(r).finally((function(){return clearTimeout(n)}))}))}var U=new WeakMap,q=new WeakMap,B=new WeakMap,V=new WeakMap,Y=new WeakMap,Z=new WeakMap,K=new WeakMap,Q=new WeakSet,X=function(){return f((function t(){var i,o,a,c,f,l,p;u(this,t),m(i=this,o=Q),o.add(i),g(this,U,void 0),g(this,q,void 0),g(this,B,void 0),g(this,V,void 0),g(this,Y,void 0),g(this,Z,void 0),g(this,K,void 0),r(U,this,new E),r(q,this,new k),r(B,this,H()),r(V,this,new Map),r(Y,this,!1),r(Z,this,void 0),a=e(V,this),c=e(B,this),a.set("exit",{synopsis:"exit",description:"Terminate the current process",action:function(t){var e,r=t.exit;null===(e=x)||void 0===e||e.call(void 0),r()}}),a.set("clear",{synopsis:"clear",description:"Clear the entire standard output stream.",action:function(t){return t.stdout.clear()}}),c.alias.cls="clear",a.set("pwd",{synopsis:"pwd",description:"Print current working directory.",action:function(t){return t.stdout.writeln("$CWD")}}),a.set("echo",{synopsis:"echo [...args]",description:"Write arguments to the standard output followed by a new line character.",action:function(t){var e=t.argv;return t.stdout.writeln(e.join(" "))}}),c.alias.print="echo",a.set("alias",{synopsis:"alias [-p] [name=[value] ... ]",description:"Defines aliases for commands",action:function(t){var e=t.argv,r=t.stdout;!e.length||e.includes("-p")?(r.write("Aliases:"),Object.entries(c.alias).forEach((function(t){var e=h(t,2),n=e[0],i=e[1];r.write("\n\talias "+n+"='"+i+"'")})),r.write("\n")):e.forEach((function(t){var e=W(t);if(e){var r=h(e,3),n=r[1],i=r[2];c.alias[n.trim()]=i.trim()}}))}}),a.set("unalias",{synopsis:"unalias [name ... ]",description:"Removes aliases for commands",action:function(t){var e=t.argv;e.length&&e.forEach((function(t){delete c.alias[t]}))}}),a.set("export",{synopsis:"export [-p] [name=[value] ... ]",description:"Set shell variables by name and value",action:function(t){var e=t.argv,r=t.env,n=t.stdout;!e.length||e.includes("-p")?Object.entries(r).forEach((function(t){var e=h(t,2),r=e[0],i=e[1];n.write("var "+r+'="'+(null!=i&&i.toString().includes("$")?i.toString().split("").join("\\"):i)+'"\n')})):e.forEach((function(t){var e=W(t);if(e){var n=h(e,3),i=n[1],o=n[2];r[i.trim()]=o.trim()}}))}}),a.set("history",{synopsis:"history [-c] [-n]",description:"Retrieve previous input entries",action:function(t){var e=t.argv,r=t.history,n=t.stdout;e.includes("-c")?r.splice(0):e.includes("-n")?n.writeln("History: ".concat(r.length)):r.forEach((function(t,e){n.writeln(" "+e+"\t"+t)}))}}),a.set("help",{synopsis:"help [command]",description:"Displays information on available commands.",action:function(t){var e=t.argv,r=t.stdout;if(e[0]){var n=e[0],i=a.get(n);if(!i)throw"help: no information matching '"+n+"'";var o=i.synopsis,s=i.description;r.writeln(n+": "+o+"\n\t"+s)}else r.write("ViteShell, "+S+" Help\n\nA list of all available commands\n\n"),Array.from(a.values()).map((function(t){return t.synopsis})).sort().forEach((function(t){return r.writeln(t)}))}}),c.alias.info="help",c.alias.man="help",a.set("read",{synopsis:"read [prompt] [variable]",description:"Capture input and save it in the env object.",action:(f=s(n().mark((function t(e){var r,i,o,a;return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=e.argv,i=e.env,o=e.stdin,a=e.stdout,!r[0]||!r[1]){t.next=8;break}return a.write(r[0]),t.next=5,o.readline();case 5:i[r[1]]=t.sent,t.next=9;break;case 8:throw"invalid arguments: specify the prompt and variable name";case 9:case"end":return t.stop()}}),t)}))),function(t){return f.apply(this,arguments)})}),a.set("sleep",{synopsis:"sleep [seconds]",description:"Delay for a specified amount of time (in seconds).",action:(l=s(n().mark((function t(e){var r,i,o;return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=e.argv,i=e.onExit,o=parseInt(r[0],10),!(isNaN(o)||o<=0)){t.next=4;break}throw"invalid time specified (minimum is 1)";case 4:return t.next=6,new Promise((function(t){var e;i((function(){clearTimeout(e),t()})),e=setTimeout((function(){return t()}),1e3*o)}));case 6:case"end":return t.stop()}}),t)}))),function(t){return l.apply(this,arguments)})}),a.set("grep",{synopsis:"grep [keyword] [context ...]",description:"Searches for matching phrases in the text",action:(p=s(n().mark((function t(e){var r,i,o;return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=e.argv,i=e.stdout,!(r.length<2)){t.next=3;break}throw"invalid arguments";case 3:o=new RegExp(r[0],"g"),r.slice(1).forEach((function(t){o.test(t)&&i.writeln(t.replaceAll(r[0],(function(t){return"**"+t+"**"})))}));case 5:case"end":return t.stop()}}),t)}))),function(t){return p.apply(this,arguments)})}),a.set("date",{synopsis:"date",description:"Displays the current time and date",action:function(t){return t.stdout.writeln((new Date).toString())}})}),[{key:"alias",get:function(){return e(B,this).alias}},{key:"env",get:function(){return e(B,this).env}},{key:"history",get:function(){return e(B,this).history}},{key:"onoutput",set:function(t){if(!w(t))throw new TypeError("".concat(O,": onoutput handler must be a function."));e(U,this).onoutput=t}},{key:"onerror",set:function(t){if(!w(t))throw new TypeError("".concat(O,": onerror handler must be a function."));e(U,this).onerror=t}},{key:"onclear",set:function(t){if(!w(t))throw new TypeError("".concat(O,": onclear handler must be a function."));e(U,this).onclear=t}},{key:"onexit",set:function(t){if(!w(t))throw new TypeError("".concat(O,": onexit handler must be a function."));x=t}},{key:"addCommand",value:function(t,r){if(e(V,this).has(t))throw new Error("".concat(O,": '").concat(t,"' command already exists. If you are providing a custom command implementation, remove the command first."));if(!D(r))throw new Error("".concat(O,": invalid command configuration."));e(V,this).set(t,r)}},{key:"removeCommand",value:function(t){e(V,this).delete(t)}},{key:"listCommands",value:function(){return Array.from(e(V,this).keys())}},{key:"exportState",value:function(){return JSON.stringify(e(B,this))}},{key:"loadState",value:function(t){r(B,this,Object.assign(H(),z(t)))}},{key:"reset",value:function(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";r(Y,this,!0),e(q,this).reset(),e(U,this).clear(),n&&(n=M(this.env,n+"\n"),e(U,this).write(n)),t(Q,this,tt).call(this)}},{key:"setExecutionTimeout",value:function(t){if(!("number"==typeof t&&t>0))throw new TypeError("".concat(O,": invalid value for timeout."));r(K,this,1e3*t)}},{key:"execute",value:(i=s(n().mark((function i(){var o,a,u,c,f,l,h=this,v=arguments;return n().wrap((function(i){for(;;)switch(i.prev=i.next){case 0:if(o=v.length>0&&void 0!==v[0]?v[0]:"",e(Y,this)){i.next=3;break}return i.abrupt("return",Promise.reject(I));case 3:if(!e(q,this).isBusy){i.next=8;break}return e(q,this).insert(o),i.abrupt("return",Promise.resolve());case 8:e(q,this).reset();case 9:if(e(U,this).reset(),"string"==typeof o&&o.trim()){i.next=13;break}return t(Q,this,tt).call(this),i.abrupt("return",Promise.resolve());case 13:return(o=o.trim())!=this.history.at(-1)&&this.history.push(o),a=z(e(B,this)),u=new AbortController,c=u.signal,r(Z,this,u),c.addEventListener("abort",(function(){e(q,h).reset()})),f=F(a,e(q,this),e(U,this),u),l=J(u,function(){var r=s(n().mark((function r(i,s){var u,c,l,v;return n().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:r.prev=0,u=G(o),c=y(u),r.prev=3,c.s();case 5:if((l=c.n()).done){r.next=26;break}if(v=l.value,!i.aborted){r.next=9;break}throw P;case 9:return r.prev=9,r.next=12,t(Q,h,et).call(h,v,f);case 12:a.env[j]="0",n=e(B,h),d=a,m=void 0,Object.assign(n.alias,d.alias),Object.assign(n.env,d.env),n.history.splice(0),(m=n.history).push.apply(m,p(d.history)),r.next=24;break;case 16:if(r.prev=16,r.t0=r.catch(9),!(u.length>1)){r.next=23;break}f.stderr.write(r.t0+"\n"),a.env[j]="1",r.next=24;break;case 23:throw r.t0;case 24:r.next=5;break;case 26:r.next=31;break;case 28:r.prev=28,r.t1=r.catch(3),c.e(r.t1);case 31:return r.prev=31,c.f(),r.finish(31);case 34:r.next=39;break;case 36:r.prev=36,r.t2=r.catch(0),s(r.t2.toString());case 39:case"end":return r.stop()}var n,d,m}),r,null,[[0,36],[3,28,31,34],[9,16]])})));return function(t,e){return r.apply(this,arguments)}}(),e(K,this)),i.abrupt("return",l.catch((function(t){e(U,h).error(t+"\n"),e(B,h).env[j]="1"})).finally((function(){t(Q,h,tt).call(h)})));case 23:case"end":return i.stop()}}),i,this)}))),function(){return i.apply(this,arguments)})},{key:"abort",value:function(t){var r;null===(r=e(Z,this))||void 0===r||r.abort(t||P)}}],[{key:"version",get:function(){return S}}]);var i}();function tt(){this.env[N]=""+b(),e(U,this).reset(),e(U,this).write(M(this.env,this.env.PS1))}function et(t,e){return rt.apply(this,arguments)}function rt(){return rt=s(n().mark((function i(o,a){var u,c,f,l,h,v,d,y=this;return n().wrap((function(i){for(;;)switch(i.prev=i.next){case 0:if(u="",c=function(){var e=s(n().mark((function e(){var r;return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!o.OR&&!o.AND){e.next=6;break}if(!(r=C(o,!u.length))){e.next=6;break}return u.length&&(a.stderr.writeln(u),u=""),e.next=6,t(Q,y,et).call(y,r,a);case 6:if(!u.length){e.next=8;break}throw u;case 8:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),(f=this.alias[o.cmd])&&(h=f.split(" "),o.cmd=h.shift()||"",h.length&&(l=o.argv).unshift.apply(l,p(h)),o.args=(o.cmd+" "+o.argv.join(" ")).trim()),v=e(V,this).get(o.cmd)){i.next=10;break}return u=o.cmd+": "+T,i.next=9,c();case 9:return i.abrupt("return",i.sent);case 10:return a.cmd=o.cmd,a.argv=o.argv,a.env[N]=""+b(),e(U,this).bufferOutput=void 0!==o.PIPE,i.prev=14,"exit"===o.cmd&&r(Y,this,!1),i.next=18,v.action.call(void 0,a);case 18:i.next=23;break;case 20:i.prev=20,i.t0=i.catch(14),u=o.cmd+": "+i.t0;case 23:if(!o.PIPE){i.next=27;break}return(d=o.PIPE.argv).push.apply(d,p(e(U,this).extract)),i.next=27,t(Q,this,et).call(this,o.PIPE,a);case 27:return i.next=29,c();case 29:case"end":return i.stop()}}),i,this,[[14,20]])}))),rt.apply(this,arguments)}return X}));