Skip to content

Commit

Permalink
extend plugins + write tests for plugin api
Browse files Browse the repository at this point in the history
  • Loading branch information
talentlessguy committed Jun 25, 2024
1 parent aa459a1 commit a283f15
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"deno.enable": true,
"deno.cacheOnSave": true,
"deno.lint": true,
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
},
"editor.formatOnSave": true
}
}
6 changes: 5 additions & 1 deletion spektr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ export class CLI {
}
& ParseArgsConfig = {},
) {
const { name, prefix, plugins, helpFn, ...parseOptions } = opts
const { name, prefix, helpFn, ...parseOptions } = opts
this.name = name
this.prefix = prefix
this.#parseOptions = parseOptions
this.helpFn = helpFn

const plugins = this.parent
? [...this.parent.plugins, ...(opts.plugins || [])]
: opts.plugins

if (plugins) {
this.plugins = plugins
for (const plugin of plugins) {
Expand Down
34 changes: 33 additions & 1 deletion spektr_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
spy,
} from 'https://deno.land/[email protected]/testing/mock.ts'
import { CLI } from './spektr.ts'
import { Positionals } from './types.ts'
import { Plugin, Positionals } from './types.ts'
import { ParsedOptions } from './types.ts'

describe('CLI', () => {
Expand Down Expand Up @@ -318,4 +318,36 @@ describe('CLI', () => {
assertSpyCall(mwSpy, 0)
})
})
describe('plugins', () => {
it('should apply them', () => {
const helpFnSpy = spy(() => `Plugin`)
const myPlugin: Plugin = () => ({
helpFn: () => 'Help',
helpMessage: helpFnSpy,
})

const cli = new CLI({ plugins: [myPlugin] })
cli.help()

cli.handle(['--help'])

assertSpyCall(helpFnSpy, 0)
})
it('should nest plugins', () => {
const helpFnSpy = spy(() => `Plugin`)
const myPlugin: Plugin = () => ({
helpFn: () => 'Help',
helpMessage: helpFnSpy,
})

const cli = new CLI({ plugins: [myPlugin] })
cli.help()

cli.program('sub').help()

cli.handle(['sub', '--help'])

assertSpyCall(helpFnSpy, 0)
})
})
})

0 comments on commit a283f15

Please sign in to comment.