51 lines
1.0 KiB
TypeScript
51 lines
1.0 KiB
TypeScript
import * as fs from 'fs-extra';
|
|
|
|
import { IEnvironment } from './environment';
|
|
|
|
export class Configuration {
|
|
private config: any = {};
|
|
private dirty = false;
|
|
|
|
constructor(private env: IEnvironment, private file: string, private defaults?: any) {}
|
|
|
|
public async load(): Promise<void> {
|
|
if (!await fs.pathExists(this.file)) {
|
|
this.saveDefaults();
|
|
return;
|
|
}
|
|
|
|
const json = await fs.readJson(this.file);
|
|
this.config = json;
|
|
|
|
if (this.defaults) {
|
|
this.config = Object.assign({}, this.defaults, json);
|
|
}
|
|
}
|
|
|
|
public async save(force = false): Promise<void> {
|
|
if (force) {
|
|
return this.write();
|
|
}
|
|
|
|
this.dirty = true;
|
|
}
|
|
|
|
public get(key: string, defval?: any): any {
|
|
if (!this.config[key]) {
|
|
return defval;
|
|
}
|
|
return this.config[key];
|
|
}
|
|
|
|
private saveDefaults(): void {
|
|
this.config = this.defaults || {};
|
|
this.save(true);
|
|
}
|
|
|
|
private async write(): Promise<void> {
|
|
return fs.writeJson(this.file, this.config);
|
|
}
|
|
|
|
private writeTask(): void {}
|
|
}
|