init
This commit is contained in:
74
src/models/Config.tsx
Normal file
74
src/models/Config.tsx
Normal file
@@ -0,0 +1,74 @@
|
||||
import { Env } from "./Env";
|
||||
|
||||
|
||||
export class ConfigTemplate {
|
||||
public static Empty: ConfigTemplate = new ConfigTemplate();
|
||||
|
||||
constructor(text: string = "") {
|
||||
this._contentText = text;
|
||||
this.extractParams();
|
||||
}
|
||||
|
||||
private _contentText: string = "";
|
||||
private _params: string[] = [];
|
||||
|
||||
public get content(): string {
|
||||
return this._contentText;
|
||||
}
|
||||
|
||||
public get Params(): string[] {
|
||||
return [...this._params];
|
||||
}
|
||||
|
||||
private extractParams() {
|
||||
let regex = /@(\w+)@/g;
|
||||
let matches;
|
||||
let paramsSet = new Set<string>();
|
||||
|
||||
while ((matches = regex.exec(this._contentText)) !== null) {
|
||||
if (matches.length > 1) {
|
||||
paramsSet.add(matches[1]);
|
||||
}
|
||||
}
|
||||
|
||||
this._params = Array.from(paramsSet);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
export class Config {
|
||||
public static get ENV_NAME_PARAM(): string { return "env_name" };
|
||||
|
||||
public envs: Env[] = [];
|
||||
public template: ConfigTemplate = ConfigTemplate.Empty;
|
||||
|
||||
addEnvs(envs: Env[]) {
|
||||
this.envs = envs;
|
||||
}
|
||||
|
||||
addTemplate(text: string) {
|
||||
this.template = new ConfigTemplate(text);
|
||||
}
|
||||
|
||||
getTemplateAsJson(): string {
|
||||
try {
|
||||
return this.template.content ;
|
||||
} catch (error) {
|
||||
console.error("Error converting template content to JSON:", error);
|
||||
return "{}";
|
||||
}
|
||||
}
|
||||
|
||||
validateParams(): string[] {
|
||||
const envKeys = this.envs.map(env => env.params.map(param => param.name)).flat();
|
||||
const missingParams = this.template.Params.filter(param => param != Config.ENV_NAME_PARAM && !envKeys.includes(param));
|
||||
|
||||
if (missingParams.length > 0) {
|
||||
console.error("Template: missing parameters in environments:", missingParams);
|
||||
}
|
||||
|
||||
return missingParams;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user