Skip to content

Commit

Permalink
Merge pull request #1276 from oclif/mdonnalley/finally-hook
Browse files Browse the repository at this point in the history
feat: add finally hook
  • Loading branch information
iowillhoit authored Dec 20, 2024
2 parents f0d4464 + 16901c3 commit b79ac41
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
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()
}
}

0 comments on commit b79ac41

Please sign in to comment.