Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restore baseFlags support #1085

Merged
merged 1 commit into from
May 23, 2024
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
14 changes: 8 additions & 6 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export abstract class Command {
/** An order-dependent object of arguments for the command */
public static args: ArgInput = {}

public static baseFlags: FlagInput

/**
* Emit deprecation warning when a command alias is used
*/
Expand Down Expand Up @@ -265,16 +267,16 @@ export abstract class Command {
}
}

protected async parse<F extends FlagOutput, A extends ArgOutput>(
options?: Input<F, A>,
protected async parse<F extends FlagOutput, B extends FlagOutput, A extends ArgOutput>(
options?: Input<F, B, A>,
argv = this.argv,
): Promise<ParserOutput<F, A>> {
if (!options) options = this.ctor as Input<F, A>
if (!options) options = this.ctor as Input<F, B, A>

const opts = {
context: this,
...options,
flags: aggregateFlags<F>(options.flags, options.enableJsonFlag),
flags: aggregateFlags<F, B>(options.flags, options.baseFlags, options.enableJsonFlag),
}

const hookResult = await this.config.runHook('preparse', {argv: [...argv], options: opts})
Expand All @@ -285,7 +287,7 @@ export abstract class Command {
? hookResult.successes.find((s) => s.plugin.root === Cache.getInstance().get('rootPlugin')?.root)?.result ?? argv
: argv
this.argv = [...argvToParse]
const results = await Parser.parse<F, A>(argvToParse, opts)
const results = await Parser.parse<F, B, A>(argvToParse, opts)
this.warnIfFlagDeprecated(results.flags ?? {})

return results
Expand Down Expand Up @@ -320,7 +322,7 @@ export abstract class Command {
}

protected warnIfFlagDeprecated(flags: Record<string, unknown>): void {
const allFlags = aggregateFlags(this.ctor.flags, this.ctor.enableJsonFlag)
const allFlags = aggregateFlags(this.ctor.flags, this.ctor.baseFlags, this.ctor.enableJsonFlag)
for (const flag of Object.keys(flags)) {
const flagDef = allFlags[flag]
const deprecated = flagDef?.deprecated
Expand Down
3 changes: 1 addition & 2 deletions src/interfaces/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {FlagInput} from './parser'
* Infer the flags that are returned by Command.parse. This is useful for when you want to assign the flags as a class property.
*
* @example
* export type StatusFlags = Interfaces.InferredFlags<typeof Status.flags>
* export type StatusFlags = Interfaces.InferredFlags<typeof Status.flags && typeof Status.baseFlags>
*
* export abstract class BaseCommand extends Command {
* static enableJsonFlag = true
Expand All @@ -18,7 +18,6 @@ import {FlagInput} from './parser'
*
* export default class Status extends BaseCommand {
* static flags = {
* ...BaseCommand.flags,
* force: Flags.boolean({char: 'f', description: 'a flag'}),
* }
*
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface Hooks {
preparse: {
options: {
argv: string[]
options: Input<OutputFlags<any>, OutputFlags<any>>
options: Input<OutputFlags<any>, OutputFlags<any>, OutputFlags<any>>
}
return: string[]
}
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,9 @@ export type FlagDefinition<

export type Flag<T> = BooleanFlag<T> | OptionFlag<T>

export type Input<TFlags extends FlagOutput, AFlags extends ArgOutput> = {
export type Input<TFlags extends FlagOutput, BFlags extends FlagOutput, AFlags extends ArgOutput> = {
flags?: FlagInput<TFlags>
baseFlags?: FlagInput<BFlags>
enableJsonFlag?: true | false
args?: ArgInput<AFlags>
strict?: boolean | undefined
Expand Down
11 changes: 6 additions & 5 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {validate} from './validate'

export {flagUsages} from './help'

export async function parse<TFlags extends OutputFlags<any>, TArgs extends OutputArgs<any>>(
argv: string[],
options: Input<TFlags, TArgs>,
): Promise<ParserOutput<TFlags, TArgs>> {
export async function parse<
TFlags extends OutputFlags<any>,
BFlags extends OutputFlags<any>,
TArgs extends OutputArgs<any>,
>(argv: string[], options: Input<TFlags, BFlags, TArgs>): Promise<ParserOutput<TFlags, BFlags, TArgs>> {
const input = {
'--': options['--'],
args: (options.args ?? {}) as ArgInput<any>,
Expand All @@ -19,5 +20,5 @@ export async function parse<TFlags extends OutputFlags<any>, TArgs extends Outpu
const parser = new Parser(input)
const output = await parser.parse()
await validate({input, output})
return output as ParserOutput<TFlags, TArgs>
return output as ParserOutput<TFlags, BFlags, TArgs>
}
6 changes: 4 additions & 2 deletions src/util/aggregate-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ const json = boolean({
helpGroup: 'GLOBAL',
})

export function aggregateFlags<F extends FlagOutput>(
export function aggregateFlags<F extends FlagOutput, B extends FlagOutput>(
flags: FlagInput<F> | undefined,
baseFlags: FlagInput<B> | undefined,
enableJsonFlag: boolean | undefined,
): FlagInput<F> {
return (enableJsonFlag ? {json, ...flags} : flags) as FlagInput<F>
const combinedFlags = {...baseFlags, ...flags}
return (enableJsonFlag ? {json, ...combinedFlags} : combinedFlags) as FlagInput<F>
}
10 changes: 4 additions & 6 deletions src/util/cache-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {ensureArgObject} from './ensure-arg-object'
import {pickBy} from './util'

// In order to collect static properties up the inheritance chain, we need to recursively
// access the prototypes until there's nothing left.
// access the prototypes until there's nothing left. This allows us to combine baseFlags
// and flags as well as add in the json flag if enableJsonFlag is enabled.
function mergePrototype(result: Command.Class, cmd: Command.Class): Command.Class {
const proto = Object.getPrototypeOf(cmd)
const filteredProto = pickBy(proto, (v) => v !== undefined) as Command.Class
Expand Down Expand Up @@ -85,14 +86,11 @@ export async function cacheCommand(

// @ts-expect-error because v2 commands have flags stored in _flags
const uncachedFlags = cmd.flags ?? cmd._flags
// @ts-expect-error because v2 commands have base flags stored in `_baseFlags` and v4 commands never have `baseFlags`
// @ts-expect-error because v2 commands have base flags stored in _baseFlags
const uncachedBaseFlags = cmd.baseFlags ?? cmd._baseFlags

const [flags, args] = await Promise.all([
await cacheFlags(
aggregateFlags({...uncachedFlags, ...uncachedBaseFlags}, cmd.enableJsonFlag),
respectNoCacheDefault,
),
await cacheFlags(aggregateFlags(uncachedFlags, uncachedBaseFlags, cmd.enableJsonFlag), respectNoCacheDefault),
await cacheArgs(ensureArgObject(cmd.args), respectNoCacheDefault),
])

Expand Down
Loading