-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ng-dev): create system for registering functions to be called on…
… command completion Create a callback system to call functions after a command completes.
- Loading branch information
1 parent
e238b8f
commit 8ea71f1
Showing
3 changed files
with
52 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import yargs, {Argv} from 'yargs'; | ||
|
||
// A function to be called when the command completes. | ||
type CompletedFn = (err: Error | null) => Promise<void> | void; | ||
|
||
/** List of functions to be called upon command completion. */ | ||
const completedFunctions: CompletedFn[] = []; | ||
|
||
/** Register a function to be called when the command completes. */ | ||
export function registerCompletedFunction(fn: CompletedFn) { | ||
completedFunctions.push(fn); | ||
} | ||
|
||
/** | ||
* 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(process.argv.slice(2))) | ||
.exitProcess(false) | ||
.parse(process.argv.slice(2), async (err: Error | null) => { | ||
for (const completedFunc of completedFunctions) { | ||
await completedFunc(err); | ||
} | ||
}); | ||
} |