Suprocess Spawning: runing other files using subprocesses
An example of a subprocess being spawned to run specified files using CLI flags.
import { parseArgs } from "jsr:@std/cli";
Grab the file name using the standard libraries parseArgs funciton. If no file is provided, exit with an error.
import { expandGlob } from "jsr:@std/fs";
const flags = parseArgs(Deno.args, {
string: ["file"],
default: {
file: "",
},
});
if (!flags.file) {
console.error("No file provided");
Deno.exit(1);
}
Use the expandGlob function to find all files that match the provided file name.
const FilesList = await Array.fromAsync(
expandGlob(`**/*${flags.file}*`, { root: "." }),
);
const files = FilesList.filter((files) => files.name.includes(flags.file));
If no files are found, exit with an error.
if (files.length === 0) {
console.error("No files found");
Deno.exit(1);
}
If multiple files are found, exit with an error.
if (files.length > 1) {
console.error("Multiple files found");
Deno.exit(1);
}
const file = files[0];
Use the Deno.Command class to create a new command that will run the specified file.
const command = new Deno.Command(Deno.execPath(), {
args: [file?.path],
});
Try to spawn the command and catch any errors that may occur. Wait for the subprocess to finish and log the exit code.
try {
const child = command.spawn();
child.ref();
} catch (error) {
console.error("Error while running the file: ", error);
Deno.exit(4);
}
Run this example locally using the Deno CLI:
deno run --allow-net --allow-run --allow-read https://docs.deno.com/examples/subprocess-running-files.ts