diff options
author | Leo Kettmeir <crowlkats@toaxl.com> | 2022-11-28 12:33:51 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-28 12:33:51 +0100 |
commit | 1dd4843b62085a2aeb3134573adf9a7c47c154e2 (patch) | |
tree | 534a7b210655530ffce89b3c124690330867f8bc /runtime/js/40_spawn.js | |
parent | fb04e87387e04053bf41a1512b4850adf62202c6 (diff) |
feat(unstable): rework Deno.Command (#16812)
Refactors the `Deno.Command` class to not handle any state, but only being an intermediary to calling its methods, and as such any methods and properties besides `output`, `outputSync` & `spawn` have been removed. Interracting with a `spawn`ed subprocess now works by using the methods and properties on the returned class of the `spawn` method.
Diffstat (limited to 'runtime/js/40_spawn.js')
-rw-r--r-- | runtime/js/40_spawn.js | 136 |
1 files changed, 22 insertions, 114 deletions
diff --git a/runtime/js/40_spawn.js b/runtime/js/40_spawn.js index 8f44c8929..863063e3f 100644 --- a/runtime/js/40_spawn.js +++ b/runtime/js/40_spawn.js @@ -277,136 +277,44 @@ }; } - class Command { - #command; - #options; - - #child; - - #consumed; - - constructor(command, options) { - this.#command = command; - this.#options = options; - } + function createCommand(spawn, spawnSync, spawnChild) { + return class Command { + #command; + #options; + + constructor(command, options) { + this.#command = command; + this.#options = options; + } - output() { - if (this.#child) { - return this.#child.output(); - } else { - if (this.#consumed) { + output() { + if (this.#options?.stdin === "piped") { throw new TypeError( - "Command instance is being or has already been consumed.", + "Piped stdin is not supported for this function, use 'Deno.Command.spawn()' instead", ); } + return spawn(this.#command, this.#options); + } + + outputSync() { 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); + return spawnSync(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"); + spawn() { + return spawnChild(this.#command, this.#options); } - - 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, + ChildProcess: Child, + createCommand, createSpawn, createSpawnChild, createSpawnSync, |