Skip to content

Commit

Permalink
fixup! feat(ng-dev): create system for registering functions to be ca…
Browse files Browse the repository at this point in the history
…lled on command completion
  • Loading branch information
josephperrott committed Aug 11, 2022
1 parent 8ea71f1 commit 847667b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ng-dev/cli.ts
Expand Up @@ -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 <command>', false, buildAuthParser)
Expand Down
4 changes: 3 additions & 1 deletion ng-dev/utils/logging.ts
Expand Up @@ -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();
Expand Down
14 changes: 12 additions & 2 deletions ng-dev/utils/ng-dev-service.ts
Expand Up @@ -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
Expand All @@ -50,6 +53,13 @@ export function canUseNgDevService<T extends {githubToken: string | null}>(
// TODO(josephperrott): Show flags once this tooling is rolled out.
.hide('use-auth-service')
.middleware(async (args: Arguments<T & {useAuthService: boolean}>) => {
// 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();

Expand All @@ -72,7 +82,7 @@ export function canUseNgDevService<T extends {githubToken: string | null}>(
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)
);
}
14 changes: 6 additions & 8 deletions 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> | void;
Expand All @@ -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);
}
});
}

0 comments on commit 847667b

Please sign in to comment.