@phpsandbox/sdk 0.0.27 → 0.0.29

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.
@@ -532,8 +532,7 @@ var Filesystem = class {
532
532
  };
533
533
  const subscription = new FilesystemSubscription(path, this.okra, wrappedDisposable);
534
534
  this.watches.set(path, { options, path, onDidChange });
535
- this.okra.invoke("fs.watch", { path, options });
536
- return subscription;
535
+ return this.okra.invoke("fs.watch", { path, options }).then(() => subscription);
537
536
  }
538
537
  exists(path) {
539
538
  return this.stat(path).then(() => true).catch(() => false);
@@ -910,6 +909,14 @@ var Repl = class {
910
909
  };
911
910
 
912
911
  // src/shell.ts
912
+ function resolveExecInput(command, argsOrOpts = [], opts) {
913
+ const args = Array.isArray(argsOrOpts) ? argsOrOpts : [];
914
+ const resolvedOpts = Array.isArray(argsOrOpts) ? opts : argsOrOpts;
915
+ return {
916
+ command: Array.isArray(command) ? command : args.length > 0 ? [command, ...args] : command,
917
+ opts: resolvedOpts
918
+ };
919
+ }
913
920
  var CommandError = class extends Error {
914
921
  constructor(output, exitCode) {
915
922
  super(output);
@@ -933,10 +940,83 @@ var Shell = class {
933
940
  constructor(okra) {
934
941
  this.okra = okra;
935
942
  }
936
- async exec(command) {
937
- const result = await this.okra.invoke("shell.exec", { command });
943
+ onOutput(id, handler) {
944
+ this.okra.listen(`shell.output.${id}`, handler);
945
+ }
946
+ listen(event, handler) {
947
+ return this.okra.listen(event, handler);
948
+ }
949
+ async exec(command, argsOrOpts = [], opts) {
950
+ const input = resolveExecInput(command, argsOrOpts, opts);
951
+ const { abortSignal, ...payloadOpts } = input.opts ?? {};
952
+ const result = await this.okra.invoke(
953
+ "shell.exec",
954
+ {
955
+ command: input.command,
956
+ opts: Object.keys(payloadOpts).length > 0 ? payloadOpts : void 0
957
+ },
958
+ { abortSignal }
959
+ );
938
960
  return new Result(result.output, result.exitCode);
939
961
  }
962
+ execStream(command, argsOrOpts = [], opts) {
963
+ const input = resolveExecInput(command, argsOrOpts, opts);
964
+ const id = input.opts?.id || nanoid();
965
+ const disposables = /* @__PURE__ */ new Set();
966
+ let controller = null;
967
+ let closed = false;
968
+ const dispose = () => {
969
+ for (const disposable of disposables) {
970
+ disposable.dispose();
971
+ }
972
+ disposables.clear();
973
+ };
974
+ const close = () => {
975
+ if (closed) {
976
+ return;
977
+ }
978
+ closed = true;
979
+ dispose();
980
+ if (controller) {
981
+ try {
982
+ controller.close();
983
+ } catch {
984
+ }
985
+ }
986
+ };
987
+ const fail = (error) => {
988
+ if (closed) {
989
+ return;
990
+ }
991
+ closed = true;
992
+ dispose();
993
+ if (controller) {
994
+ controller.error(error);
995
+ }
996
+ };
997
+ const output = new ReadableStream({
998
+ start: (_controller) => {
999
+ controller = _controller;
1000
+ disposables.add(
1001
+ this.listen(`shell.output.${id}`, (data) => {
1002
+ controller?.enqueue(data.output);
1003
+ })
1004
+ );
1005
+ },
1006
+ cancel: () => {
1007
+ closed = true;
1008
+ dispose();
1009
+ }
1010
+ });
1011
+ const result = this.exec(input.command, { ...input.opts, id }).then((response) => {
1012
+ close();
1013
+ return response;
1014
+ }).catch((error) => {
1015
+ fail(error);
1016
+ throw error;
1017
+ });
1018
+ return { output, result };
1019
+ }
940
1020
  };
941
1021
 
942
1022
  // node_modules/@msgpack/msgpack/dist.esm/utils/utf8.mjs