diff --git a/ng-dev/cli.ts b/ng-dev/cli.ts index ce1ed983c..91222a43e 100644 --- a/ng-dev/cli.ts +++ b/ng-dev/cli.ts @@ -26,7 +26,7 @@ import {Argv} from 'yargs'; runParserWithCompletedFunctions((yargs: Argv) => { return yargs .scriptName('ng-dev') - .middleware(captureLogOutputForCommand) + .middleware(captureLogOutputForCommand, true) .demandCommand() .recommendCommands() .command('auth ', false, buildAuthParser) diff --git a/ng-dev/utils/logging.ts b/ng-dev/utils/logging.ts index 0c5f63eb2..6d52be5c8 100644 --- a/ng-dev/utils/logging.ts +++ b/ng-dev/utils/logging.ts @@ -133,8 +133,10 @@ const LOG_LEVEL_COLUMNS = 7; * response is executed. */ export async function captureLogOutputForCommand(argv: Arguments) { + // TODO(josephperrott): remove this guard against running multiple times after + // https://github.com/yargs/yargs/issues/2223 is fixed if (FILE_LOGGING_ENABLED) { - throw Error('`captureLogOutputForCommand` cannot be called multiple times'); + return; } const repoDir = determineRepoBaseDirFromCwd(); diff --git a/ng-dev/utils/ng-dev-service.ts b/ng-dev/utils/ng-dev-service.ts index a89ec733f..f7dd11a5e 100644 --- a/ng-dev/utils/ng-dev-service.ts +++ b/ng-dev/utils/ng-dev-service.ts @@ -26,6 +26,9 @@ const firebaseConfig = { appId: '1:823469418460:web:009b51c93132b218761119', }; +/** Whether or not the middleware has already been run. */ +let ngDevServiceMiddlewareHasRun = false; + /** * Sets up middleware to ensure that configuration and setup is completed for commands which * require the ng-dev service @@ -50,6 +53,13 @@ export function canUseNgDevService( // TODO(josephperrott): Show flags once this tooling is rolled out. .hide('use-auth-service') .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; + initializeApp(firebaseConfig); await restoreNgTokenFromDiskIfValid(); @@ -72,7 +82,7 @@ export function canUseNgDevService( Log.error(' ✘ You must be logged in to run this command\n'); Log.log('Log in by running the following command:'); Log.log(' yarn ng-dev auth login'); - argv.exit(1, new Error('The user is not logged in')); - }) + argv.exit(1, new Error()); + }, true) ); } diff --git a/ng-dev/utils/yargs.ts b/ng-dev/utils/yargs.ts index fe7607552..a53b3649d 100644 --- a/ng-dev/utils/yargs.ts +++ b/ng-dev/utils/yargs.ts @@ -1,4 +1,4 @@ -import yargs, {Argv} from 'yargs'; +import yargs, {Arguments, Argv} from 'yargs'; // A function to be called when the command completes. type CompletedFn = (err: Error | null) => Promise | void; @@ -16,11 +16,9 @@ export function registerCompletedFunction(fn: CompletedFn) { * functions after the command completes. */ export function runParserWithCompletedFunctions(applyConfiguration: (argv: Argv) => Argv) { - applyConfiguration(yargs(process.argv.slice(2))) - .exitProcess(false) - .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) => { + for (const completedFunc of completedFunctions) { + await completedFunc(err); + } + }); }