Compare commits
10 Commits
ddc5311fb9
...
18193b71c1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
18193b71c1 | ||
|
|
88984a21b2 | ||
|
|
c4221c9bea | ||
|
|
325699d036 | ||
|
|
5cc15eb87a | ||
|
|
0b347c5c94 | ||
|
|
fb57cf6376 | ||
|
|
5890745ed2 | ||
|
|
15f890abda | ||
|
|
acce21c0e6 |
@@ -1,108 +1,140 @@
|
|||||||
import { test, expect } from '@playwright/test';
|
import { test, expect } from '@playwright/test';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
|
||||||
test.describe('Environment Management', () => {
|
test.describe('Environment Management', () => {
|
||||||
test('should not allow removing DEFAULT environment', async ({ page }) => {
|
test('should not allow removing DEFAULT environment', async ({ page }) => {
|
||||||
await page.goto('/');
|
await page.goto('/');
|
||||||
await page.click('button:has-text("Create new")');
|
await page.click('button:has-text("Create new")');
|
||||||
|
|
||||||
// Try to remove DEFAULT - should be blocked
|
|
||||||
const removeButton = page.locator('button.btn-danger[title="Remove environment"]');
|
const removeButton = page.locator('button.btn-danger[title="Remove environment"]');
|
||||||
|
|
||||||
// The button should be disabled for DEFAULT
|
|
||||||
await expect(removeButton).toBeDisabled();
|
await expect(removeButton).toBeDisabled();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should remove non-DEFAULT environment', async ({ page }) => {
|
test('should remove non-DEFAULT environment', async ({ page }) => {
|
||||||
await page.goto('/');
|
await page.goto('/');
|
||||||
await page.click('button:has-text("Create new")');
|
await page.click('button:has-text("Create new")');
|
||||||
|
page.once('dialog', async dialog => { await dialog.accept('toRemove'); });
|
||||||
// Create a new environment
|
|
||||||
page.once('dialog', async dialog => {
|
|
||||||
await dialog.accept('toRemove');
|
|
||||||
});
|
|
||||||
await page.click('button.btn-success[title="Add environment"]');
|
await page.click('button.btn-success[title="Add environment"]');
|
||||||
await page.waitForTimeout(500);
|
await page.waitForTimeout(500);
|
||||||
|
|
||||||
// Verify we have 2 envs
|
|
||||||
await expect(page.locator('#environments option')).toHaveCount(2);
|
await expect(page.locator('#environments option')).toHaveCount(2);
|
||||||
|
page.once('dialog', async dialog => { await dialog.accept(); });
|
||||||
// Remove the new environment
|
|
||||||
page.once('dialog', async dialog => {
|
|
||||||
await dialog.accept(); // Confirm removal
|
|
||||||
});
|
|
||||||
await page.click('button.btn-danger[title="Remove environment"]');
|
await page.click('button.btn-danger[title="Remove environment"]');
|
||||||
await page.waitForTimeout(300);
|
await page.waitForTimeout(300);
|
||||||
|
|
||||||
// Verify we're back to 1 env (DEFAULT)
|
|
||||||
await expect(page.locator('#environments option')).toHaveCount(1);
|
await expect(page.locator('#environments option')).toHaveCount(1);
|
||||||
await expect(page.locator('#environments')).toContainText('DEFAULT');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should create new environment and switch without errors', async ({ page }) => {
|
test('should create new environment and switch without errors', async ({ page }) => {
|
||||||
await page.goto('/');
|
await page.goto('/');
|
||||||
await page.click('button:has-text("Create new")');
|
await page.click('button:has-text("Create new")');
|
||||||
|
page.once('dialog', async dialog => { await dialog.accept('env1'); });
|
||||||
// Verify DEFAULT environment is loaded
|
|
||||||
await expect(page.locator('#environments')).toContainText('DEFAULT');
|
|
||||||
|
|
||||||
// Create a new environment
|
|
||||||
page.once('dialog', async dialog => {
|
|
||||||
await dialog.accept('env1');
|
|
||||||
});
|
|
||||||
await page.click('button.btn-success[title="Add environment"]');
|
await page.click('button.btn-success[title="Add environment"]');
|
||||||
await page.waitForTimeout(500);
|
await page.waitForTimeout(500);
|
||||||
|
|
||||||
// Verify new environment is created
|
|
||||||
await expect(page.locator('#environments option')).toHaveCount(2);
|
await expect(page.locator('#environments option')).toHaveCount(2);
|
||||||
|
|
||||||
// Switch back to DEFAULT (by index 0)
|
|
||||||
await page.locator('#environments').selectOption({ index: 0 });
|
await page.locator('#environments').selectOption({ index: 0 });
|
||||||
await page.waitForTimeout(300);
|
await page.waitForTimeout(300);
|
||||||
|
|
||||||
// Verify the page is still working
|
|
||||||
await expect(page.locator('#environments')).toBeVisible();
|
|
||||||
|
|
||||||
// Switch to env1 (by text) - this should NOT cause error
|
|
||||||
await page.locator('#environments').selectOption('env1');
|
await page.locator('#environments').selectOption('env1');
|
||||||
await page.waitForTimeout(300);
|
await page.waitForTimeout(300);
|
||||||
|
|
||||||
// Verify the page is still working (no white screen of death)
|
|
||||||
await expect(page.locator('#environments')).toBeVisible();
|
await expect(page.locator('#environments')).toBeVisible();
|
||||||
await expect(page.locator('#environments option')).toHaveCount(2);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should create multiple environments and switch between them', async ({ page }) => {
|
test('should create multiple environments and switch between them', async ({ page }) => {
|
||||||
await page.goto('/');
|
await page.goto('/');
|
||||||
await page.click('button:has-text("Create new")');
|
await page.click('button:has-text("Create new")');
|
||||||
|
page.once('dialog', async dialog => { await dialog.accept('env1'); });
|
||||||
// Create env1
|
|
||||||
page.once('dialog', async dialog => {
|
|
||||||
await dialog.accept('env1');
|
|
||||||
});
|
|
||||||
await page.click('button.btn-success[title="Add environment"]');
|
await page.click('button.btn-success[title="Add environment"]');
|
||||||
await page.waitForTimeout(500);
|
await page.waitForTimeout(500);
|
||||||
|
page.once('dialog', async dialog => { await dialog.accept('env2'); });
|
||||||
// Create env2
|
|
||||||
page.once('dialog', async dialog => {
|
|
||||||
await dialog.accept('env2');
|
|
||||||
});
|
|
||||||
await page.click('button.btn-success[title="Add environment"]');
|
await page.click('button.btn-success[title="Add environment"]');
|
||||||
await page.waitForTimeout(500);
|
await page.waitForTimeout(500);
|
||||||
|
|
||||||
// Verify we have 3 envs (DEFAULT + env1 + env2)
|
|
||||||
await expect(page.locator('#environments option')).toHaveCount(3);
|
await expect(page.locator('#environments option')).toHaveCount(3);
|
||||||
|
|
||||||
// Switch to each env and verify page doesn't crash
|
|
||||||
await page.locator('#environments').selectOption({ index: 0 });
|
await page.locator('#environments').selectOption({ index: 0 });
|
||||||
await page.waitForTimeout(200);
|
await page.waitForTimeout(200);
|
||||||
await expect(page.locator('#environments')).toBeVisible();
|
|
||||||
|
|
||||||
await page.locator('#environments').selectOption('env1');
|
await page.locator('#environments').selectOption('env1');
|
||||||
await page.waitForTimeout(200);
|
await page.waitForTimeout(200);
|
||||||
await expect(page.locator('#environments')).toBeVisible();
|
|
||||||
|
|
||||||
await page.locator('#environments').selectOption('env2');
|
await page.locator('#environments').selectOption('env2');
|
||||||
await page.waitForTimeout(200);
|
await page.waitForTimeout(200);
|
||||||
await expect(page.locator('#environments')).toBeVisible();
|
await expect(page.locator('#environments')).toBeVisible();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should add params and edit template manually', async ({ page }) => {
|
||||||
|
await page.goto('/');
|
||||||
|
await page.click('button:has-text("Create new")');
|
||||||
|
const nameInput = page.locator('input[placeholder="name"]').first();
|
||||||
|
const valueInput = page.locator('input[placeholder="value"]').first();
|
||||||
|
const addButton = page.locator('button.btn-success').first();
|
||||||
|
await nameInput.fill('host');
|
||||||
|
await valueInput.fill('localhost:8080');
|
||||||
|
await addButton.click();
|
||||||
|
await page.waitForTimeout(500);
|
||||||
|
await nameInput.fill('port');
|
||||||
|
await valueInput.fill('9090');
|
||||||
|
await addButton.click();
|
||||||
|
await page.waitForTimeout(500);
|
||||||
|
await page.click('a:has-text("Content Template")');
|
||||||
|
await page.waitForTimeout(500);
|
||||||
|
await expect(page.locator('button:has-text("Edit")')).toBeVisible();
|
||||||
|
await page.click('button:has-text("Edit")');
|
||||||
|
await page.waitForTimeout(500);
|
||||||
|
const textarea = page.locator('textarea');
|
||||||
|
await expect(textarea).toBeVisible();
|
||||||
|
await textarea.fill('{\n "!!! host": "@host@",\n "!!! port": "@port@",\n "!!! custom": "@custom@"\n}');
|
||||||
|
await page.waitForTimeout(300);
|
||||||
|
await page.click('button:has-text("Save")');
|
||||||
|
await page.waitForTimeout(500);
|
||||||
|
await expect(page.locator('button:has-text("Edit")')).toBeVisible();
|
||||||
|
const pageContent = await page.content();
|
||||||
|
expect(pageContent).toContain('!!! custom');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not duplicate params when placeholder already exists', async ({ page }) => {
|
||||||
|
await page.goto('/');
|
||||||
|
await page.click('button:has-text("Create new")');
|
||||||
|
const nameInput = page.locator('input[placeholder="name"]').first();
|
||||||
|
const valueInput = page.locator('input[placeholder="value"]').first();
|
||||||
|
const addButton = page.locator('button.btn-success').first();
|
||||||
|
await nameInput.fill('host');
|
||||||
|
await valueInput.fill('localhost:8080');
|
||||||
|
await addButton.click();
|
||||||
|
await page.waitForTimeout(500);
|
||||||
|
await page.click('a:has-text("Content Template")');
|
||||||
|
await page.waitForTimeout(500);
|
||||||
|
await page.click('button:has-text("Edit")');
|
||||||
|
await page.waitForTimeout(300);
|
||||||
|
const textarea = page.locator('textarea');
|
||||||
|
await textarea.fill('{\n "!!! host": "@host@",\n "apiUrl": "http://@host@/api"\n}');
|
||||||
|
await page.waitForTimeout(300);
|
||||||
|
await page.click('button:has-text("Save")');
|
||||||
|
await page.waitForTimeout(500);
|
||||||
|
await page.click('a:has-text("Env")');
|
||||||
|
await page.waitForTimeout(300);
|
||||||
|
await nameInput.fill('host');
|
||||||
|
await valueInput.fill('updated-host:9090');
|
||||||
|
await addButton.click();
|
||||||
|
await page.waitForTimeout(500);
|
||||||
|
await page.click('a:has-text("Content Template")');
|
||||||
|
await page.waitForTimeout(500);
|
||||||
|
const templateContent = await page.locator('.config-template-editor').textContent();
|
||||||
|
const hostKeyCount = (templateContent.match(/!!! host/g) || []).length;
|
||||||
|
expect(hostKeyCount).toBe(1);
|
||||||
|
const hostPlaceholderCount = (templateContent.match(/@host@/g) || []).length;
|
||||||
|
expect(hostPlaceholderCount).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should download config file with correct filename', async ({ page }) => {
|
||||||
|
await page.goto('/');
|
||||||
|
await page.click('button:has-text("Create new")');
|
||||||
|
await page.waitForTimeout(500);
|
||||||
|
const [download] = await Promise.all([
|
||||||
|
page.waitForEvent('download'),
|
||||||
|
page.click('button:has-text("Download")')
|
||||||
|
]);
|
||||||
|
const filename = download.suggestedFilename();
|
||||||
|
expect(filename).toMatch(/^config_\d{2}-\d{2}-\d{2}-\d{4}\.json\.xml$/);
|
||||||
|
// Save and read the file to verify content
|
||||||
|
await download.saveAs('temp-config.xml');
|
||||||
|
const contentStr = fs.readFileSync('temp-config.xml', 'utf8');
|
||||||
|
expect(contentStr).toContain('engine');
|
||||||
|
expect(contentStr).toContain('DEFAULT');
|
||||||
|
expect(contentStr).toContain('template');
|
||||||
|
fs.unlinkSync('temp-config.xml');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
13
src/App.tsx
13
src/App.tsx
@@ -90,6 +90,15 @@ function App() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleTemplateSaved(newContent: string) {
|
||||||
|
setConfig(prevConfig => {
|
||||||
|
const newConfig = new Config();
|
||||||
|
newConfig.envs = prevConfig.envs;
|
||||||
|
newConfig.addTemplate(newContent);
|
||||||
|
return newConfig;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<main className="container-fluid m-2">
|
<main className="container-fluid m-2">
|
||||||
@@ -98,7 +107,7 @@ function App() {
|
|||||||
AppState.Instance.loadConfig(x);
|
AppState.Instance.loadConfig(x);
|
||||||
setEnvs(x.envs);
|
setEnvs(x.envs);
|
||||||
setConfig(x);
|
setConfig(x);
|
||||||
}} />
|
}} config={config} />
|
||||||
</div>
|
</div>
|
||||||
{envs.length > 0 ?
|
{envs.length > 0 ?
|
||||||
(<div className="row">
|
(<div className="row">
|
||||||
@@ -111,7 +120,7 @@ function App() {
|
|||||||
onRemove={handleEnvRemoved} />
|
onRemove={handleEnvRemoved} />
|
||||||
</section>
|
</section>
|
||||||
<section id="content" className="col-8 col-xl-7 border-start ms-1">
|
<section id="content" className="col-8 col-xl-7 border-start ms-1">
|
||||||
<Content env={currentEnv} config={config} />
|
<Content env={currentEnv} config={config} onTemplateSaved={handleTemplateSaved} />
|
||||||
</section>
|
</section>
|
||||||
</div>)
|
</div>)
|
||||||
:
|
:
|
||||||
|
|||||||
39
src/builders/ConfigBuilder.ts
Normal file
39
src/builders/ConfigBuilder.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import { Config } from "../models/Config";
|
||||||
|
import { EnvBuilder } from "./EnvBuilder";
|
||||||
|
|
||||||
|
export class ConfigBuilder {
|
||||||
|
/**
|
||||||
|
* Builds a full XML config file with all environments and template.
|
||||||
|
*/
|
||||||
|
public static buildFullXml(config: Config): string {
|
||||||
|
const lines: string[] = [];
|
||||||
|
lines.push("<engine>");
|
||||||
|
|
||||||
|
// Add all environments
|
||||||
|
for (const env of config.envs) {
|
||||||
|
const envXml = new EnvBuilder().buildEnv(env);
|
||||||
|
lines.push(envXml);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add template
|
||||||
|
lines.push(" <template>");
|
||||||
|
lines.push(config.template.content);
|
||||||
|
lines.push(" </template>");
|
||||||
|
|
||||||
|
lines.push("</engine>");
|
||||||
|
return lines.join("\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates filename with timestamp: config_yy-dd-MM-HHmm.json.xml
|
||||||
|
*/
|
||||||
|
public static generateFilename(): string {
|
||||||
|
const now = new Date();
|
||||||
|
const yy = now.getFullYear().toString().slice(-2);
|
||||||
|
const dd = String(now.getDate()).padStart(2, '0');
|
||||||
|
const MM = String(now.getMonth() + 1).padStart(2, '0');
|
||||||
|
const HH = String(now.getHours()).padStart(2, '0');
|
||||||
|
const mm = String(now.getMinutes()).padStart(2, '0');
|
||||||
|
return `config_${yy}-${dd}-${MM}-${HH}${mm}.json.xml`;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,6 +16,14 @@ export class EnvBuilder implements IBuilder<Env> {
|
|||||||
this._src = v;
|
this._src = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds XML for a single environment (static method for direct use)
|
||||||
|
*/
|
||||||
|
public buildEnv(env: Env): string {
|
||||||
|
this.src = env;
|
||||||
|
return this.build();
|
||||||
|
}
|
||||||
|
|
||||||
build(): string {
|
build(): string {
|
||||||
return this
|
return this
|
||||||
.open()
|
.open()
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
import { Env } from "../models/Env";
|
import { Env } from "../models/Env";
|
||||||
import { EnvBuilder } from "./EnvBuilder";
|
import { EnvBuilder } from "./EnvBuilder";
|
||||||
|
import { ConfigBuilder } from "./ConfigBuilder";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export interface IBuilder<T> {
|
export interface IBuilder<T> {
|
||||||
get src(): T;
|
get src(): T;
|
||||||
@@ -20,7 +17,9 @@ export class Builder {
|
|||||||
};
|
};
|
||||||
|
|
||||||
public static getEnvs(envs: Env[]): string {
|
public static getEnvs(envs: Env[]): string {
|
||||||
return envs.map(x => Builder.getEnv(x).build()).join("\r\n");
|
return envs.map(x => Builder.getEnv(x).build()).join("\r\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export { ConfigBuilder };
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { Env } from "../models/Env";
|
import { Env } from "../models/Env";
|
||||||
import { ConfigReader } from "../models/ConfigReader";
|
import { ConfigReader } from "../models/ConfigReader";
|
||||||
import { Config } from "../models/Config";
|
import { Config } from "../models/Config";
|
||||||
|
import { ConfigBuilder } from "../builders/ConfigBuilder";
|
||||||
|
|
||||||
|
export function FileChooser(props: { onSelected: (x: Config) => void, config?: Config }) {
|
||||||
export function FileChooser(props: { onSelected: (x: Config) => void }) {
|
|
||||||
async function handleFile(x: React.ChangeEvent<HTMLInputElement>) {
|
async function handleFile(x: React.ChangeEvent<HTMLInputElement>) {
|
||||||
let file = x.target.files![0];
|
let file = x.target.files![0];
|
||||||
|
|
||||||
@@ -23,11 +23,42 @@ export function FileChooser(props: { onSelected: (x: Config) => void }) {
|
|||||||
props.onSelected(cfg);
|
props.onSelected(cfg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleDownload() {
|
||||||
|
if (!props.config) {
|
||||||
|
alert("No configuration loaded");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const xmlContent = ConfigBuilder.buildFullXml(props.config);
|
||||||
|
const filename = ConfigBuilder.generateFilename();
|
||||||
|
|
||||||
|
// Create blob and download
|
||||||
|
const blob = new Blob([xmlContent], { type: "text/xml" });
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = url;
|
||||||
|
link.download = filename;
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="col-2">
|
<div className="col-2">
|
||||||
<button className="btn btn-primary" onClick={handleNew} >Create new</button>
|
<button className="btn btn-primary" onClick={handleNew} >Create new</button>
|
||||||
</div>
|
</div>
|
||||||
|
<div className="col-auto">
|
||||||
|
<button
|
||||||
|
className="btn btn-success"
|
||||||
|
onClick={handleDownload}
|
||||||
|
disabled={!props.config}
|
||||||
|
title="Download full config template"
|
||||||
|
>
|
||||||
|
⬇ Download
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
<div className="col-1">or</div>
|
<div className="col-1">or</div>
|
||||||
|
|
||||||
<div className="col">
|
<div className="col">
|
||||||
|
|||||||
142
src/componets/content/ConfigTemplate.tsx
Normal file
142
src/componets/content/ConfigTemplate.tsx
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
import { useState, useEffect } from "react";
|
||||||
|
import Highlight from 'react-highlight';
|
||||||
|
import 'highlight.js/styles/far.css';
|
||||||
|
import { Config } from "../../models/Config";
|
||||||
|
|
||||||
|
interface ConfigTemplateProps {
|
||||||
|
config: Config;
|
||||||
|
onSaved: (newContent: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ConfigTemplate(props: ConfigTemplateProps) {
|
||||||
|
const [mode, setMode] = useState<'view' | 'edit'>('view');
|
||||||
|
const [draftContent, setDraftContent] = useState(props.config.template.content);
|
||||||
|
const [originalContent, setOriginalContent] = useState(props.config.template.content);
|
||||||
|
const [jsonError, setJsonError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
// Sync draft when config changes (only in view mode)
|
||||||
|
useEffect(() => {
|
||||||
|
if (mode === 'view') {
|
||||||
|
setDraftContent(props.config.template.content);
|
||||||
|
}
|
||||||
|
}, [props.config.template.content, mode]);
|
||||||
|
|
||||||
|
function handleEdit() {
|
||||||
|
setOriginalContent(props.config.template.content);
|
||||||
|
setDraftContent(props.config.template.content);
|
||||||
|
setJsonError(null);
|
||||||
|
setMode('edit');
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleRevert() {
|
||||||
|
setDraftContent(originalContent);
|
||||||
|
setJsonError(null);
|
||||||
|
setMode('view');
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleSave() {
|
||||||
|
// Validate JSON before saving
|
||||||
|
try {
|
||||||
|
JSON.parse(draftContent);
|
||||||
|
setJsonError(null);
|
||||||
|
props.onSaved(draftContent);
|
||||||
|
setMode('view');
|
||||||
|
} catch (e) {
|
||||||
|
setJsonError((e as Error).message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleDraftChange(value: string) {
|
||||||
|
setDraftContent(value);
|
||||||
|
// Validate JSON on every change
|
||||||
|
try {
|
||||||
|
if (value.trim()) {
|
||||||
|
// Replace @placeholders@ with valid JSON values for validation
|
||||||
|
const sanitizedValue = value.replace(/"@?(\w+)@?"/g, '"__PLACEHOLDER__"');
|
||||||
|
JSON.parse(sanitizedValue);
|
||||||
|
setJsonError(null);
|
||||||
|
} else {
|
||||||
|
setJsonError(null);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
setJsonError((e as Error).message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleKeyDown(e: React.KeyboardEvent<HTMLTextAreaElement>) {
|
||||||
|
if (e.key === 'Tab') {
|
||||||
|
e.preventDefault();
|
||||||
|
const textarea = e.currentTarget;
|
||||||
|
const start = textarea.selectionStart;
|
||||||
|
const end = textarea.selectionEnd;
|
||||||
|
const value = textarea.value;
|
||||||
|
|
||||||
|
// Insert 2 spaces at cursor position
|
||||||
|
const newValue = value.substring(0, start) + ' ' + value.substring(end);
|
||||||
|
handleDraftChange(newValue);
|
||||||
|
|
||||||
|
// Move cursor after the inserted spaces
|
||||||
|
setTimeout(() => {
|
||||||
|
textarea.selectionStart = textarea.selectionEnd = start + 2;
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const isValidJson = jsonError === null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="config-template-editor">
|
||||||
|
{mode === 'view' ? (
|
||||||
|
<>
|
||||||
|
<div className="mb-2">
|
||||||
|
<button className="btn btn-primary btn-sm" onClick={handleEdit}>
|
||||||
|
✎ Edit
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<Highlight className="language-json">
|
||||||
|
{props.config.template.content || "{}"}
|
||||||
|
</Highlight>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<div className="mb-2 d-flex gap-2 align-items-center">
|
||||||
|
<button
|
||||||
|
className="btn btn-success btn-sm"
|
||||||
|
onClick={handleSave}
|
||||||
|
disabled={!isValidJson}
|
||||||
|
>
|
||||||
|
✓ Save
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className="btn btn-secondary btn-sm"
|
||||||
|
onClick={handleRevert}
|
||||||
|
>
|
||||||
|
× Revert
|
||||||
|
</button>
|
||||||
|
<span className={`ms-2 ${isValidJson ? 'text-success' : 'text-danger'}`}>
|
||||||
|
{isValidJson ? 'Valid JSON' : 'Invalid JSON'}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
{jsonError && (
|
||||||
|
<div className="alert alert-danger py-1 px-2 mb-2" style={{ fontSize: '0.875rem' }}>
|
||||||
|
{jsonError}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<textarea
|
||||||
|
className={`form-control font-monospace ${isValidJson ? 'border-success' : 'border-danger'}`}
|
||||||
|
value={draftContent}
|
||||||
|
onChange={(e) => handleDraftChange(e.target.value)}
|
||||||
|
onKeyDown={handleKeyDown}
|
||||||
|
rows={20}
|
||||||
|
style={{
|
||||||
|
fontFamily: 'monospace',
|
||||||
|
whiteSpace: 'pre',
|
||||||
|
overflowX: 'auto'
|
||||||
|
}}
|
||||||
|
spellCheck={false}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
.highlihgt-scrolled {
|
.highlihgt-scrolled {
|
||||||
overflow-x: auto;
|
overflow-x: auto;
|
||||||
max-width: 90%;
|
/ max-width: 90%;
|
||||||
}
|
}
|
||||||
@@ -4,17 +4,22 @@ import Highlight from 'react-highlight'
|
|||||||
import 'highlight.js/styles/far.css'
|
import 'highlight.js/styles/far.css'
|
||||||
import { Builder } from "../../builders";
|
import { Builder } from "../../builders";
|
||||||
import { Config } from "../../models/Config";
|
import { Config } from "../../models/Config";
|
||||||
|
import { ConfigTemplate } from "./ConfigTemplate";
|
||||||
|
|
||||||
|
|
||||||
export function Content(props: { config: Config, env: Env }) {
|
export function Content(props: { config: Config, env: Env, onTemplateSaved: (newContent: string) => void }) {
|
||||||
const [selectTab, setTab] = useState(ContentType.Env);
|
const [selectTab, setTab] = useState(ContentType.Env);
|
||||||
|
|
||||||
|
// Validate placeholders for warning badge
|
||||||
|
const missingPlaceholders = props.config.validatePlaceholders();
|
||||||
|
const hasValidationWarnings = missingPlaceholders.length > 0;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<ContentTabs onSelected={(id) => setTab(id)} />
|
<ContentTabs onSelected={(id) => setTab(id)} selectedTab={selectTab} hasValidationWarnings={hasValidationWarnings} />
|
||||||
<div className="">
|
<div className="">
|
||||||
{selectTab == ContentType.Env ? (<ContentParams env={props.env} />) : ""}
|
{selectTab == ContentType.Env ? (<ContentParams env={props.env} />) : ""}
|
||||||
{selectTab == ContentType.Json ? (<ContentTemplate env={props.env} config={props.config} />) : ""}
|
{selectTab == ContentType.Json ? (<ConfigTemplate config={props.config} onSaved={props.onTemplateSaved} />) : ""}
|
||||||
{selectTab == ContentType.Raw ? (<ContentRaw config={props.config} env={props.env} />) : ""}
|
{selectTab == ContentType.Raw ? (<ContentRaw config={props.config} env={props.env} />) : ""}
|
||||||
{selectTab == ContentType.Test ? (<ContentTest config={props.config} env={props.env} />) : ""}
|
{selectTab == ContentType.Test ? (<ContentTest config={props.config} env={props.env} />) : ""}
|
||||||
</div>
|
</div>
|
||||||
@@ -29,16 +34,13 @@ enum ContentType {
|
|||||||
Test = 3
|
Test = 3
|
||||||
}
|
}
|
||||||
|
|
||||||
function ContentTabs(props: { onSelected: (id: ContentType) => void }) {
|
function ContentTabs(props: { onSelected: (id: ContentType) => void, selectedTab: ContentType, hasValidationWarnings: boolean }) {
|
||||||
const [selectTab, setSelect] = useState(ContentType.Env);
|
|
||||||
|
|
||||||
function clickHandler(type: ContentType) {
|
function clickHandler(type: ContentType) {
|
||||||
setSelect(type);
|
|
||||||
props.onSelected(type);
|
props.onSelected(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isActive(type: ContentType): string {
|
function isActive(type: ContentType): string {
|
||||||
return type == selectTab ? " active" : " ";
|
return type == props.selectedTab ? " active" : " ";
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -47,7 +49,12 @@ function ContentTabs(props: { onSelected: (id: ContentType) => void }) {
|
|||||||
<a className={"nav-link" + isActive(ContentType.Env)} aria-current="page" href="#" onClick={() => clickHandler(ContentType.Env)}>Env</a>
|
<a className={"nav-link" + isActive(ContentType.Env)} aria-current="page" href="#" onClick={() => clickHandler(ContentType.Env)}>Env</a>
|
||||||
</li>
|
</li>
|
||||||
<li className="nav-item">
|
<li className="nav-item">
|
||||||
<a className={"nav-link" + isActive(ContentType.Json)} href="#" onClick={() => clickHandler(ContentType.Json)} >Content Template</a>
|
<a className={"nav-link" + isActive(ContentType.Json)} href="#" onClick={() => clickHandler(ContentType.Json)} >
|
||||||
|
Content Template
|
||||||
|
{props.hasValidationWarnings && (
|
||||||
|
<span className="badge bg-warning text-dark ms-1">!</span>
|
||||||
|
)}
|
||||||
|
</a>
|
||||||
</li>
|
</li>
|
||||||
<li className="nav-item">
|
<li className="nav-item">
|
||||||
<a className={"nav-link" + isActive(ContentType.Raw)} href="#" onClick={() => clickHandler(ContentType.Raw)}>Raw template</a>
|
<a className={"nav-link" + isActive(ContentType.Raw)} href="#" onClick={() => clickHandler(ContentType.Raw)}>Raw template</a>
|
||||||
@@ -139,18 +146,6 @@ function fillTemplate(config: Config, env: Env): string {
|
|||||||
return filledTemplate;
|
return filledTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
function ContentTemplate(props: { config: Config, env: Env }) {
|
|
||||||
|
|
||||||
let text = props.config.getTemplateAsJson() ?? "{//no content. at all. tottaly empty!!!\n}";
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<Highlight className="language-json" >
|
|
||||||
{text ?? ""}
|
|
||||||
</Highlight>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function ContentParams(props: { env: Env }) {
|
function ContentParams(props: { env: Env }) {
|
||||||
const bldr = Builder.getEnv(props.env);
|
const bldr = Builder.getEnv(props.env);
|
||||||
|
|
||||||
|
|||||||
@@ -62,12 +62,13 @@ export class Config {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the template JSON by adding/updating params from the given environment.
|
* Updates the template JSON by adding/updating params from the given environment.
|
||||||
* Params are added as "paramName": "paramValue" pairs.
|
* Params are added as "!!! paramName": "@paramName@" placeholder pairs.
|
||||||
* Existing params in template are preserved if not in the env.
|
* If a param's @placeholder@ already exists in template, it won't be added.
|
||||||
|
* Existing template content is preserved.
|
||||||
*/
|
*/
|
||||||
updateTemplateFromEnv(env: Env) {
|
updateTemplateFromEnv(env: Env) {
|
||||||
let templateObj: Record<string, any> = {};
|
let templateObj: Record<string, any> = {};
|
||||||
|
|
||||||
// Try to parse existing template as JSON
|
// Try to parse existing template as JSON
|
||||||
try {
|
try {
|
||||||
if (this.template.content.trim()) {
|
if (this.template.content.trim()) {
|
||||||
@@ -78,10 +79,18 @@ export class Config {
|
|||||||
console.warn("Template is not valid JSON, starting fresh");
|
console.warn("Template is not valid JSON, starting fresh");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add/update params from the environment
|
// Add/update params from the environment as placeholders
|
||||||
for (const param of env.params) {
|
for (const param of env.params) {
|
||||||
if (param.name && param.name.trim() !== "") {
|
if (param.name && param.name.trim() !== "") {
|
||||||
templateObj[param.name] = param.value ?? "";
|
const placeholderValue = `@${param.name}@`;
|
||||||
|
|
||||||
|
// Check if this placeholder already exists anywhere in the template
|
||||||
|
const placeholderAlreadyExists = this.template.content.includes(placeholderValue);
|
||||||
|
|
||||||
|
if (!placeholderAlreadyExists) {
|
||||||
|
const placeholderKey = `!!! ${param.name}`;
|
||||||
|
templateObj[placeholderKey] = placeholderValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,5 +109,50 @@ export class Config {
|
|||||||
|
|
||||||
return missingParams;
|
return missingParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates that all @placeholders@ in template have corresponding params.
|
||||||
|
* Checks DEFAULT env first, then all custom envs.
|
||||||
|
* Returns array of placeholder names that are not defined.
|
||||||
|
*/
|
||||||
|
validatePlaceholders(): string[] {
|
||||||
|
const defaultEnv = this.envs.find(e => e.name === "DEFAULT");
|
||||||
|
const customEnvs = this.envs.filter(e => e.name !== "DEFAULT");
|
||||||
|
|
||||||
|
// Collect all param names from DEFAULT
|
||||||
|
const defaultParamNames = new Set(
|
||||||
|
defaultEnv?.params.map(p => p.name).filter(n => n && n.trim() !== "") || []
|
||||||
|
);
|
||||||
|
|
||||||
|
// Collect all param names from all custom envs
|
||||||
|
const customParamNames = new Set(
|
||||||
|
customEnvs.flatMap(e => e.params.map(p => p.name).filter(n => n && n.trim() !== ""))
|
||||||
|
);
|
||||||
|
|
||||||
|
// Extract all @placeholders@ from template
|
||||||
|
const placeholderRegex = /@(\w+)@/g;
|
||||||
|
const placeholdersInTemplate = new Set<string>();
|
||||||
|
let match;
|
||||||
|
while ((match = placeholderRegex.exec(this.template.content)) !== null) {
|
||||||
|
placeholdersInTemplate.add(match[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find placeholders that don't have matching params
|
||||||
|
const missingParams: string[] = [];
|
||||||
|
for (const placeholder of placeholdersInTemplate) {
|
||||||
|
if (placeholder === Config.ENV_NAME_PARAM) continue; // Skip built-in
|
||||||
|
|
||||||
|
// Check if exists in DEFAULT or in ANY custom env
|
||||||
|
const inDefault = defaultParamNames.has(placeholder);
|
||||||
|
const inCustom = customParamNames.has(placeholder);
|
||||||
|
|
||||||
|
// Valid if: in DEFAULT, or in at least one custom env
|
||||||
|
if (!inDefault && !inCustom) {
|
||||||
|
missingParams.push(placeholder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return missingParams;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
11
src/test/ConfigBuilder.test.ts
Normal file
11
src/test/ConfigBuilder.test.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { ConfigBuilder } from '../builders/ConfigBuilder';
|
||||||
|
|
||||||
|
describe('ConfigBuilder', () => {
|
||||||
|
describe('generateFilename', () => {
|
||||||
|
it('should generate filename with correct format', () => {
|
||||||
|
const filename = ConfigBuilder.generateFilename();
|
||||||
|
expect(filename).toMatch(/^config_\d{2}-\d{2}-\d{2}-\d{4}\.json\.xml$/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user