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

Commit

Permalink
feat: add postrun hook (#111)
Browse files Browse the repository at this point in the history
* post run hook

* tested post run hook

* removed package-lock.json

* Added postrun hook test for command failing

* capturing command result in posthook

* Added a test for post-run hook capturing the result of command

* removed useless hooktest script

* Update src/hooks.ts

Co-authored-by: RasPhilCo <[email protected]>

* Added newlines for readability

* Fixed semicolon

Co-authored-by: Max Camp-Oberhauser <[email protected]>
Co-authored-by: RasPhilCo <[email protected]>
  • Loading branch information
3 people authored Jun 9, 2020
1 parent 89bb82b commit 149529d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,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) {
Expand Down
6 changes: 6 additions & 0 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -32,6 +37,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
6 changes: 6 additions & 0 deletions test/fixtures/typescript/src/commands/foo/bar/fail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class Command {
static run() {
console.log('it fails!')
throw new Error('random error')
}
}
6 changes: 6 additions & 0 deletions test/fixtures/typescript/src/commands/foo/bar/test-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class Command {
static run() {
console.log('it works!')
return 'returned success!'
}
}
6 changes: 6 additions & 0 deletions test/fixtures/typescript/src/hooks/postrun.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
22 changes: 20 additions & 2 deletions test/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 149529d

Please sign in to comment.