From 6c09aef25d4d63ddb2acf6dad3a76a53e2ba9ad7 Mon Sep 17 00:00:00 2001 From: Ken Lewis <46458081+klewis-sfdc@users.noreply.github.com> Date: Thu, 25 Aug 2022 13:59:45 -0700 Subject: [PATCH] Update configUtil and its usages to get configuration values from core 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 --- .../src/generator/configUtil.ts | 62 +++++++++---------- .../src/commands/forceRefreshSObjects.ts | 3 + 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/packages/salesforcedx-sobjects-faux-generator/src/generator/configUtil.ts b/packages/salesforcedx-sobjects-faux-generator/src/generator/configUtil.ts index f7700e74c3..823bbeb66c 100644 --- a/packages/salesforcedx-sobjects-faux-generator/src/generator/configUtil.ts +++ b/packages/salesforcedx-sobjects-faux-generator/src/generator/configUtil.ts @@ -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 { - 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 { - 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 { + 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; + } } diff --git a/packages/salesforcedx-vscode-core/src/commands/forceRefreshSObjects.ts b/packages/salesforcedx-vscode-core/src/commands/forceRefreshSObjects.ts index 5415bd1a45..0b8a6d40c2 100644 --- a/packages/salesforcedx-vscode-core/src/commands/forceRefreshSObjects.ts +++ b/packages/salesforcedx-vscode-core/src/commands/forceRefreshSObjects.ts @@ -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;