Skip to content

Commit 9d99e61

Browse files
committed
refactor: steve review
1 parent e36e102 commit 9d99e61

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

MIGRATING_V5-V6.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ We've had a series of bugs where files controlled by the config stack could lose
66

77
V6 introducing a file locking mechanism previously only available on the `alias` file and a process of resolving conflicts based on timestamps.
88

9-
But that comes with breaking changes to to reduce the risk of "getting around" the safeties.
9+
But that comes with breaking changes to reduce the risk of "getting around" the safeties.
1010

1111
## Breaking changes related to the Config Stack
1212

src/config/config.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ export class Config extends ConfigFile<ConfigFile.Options, ConfigProperties> {
387387

388388
await config.read();
389389

390-
if (value == null || value === undefined) {
390+
if (value == null) {
391391
config.unset(propertyName);
392392
} else {
393393
config.set(propertyName, value);
@@ -598,8 +598,8 @@ export class Config extends ConfigFile<ConfigFile.Options, ConfigProperties> {
598598
}
599599

600600
/**
601-
* If toOld is specified: migrate all deprecated configs back to their original key.
602-
* - For example, target-org will be renamed to defaultusername.
601+
* convert from "new" to "old" config names
602+
* - For example, `target-org` will be renamed to `defaultusername`.
603603
*/
604604
const translateToSfdx = (sfContents: ConfigProperties): ConfigProperties =>
605605
Object.fromEntries(
@@ -610,8 +610,8 @@ const translateToSfdx = (sfContents: ConfigProperties): ConfigProperties =>
610610
);
611611

612612
/**
613-
* If toOld is specified: migrate all deprecated configs to the new key.
614-
* - For example, target-org will be renamed to defaultusername.
613+
* convert from "old" to "new" config names
614+
* - For example, `defaultusername` will be renamed to `target-org`
615615
*/
616616
const translateToSf = (sfdxContents: ConfigProperties, SfConfig: Config): ConfigProperties =>
617617
Object.fromEntries(
@@ -642,20 +642,23 @@ const writeToSfdx = async (path: string, contents: ConfigProperties): Promise<vo
642642
const translated = translateToSfdx(contents);
643643
await fs.promises.mkdir(pathDirname(path), { recursive: true });
644644
await fs.promises.writeFile(path, JSON.stringify(translated, null, 2));
645-
} catch (error) {
646-
/* Do nothing */
645+
} catch (e) {
646+
const logger = Logger.childFromRoot('core:config:writeToSfdx');
647+
logger.debug(`Error writing to sfdx config file at ${path}: ${e instanceof Error ? e.message : ''}`);
647648
}
648649
};
649650

650651
/** turn the sfdx config file into a LWWState based on its contents and its timestamp */
651-
const stateFromSfdxFileSync = (filePath: string, config: Config): LWWState<ConfigProperties> => {
652+
const stateFromSfdxFileSync = (path: string, config: Config): LWWState<ConfigProperties> => {
652653
try {
653-
const fileContents = fs.readFileSync(filePath, 'utf8');
654-
const mtimeNs = fs.statSync(filePath, { bigint: true }).mtimeNs;
655-
const translatedContents = translateToSf(parseJsonMap<ConfigProperties>(fileContents, filePath), config);
654+
const fileContents = fs.readFileSync(path, 'utf8');
655+
const mtimeNs = fs.statSync(path, { bigint: true }).mtimeNs;
656+
const translatedContents = translateToSf(parseJsonMap<ConfigProperties>(fileContents, path), config);
656657
// get the file timestamp
657658
return stateFromContents(translatedContents, mtimeNs);
658659
} catch (e) {
660+
const logger = Logger.childFromRoot('core:config:stateFromSfdxFileSync');
661+
logger.debug(`Error reading state from sfdx config file at ${path}: ${e instanceof Error ? e.message : ''}`);
659662
return {};
660663
}
661664
};

src/config/configFile.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ export class ConfigFile<
254254
);
255255
}
256256

257-
// get the file modstamp. Do this after the lock acquisition in case the file is being written to.
258257
const fileTimestamp = (await fs.promises.stat(this.getPath(), { bigint: true })).mtimeNs;
259258
const fileContents = parseJsonMap<P>(await fs.promises.readFile(this.getPath(), 'utf8'), this.getPath());
260259
this.logAndMergeContents(fileTimestamp, fileContents);
@@ -263,11 +262,13 @@ export class ConfigFile<
263262
}
264263
// write the merged LWWMap to file
265264
this.logger.debug(`Writing to config file: ${this.getPath()}`);
266-
await fs.promises.writeFile(this.getPath(), JSON.stringify(this.getContents(), null, 2));
267-
268-
// unlock the file
269-
if (typeof unlockFn !== 'undefined') {
270-
await unlockFn();
265+
try {
266+
await fs.promises.writeFile(this.getPath(), JSON.stringify(this.getContents(), null, 2));
267+
} finally {
268+
// unlock the file
269+
if (typeof unlockFn !== 'undefined') {
270+
await unlockFn();
271+
}
271272
}
272273

273274
return this.getContents();
@@ -301,12 +302,15 @@ export class ConfigFile<
301302
// write the merged LWWMap to file
302303

303304
this.logger.trace(`Writing to config file: ${this.getPath()}`);
304-
fs.writeFileSync(this.getPath(), JSON.stringify(this.toObject(), null, 2));
305-
306-
if (typeof unlockFn !== 'undefined') {
307-
// unlock the file
308-
unlockFn();
305+
try {
306+
fs.writeFileSync(this.getPath(), JSON.stringify(this.toObject(), null, 2));
307+
} finally {
308+
if (typeof unlockFn !== 'undefined') {
309+
// unlock the file
310+
unlockFn();
311+
}
309312
}
313+
310314
return this.getContents();
311315
}
312316

0 commit comments

Comments
 (0)