diff options
author | Leo Kettmeir <crowlkats@toaxl.com> | 2022-11-13 20:00:24 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-13 14:00:24 -0500 |
commit | 52dc3ef1a4db10ce35345c84e07bbae0c463ba18 (patch) | |
tree | ef111110376070f16867c7850f077b2b27834085 /runtime/js | |
parent | 15db936348d7c76013244344b65dfa5a440a99f2 (diff) |
feat(unstable): "Deno.Command()" API (#16516)
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Diffstat (limited to 'runtime/js')
-rw-r--r-- | runtime/js/40_spawn.js | 128 | ||||
-rw-r--r-- | runtime/js/90_deno_ns.js | 1 |
2 files changed, 129 insertions, 0 deletions
diff --git a/runtime/js/40_spawn.js b/runtime/js/40_spawn.js index 4d2fb1607..e262a1325 100644 --- a/runtime/js/40_spawn.js +++ b/runtime/js/40_spawn.js @@ -291,8 +291,136 @@ }; } + class Command { + #command; + #options; + + #child; + + #consumed; + + constructor(command, options) { + this.#command = command; + this.#options = options; + } + + output() { + if (this.#child) { + return this.#child.output(); + } else { + if (this.#consumed) { + throw new TypeError( + "Command instance is being or has already been consumed.", + ); + } + if (this.#options?.stdin === "piped") { + throw new TypeError( + "Piped stdin is not supported for this function, use 'Deno.Command.spawn()' instead", + ); + } + + this.#consumed = true; + return Deno.spawn(this.#command, this.#options); + } + } + + outputSync() { + if (this.#consumed) { + throw new TypeError( + "Command instance is being or has already been consumed.", + ); + } + if (this.#child) { + throw new TypeError("Was spawned"); + } + if (this.#options?.stdin === "piped") { + throw new TypeError( + "Piped stdin is not supported for this function, use 'Deno.Command.spawn()' instead", + ); + } + + this.#consumed = true; + return Deno.spawnSync(this.#command, this.#options); + } + + spawn() { + if (this.#consumed) { + throw new TypeError( + "Command instance is being or has already been consumed.", + ); + } + + this.#consumed = true; + this.#child = Deno.spawnChild(this.#command, this.#options); + } + + get stdin() { + if (!this.#child) { + throw new TypeError("Wasn't spawned"); + } + + return this.#child.stdin; + } + + get stdout() { + if (!this.#child) { + throw new TypeError("Wasn't spawned"); + } + + return this.#child.stdout; + } + + get stderr() { + if (!this.#child) { + throw new TypeError("Wasn't spawned"); + } + + return this.#child.stderr; + } + + get status() { + if (!this.#child) { + throw new TypeError("Wasn't spawned"); + } + + return this.#child.status; + } + + get pid() { + if (!this.#child) { + throw new TypeError("Wasn't spawned"); + } + + return this.#child.pid; + } + + kill(signo = "SIGTERM") { + if (!this.#child) { + throw new TypeError("Wasn't spawned"); + } + this.#child.kill(signo); + } + + ref() { + if (!this.#child) { + throw new TypeError("Wasn't spawned"); + } + + this.#child.ref(); + } + + unref() { + if (!this.#child) { + throw new TypeError("Wasn't spawned"); + } + + this.#child.unref(); + } + } + window.__bootstrap.spawn = { Child, + Command, createSpawn, createSpawnChild, createSpawnSync, diff --git a/runtime/js/90_deno_ns.js b/runtime/js/90_deno_ns.js index 033ad421e..16dd5c72f 100644 --- a/runtime/js/90_deno_ns.js +++ b/runtime/js/90_deno_ns.js @@ -149,6 +149,7 @@ spawnChild: __bootstrap.spawn.spawnChild, spawn: __bootstrap.spawn.spawn, spawnSync: __bootstrap.spawn.spawnSync, + Command: __bootstrap.spawn.Command, serve: __bootstrap.flash.serve, upgradeHttp: __bootstrap.http.upgradeHttp, upgradeHttpRaw: __bootstrap.flash.upgradeHttpRaw, |