Skip to content

Commit

Permalink
refactor: steve review
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Oct 31, 2023
1 parent e36e102 commit 9d99e61
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion MIGRATING_V5-V6.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 14 additions & 11 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ export class Config extends ConfigFile<ConfigFile.Options, ConfigProperties> {

await config.read();

if (value == null || value === undefined) {
if (value == null) {
config.unset(propertyName);
} else {
config.set(propertyName, value);
Expand Down Expand Up @@ -598,8 +598,8 @@ export class Config extends ConfigFile<ConfigFile.Options, ConfigProperties> {
}

/**
* 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(
Expand All @@ -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(
Expand Down Expand Up @@ -642,20 +642,23 @@ const writeToSfdx = async (path: string, contents: ConfigProperties): Promise<vo
const translated = translateToSfdx(contents);
await fs.promises.mkdir(pathDirname(path), { recursive: true });
await fs.promises.writeFile(path, JSON.stringify(translated, null, 2));
} catch (error) {
/* Do nothing */
} catch (e) {
const logger = Logger.childFromRoot('core:config:writeToSfdx');
logger.debug(`Error writing to sfdx config file at ${path}: ${e instanceof Error ? e.message : ''}`);
}
};

/** turn the sfdx config file into a LWWState based on its contents and its timestamp */
const stateFromSfdxFileSync = (filePath: string, config: Config): LWWState<ConfigProperties> => {
const stateFromSfdxFileSync = (path: string, config: Config): LWWState<ConfigProperties> => {
try {
const fileContents = fs.readFileSync(filePath, 'utf8');
const mtimeNs = fs.statSync(filePath, { bigint: true }).mtimeNs;
const translatedContents = translateToSf(parseJsonMap<ConfigProperties>(fileContents, filePath), config);
const fileContents = fs.readFileSync(path, 'utf8');
const mtimeNs = fs.statSync(path, { bigint: true }).mtimeNs;
const translatedContents = translateToSf(parseJsonMap<ConfigProperties>(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 {};
}
};
26 changes: 15 additions & 11 deletions src/config/configFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<P>(await fs.promises.readFile(this.getPath(), 'utf8'), this.getPath());
this.logAndMergeContents(fileTimestamp, fileContents);
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}

Expand Down

2 comments on commit 9d99e61

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

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

Logger Benchmarks - ubuntu-latest

Benchmark suite Current: 9d99e61 Previous: 13276dc Ratio
Child logger creation 474829 ops/sec (±1.31%) 521703 ops/sec (±0.67%) 1.10
Logging a string on root logger 486835 ops/sec (±10.67%) 614815 ops/sec (±10.50%) 1.26
Logging an object on root logger 382325 ops/sec (±11.56%) 377927 ops/sec (±17.54%) 0.99
Logging an object with a message on root logger 276471 ops/sec (±13.26%) 207606 ops/sec (±17.31%) 0.75
Logging an object with a redacted prop on root logger 271589 ops/sec (±15.43%) 259063 ops/sec (±16.74%) 0.95
Logging a nested 3-level object on root logger 3360 ops/sec (±209.82%) 4529 ops/sec (±205.08%) 1.35

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

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

Logger Benchmarks - windows-latest

Benchmark suite Current: 9d99e61 Previous: 13276dc Ratio
Child logger creation 373101 ops/sec (±6.19%) 485956 ops/sec (±1.59%) 1.30
Logging a string on root logger 410241 ops/sec (±13.08%) 575327 ops/sec (±14.85%) 1.40
Logging an object on root logger 263545 ops/sec (±14.01%) 273010 ops/sec (±19.92%) 1.04
Logging an object with a message on root logger 161199 ops/sec (±19.68%) 165614 ops/sec (±23.53%) 1.03
Logging an object with a redacted prop on root logger 160561 ops/sec (±23.64%) 157447 ops/sec (±21.72%) 0.98
Logging a nested 3-level object on root logger 109823 ops/sec (±24.51%) 115296 ops/sec (±21.94%) 1.05

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.