Commit ed46c19e authored by David Sveningsson's avatar David Sveningsson

fix(config): `init` can now safely be called multiple times

parent 5f9dee12
......@@ -614,6 +614,17 @@ describe("config", () => {
});
describe("init()", () => {
it("should handle being called multiple times", () => {
expect.assertions(1);
const config = Config.fromObject({});
const spy = jest
.spyOn(config as any, "precompileTransformers")
.mockReturnValue([]);
config.init();
config.init();
expect(spy).toHaveBeenCalledTimes(1);
});
it("should handle unset fields", () => {
const config = Config.fromObject({
plugins: null,
......
......@@ -75,6 +75,8 @@ function loadFromFile(filename: string): ConfigData {
export class Config {
private config: ConfigData;
private configurations: Map<string, ConfigData>;
private initialized: boolean;
protected metaTable: MetaTable;
protected plugins: Plugin[];
protected transformers: TransformerEntry[];
......@@ -130,6 +132,7 @@ export class Config {
this.config = mergeInternal(initial, options || {});
this.metaTable = null;
this.rootDir = this.findRootDir();
this.initialized = false;
/* load plugins */
this.plugins = this.loadPlugins(this.config.plugins || []);
......@@ -150,13 +153,20 @@ export class Config {
/**
* Initialize plugins, transforms etc.
*
* Must be called before trying to use config.
* Must be called before trying to use config. Can safely be called multiple
* times.
*/
public init(): void {
if (this.initialized) {
return;
}
/* precompile transform patterns */
this.transformers = this.precompileTransformers(
this.config.transform || {}
);
this.initialized = true;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment