diff --git a/src/config.ts b/src/config.ts index 3122a2cf..1a860e97 100644 --- a/src/config.ts +++ b/src/config.ts @@ -354,7 +354,8 @@ export class Config implements IConfig { } const command = c.load() await this.runHook('prerun', {Command: command, argv}) - await command.run(argv, this) + const result = await command.run(argv, this) + await this.runHook('postrun', {Command: command, result: result, argv}) } scopedEnvVar(k: string) { diff --git a/src/hooks.ts b/src/hooks.ts index 48cb70aa..e786a0eb 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -10,6 +10,11 @@ export interface Hooks { Command: Config.Command.Class; argv: string[]; }; + postrun: { + Command: Config.Command.Class; + result?: any; + argv: string[]; + }; preupdate: {channel: string}; update: {channel: string}; 'command_not_found': {id: string}; @@ -32,6 +37,7 @@ export namespace Hook { export type Init = Hook export type PluginsPreinstall = Hook export type Prerun = Hook + export type Postrun = Hook export type Preupdate = Hook export type Update = Hook export type CommandNotFound = Hook diff --git a/test/fixtures/typescript/package.json b/test/fixtures/typescript/package.json index 5f5b178d..29150400 100644 --- a/test/fixtures/typescript/package.json +++ b/test/fixtures/typescript/package.json @@ -8,6 +8,9 @@ "init": "./lib/hooks/init", "prerun": [ "./lib/hooks/prerun" + ], + "postrun": [ + "./lib/hooks/postrun" ] } }, diff --git a/test/fixtures/typescript/src/commands/foo/bar/fail.ts b/test/fixtures/typescript/src/commands/foo/bar/fail.ts new file mode 100644 index 00000000..a69e6340 --- /dev/null +++ b/test/fixtures/typescript/src/commands/foo/bar/fail.ts @@ -0,0 +1,6 @@ +export class Command { + static run() { + console.log('it fails!') + throw new Error('random error') + } +} diff --git a/test/fixtures/typescript/src/commands/foo/bar/test-result.ts b/test/fixtures/typescript/src/commands/foo/bar/test-result.ts new file mode 100644 index 00000000..f46c8fb6 --- /dev/null +++ b/test/fixtures/typescript/src/commands/foo/bar/test-result.ts @@ -0,0 +1,6 @@ +export class Command { + static run() { + console.log('it works!') + return 'returned success!' + } +} diff --git a/test/fixtures/typescript/src/hooks/postrun.ts b/test/fixtures/typescript/src/hooks/postrun.ts new file mode 100644 index 00000000..be765f28 --- /dev/null +++ b/test/fixtures/typescript/src/hooks/postrun.ts @@ -0,0 +1,6 @@ +export default function postrun(options: any) { + console.log('running ts postrun hook') + if (options.Command.id === 'foo:bar:test-result') { + console.log(options.result) + } +} diff --git a/test/typescript.test.ts b/test/typescript.test.ts index 9e153ca1..42883e7a 100644 --- a/test/typescript.test.ts +++ b/test/typescript.test.ts @@ -20,9 +20,27 @@ describe('typescript', () => { withConfig .stdout() - .it('runs ts command and prerun hooks', async ctx => { + .it('runs ts command and prerun & postrun hooks', async ctx => { await ctx.config.runCommand('foo:bar:baz') - expect(ctx.stdout).to.equal('running ts prerun hook\nit works!\n') + expect(ctx.stdout).to.equal('running ts prerun hook\nit works!\nrunning ts postrun hook\n') + }) + + withConfig + .stdout() + .it('runs faulty command, only prerun hook triggers', async ctx => { + try { + await ctx.config.runCommand('foo:bar:fail') + } catch { + console.log('caught error') + } + expect(ctx.stdout).to.equal('running ts prerun hook\nit fails!\ncaught error\n') + }) + + withConfig + .stdout() + .it('runs ts command, postrun hook captures command result', async ctx => { + await ctx.config.runCommand('foo:bar:test-result') + expect(ctx.stdout).to.equal('running ts prerun hook\nit works!\nrunning ts postrun hook\nreturned success!\n') }) withConfig