From 9868b9a12a256c0136ca1c4ad259fe1c2e431f5e Mon Sep 17 00:00:00 2001 From: Joey Perrott Date: Thu, 11 Aug 2022 17:21:12 +0000 Subject: [PATCH] fixup! feat(ng-dev): create system for registering functions to be called on command completion --- ng-dev/utils/ng-dev-service.ts | 20 +++++++++++--------- ng-dev/utils/yargs.ts | 25 ++++++++++++++++++++----- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/ng-dev/utils/ng-dev-service.ts b/ng-dev/utils/ng-dev-service.ts index 5dc79aa19..beeb8c7cc 100644 --- a/ng-dev/utils/ng-dev-service.ts +++ b/ng-dev/utils/ng-dev-service.ts @@ -57,15 +57,15 @@ export async function useNgDevService( }) .middleware( async (args: Arguments) => { - // TODO(josephperrott): remove this guard against running multiple times after - // https://github.com/yargs/yargs/issues/2223 is fixed - if (ngDevServiceMiddlewareHasRun) { - return; - } - ngDevServiceMiddlewareHasRun = true; + // TODO(josephperrott): remove this guard against running multiple times after + // https://github.com/yargs/yargs/issues/2223 is fixed + if (ngDevServiceMiddlewareHasRun) { + return; + } + ngDevServiceMiddlewareHasRun = true; - initializeApp(firebaseConfig); - await restoreNgTokenFromDiskIfValid(); + initializeApp(firebaseConfig); + await restoreNgTokenFromDiskIfValid(); if (args.githubEscapeHatch === true) { Log.warn('This escape hatch should only be used if the service is erroring. Please'); @@ -91,6 +91,8 @@ export async function useNgDevService( Log.log('Log in by running the following command:'); Log.log(' yarn ng-dev auth login'); throw new Error('The user is not logged in'); - }, true) + }, + true, + ) ); } diff --git a/ng-dev/utils/yargs.ts b/ng-dev/utils/yargs.ts index a53b3649d..59085dc93 100644 --- a/ng-dev/utils/yargs.ts +++ b/ng-dev/utils/yargs.ts @@ -1,4 +1,5 @@ import yargs, {Arguments, Argv} from 'yargs'; +import {Log} from './logging.js'; // A function to be called when the command completes. type CompletedFn = (err: Error | null) => Promise | void; @@ -11,14 +12,28 @@ export function registerCompletedFunction(fn: CompletedFn) { completedFunctions.push(fn); } +/** Error to be thrown when yargs completes without running a command. */ +export class YargsError extends Error { + constructor() { + super('Error is parse or validation of command'); + } +} + /** * Run the yargs process, as configured by the supplied function, calling a set of completion * functions after the command completes. */ export function runParserWithCompletedFunctions(applyConfiguration: (argv: Argv) => Argv) { - applyConfiguration(yargs([])).parse(process.argv.slice(2), async (err: Error | null) => { - for (const completedFunc of completedFunctions) { - await completedFunc(err); - } - }); + applyConfiguration(yargs([])).parse( + process.argv.slice(2), + async (err: Error | null, _: Arguments, output: string) => { + if (output) { + err = new YargsError(); + Log.log(output); + } + for (const completedFunc of completedFunctions) { + await completedFunc(err); + } + }, + ); }