@webda/core
Version:
Expose API with Lambda
48 lines • 1.83 kB
JavaScript
import { existsSync, watchFile } from "fs";
import { WebdaError } from "../index.js";
import { FileUtils } from "../utils/serializers.js";
import ConfigurationService from "./configuration.js";
/**
* Allow for dynamic configuration from a file
* @WebdaModda Webda/FileConfiguration
*/
export class FileConfigurationService extends ConfigurationService {
/** @ignore */
async init() {
// Do not call super as we diverged
if (!this.parameters.source) {
throw new WebdaError.CodeError("FILE_CONFIGURATION_SOURCE_MISSING", "Need a source for FileConfigurationService");
}
// Load it from where it should be
this.parameters.source = this._webda.getAppPath(this.parameters.source);
if (!existsSync(this.parameters.source)) {
throw new WebdaError.CodeError("FILE_CONFIGURATION_SOURCE_MISSING", "Need a source for FileConfigurationService");
}
watchFile(this.parameters.source, this.checkUpdate.bind(this));
// Add webda info
this.watch("$.services", (updates) => this._webda.reinit(updates));
await this.checkUpdate();
return this;
}
/**
* Load the JSON from source defined file
* @override
*/
async loadConfiguration() {
return FileUtils.load(this.parameters.source);
}
/**
* Read the file and store it
*/
async initConfiguration() {
this.parameters.source = this._webda.getAppPath(this.parameters.source);
/**
* Auto-generate file if missing
*/
if (!existsSync(this.parameters.source) && this.parameters.default) {
FileUtils.save(this.parameters.default, this.parameters.source);
}
return this.loadAndStoreConfiguration();
}
}
//# sourceMappingURL=fileconfiguration.js.map