Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"test": "vitest --typecheck run",
"typecheck": "pnpm run --stream --color \"/^typecheck:/\"",
"typecheck:deno": "pnpm -r --if-present typecheck:deno",
"typecheck:tsc": "tsc --noEmit --diagnostics"
"typecheck:tsc": "tsgo --noEmit --diagnostics"
Comment thread
kazupon marked this conversation as resolved.
},
"devDependencies": {
"@eslint/compat": "^1.2.9",
Expand Down
40 changes: 40 additions & 0 deletions packages/gunshi/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { z } from 'zod/v4-mini'
import { defineMockLog } from '../test/utils.ts'
import { cli } from './cli.ts'
import { define, lazy } from './definition.ts'
import { plugin } from './plugin.ts'
import { renderValidationErrors } from './renderer.ts'

import type { Args } from 'args-tokens'
Expand Down Expand Up @@ -1152,6 +1153,45 @@ describe('command decorators', () => {
})
})

test('plugins option', async () => {
const msgs: string[] = []
vi.spyOn(console, 'log').mockImplementation(msg => msgs.push(msg))

function logger() {
return plugin({
name: 'logger',
setup: ctx => {
ctx.decorateCommand(baseRunner => ctx => {
console.log(`before command: ${ctx.name}`)
const ret = baseRunner(ctx)
if (typeof ret === 'string') {
console.log(`command output: ${ret}`)
}
console.log(`after command: ${ctx.name}`)
return ret
})
}
})
}

const command = {
name: 'test',
run: ctx => {
return `executed ${ctx.name}`
}
} satisfies Command<GunshiParams>

await cli([], command, {
plugins: [logger()]
})

expect(msgs).toEqual([
'before command: test',
'command output: executed test',
'after command: test'
])
})

describe('edge cases', () => {
test(`'description' option`, async () => {
const command = define({
Expand Down
2 changes: 1 addition & 1 deletion packages/gunshi/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function cli<G extends GunshiParams = DefaultGunshiParams>(
completion(),
dryRun()
]
const plugins = await applyPlugins(pluginContext, builtInPlugins)
const plugins = await applyPlugins(pluginContext, [...builtInPlugins, ...(options.plugins || [])])

const cliOptions = normalizeCliOptions(options, entry, decorators)

Expand Down
3 changes: 2 additions & 1 deletion packages/gunshi/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ export const COMMAND_OPTIONS_DEFAULT: CliOptions<DefaultGunshiParams> = {
renderHeader: undefined,
renderUsage: undefined,
renderValidationErrors: undefined,
translationAdapterFactory: undefined
translationAdapterFactory: undefined,
plugins: undefined
}

export const COMMAND_BUILTIN_RESOURCE_KEYS = [
Expand Down
9 changes: 7 additions & 2 deletions packages/gunshi/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* @license MIT
*/

import type { Args, ArgToken, ArgValues } from 'args-tokens'

import { ARG_PREFIX, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX } from './constants.ts'
import { Plugin } from './plugin.ts'

import type { Args, ArgToken, ArgValues } from 'args-tokens'

export type Awaitable<T> = T | Promise<T>

Expand Down Expand Up @@ -254,6 +255,10 @@ export interface CliOptions<G extends GunshiParams<any> = DefaultGunshiParams> {
* Translation adapter factory.
*/
translationAdapterFactory?: TranslationAdapterFactory
/**
* User plugins.
*/
plugins?: Plugin[]
}

/**
Expand Down