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 1e09df3 commit 9868b9a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
20 changes: 11 additions & 9 deletions ng-dev/utils/ng-dev-service.ts
Expand Up @@ -57,15 +57,15 @@ export async function useNgDevService<T>(
})
.middleware(
async (args: Arguments<T & {githubToken: string | null; githubEscapeHatch: 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;
// 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');
Expand All @@ -91,6 +91,8 @@ export async function useNgDevService<T>(
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,
)
);
}
25 changes: 20 additions & 5 deletions 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> | void;
Expand All @@ -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);
}
},
);
}

0 comments on commit 9868b9a

Please sign in to comment.