Skip to content

Commit

Permalink
Update configUtil and its usages to get configuration values from cor…
Browse files Browse the repository at this point in the history
…e APIs in salesforcedx-sobjects-faux-generator (#4378)

* refactor: use core API to get default username

or alias

* refactor: update return statement

* refactor: move method into class, add todo

* refactor: update getUsername in faux package to ensure we pass the username and not an alias

* refactor: remove unused

* refactor: reload config aggregators on conf chg

* refactor: rollback reload

* refactor: reload ConfigAgg to get latest values

* refactor: add await

* refactor: update typo

Co-authored-by: Gordon Bockus <[email protected]>
  • Loading branch information
klewis-sfdc and gbockus-sf authored Aug 25, 2022
1 parent 795a122 commit 6c09aef
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,46 @@ import {
ConfigAggregator,
ConfigFile,
ConfigValue,
OrgConfigProperties,
StateAggregator
} from '@salesforce/core';
import * as path from 'path';

const defaultUserNameKey = 'defaultusername';

export class ConfigUtil {
public static async getUsername(
projectPath: string
): Promise<string | undefined> {
const defaultUserName = (await this.getConfigValue(
projectPath,
defaultUserNameKey
)) as string;
const info = await StateAggregator.getInstance();
const username = info.aliases.resolveValue(defaultUserName);
return username;
}
const configAggregator = await ConfigUtil.getConfigAggregator(projectPath);
const defaultUsernameOrAlias = configAggregator.getPropertyValue(
OrgConfigProperties.TARGET_ORG
);

public static async getConfigValue(
projectPath: string,
key: string
): Promise<ConfigValue | undefined> {
try {
const myLocalConfig = await ConfigFile.create({
isGlobal: false,
rootFolder: path.join(projectPath, '.sfdx'),
filename: 'sfdx-config.json'
});
const localValue = myLocalConfig.get(key);
if (localValue) {
return localValue;
} else {
const aggregator = await ConfigAggregator.create();
const globalValue = aggregator.getPropertyValue(key);
if (globalValue) {
return globalValue;
}
}
} catch (err) {
return undefined;
if (defaultUsernameOrAlias) {
const info = await StateAggregator.getInstance();
const username = info.aliases.resolveValue(
String(defaultUsernameOrAlias)
);
return username;
}
return undefined;
}

// TODO: Consolidate usages of ConfigAggregator - W-11623895
private static async getConfigAggregator(
projectPath: string
): Promise<ConfigAggregator> {
const origCurrentWorkingDirectory = process.cwd();
// Change the current working directory to the project path,
// so that ConfigAggregator reads the local project values
process.chdir(projectPath);
const configAggregator = await ConfigAggregator.create();
// ConfigAggregator caches instances internally by working directory
// path. Calling reload ensures that this instance of ConfigAggregator
// has the latest values from the config file.
await configAggregator.reload();
// Change the current working directory back to what it was
// before returning
process.chdir(origCurrentWorkingDirectory);
return configAggregator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ export class ForceRefreshSObjectsExecutor extends SfdxCommandletExecutor<{}> {
} catch (result) {
console.log('Generate error ' + result.error);
telemetryService.sendException(result.name, result.error);
ForceRefreshSObjectsExecutor.isActive = false;

throw result;
}

ForceRefreshSObjectsExecutor.isActive = false;
Expand Down

0 comments on commit 6c09aef

Please sign in to comment.