From 33012e294725146b51b113710c92477471a1026a Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Tue, 23 Feb 2021 16:47:50 -0500 Subject: [PATCH] Refactor suite-setup-util to avoid knock on errors. This is a small refactor that does a few things: * migrate the manual promise chaining to async/await * unify the error handling (so that it is all done in the same way) * migrate from `process.stderr.write` to `console.error` (`process.stderr.write` only accepts a string or buffer, if you pass it an instance of `Error` you get an error that actually masks the _real_ error) * migrate to `process.exitCode` instead of `process.exit` (forcing exit with `process.exit()` _in general_ should be avoided, and doesn't seem required in this context) --- test-packages/support/suite-setup-util.ts | 48 +++++++++++------------ 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/test-packages/support/suite-setup-util.ts b/test-packages/support/suite-setup-util.ts index 3ae6e0886b..c81fb66911 100644 --- a/test-packages/support/suite-setup-util.ts +++ b/test-packages/support/suite-setup-util.ts @@ -152,33 +152,29 @@ export async function emitDynamicSuites() { } } -if (require.main === module) { - if (process.argv.includes('--list')) { - allSuites() - .then(result => { - process.stdout.write(JSON.stringify(result, null, 2) + '\n'); - }) - .catch(err => { - process.stderr.write(err); - process.exit(-1); - }); - } +async function main() { + try { + if (process.argv.includes('--list')) { + const result = await allSuites(); - if (process.argv.includes('--matrix')) { - githubMatrix() - .then(result => { - process.stdout.write(JSON.stringify(result)); - }) - .catch(err => { - process.stderr.write(err); - process.exit(-1); - }); - } + process.stdout.write(JSON.stringify(result, null, 2) + '\n'); + } - if (process.argv.includes('--emit')) { - emitDynamicSuites().catch(err => { - console.log(err); - process.exit(-1); - }); + if (process.argv.includes('--matrix')) { + const result = await githubMatrix(); + + process.stdout.write(JSON.stringify(result)); + } + + if (process.argv.includes('--emit')) { + await emitDynamicSuites(); + } + } catch (error) { + console.error(error); + process.exitCode = -1; } } + +if (require.main === module) { + main(); +}