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 (#765) Create a callback system to call functions after a command completes. PR Close #765
- Loading branch information
1 parent
5c9772c
commit 55daf7d
Showing
6 changed files
with
75 additions
and
21 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
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,34 @@ | ||
import yargs, {Arguments, 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 async function runParserWithCompletedFunctions(applyConfiguration: (argv: Argv) => Argv) { | ||
let err: Error | null = null; | ||
try { | ||
await applyConfiguration(yargs(process.argv.slice(2))) | ||
.exitProcess(false) | ||
.parse(); | ||
} catch (e) { | ||
err = e as Error; | ||
if ([undefined, 0].includes(process.exitCode)) { | ||
process.exitCode = 1; | ||
} | ||
} finally { | ||
for (const completedFunc of completedFunctions) { | ||
await completedFunc(err); | ||
} | ||
} | ||
} |