Skip to content

Commit

Permalink
feat: expose json flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Sep 26, 2023
1 parent a893f34 commit 52f1b12
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 32 deletions.
7 changes: 6 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('--')
Expand Down Expand Up @@ -321,7 +326,7 @@ export abstract class Command {
const opts = {
context: this,
...options,
flags: (options.enableJsonFlag ? {...combinedFlags, json} : combinedFlags) as FlagInput<F>,
flags: (options.enableJsonFlag ? {...combinedFlags, json: json()} : combinedFlags) as FlagInput<F>,
}

const results = await Parser.parse<F, B, A>(argv, opts)
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
15 changes: 10 additions & 5 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ export const help = (opts: Partial<BooleanFlag<boolean>> = {}): BooleanFlag<void
},
})

export const json = (opts: Partial<BooleanFlag<boolean>> = {}): BooleanFlag<boolean> => 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 extends ReadonlyArray<unknown>> = T[number];

export function option<T extends readonly string[], P extends CustomOptions>(
Expand Down Expand Up @@ -209,8 +219,3 @@ export function option<T extends readonly string[], P extends CustomOptions>(
type: 'option',
})
}

export const json = boolean({
description: 'Format output as json.',
helpGroup: 'GLOBAL',
})
26 changes: 1 addition & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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'
Expand Down
34 changes: 34 additions & 0 deletions test/command/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})

0 comments on commit 52f1b12

Please sign in to comment.