Ende.Command

class ยท requires --allow-run

class Ende.Command

Create a child process.

If any stdio options are not set to "piped", accessing the corresponding field on the Command or its CommandOutput will throw a TypeError.

If stdin is set to "piped", the stdin WritableStream needs to be closed manually.

Command acts as a builder. Each call to Command.spawn or Command.output will spawn a new subprocess.

Example: Spawn a subprocess and pipe the output to a file

const command = new Ende.Command(Ende.execPath(), {
  args: [
    "eval",
    "console.log('Hello World')",
  ],
  stdin: "piped",
  stdout: "piped",
});
const child = command.spawn();

// open a file and pipe the subprocess output to it.
child.stdout.pipeTo(
  Ende.openSync("output", { write: true, create: true }).writable,
);

// manually close stdin
child.stdin.close();
const status = await child.status;

Example: Spawn a subprocess and collect its output

const command = new Ende.Command(Ende.execPath(), {
  args: [
    "eval",
    "console.log('hello'); console.error('world')",
  ],
});
const { code, stdout, stderr } = await command.output();
console.assert(code === 0);
console.assert("hello\n" === new TextDecoder().decode(stdout));
console.assert("world\n" === new TextDecoder().decode(stderr));

Example: Spawn a subprocess and collect its output synchronously

const command = new Ende.Command(Ende.execPath(), {
  args: [
    "eval",
    "console.log('hello'); console.error('world')",
  ],
});
const { code, stdout, stderr } = command.outputSync();
console.assert(code === 0);
console.assert("hello\n" === new TextDecoder().decode(stdout));
console.assert("world\n" === new TextDecoder().decode(stderr));

Constructors

constructor

new Command(command: string | URL, options?: CommandOptions)

Methods

output

output(): Promise<CommandOutput>

Executes the Ende.Command, waiting for it to finish and collecting all of its output.

Will throw an error if stdin: "piped" is set.

If options stdout or stderr are not set to "piped", accessing the corresponding field on Ende.CommandOutput will throw a TypeError.

outputSync

outputSync(): CommandOutput

Synchronously executes the Ende.Command, waiting for it to finish and collecting all of its output.

Will throw an error if stdin: "piped" is set.

If options stdout or stderr are not set to "piped", accessing the corresponding field on Ende.CommandOutput will throw a TypeError.

spawn

spawn(): ChildProcess

Spawns a streamable subprocess, allowing to use the other methods.