fix: preserve existing template when adding parameters

This commit is contained in:
sokol
2026-02-20 17:09:34 +03:00
parent 28fc3f9e21
commit 1beb9f2026
3 changed files with 387 additions and 5 deletions

View File

@@ -97,29 +97,48 @@ export class Config {
* Updates template by adding placeholders for environment params
*/
public updateTemplateFromEnv(env: Env): void {
let templateObj: Record<string, string> = {};
// If template is empty, initialize with empty object
if (!this.template.content || !this.template.content.trim()) {
this.template = new ConfigTemplate('{}');
}
// Try to parse existing template
let templateObj: Record<string, any> = {};
let hasExistingContent = false;
try {
if (this.template.content.trim()) {
templateObj = JSON.parse(this.template.content);
hasExistingContent = Object.keys(templateObj).length > 0;
}
} catch {
// Start fresh if invalid JSON
// If invalid JSON, preserve the raw content and don't modify
return;
}
// Add placeholders for params that don't exist yet
let hasChanges = false;
for (const param of env.params) {
if (param.name && param.name.trim()) {
const placeholder = `@${param.name}@`;
const paramName = param.name.trim();
const placeholder = `@${paramName}@`;
const templateKey = `!!! ${paramName}`;
// Check if placeholder exists anywhere in template
if (!this.template.content.includes(placeholder)) {
templateObj[`!!! ${param.name}`] = placeholder;
// Only add if not already in templateObj
if (!templateObj[templateKey]) {
templateObj[templateKey] = placeholder;
hasChanges = true;
}
}
}
}
this.template = new ConfigTemplate(JSON.stringify(templateObj, null, 4));
// Only update if there are actual changes
if (hasChanges || !hasExistingContent) {
this.template = new ConfigTemplate(JSON.stringify(templateObj, null, 4));
}
}
/**