brsh 2.2.0 → 2.2.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.
@@ -950,7 +950,7 @@ var g, _, v, y, b, x, S, C, w, T, E = e((() => {
950
950
  super(t, n), this.script = e, this.source = r;
951
951
  }
952
952
  get captureInput() {
953
- return !0;
953
+ return !!(this.sandbox && this.sandbox.runningCommand && this.sandbox.runningCommand.captureInput);
954
954
  }
955
955
  onInput(e) {
956
956
  this.sandbox && this.sandbox.onInput(e);
@@ -963,7 +963,11 @@ var g, _, v, y, b, x, S, C, w, T, E = e((() => {
963
963
  this.sandbox = new r.shell({
964
964
  cwd: this.context.fs.cwd,
965
965
  ...e
966
- }), this.sandbox.context.source(this.context), this.sandbox.on("stdOut", (e) => this.stdOut = e), this.sandbox.on("stdErr", (e) => this.stdErr = e), this._scriptFunctions = {};
966
+ }), this.sandbox.context.source(this.context), this.sandbox.on("stdOut", (e) => {
967
+ this.stdOut = e, this.flush();
968
+ }), this.sandbox.on("stdErr", (e) => {
969
+ this.stdErr = e, this.flush();
970
+ }), this._scriptFunctions = {};
967
971
  let t = this.script.split(/\r?\n/);
968
972
  if (!a(t[0])) {
969
973
  let e = t[0].startsWith("#!") ? t[0].slice(2).split(/\s/)[0] : t[0];
@@ -1018,29 +1022,37 @@ var g, _, v, y, b, x, S, C, w, T, E = e((() => {
1018
1022
  return t;
1019
1023
  }
1020
1024
  _parseIf(e, t) {
1021
- let n = e[t].trim().replace(/^if\s+/, "").replace(/\s*;\s*then\s*$/, "").replace(/\s+then\s*$/, "").trim(), r = t + 1;
1022
- r < e.length && e[r].trim() === "then" && r++;
1023
- let i = [], a = [], o = !1, s = !1, c = 1;
1024
- for (; r < e.length;) {
1025
- let t = e[r].trim();
1026
- if (t.startsWith("if ") && c++, t === "fi" && (c--, c === 0)) {
1027
- s && a.push("fi"), r++;
1025
+ let n = (e) => e.replace(/^(?:if|elif)\s+/, "").replace(/\s*;\s*then\s*$/, "").replace(/\s+then\s*$/, "").trim(), r = [{
1026
+ condition: n(e[t].trim()),
1027
+ bodyLines: []
1028
+ }], i = [], a = r[0], o = !1, s = 1, c = t + 1;
1029
+ for (c < e.length && e[c].trim() === "then" && c++; c < e.length;) {
1030
+ let t = e[c].trim();
1031
+ if ((t.startsWith("if ") || t === "if") && s++, t === "fi" && (s--, s === 0)) {
1032
+ c++;
1028
1033
  break;
1029
1034
  }
1030
- if (c === 1 && (t === "else" || t.startsWith("elif "))) {
1031
- o ||= !0, t.startsWith("elif ") ? (s = !0, a.push(t.replace(/^elif/, "if"))) : s && a.push(e[r]), r++;
1035
+ if (s === 1 && t.startsWith("elif ")) {
1036
+ let i = {
1037
+ condition: n(t),
1038
+ bodyLines: []
1039
+ };
1040
+ r.push(i), a = i, c++, c < e.length && e[c].trim() === "then" && c++;
1041
+ continue;
1042
+ }
1043
+ if (s === 1 && t === "else") {
1044
+ o = !0, c++;
1032
1045
  continue;
1033
1046
  }
1034
- o ? a.push(e[r]) : i.push(e[r]), r++;
1047
+ o ? i.push(e[c]) : a.bodyLines.push(e[c]), c++;
1035
1048
  }
1036
1049
  return {
1037
1050
  node: {
1038
1051
  type: "if",
1039
- condition: n,
1040
- thenLines: i,
1041
- elseLines: a
1052
+ clauses: r,
1053
+ elseLines: i
1042
1054
  },
1043
- consumed: r - t
1055
+ consumed: c - t
1044
1056
  };
1045
1057
  }
1046
1058
  _parseWhile(e, t) {
@@ -1096,8 +1108,16 @@ var g, _, v, y, b, x, S, C, w, T, E = e((() => {
1096
1108
  return t;
1097
1109
  }
1098
1110
  run() {
1099
- return r._executeFunctionBody(n).catch((e) => {
1100
- if (e && e.__type === "return") {
1111
+ let e = this.arguments.map((e, t) => ({
1112
+ key: String(t + 1),
1113
+ prev: r.sandbox.context.getVar(String(t + 1))
1114
+ }));
1115
+ this.arguments.forEach((e, t) => r.sandbox.context.setVar(String(t + 1), e));
1116
+ let t = () => e.forEach(({ key: e, prev: t }) => t ? r.sandbox.context.setVar(e, t) : r.sandbox.context.unsetVar(e));
1117
+ return r._executeFunctionBody(n).then(() => {
1118
+ t();
1119
+ }).catch((e) => {
1120
+ if (t(), e && e.__type === "return") {
1101
1121
  this.exitCode = e.code;
1102
1122
  return;
1103
1123
  }
@@ -1120,10 +1140,12 @@ var g, _, v, y, b, x, S, C, w, T, E = e((() => {
1120
1140
  return e.type === "if" ? this._executeIf(e) : e.type === "while" ? this._executeWhile(e) : e.type === "for" ? this._executeFor(e) : e.type === "case" ? this._executeCase(e) : Promise.resolve();
1121
1141
  }
1122
1142
  _executeIf(e) {
1123
- return this.sandbox.onCommand(e.condition, !1).then(() => {
1124
- if (this.sandbox.lastCode === 0) return this._executeNodes(this._parseLines(e.thenLines));
1125
- if (e.elseLines.length > 0) return this._executeNodes(this._parseLines(e.elseLines));
1126
- });
1143
+ let t = (n) => {
1144
+ if (n >= e.clauses.length) return e.elseLines.length > 0 ? this._executeNodes(this._parseLines(e.elseLines)) : Promise.resolve();
1145
+ let r = e.clauses[n];
1146
+ return this.sandbox.onCommand(r.condition, !1).then(() => this.sandbox.lastCode === 0 ? this._executeNodes(this._parseLines(r.bodyLines)) : t(n + 1));
1147
+ };
1148
+ return t(0);
1127
1149
  }
1128
1150
  _executeWhile(e) {
1129
1151
  let t = () => this.sandbox.onCommand(e.condition, !1).then(() => {
@@ -3846,7 +3868,11 @@ var V, re, H, U, W, ie, G, ae, oe, se, K = e((() => {
3846
3868
  }));
3847
3869
  }
3848
3870
  onInput(e) {
3849
- if (this.inputBuffer ||= "", e.length === 1 && (this.inputBuffer += e), this.inputBuffer.length === this.nchars) {
3871
+ if (this.inputBuffer ||= "", e.length === 1 && (this.inputBuffer += e), e === "Enter") {
3872
+ this.var ? (this.context.setVar(this.var, this.inputBuffer || ""), this.completionCallback()) : this.completionCallback(this.inputBuffer || "");
3873
+ return;
3874
+ }
3875
+ if (this.inputBuffer.length === this.nchars) {
3850
3876
  if (this.var) {
3851
3877
  this.context.setVar(this.var, this.inputBuffer), this.completionCallback();
3852
3878
  return;