Skip to content

Commit

Permalink
feat: remove pass through getter and setter
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Sep 26, 2023
1 parent 52f1b12 commit 178b9a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 37 deletions.
40 changes: 14 additions & 26 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,7 @@ export abstract class Command {

public static hasDynamicHelp = false

protected static '_--' = false

public static enableJsonFlag = false

public static get '--'(): boolean {
return Command['_--']
}

public static set '--'(value: boolean) {
Command['_--'] = value
}

public get passThroughEnabled(): boolean {
return Command['_--']
}

/**
* instantiate and run the command
*
Expand Down Expand Up @@ -243,21 +228,24 @@ 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 the JSON_FLAG_OVERRIDE env var (set by Flags.json parse) is set to true, return true
// Checking for this first allows commands to define a --json flag using Flags.json()
// without setting enableJsonFlag static property.
if (this.config.scopedEnvVarTrue?.('JSON_FLAG_OVERRIDE')) return true

if (flagOverride) return true

// if the command doesn't support json, return false
// 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('--')
const jsonIndex = this.argv.indexOf('--json')
return jsonIndex > -1 && (ptIndex === -1 || jsonIndex < ptIndex)
}
// If the CONTENT_TYPE env var is set to json, return true
if (this.config.scopedEnvVar?.('CONTENT_TYPE')?.toLowerCase() === 'json') return true

return this.argv.includes('--json') || this.config.scopedEnvVar?.('CONTENT_TYPE')?.toLowerCase() === 'json'
const passThroughIndex = this.argv.indexOf('--')
const jsonIndex = this.argv.indexOf('--json')
return passThroughIndex === -1
// If '--' is not present, then check for `--json` in this.argv
? jsonIndex > -1
// If '--' is present, return true only the --json flag exists and is before the '--'
: jsonIndex > -1 && jsonIndex < passThroughIndex
}

/**
Expand Down
14 changes: 3 additions & 11 deletions test/command/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,6 @@ describe('command', () => {
.do(async () => {
class CMD extends Command {
static enableJsonFlag = true
static '--' = true

async run() {
const {flags} = await cmd.parse(CMD, ['--json'])
Expand All @@ -627,16 +626,12 @@ describe('command', () => {
.stdout()
.do(async () => {
class CMD extends Command {
// static initialization block is required whenever using ES2022
static {
this.enableJsonFlag = true
this['--'] = true
}
static enableJsonFlag = true

async run() {
const {flags} = await cmd.parse(CMD, ['--', '--json'])
expect(flags.json).to.equal(false, 'json flag should be false')
expect(this.passThroughEnabled).to.equal(true, 'pass through should be true')
// expect(this.passThroughEnabled).to.equal(true, 'pass through should be true')
}
}

Expand All @@ -650,7 +645,6 @@ describe('command', () => {
.do(async () => {
class CMD extends Command {
static enableJsonFlag = true
static '--' = true

async run() {
const {flags} = await cmd.parse(CMD, ['--foo', '--json'])
Expand All @@ -668,12 +662,11 @@ describe('command', () => {
.do(async () => {
class CMD extends Command {
static enableJsonFlag = true
static '--' = true

async run() {
const {flags} = await cmd.parse(CMD, ['--foo', '--', '--json'])
expect(flags.json).to.equal(false, 'json flag should be false')
expect(this.passThroughEnabled).to.equal(true, 'pass through should be true')
// expect(this.passThroughEnabled).to.equal(true, 'pass through should be true')
}
}

Expand All @@ -687,7 +680,6 @@ describe('command', () => {
.do(async () => {
class CMD extends Command {
static enableJsonFlag = true
static '--' = true

async run() {}
}
Expand Down

0 comments on commit 178b9a9

Please sign in to comment.