From 9d99e6174905dfd7c1df6d299799ac7706756735 Mon Sep 17 00:00:00 2001 From: mshanemc Date: Tue, 31 Oct 2023 09:58:38 -0500 Subject: [PATCH] refactor: steve review --- MIGRATING_V5-V6.md | 2 +- src/config/config.ts | 25 ++++++++++++++----------- src/config/configFile.ts | 26 +++++++++++++++----------- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/MIGRATING_V5-V6.md b/MIGRATING_V5-V6.md index 21a8bdac38..afcec25d78 100644 --- a/MIGRATING_V5-V6.md +++ b/MIGRATING_V5-V6.md @@ -6,7 +6,7 @@ We've had a series of bugs where files controlled by the config stack could lose V6 introducing a file locking mechanism previously only available on the `alias` file and a process of resolving conflicts based on timestamps. -But that comes with breaking changes to to reduce the risk of "getting around" the safeties. +But that comes with breaking changes to reduce the risk of "getting around" the safeties. ## Breaking changes related to the Config Stack diff --git a/src/config/config.ts b/src/config/config.ts index c9caf6ea0e..1d14a49d5f 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -387,7 +387,7 @@ export class Config extends ConfigFile { await config.read(); - if (value == null || value === undefined) { + if (value == null) { config.unset(propertyName); } else { config.set(propertyName, value); @@ -598,8 +598,8 @@ export class Config extends ConfigFile { } /** - * If toOld is specified: migrate all deprecated configs back to their original key. - * - For example, target-org will be renamed to defaultusername. + * convert from "new" to "old" config names + * - For example, `target-org` will be renamed to `defaultusername`. */ const translateToSfdx = (sfContents: ConfigProperties): ConfigProperties => Object.fromEntries( @@ -610,8 +610,8 @@ const translateToSfdx = (sfContents: ConfigProperties): ConfigProperties => ); /** - * If toOld is specified: migrate all deprecated configs to the new key. - * - For example, target-org will be renamed to defaultusername. + * convert from "old" to "new" config names + * - For example, `defaultusername` will be renamed to `target-org` */ const translateToSf = (sfdxContents: ConfigProperties, SfConfig: Config): ConfigProperties => Object.fromEntries( @@ -642,20 +642,23 @@ const writeToSfdx = async (path: string, contents: ConfigProperties): Promise => { +const stateFromSfdxFileSync = (path: string, config: Config): LWWState => { try { - const fileContents = fs.readFileSync(filePath, 'utf8'); - const mtimeNs = fs.statSync(filePath, { bigint: true }).mtimeNs; - const translatedContents = translateToSf(parseJsonMap(fileContents, filePath), config); + const fileContents = fs.readFileSync(path, 'utf8'); + const mtimeNs = fs.statSync(path, { bigint: true }).mtimeNs; + const translatedContents = translateToSf(parseJsonMap(fileContents, path), config); // get the file timestamp return stateFromContents(translatedContents, mtimeNs); } catch (e) { + const logger = Logger.childFromRoot('core:config:stateFromSfdxFileSync'); + logger.debug(`Error reading state from sfdx config file at ${path}: ${e instanceof Error ? e.message : ''}`); return {}; } }; diff --git a/src/config/configFile.ts b/src/config/configFile.ts index 2cdeb88580..f727af2434 100644 --- a/src/config/configFile.ts +++ b/src/config/configFile.ts @@ -254,7 +254,6 @@ export class ConfigFile< ); } - // get the file modstamp. Do this after the lock acquisition in case the file is being written to. const fileTimestamp = (await fs.promises.stat(this.getPath(), { bigint: true })).mtimeNs; const fileContents = parseJsonMap

(await fs.promises.readFile(this.getPath(), 'utf8'), this.getPath()); this.logAndMergeContents(fileTimestamp, fileContents); @@ -263,11 +262,13 @@ export class ConfigFile< } // write the merged LWWMap to file this.logger.debug(`Writing to config file: ${this.getPath()}`); - await fs.promises.writeFile(this.getPath(), JSON.stringify(this.getContents(), null, 2)); - - // unlock the file - if (typeof unlockFn !== 'undefined') { - await unlockFn(); + try { + await fs.promises.writeFile(this.getPath(), JSON.stringify(this.getContents(), null, 2)); + } finally { + // unlock the file + if (typeof unlockFn !== 'undefined') { + await unlockFn(); + } } return this.getContents(); @@ -301,12 +302,15 @@ export class ConfigFile< // write the merged LWWMap to file this.logger.trace(`Writing to config file: ${this.getPath()}`); - fs.writeFileSync(this.getPath(), JSON.stringify(this.toObject(), null, 2)); - - if (typeof unlockFn !== 'undefined') { - // unlock the file - unlockFn(); + try { + fs.writeFileSync(this.getPath(), JSON.stringify(this.toObject(), null, 2)); + } finally { + if (typeof unlockFn !== 'undefined') { + // unlock the file + unlockFn(); + } } + return this.getContents(); }