From a283f1573212baff48496d3a157a12e37330cd2e Mon Sep 17 00:00:00 2001 From: v1rtl Date: Tue, 25 Jun 2024 20:13:51 +0300 Subject: [PATCH] extend plugins + write tests for plugin api --- .vscode/settings.json | 4 +++- spektr.ts | 6 +++++- spektr_test.ts | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 7cdac36..b523806 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,9 @@ { "deno.enable": true, + "deno.cacheOnSave": true, + "deno.lint": true, "[typescript]": { "editor.defaultFormatter": "denoland.vscode-deno" }, "editor.formatOnSave": true -} +} \ No newline at end of file diff --git a/spektr.ts b/spektr.ts index 6091e1b..9ee0250 100644 --- a/spektr.ts +++ b/spektr.ts @@ -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) { diff --git a/spektr_test.ts b/spektr_test.ts index 1b2571f..7c36e59 100644 --- a/spektr_test.ts +++ b/spektr_test.ts @@ -11,7 +11,7 @@ import { spy, } from 'https://deno.land/std@0.224.0/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', () => { @@ -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) + }) + }) })