brsh 3.1.0 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/shell.min.js CHANGED
@@ -41,5 +41,5 @@
41
41
  `+r.join(` `)+`\r
42
42
  `),this._redrawPromptLine())}_splitInput(e){let t=e.split(`#`)[0];return this._parseOperators(t)}_parseOperators(e){let t=[],n=``,r=0,i=!1,a=!1;for(;r<e.length;){let o=e[r];if(o===`'`&&!a){i=!i,n+=o,r++;continue}if(o===`"`&&!i){a=!a,n+=o,r++;continue}if(!i&&!a){if(o===`&`&&e[r+1]===`&`){t.push({command:n.trim(),op:`&&`}),n=``,r+=2;continue}if(o===`|`&&e[r+1]===`|`){t.push({command:n.trim(),op:`||`}),n=``,r+=2;continue}if(o===`|`){t.push({command:n.trim(),op:`|`}),n=``,r++;continue}if(o===`;`){t.push({command:n.trim(),op:`;`}),n=``,r++;continue}}n+=o,r++}return n.trim()&&t.push({command:n.trim(),op:null}),t}_parseCommand(e){if(e.startsWith(`./`)||e.startsWith(`/`)){let t=this.context.fs._getFileRaw(e);if(!t)throw Error(`${e}: No such file or directory`);if(t.constructor===Object)throw Error(`${e}: is a directory`);if(t.prototype&&typeof t.prototype.run==`function`&&typeof t.prototype.runCommand==`function`){if(!this.context.fs.isExecutable(e))throw Error(`${e}: permission denied`);return t}if(t.constructor===String&&t.startsWith(`#!`)){if(!this.context.fs.isExecutable(e))throw Error(`${e}: permission denied`);return i.bind(this,t)}if(this._isWasmFile(e,t)){if(!this.context.fs.isExecutable(e))throw Error(`${e}: permission denied`);return a.bind(null,e)}if(this.context.fs.isRealFilesystem&&typeof t==`string`&&e.endsWith(`.js`)){if(!this.context.fs.isExecutable(e))throw Error(`${e}: permission denied`);let t=this.context.fs.absolutePath(e);try{let e=require(t),n=e.default||e;if(typeof n==`function`&&typeof n.prototype.run==`function`&&typeof n.prototype.runCommand==`function`)return n}catch{}}throw Error(`${e}: permission denied`)}let t=this.context.getCommand(e),n=e;if(!t&&!e.endsWith(`.wasm`)&&(t=this.context.getCommand(`${e}.wasm`),t&&(n=`${e}.wasm`)),!t)throw Error(`${e}: Command not found`);let r=`${this.path}/${n}`;if(this._isWasmFile(r,t)){if(!this.context.fs.isExecutable(r))throw Error(`${e}: permission denied`);return a.bind(null,r)}return typeof t==`string`&&t.startsWith(`#!`)?i.bind(this,t):t}async _parseCommandLine(t){let n=e._splitCommand(t);return(await Promise.all(n.map(({value:e,hasQuote:t})=>this._replaceVariables(e).then(e=>({value:e,hasQuote:t}))))).filter(({value:e,hasQuote:t})=>e.length>0||t)}static _sanitiseCommandPart(e){return e.startsWith(`"`)&&e.endsWith(`"`)||e.startsWith(`'`)&&e.endsWith(`'`)?e.substring(1,e.length-1):e}static _sanitiseArray(e){return e.filter(e=>e.length>0)}async _replaceVariables(t){let n=[...t.matchAll(/\$\(\(([^)]+)\)\)/g)];for(let e of n)t=t.replace(e[0],String(this._evalArithmetic(e[1])));let r=[...t.matchAll(/\$\((?!\()([^)]+)\)/g)];for(let e of r){let n=await this._runAndCapture(e[1]);t=t.replace(e[0],n)}let i=t.matchAll(g),a=i.next();for(;!a.done;){let n=a.value[1]||a.value[2],r=this.context.getVar(n);t=t.replace(new RegExp(e._escapeRegExp(a.value[0]),`g`),String(r)),a=i.next()}return t}_evalArithmetic(e){let t=e.replace(/\${?([\w\d]+)}?/g,(e,t)=>String(parseInt(this.context.getVar(t))||0));if(t=t.replace(/\b([A-Za-z_]\w*)\b/g,(e,t)=>String(parseInt(this.context.getVar(t))||0)),!/^[\d\s+\-*/%()]+$/.test(t.trim()))return 0;try{return Function(`return (`+t+`)`)()}catch{return 0}}async _runAndCapture(e){let t=new globalThis.shell({cwd:this.context.fs.cwd,filesystem:this.context.fs.getRaw(),permissions:this.context.fs.getRawPermissions(),subshell:!0});t.context.source(this.context);let n=[];return t.on(`stdOut`,e=>n.push(e)),await t.onCommand(e,!1),n.join(`
