From 421fe4b5f03145488bdd0736af17deff79066264 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Tue, 10 Oct 2023 11:17:24 -0400 Subject: [PATCH] MM-54325 Have web app build script return error codes on failure (#24723) * MM-54325 Have web app build script return error codes on failure * Make web app --runner option not return an error code --- webapp/scripts/build.js | 21 ++++++++++++--------- webapp/scripts/dev-server.js | 13 +++++++++++-- webapp/scripts/run.js | 15 ++++++++++++--- webapp/scripts/utils.js | 15 +++++++++++++++ 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/webapp/scripts/build.js b/webapp/scripts/build.js index 3dfd0a7871..24a13a7390 100644 --- a/webapp/scripts/build.js +++ b/webapp/scripts/build.js @@ -6,7 +6,7 @@ const chalk = require('chalk'); const concurrently = require('concurrently'); -const {getPlatformCommands} = require('./utils.js'); +const {getExitCode, getPlatformCommands} = require('./utils.js'); async function buildAll() { console.log(chalk.inverse.bold('Building subpackages...') + '\n'); @@ -20,9 +20,9 @@ async function buildAll() { ); await result; - } catch (e) { - console.error(chalk.inverse.bold.red('Failed to build subpackages'), e); - return; + } catch (closeEvents) { + console.error(chalk.inverse.bold.red('Failed to build subpackages'), closeEvents); + return getExitCode(closeEvents); } console.log('\n' + chalk.inverse.bold('Subpackages built! Building web app...') + '\n'); @@ -34,12 +34,15 @@ async function buildAll() { {command: 'npm:build --workspace=channels', name: 'webapp', prefixColor: 'cyan'}, ]); await result; - } catch (e) { - console.error(chalk.inverse.bold.red('Failed to build web app'), e); - return; + } catch (closeEvents) { + console.error(chalk.inverse.bold.red('Failed to build web app'), closeEvents); + return getExitCode(closeEvents); } - console.log('\n' + chalk.inverse.bold('Web app built! ')); + console.log('\n' + chalk.inverse.bold('Web app built!')); + return 0; } -buildAll(); +buildAll().then((exitCode) => { + process.exitCode = exitCode; +}); diff --git a/webapp/scripts/dev-server.js b/webapp/scripts/dev-server.js index b266dadd93..6ff1aa5150 100644 --- a/webapp/scripts/dev-server.js +++ b/webapp/scripts/dev-server.js @@ -25,7 +25,16 @@ async function watchAllWithDevServer() { killOthers: 'failure', }, ); - await result; + + let exitCode = 0; + try { + await result; + } catch (closeEvents) { + exitCode = getExitCode(closeEvents, 0); + } + return exitCode; } -watchAllWithDevServer(); +watchAllWithDevServer().then((exitCode) => { + process.exit(exitCode); +}); diff --git a/webapp/scripts/run.js b/webapp/scripts/run.js index 6feaa2fc17..e806f49d27 100644 --- a/webapp/scripts/run.js +++ b/webapp/scripts/run.js @@ -7,7 +7,7 @@ const chalk = require('chalk'); const concurrently = require('concurrently'); const {makeRunner} = require('./runner.js'); -const {getPlatformCommands} = require('./utils.js'); +const {getExitCode, getPlatformCommands} = require('./utils.js'); async function watchAll(useRunner) { if (!useRunner) { @@ -41,9 +41,18 @@ async function watchAll(useRunner) { } }); - await result; + let exitCode = 0; + try { + await result; + } catch (closeEvents) { + exitCode = getExitCode(closeEvents, 0); + } + return exitCode; } const useRunner = process.argv[2] === '--runner' || process.env.MM_USE_WEBAPP_RUNNER; -watchAll(useRunner); +watchAll(useRunner).then((exitCode) => { + process.exit(exitCode); +}); + diff --git a/webapp/scripts/utils.js b/webapp/scripts/utils.js index f0e0eed403..dfcf430d71 100644 --- a/webapp/scripts/utils.js +++ b/webapp/scripts/utils.js @@ -42,6 +42,21 @@ function getColorForWorkspace(workspace) { return index === -1 ? chalk.white : workspaceColors[index % workspaceColors.length]; } +/** + * @param {import("concurrently").CloseEvent[]} closeEvents - An array of CloseEvents thrown by concurrently when waiting on a result + * @param {number} codeOnSignal - Which error code to return when the process is interrupted + */ +function getExitCode(closeEvents, codeOnSignal = 1) { + const exitCode = closeEvents.find((event) => !event.killed && event.exitCode > 0)?.exitCode; + + if (typeof exitCode === 'string') { + return codeOnSignal + } else { + return exitCode; + } +} + module.exports = { + getExitCode, getPlatformCommands, };