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: allow CONTENT_TYPE env to work for all oclif cmds #753

Merged
merged 3 commits into from
Aug 3, 2023
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 src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export abstract class Command {
return jsonIndex > -1 && (ptIndex === -1 || jsonIndex < ptIndex)
}

return this.argv.includes('--json')
return this.argv.includes('--json') || this.config.scopedEnvVar?.('CONTENT_TYPE')?.toLowerCase() === 'json'
}

/**
Expand Down
16 changes: 16 additions & 0 deletions test/command/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,22 @@ describe('command', () => {
})
.it('json enabled/pass through disabled/--json flag before --/jsonEnabled() should be true')

fancy
.stdout()
.do(async () => {
class CMD extends Command {
static enableJsonFlag = true
async run() {}
}

// mock a scopedEnvVar being set to JSON
const cmd = new CMD([], {
bin: 'FOO', scopedEnvVar: (foo: string) => foo.includes('CONTENT_TYPE') ? 'json' : undefined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's an .env() on fancy-test that lets you set env vars if you prefer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that didn't work when I tried it

} as any)
expect(cmd.jsonEnabled()).to.equal(true)
})
.it('json enabled from env')

fancy
.stdout()
.do(async () => {
Expand Down
File renamed without changes.
File renamed without changes.