From 52f1b12f3c9e690b19ac0341f9d0cbce17592db8 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Tue, 26 Sep 2023 09:52:00 -0600 Subject: [PATCH] feat: expose json flag --- src/command.ts | 7 ++++++- src/config/config.ts | 2 +- src/flags.ts | 15 ++++++++++----- src/index.ts | 26 +------------------------- test/command/command.test.ts | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 32 deletions(-) diff --git a/src/command.ts b/src/command.ts index 442b9a1f6..7b9263df5 100644 --- a/src/command.ts +++ b/src/command.ts @@ -243,8 +243,13 @@ export abstract class Command { * @returns {boolean} true if the command supports json and the --json flag is present */ public jsonEnabled(): boolean { + const flagOverride = this.config.scopedEnvVarTrue?.('JSON_FLAG_OVERRIDE') + + if (flagOverride) return true + // if the command doesn't support json, return false if (!this.ctor.enableJsonFlag) return false + // if the command parameter pass through is enabled, return true if the --json flag is before the '--' separator if (this.passThroughEnabled) { const ptIndex = this.argv.indexOf('--') @@ -321,7 +326,7 @@ export abstract class Command { const opts = { context: this, ...options, - flags: (options.enableJsonFlag ? {...combinedFlags, json} : combinedFlags) as FlagInput, + flags: (options.enableJsonFlag ? {...combinedFlags, json: json()} : combinedFlags) as FlagInput, } const results = await Parser.parse(argv, opts) diff --git a/src/config/config.ts b/src/config/config.ts index 3ff0278f5..18df7da5e 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -870,7 +870,7 @@ export async function toCached(cmd: Command.Class, plugin?: IPlugin, respectNoCa const c = mergePrototype(cmd, cmd) const cmdFlags = { - ...(c.enableJsonFlag ? {json} : {}), + ...(c.enableJsonFlag ? {json: json()} : {}), ...c.flags, ...c.baseFlags, } as typeof c['flags'] diff --git a/src/flags.ts b/src/flags.ts index eb4887ed6..f38733570 100644 --- a/src/flags.ts +++ b/src/flags.ts @@ -145,6 +145,16 @@ export const help = (opts: Partial> = {}): BooleanFlag> = {}): BooleanFlag => boolean({ + description: 'Format output as json.', + helpGroup: 'GLOBAL', + ...opts, + async parse(input, cmd) { + if (input) process.env[cmd.config.scopedEnvVarKey('JSON_FLAG_OVERRIDE')] = 'true' + return input + }, +}) + type ElementType> = T[number]; export function option( @@ -209,8 +219,3 @@ export function option( type: 'option', }) } - -export const json = boolean({ - description: 'Format output as json.', - helpGroup: 'GLOBAL', -}) diff --git a/src/index.ts b/src/index.ts index 71b645bb3..b9ce5e1bc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,3 @@ -import { - boolean, - custom, - directory, - file, - help, - integer, - option, - string, - url, - version, -} from './flags' import {stderr} from './cli-ux/stream' function checkCWD() { @@ -27,22 +15,10 @@ checkCWD() export * as Args from './args' export * as Errors from './errors' -export const Flags = { - custom, - option, - boolean, - string, - url, - integer, - directory, - file, - help, - version, -} - export * as Interfaces from './interfaces' export * as Parser from './parser' export * as ux from './cli-ux' +export * as Flags from './flags' export {CommandHelp, HelpBase, Help, loadHelpClass} from './help' export {Config, toCached, Plugin, tsPath} from './config' export {HelpSection, HelpSectionRenderer, HelpSectionKeyValueTable} from './help/formatter' diff --git a/test/command/command.test.ts b/test/command/command.test.ts index c0aa447f2..6bb67ac66 100644 --- a/test/command/command.test.ts +++ b/test/command/command.test.ts @@ -711,5 +711,39 @@ describe('command', () => { expect(cmd.jsonEnabled()).to.equal(false) }) .it('json disabled/pass through enable/--json flag before --/jsonEnabled() should be false') + + fancy + .stdout() + .do(async () => { + class CMD extends Command { + static flags = { + json: Flags.json(), + } + + async run() {} + } + + const cmd = new CMD(['--json'], { + bin: 'FOO', scopedEnvVarTrue: (foo: string) => foo.includes('JSON_FLAG_OVERRIDE'), + } as any) + expect(cmd.jsonEnabled()).to.equal(true) + }) + .it('Flags.json to enable json, enableJsonFlag not set') + + fancy + .stdout() + .do(async () => { + class CMD extends Command { + static flags = { + json: Flags.json(), + } + + async run() {} + } + + const cmd = new CMD([], {} as any) + expect(cmd.jsonEnabled()).to.equal(false) + }) + .it('Flags.json in flags definition but not used, enableJsonFlag not set') }) })