Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

feat: add postrun hook #111

Merged
merged 11 commits into from
Jun 9, 2020
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ export class Config implements IConfig {
const command = c.load()
await this.runHook('prerun', {Command: command, argv})
await command.run(argv, this)
await this.runHook('postrun', {Command: command, argv})
Copy link
Contributor

@amphro amphro May 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should capture the results of the command and pass that into the post run hook. I would imagine a lot of post hooks would want to try and do something with the command results.

}

scopedEnvVar(k: string) {
Expand Down
5 changes: 5 additions & 0 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export interface Hooks {
Command: Config.Command.Class;
argv: string[];
};
postrun: {
Command: Config.Command.Class;
argv: string[];
};
preupdate: {channel: string};
update: {channel: string};
'command_not_found': {id: string};
Expand All @@ -32,6 +36,7 @@ export namespace Hook {
export type Init = Hook<Hooks['init']>
export type PluginsPreinstall = Hook<Hooks['plugins:preinstall']>
export type Prerun = Hook<Hooks['prerun']>
export type Postrun = Hook<Hooks['postrun']>
export type Preupdate = Hook<Hooks['preupdate']>
export type Update = Hook<Hooks['update']>
export type CommandNotFound = Hook<Hooks['command_not_found']>
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"init": "./lib/hooks/init",
"prerun": [
"./lib/hooks/prerun"
],
"postrun": [
"./lib/hooks/postrun"
]
}
},
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/typescript/src/hooks/postrun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function postrun() {
console.log('running ts postrun hook')
}
6 changes: 2 additions & 4 deletions test/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ describe('typescript', () => {
commandsDir: p('src/commands'),
})
})

withConfig
RasPhilCo marked this conversation as resolved.
Show resolved Hide resolved
.stdout()
.it('runs ts command and prerun hooks', async ctx => {
.it('runs ts command and prerun and postrun hooks', async ctx => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well have this as a separate test. Also, what about if the command errors? Add a test for that. I'm assuming the hook won't fire. Is that the right behavior?

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
RasPhilCo marked this conversation as resolved.
Show resolved Hide resolved
.stdout()
.it('runs init hook', async ctx => {
Expand Down