Skip to content

Commit cf7d011

Browse files
committed
simplify config merging
1 parent ee7ce69 commit cf7d011

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

src/config.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@ interface UserConfig extends Record<string, string> {
1616
projectId: string;
1717
}
1818

19-
const cliConfig = argv(process.argv.slice(2)) as unknown as Partial<UserConfig>;
20-
2119
const defaults: UserConfig = {
2220
apiBaseUrl: "https://cloud.mongodb.com/",
2321
clientId: "0oabtxactgS3gHIR0297",
2422
stateFile: path.join(localDataPath, "state.json"),
2523
projectId: "",
2624
};
2725

28-
const mergedUserConfig = mergeConfigs(defaults, getFileConfig(), getEnvConfig(), cliConfig);
26+
const mergedUserConfig = Object.assign({}, defaults, getFileConfig(), getEnvConfig(), getCliConfig());
2927

3028
const __filename = fileURLToPath(import.meta.url);
3129
const __dirname = path.dirname(__filename);
@@ -93,17 +91,7 @@ function getFileConfig(): Partial<UserConfig> {
9391
}
9492
}
9593

96-
// Merges several user-supplied configs into one. The precedence is from right to left where the last
97-
// config in the `partialConfigs` array overrides the previous ones. The `defaults` config is used as a base.
98-
function mergeConfigs(defaults: UserConfig, ...partialConfigs: Array<Partial<UserConfig>>): UserConfig {
99-
const mergedConfig: UserConfig = { ...defaults };
100-
for (const key of Object.keys(defaults)) {
101-
for (const partialConfig of partialConfigs) {
102-
if (partialConfig[key]) {
103-
mergedConfig[key] = partialConfig[key];
104-
}
105-
}
106-
}
107-
108-
return mergedConfig;
94+
// Reads the cli args and parses them into a UserConfig object.
95+
function getCliConfig() {
96+
return argv(process.argv.slice(2)) as unknown as Partial<UserConfig>;
10997
}

0 commit comments

Comments
 (0)