Skip to content

Commit

Permalink
feat: add return value to hooks and commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Sep 27, 2022
1 parent 631fae4 commit 86a6490
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ const castArray = <T>(input?: T | T[]): T[] => {

export function command(args: string[] | string, opts: loadConfig.Options = {}): {
run(ctx: {
config: Interfaces.Config; expectation: string
config: Interfaces.Config; expectation: string; returned: unknown
}): Promise<void>
} {
return {
async run(ctx: {config: Interfaces.Config; expectation: string}) {
async run(ctx: {config: Interfaces.Config; expectation: string; returned: unknown}) {
if (!ctx.config || opts.reset) ctx.config = await loadConfig(opts).run({} as any)
args = castArray(args)
const [id, ...extra] = args
const cmdId = toStandardizedId(id, ctx.config)
ctx.expectation = ctx.expectation || `runs ${args.join(' ')}`
await ctx.config.runHook('init', {id: cmdId, argv: extra})
await ctx.config.runCommand(cmdId, extra)
ctx.returned = await ctx.config.runCommand(cmdId, extra)
},
}
}
9 changes: 5 additions & 4 deletions src/exit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import {expect} from 'chai'
// eslint-disable-next-line valid-jsdoc
/**
* ensures that a oclif command or hook exits
*
* @param {number} code expected code
* @default 0
*/
export default (code = 0) => ({
export default (code = 0): {
run(): never; catch(ctx: {
error: any
}): void
} => ({
run() {
expect(process.exitCode).to.equal(code)
throw new Error(`Expected to exit with code ${code} but it ran without exiting`)
Expand Down
12 changes: 7 additions & 5 deletions src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import {loadConfig} from './load-config'
* @param {string} event hook to run
* @param {object} hookOpts options to pass to hook. Config object will be passed automatically.
*/
export default (event: string, hookOpts: Record<string, unknown> = {}, options: loadConfig.Options = {}) => ({
async run(ctx: {config: Interfaces.Config; expectation: string}) {
export default (event: string, hookOpts: Record<string, unknown> = {}, options: loadConfig.Options = {}): {
run(ctx: {
config: Interfaces.Config; expectation: string; returned: unknown
}): Promise<void>;
} => ({
async run(ctx: {config: Interfaces.Config; expectation: string; returned: unknown}) {
if (!event) throw new Error('no hook provided')
// eslint-disable-next-line require-atomic-updates
if (!ctx.config) ctx.config = await loadConfig(options).run({} as any)
// eslint-disable-next-line require-atomic-updates
ctx.expectation = ctx.expectation || `runs ${event} hook`
await ctx.config.runHook(event, hookOpts || {})
ctx.returned = await ctx.config.runHook(event, hookOpts || {})
},
})
6 changes: 5 additions & 1 deletion test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ describe('command', () => {
.loadConfig({root})
.stdout()
.command(['foo:bar'])
.do(output => expect(output.stdout).to.equal('hello world!\n'))
.do(output => {
expect(output.stdout).to.equal('hello world!\n')
const {name} = output.returned as {name: string}
expect(name).to.equal('world')
})
.it()

test
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/multi/src/commands/foo/bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CLI extends Command {
const {flags} = await this.parse(CLI)
const name = flags.name || 'world'
this.log(`hello ${name}!`)
return {name}
}
}

Expand Down

0 comments on commit 86a6490

Please sign in to comment.