Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Move SettingsLoaders to own files to help with TreeShaking
- Loading branch information
1 parent
bb56ed9
commit f241a11
Showing
12 changed files
with
323 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { ConfluenceSettings, DEFAULT_SETTINGS } from "src/Settings"; | ||
import { DefaultSettingsLoader } from "./DefaultSettingsLoader"; | ||
import { EnvironmentVariableSettingsLoader } from "./EnvironmentVariableSettingsLoader"; | ||
import { ConfigFileSettingsLoader } from "./ConfigFileSettingsLoader"; | ||
import { CommandLineArgumentSettingsLoader } from "./CommandLineArgumentSettingsLoader"; | ||
import { SettingsLoader } from "."; | ||
|
||
export class AutoSettingsLoader extends SettingsLoader { | ||
constructor(private loaders: SettingsLoader[] = []) { | ||
super(); | ||
|
||
if (loaders.length === 0) { | ||
this.loaders.push(new DefaultSettingsLoader()); | ||
this.loaders.push(new ConfigFileSettingsLoader()); | ||
this.loaders.push(new EnvironmentVariableSettingsLoader()); | ||
this.loaders.push(new CommandLineArgumentSettingsLoader()); | ||
} | ||
} | ||
|
||
private combineSettings(): ConfluenceSettings { | ||
let settings: Partial<ConfluenceSettings> = {}; | ||
|
||
for (const loader of this.loaders) { | ||
const partialSettings = loader.loadPartial(); | ||
for (const key in partialSettings) { | ||
const propertyKey = key as keyof ConfluenceSettings; | ||
if ( | ||
Object.prototype.hasOwnProperty.call( | ||
partialSettings, | ||
propertyKey | ||
) | ||
) { | ||
const element = partialSettings[propertyKey]; | ||
if ( | ||
element && | ||
typeof element === typeof DEFAULT_SETTINGS[propertyKey] | ||
) { | ||
settings = { | ||
...settings, | ||
[propertyKey]: element, | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return settings as ConfluenceSettings; | ||
} | ||
|
||
loadPartial(): Partial<ConfluenceSettings> { | ||
return this.combineSettings(); | ||
} | ||
} |
Oops, something went wrong.