Skip to content

Commit a8a99cc

Browse files
committed
refactor: use Alias.getKeysByValue, handle multiple matches
1 parent c52896d commit a8a99cc

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

src/shared/orgListUtil.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ export class OrgListUtil {
132132

133133
for (const authInfo of authInfos) {
134134
const currentValue = OrgListUtil.removeRestrictedInfoFromConfig(authInfo.getFields()) as ExtendedAuthFields;
135-
136135
const [alias, lastUsed] = await Promise.all([
137136
getAliasByUsername(currentValue.username),
138137
fs.stat(join(Global.DIR, `${currentValue.username}.json`)),

src/shared/utils.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@ import { Aliases } from '@salesforce/core';
88

99
export const getAliasByUsername = async (username: string): Promise<string> => {
1010
const alias = await Aliases.create(Aliases.getDefaultOptions());
11-
const aliasContent = alias.getContents().orgs;
12-
if (aliasContent) {
13-
for (const aliasedName of Object.keys(aliasContent)) {
14-
if (aliasContent[aliasedName] === username) return aliasedName;
15-
}
16-
}
17-
return undefined;
11+
const keys = alias.getKeysByValue(username);
12+
return keys?.length ? keys[0] : undefined;
1813
};
1914

2015
export const camelCaseToTitleCase = (text: string): string => {

test/shared/utils.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,24 @@ describe('getAliasByUsername', () => {
1313
beforeEach(async () => {
1414
stubMethod($$.SANDBOX, Aliases, 'create').resolves(Aliases.prototype);
1515
stubMethod($$.SANDBOX, Aliases, 'getDefaultOptions').returns({});
16-
stubMethod($$.SANDBOX, Aliases.prototype, 'getContents').returns({
17-
orgs: {
18-
alias1: 'username1',
19-
alias2: 'username2',
20-
},
21-
});
16+
stubMethod($$.SANDBOX, Aliases.prototype, 'getKeysByValue')
17+
.withArgs('username1')
18+
.returns(['alias1'])
19+
.withArgs('username2')
20+
.returns(['alias2', 'alias2b']);
2221
});
2322
afterEach(() => {
2423
$$.SANDBOX.restore();
2524
});
2625

2726
it('returns alias for a username that exists', async () => {
2827
expect(await getAliasByUsername('username1')).to.equal('alias1');
28+
});
29+
30+
it('returns first alias for a username that has multiple aliases', async () => {
2931
expect(await getAliasByUsername('username2')).to.equal('alias2');
3032
});
33+
3134
it('returns undefined when no matching username is found', async () => {
3235
expect(await getAliasByUsername('username3')).to.be.undefined;
3336
});

0 commit comments

Comments
 (0)