Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add finally hook #1276

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/interfaces/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export interface Hooks {
options: {argv?: string[]; id: string}
return: unknown
}
finally: {
options: {argv: string[]; id: string}
return: void
}
init: {
options: {argv: string[]; id: string | undefined}
return: void
Expand Down Expand Up @@ -75,6 +79,10 @@ export type Hook<T extends keyof P, P extends Hooks = Hooks> = (
) => Promise<P[T]['return']>

export namespace Hook {
/**
* Runs at the end of the CLI lifecycle - regardless of success or failure.
*/
export type Finally = Hook<'finally'>
/**
* Runs when the CLI is initialized before a command is executed.
*/
Expand Down
23 changes: 12 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ export async function run(argv?: string[], options?: Interfaces.LoadOptions): Pr

const initMarker = Performance.mark(OCLIF_MARKER_OWNER, 'main.run#init')

const collectPerf = async () => {
marker?.stop()
if (!initMarker?.stopped) initMarker?.stop()
await Performance.collect()
Performance.debug()
}

const showHelp = async (argv: string[]) => {
const Help = await loadHelpClass(config)
const help = new Help(config, config.pjson.oclif.helpOptions ?? config.pjson.helpOptions)
Expand Down Expand Up @@ -67,20 +60,28 @@ export async function run(argv?: string[], options?: Interfaces.LoadOptions): Pr

const [id, ...argvSlice] = normalizeArgv(config, argv)

const runFinally = async () => {
marker?.stop()
if (!initMarker?.stopped) initMarker?.stop()
await Performance.collect()
Performance.debug()
await config.runHook('finally', {argv: argvSlice, id})
}

// run init hook
await config.runHook('init', {argv: argvSlice, id})

// display version if applicable
if (versionAddition(argv, config)) {
ux.stdout(config.userAgent)
await collectPerf()
await runFinally()
return
}

// display help version if applicable
if (helpAddition(argv, config)) {
await showHelp(argv)
await collectPerf()
await runFinally()
return
}

Expand All @@ -90,7 +91,7 @@ export async function run(argv?: string[], options?: Interfaces.LoadOptions): Pr
const topic = config.flexibleTaxonomy ? null : config.findTopic(id)
if (topic) {
await showHelp([id])
await collectPerf()
await runFinally()
return
}
}
Expand All @@ -100,6 +101,6 @@ export async function run(argv?: string[], options?: Interfaces.LoadOptions): Pr
try {
return await config.runCommand(id, argvSlice, cmd)
} finally {
await collectPerf()
await runFinally()
}
}
Loading