summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/Util/SpawnAsync.js
blob: 5e6a20f952c6e442a171bf1cf0159f5b6f344855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { spawn } from "node:child_process";
import { withResolvers } from "./PromiseUtil.js";

export function spawnAsync(command, args, options) {
	let { promise, resolve, reject } = withResolvers();

	const cmd = spawn(command, args, options);
	let res = [];
	cmd.stdout.on("data", (data) => {
		res.push(data.toString("utf8"));
	});

	let err = [];
	cmd.stderr.on("data", (data) => {
		err.push(data.toString("utf8"));
	});

	cmd.on("close", (code) => {
		if (err.length > 0) {
			reject(err.join("\n"));
		} else if (code === 1) {
			reject("Internal error: process closed with error exit code.");
		} else {
			resolve(res.join("\n"));
		}
	});

	return promise;
}