From f179630df92a47184bc439b49f51a41d1e8cf39b Mon Sep 17 00:00:00 2001 From: Osyris Date: Sun, 15 Oct 2023 17:18:12 -0400 Subject: [PATCH] Improved error handling for child processes (#2) ![image](https://github.com/roblox-ts/create-roblox-ts/assets/9200592/a5f82c9d-32b4-47ab-a504-0f43d271de57) --- src/commands/init.ts | 21 ++++++++++++--------- src/index.ts | 1 + 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/commands/init.ts b/src/commands/init.ts index 6f9532b..655df27 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -1,4 +1,4 @@ -import { exec, ExecException } from "child_process"; +import { spawn } from "child_process"; import fs from "fs-extra"; import kleur from "kleur"; import { lookpath } from "lookpath"; @@ -64,14 +64,17 @@ const packageManagerCommands: { function cmd(cmdStr: string, cwd: string) { return new Promise((resolve, reject) => { - exec(cmdStr, { cwd }, (error, stdout) => { - if (error) { - reject(error); - } - resolve(stdout); - }); - }).catch((error: ExecException) => { - throw new InitError(`Command "${error.cmd}" exited with code ${error.code}\n\n${error.message}`); + const [command, ...args] = cmdStr.split(" "); + const childProcess = spawn(command, args, { cwd }); + let output = ""; + childProcess.stdout.on("data", data => (output += data)); + childProcess.stderr.on("data", data => (output += data)); + childProcess.on("close", code => + code === 0 + ? resolve(output) + : reject(new InitError(`Command "${cmdStr}" exited with code ${code}\n\n${output}`)), + ); + childProcess.on("error", reject); }); } diff --git a/src/index.ts b/src/index.ts index 53c654b..f400db7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,6 +35,7 @@ yargs }) .parseAsync() .catch(e => { + process.exitCode = 1; if (e instanceof LoggableError) { e.log(); } else {