43
43
  `).trimEnd()}_groupPipes(e){let t=[],n=[];for(let{command:r,op:i}of e)n.push(r),i!==`|`&&(t.push({commands:n,op:i}),n=[]);return n.length&&t.push({commands:n,op:null}),t}async _runPipeChain(e){if(e.length===1)return this._runCommand(e[0]);let t;for(let n=0;n<e.length;n++)if(n===e.length-1){t!==void 0&&(this._stdin=t);try{await this._runCommand(e[n])}finally{delete this._stdin}}else t=await this._captureForPipe(e[n],t)}async _captureForPipe(e,t){let n=new globalThis.shell({cwd:this.context.fs.cwd,filesystem:this.context.fs.getRaw(),permissions:this.context.fs.getRawPermissions(),subshell:!0});n.context.source(this.context),t!==void 0&&(n.stdin=t);let r=[];return n.on(`stdOut`,e=>r.push(e)),n.on(`stdErr`,e=>this.emit(`stdErr`,e)),await n.onCommand(e,!1),this.lastCode=n.lastCode,r.join(`
44
- `)}static _splitCommand(e){let t=/\s/g,n=!1,r=!1,i=0,a=[],o=!1,s=[];for(let c=0;c<e.length;c++){let l=e[c];if(i>0){a.push(l),l===`(`?i++:l===`)`&&i--;continue}if(l===`'`&&!r){n=!n,o=!0;continue}if(l===`"`&&!n){r=!r,o=!0;continue}if(!n&&!r){if(l===`$`&&c+1<e.length&&e[c+1]===`(`){i=1,a.push(`$`),a.push(`(`),c++;continue}if(l.match(t)){(a.length>0||o)&&(s.push({value:a.join(``),hasQuote:o}),a=[],o=!1);continue}}a.push(l)}if((a.length>0||o)&&s.push({value:a.join(``),hasQuote:o}),n||r)throw Error(`quote expression not closed`);return s}_isWasmFile(e,t){return e.endsWith(`.wasm`)||e.endsWith(`.wat`)?!0:t instanceof Uint8Array?t.length>=4&&t[0]===0&&t[1]===97&&t[2]===115&&t[3]===109:typeof t==`string`?t.charCodeAt(0)===0&&t.slice(1,4)===`asm`?!0:t.trimStart().startsWith(`(module`):!1}static _escapeRegExp(e){return e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)}static async create(t={}){let{filesystem:n,permissions:r,...i}=t;if(m(n)||n&&typeof n==`object`&&m(n.filesystem)){let e=await h(n,r);n=e.filesystem,r=e.permissions}let a=new e(Object.assign({},i,{filesystem:n,permissions:r}));return await a.ready,a}static async fromContainer(t,n={}){return e.create(Object.assign({},n,{filesystem:t}))}};x.STATUS_READY=`READY`,x.STATUS_WORKING=`WORKING`,x.STATUS_DESTROYED=`DESTROYED`,x.Command=o,x.InteractiveCommand=s,x.WasmCommand=a,x.createWasmCommand=a.createGenericWasmCommand||a.createCommand,x.ReplCommand=or(),x.FS=p,x.BinaryContainer=ne(),x.Storage=x.BinaryContainer.FilesystemStorage,globalThis.shell=x,globalThis.Shell=x,t.exports=x}))()})();
44
+ `)}static _splitCommand(e){let t=/\s/g,n=!1,r=!1,i=0,a=[],o=!1,s=[];for(let c=0;c<e.length;c++){let l=e[c];if(i>0){a.push(l),l===`(`?i++:l===`)`&&i--;continue}if(r&&l===`\\`&&c+1<e.length){let t=e[c+1];if(t===`"`||t===`\\`||t===`$`){a.push(t),c++;continue}}if(!n&&!r&&l===`\\`&&c+1<e.length){let t=e[++c];a.push(t),(t===`"`||t===`'`)&&(o=!0);continue}if(l===`'`&&!r){n=!n,o=!0;continue}if(l===`"`&&!n){r=!r,o=!0;continue}if(!n&&!r){if(l===`$`&&c+1<e.length&&e[c+1]===`(`){i=1,a.push(`$`),a.push(`(`),c++;continue}if(l.match(t)){(a.length>0||o)&&(s.push({value:a.join(``),hasQuote:o}),a=[],o=!1);continue}}a.push(l)}if((a.length>0||o)&&s.push({value:a.join(``),hasQuote:o}),n||r)throw Error(`quote expression not closed`);return s}_isWasmFile(e,t){return e.endsWith(`.wasm`)||e.endsWith(`.wat`)?!0:t instanceof Uint8Array?t.length>=4&&t[0]===0&&t[1]===97&&t[2]===115&&t[3]===109:typeof t==`string`?t.charCodeAt(0)===0&&t.slice(1,4)===`asm`?!0:t.trimStart().startsWith(`(module`):!1}static _escapeRegExp(e){return e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)}static async create(t={}){let{filesystem:n,permissions:r,...i}=t;if(m(n)||n&&typeof n==`object`&&m(n.filesystem)){let e=await h(n,r);n=e.filesystem,r=e.permissions}let a=new e(Object.assign({},i,{filesystem:n,permissions:r}));return await a.ready,a}static async fromContainer(t,n={}){return e.create(Object.assign({},n,{filesystem:t}))}};x.STATUS_READY=`READY`,x.STATUS_WORKING=`WORKING`,x.STATUS_DESTROYED=`DESTROYED`,x.Command=o,x.InteractiveCommand=s,x.WasmCommand=a,x.createWasmCommand=a.createGenericWasmCommand||a.createCommand,x.ReplCommand=or(),x.FS=p,x.BinaryContainer=ne(),x.Storage=x.BinaryContainer.FilesystemStorage,globalThis.shell=x,globalThis.Shell=x,t.exports=x}))()})();
45
45
  //# sourceMappingURL=shell.min.js.map