Skip to content

Commit 9dd3bca

Browse files
feat: add error & Command to finally hook options (#1422)
* test: resolve @oclif/core to local * feat: add error & Command to finally hook options (#1417) * test: remove resolution --------- Co-authored-by: Benjamin Levesque <[email protected]>
1 parent 6b8ba09 commit 9dd3bca

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

src/interfaces/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface Hooks {
2727
return: unknown
2828
}
2929
finally: {
30-
options: {argv: string[]; id: string}
30+
options: {argv: string[]; id: string; Command: Command.Loadable | undefined; error: Error | undefined}
3131
return: void
3232
}
3333
init: {

src/main.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {fileURLToPath, URL} from 'node:url'
22

33
import Cache from './cache'
4+
import {Command} from './command'
45
import {Config} from './config'
56
import {getHelpFlagAdditions, loadHelpClass, normalizeArgv} from './help'
67
import * as Interfaces from './interfaces'
@@ -60,12 +61,12 @@ export async function run(argv?: string[], options?: Interfaces.LoadOptions): Pr
6061

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

63-
const runFinally = async () => {
64+
const runFinally = async (cmd?: Command.Loadable, error?: Error) => {
6465
marker?.stop()
6566
if (!initMarker?.stopped) initMarker?.stop()
6667
await Performance.collect()
6768
Performance.debug()
68-
await config.runHook('finally', {argv: argvSlice, id})
69+
await config.runHook('finally', {argv: argvSlice, Command: cmd, error, id})
6970
}
7071

7172
// run init hook
@@ -98,9 +99,12 @@ export async function run(argv?: string[], options?: Interfaces.LoadOptions): Pr
9899

99100
initMarker?.stop()
100101

102+
let err: Error | undefined
101103
try {
102104
return await config.runCommand(id, argvSlice, cmd)
105+
} catch (error) {
106+
err = error as Error
103107
} finally {
104-
await runFinally()
108+
await runFinally(cmd, err)
105109
}
106110
}

test/config/fixtures/typescript/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
],
1212
"postrun": [
1313
"./lib/hooks/postrun"
14+
],
15+
"finally": [
16+
"./lib/hooks/finally"
1417
]
1518
}
1619
},
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default function finallyHook(options: any): void {
2+
console.log('running ts finally hook')
3+
if (options.error) {
4+
console.log('an error occurred')
5+
}
6+
}

test/config/typescript.test.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,45 @@ describe('typescript', () => {
1414
})
1515
})
1616

17-
it('runs ts command and prerun & postrun hooks', async () => {
17+
it('runs ts command and prerun, postrun & finally hooks', async () => {
1818
const {stdout} = await runCommand(['foo:bar:baz'], root)
19-
expect(stdout).to.equal('running ts init hook\nrunning ts prerun hook\nit works!\nrunning ts postrun hook\n')
19+
expect(stdout.split('\n')).to.deep.equal([
20+
'running ts init hook',
21+
'running ts prerun hook',
22+
'it works!',
23+
'running ts postrun hook',
24+
'running ts finally hook',
25+
'',
26+
])
2027
})
2128

22-
it('runs faulty command, only prerun hook triggers', async () => {
29+
it('runs faulty command, only prerun & finally hooks trigger', async () => {
2330
const {stdout} = await runCommand(['foo:bar:fail'], root)
24-
expect(stdout).to.equal('running ts init hook\nrunning ts prerun hook\nit fails!\n')
31+
expect(stdout.split('\n')).to.deep.equal([
32+
'running ts init hook',
33+
'running ts prerun hook',
34+
'it fails!',
35+
'running ts finally hook',
36+
'an error occurred',
37+
'',
38+
])
2539
})
2640

27-
it('runs ts command, postrun hook captures command result', async () => {
41+
it('runs ts command, postrun & finally hooks capture command result', async () => {
2842
const {stdout} = await runCommand(['foo:bar:test-result'], root)
29-
expect(stdout).to.equal(
30-
'running ts init hook\nrunning ts prerun hook\nit works!\nrunning ts postrun hook\nreturned success!\n',
31-
)
43+
expect(stdout.split('\n')).to.deep.equal([
44+
'running ts init hook',
45+
'running ts prerun hook',
46+
'it works!',
47+
'running ts postrun hook',
48+
'returned success!',
49+
'running ts finally hook',
50+
'',
51+
])
3252
})
3353

3454
it('runs init hook', async () => {
3555
const {stdout} = await runHook('init', {id: 'myid', argv: ['foo']}, {root})
36-
expect(stdout).to.equal('running ts init hook\n')
56+
expect(stdout.split('\n')).to.deep.equal(['running ts init hook', ''])
3757
})
3858
})

0 commit comments

Comments
 (0)