Skip to content

Commit

Permalink
feat: deprecate scopedEnvVarKey for scopedEnvVarKeys which accounts f…
Browse files Browse the repository at this point in the history
…or binAliases (#751)

* feat: deprecate scopedEnvVarKey for scopedEnvVarKeys which accounts for bin aliases

* chore: white space changes

* docs: update jsdoc
  • Loading branch information
WillieRuemmele authored Jul 28, 2023
1 parent 9df3da5 commit 4787248
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export abstract class Command {
let result
try {
// remove redirected env var to allow subsessions to run autoupdated client
delete process.env[this.config.scopedEnvVarKey('REDIRECTED')]
this.config.scopedEnvVarKeys('REDIRECTED').map(key => delete process.env[key])
await this.init()
result = await this.run()
} catch (error: any) {
Expand Down
19 changes: 17 additions & 2 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,21 +396,36 @@ export class Config implements IConfig {
}

public scopedEnvVar(k: string): string | undefined {
return process.env[this.scopedEnvVarKey(k)]
return process.env[this.scopedEnvVarKeys(k).find(k => process.env[k]) as string]
}

public scopedEnvVarTrue(k: string): boolean {
const v = process.env[this.scopedEnvVarKey(k)]
const v = process.env[this.scopedEnvVarKeys(k).find(k => process.env[k]) as string]
return v === '1' || v === 'true'
}

/**
* this DOES NOT account for bin aliases, use scopedEnvVarKeys instead which will account for bin aliases
* @param {string} k, the unscoped key you want to get the value for
* @returns {string} returns the env var key
*/
public scopedEnvVarKey(k: string): string {
return [this.bin, k]
.map(p => p.replace(/@/g, '').replace(/[/-]/g, '_'))
.join('_')
.toUpperCase()
}

/**
* gets the scoped env var keys for a given key, including bin aliases
* @param {string} k, the env key e.g. 'debug'
* @returns {string[]} e.g. ['SF_DEBUG', 'SFDX_DEBUG']
*/
public scopedEnvVarKeys(k: string): string[] {
return [this.bin, ...this.binAliases ?? []].map(alias =>
[alias.replace(/@/g, '').replace(/[/-]/g, '_'), k].join('_').toUpperCase())
}

public findCommand(id: string, opts: { must: true }): Command.Loadable

public findCommand(id: string, opts?: { must: boolean }): Command.Loadable | undefined
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export interface Config {
findMatches(id: string, argv: string[]): Command.Loadable[];
scopedEnvVar(key: string): string | undefined;
scopedEnvVarKey(key: string): string;
scopedEnvVarKeys(key: string): string[];
scopedEnvVarTrue(key: string): boolean;
s3Url(key: string): string;
s3Key(type: 'versioned' | 'unversioned', ext: '.tar.gz' | '.tar.xz', options?: Config.s3Key.Options): string;
Expand Down
52 changes: 50 additions & 2 deletions test/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const pjson = {
files: [],
commands: {},
oclif: {
binAliases: ['foo', 'bar'],
binAliases: ['bar', 'baz'],
topics: {
t1: {
description: 'desc for t1',
Expand Down Expand Up @@ -103,7 +103,55 @@ describe('Config', () => {
describe('binAliases', () => {
testConfig({pjson})
.it('will have binAliases set', config => {
expect(config.binAliases).to.deep.equal(['foo', 'bar'])
expect(config.binAliases).to.deep.equal(['bar', 'baz'])
})

testConfig({pjson}).it('will get scoped env vars with bin aliases', config => {
expect(config.scopedEnvVarKeys('abc')).to.deep.equal(['FOO_ABC', 'BAR_ABC', 'BAZ_ABC'])
})

testConfig({pjson}).it('will get scoped env vars', config => {
expect(config.scopedEnvVarKey('abc')).to.equal('FOO_ABC')
})

testConfig({pjson}).it('will get scopedEnvVar', config => {
process.env.FOO_ABC = 'find me'
expect(config.scopedEnvVar('abc')).to.deep.equal('find me')
delete process.env.FOO_ABC
})

testConfig({pjson}).it('will get scopedEnvVar via alias', config => {
process.env.BAZ_ABC = 'find me'
expect(config.scopedEnvVar('abc')).to.deep.equal('find me')
delete process.env.BAZ_ABC
})

testConfig({pjson}).it('will get scoped env vars', config => {
expect(config.scopedEnvVarKey('abc')).to.equal('FOO_ABC')
})

testConfig({pjson}).it('will get scopedEnvVarTrue', config => {
process.env.FOO_ABC = 'true'
expect(config.scopedEnvVarTrue('abc')).to.equal(true)
delete process.env.FOO_ABC
})

testConfig({pjson}).it('will get scopedEnvVarTrue via alias', config => {
process.env.BAR_ABC = 'true'
expect(config.scopedEnvVarTrue('abc')).to.equal(true)
delete process.env.BAR_ABC
})

testConfig({pjson}).it('will get scopedEnvVarTrue=1', config => {
process.env.FOO_ABC = '1'
expect(config.scopedEnvVarTrue('abc')).to.equal(true)
delete process.env.FOO_ABC
})

testConfig({pjson}).it('will get scopedEnvVarTrue=1 via alias', config => {
process.env.BAR_ABC = '1'
expect(config.scopedEnvVarTrue('abc')).to.equal(true)
delete process.env.BAR_ABC
})
})

Expand Down

1 comment on commit 4787248

@NHanright
Copy link

Choose a reason for hiding this comment

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

Hello,

We are receiving this error after this commit was pushed on our custom validation push...
Screenshot_20230728_153230_Teams

Please sign in to comment.