diff options
Diffstat (limited to 'runtime/js')
-rw-r--r-- | runtime/js/40_spawn.js | 136 | ||||
-rw-r--r-- | runtime/js/90_deno_ns.js | 1 | ||||
-rw-r--r-- | runtime/js/99_main.js | 31 |
3 files changed, 54 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, diff --git a/runtime/js/90_deno_ns.js b/runtime/js/90_deno_ns.js index cd6c07464..e3ccf1b6f 100644 --- a/runtime/js/90_deno_ns.js +++ b/runtime/js/90_deno_ns.js @@ -144,6 +144,7 @@ funlock: __bootstrap.fs.funlock, funlockSync: __bootstrap.fs.funlockSync, Child: __bootstrap.spawn.Child, + ChildProcess: __bootstrap.spawn.ChildProcess, spawnChild: __bootstrap.spawn.spawnChild, spawn: __bootstrap.spawn.spawn, spawnSync: __bootstrap.spawn.spawnSync, diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index 2ea3504e2..adfc0d360 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -482,6 +482,14 @@ delete Intl.v8BreakIterator; }, }); + ObjectAssign(internals.nodeUnstable, { + Command: __bootstrap.spawn.createCommand( + internals.nodeUnstable.spawn, + internals.nodeUnstable.spawnSync, + internals.nodeUnstable.spawnChild, + ), + }); + const finalDenoNs = { core, internal: internalSymbol, @@ -513,6 +521,14 @@ delete Intl.v8BreakIterator; ops.op_net_listen_unixpacket, ), }); + + ObjectAssign(finalDenoNs, { + Command: __bootstrap.spawn.createCommand( + finalDenoNs.spawn, + finalDenoNs.spawnSync, + finalDenoNs.spawnChild, + ), + }); } // Setup `Deno` global - we're actually overriding already existing global @@ -617,6 +633,14 @@ delete Intl.v8BreakIterator; }, }); + ObjectAssign(internals.nodeUnstable, { + Command: __bootstrap.spawn.createCommand( + internals.nodeUnstable.spawn, + internals.nodeUnstable.spawnSync, + internals.nodeUnstable.spawnChild, + ), + }); + const finalDenoNs = { core, internal: internalSymbol, @@ -640,6 +664,13 @@ delete Intl.v8BreakIterator; ops.op_net_listen_unixpacket, ), }); + ObjectAssign(finalDenoNs, { + Command: __bootstrap.spawn.createCommand( + finalDenoNs.spawn, + finalDenoNs.spawnSync, + finalDenoNs.spawnChild, + ), + }); } ObjectDefineProperties(finalDenoNs, { pid: util.readOnly(runtimeOptions.pid), |