feat(nacos): improve config history and diff viewing

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-09-08 01:47:36 +08:00
1 parent 2e72bee9a1
commit eb87c9e837
18 files changed
+508 -167

No files matched your search

-16
View File
@@ -34,7 +34,6 @@ import {
resolveNacosConsoleUrl,
sanitizeNacosConfigFileNameSegment,
splitNacosContentLiteralMatches,
summarizeNacosConfigDiff,
} from "@/lib/nacos/nacosAdmin";
describe("nacosAdmin helpers", () => {
@@ -187,21 +186,6 @@ describe("nacosAdmin helpers", () => {
expect(resolveRNacosOpenApiFallback("http://rnacos.example:8848", "/nacos")).toBeNull();
});
it("summarizes config diffs", () => {
const diff = summarizeNacosConfigDiff("a\nb", "a\nc\nd");
expect(diff.changed).toBe(true);
expect(diff.removedLines).toBe(1);
expect(diff.addedLines).toBe(2);
expect(diff.preview).toContain("- b");
expect(diff.preview).toContain("+ c");
});
it("uses the same terminal newline semantics for diff summaries", () => {
expect(summarizeNacosConfigDiff("aa", "aa\nbb")).toMatchObject({ changed: true, addedLines: 1, removedLines: 0 });
expect(summarizeNacosConfigDiff("aa", "aa\n")).toEqual({ changed: false, addedLines: 0, removedLines: 0, preview: "No content changes." });
expect(summarizeNacosConfigDiff("aa\r\n", "aa\n")).toEqual({ changed: false, addedLines: 0, removedLines: 0, preview: "No content changes." });
});
it("builds side-by-side config diff rows with inline segments", () => {
const rows = buildNacosSideBySideDiff('cloud:\n secret: "aaa"\n', 'cloud:\n secret: "aaa1"\n enabled: true\n');
expect(rows[0]).toMatchObject({ leftLineNumber: 1, rightLineNumber: 1, leftType: "equal", rightType: "equal" });
@@ -0,0 +1,38 @@
import { describe, expect, it } from "vitest";
import { inferNacosConfigFormat, normalizeNacosConfigFormat, resolveNacosConfigFormat } from "../nacosConfigLanguage";
describe("Nacos config format helpers", () => {
it("normalizes config type aliases and trims or lowercases raw values", () => {
expect(normalizeNacosConfigFormat()).toBe("");
expect(normalizeNacosConfigFormat(" ")).toBe("");
expect(normalizeNacosConfigFormat("txt")).toBe("text");
expect(normalizeNacosConfigFormat("YML")).toBe("yaml");
expect(normalizeNacosConfigFormat("Props")).toBe("properties");
expect(normalizeNacosConfigFormat(" JSON ")).toBe("json");
expect(normalizeNacosConfigFormat("toml")).toBe("toml");
});
it("infers formats from dataId extensions with aliases and unknown fallback", () => {
expect(inferNacosConfigFormat("app.yml")).toBe("yaml");
expect(inferNacosConfigFormat("app.yaml")).toBe("yaml");
expect(inferNacosConfigFormat("app.TXT")).toBe("text");
expect(inferNacosConfigFormat("app.properties")).toBe("properties");
expect(inferNacosConfigFormat("app.json")).toBe("json");
expect(inferNacosConfigFormat("app.xml")).toBe("xml");
expect(inferNacosConfigFormat("app.html")).toBe("html");
expect(inferNacosConfigFormat("app.toml")).toBe("toml");
expect(inferNacosConfigFormat("app.text")).toBe("text");
expect(inferNacosConfigFormat("docker.jar")).toBe("");
expect(inferNacosConfigFormat("no-extension")).toBe("");
expect(inferNacosConfigFormat()).toBe("");
});
it("prefers normalized config type, then dataId inference, then text", () => {
expect(resolveNacosConfigFormat("yaml", "app.json")).toBe("yaml");
expect(resolveNacosConfigFormat("YML", "")).toBe("yaml");
expect(resolveNacosConfigFormat("", "app.yml")).toBe("yaml");
expect(resolveNacosConfigFormat(undefined, "app")).toBe("text");
expect(resolveNacosConfigFormat("", "")).toBe("text");
});
});
-29
View File
@@ -565,13 +565,6 @@ export function createNacosSaveAsCopy(item: NacosConfigItem): NacosConfigItem {
};
}
export interface NacosDiffSummary {
changed: boolean;
addedLines: number;
removedLines: number;
preview: string;
}
export type NacosDiffLineType = "equal" | "delete" | "insert" | "modify" | "padding";
export interface NacosInlineSegment {
@@ -599,28 +592,6 @@ export interface NacosInlineDiffRow {
segments: NacosInlineSegment[];
}
export function summarizeNacosConfigDiff(before: string, after: string, maxPreviewLines = 40): NacosDiffSummary {
const changes = diffArrays(splitDiffLines(before), splitDiffLines(after));
const lines: string[] = [];
let addedLines = 0;
let removedLines = 0;
for (const change of changes) {
if (!change.added && !change.removed) continue;
const prefix = change.added ? "+" : "-";
for (const line of change.value) {
if (change.added) addedLines += 1;
else removedLines += 1;
if (lines.length < maxPreviewLines) lines.push(`${prefix} ${line}`);
}
}
const changed = addedLines > 0 || removedLines > 0;
if (!changed) return { changed: false, addedLines: 0, removedLines: 0, preview: "No content changes." };
if (lines.length < addedLines + removedLines) lines.push("...");
return { changed: true, addedLines, removedLines, preview: lines.join("\n") };
}
function normalizeNacosDiffText(value: string): string {
return value.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
}
@@ -0,0 +1,54 @@
import { StreamLanguage } from "@codemirror/language";
import type { Extension } from "@codemirror/state";
export function normalizeNacosConfigFormat(value?: string): string {
const normalized = value?.trim().toLowerCase();
if (!normalized) return "";
if (normalized === "txt") return "text";
if (normalized === "yml") return "yaml";
if (normalized === "props") return "properties";
return normalized;
}
export function inferNacosConfigFormat(dataId?: string): string {
const ext = dataId?.trim().toLowerCase().split(".").pop() || "";
if (ext === "yml") return "yaml";
if (["yaml", "json", "xml", "html", "properties", "toml", "text"].includes(ext)) return ext;
if (ext === "txt") return "text";
return "";
}
export function resolveNacosConfigFormat(configType?: string, dataId?: string): string {
return normalizeNacosConfigFormat(configType) || inferNacosConfigFormat(dataId) || "text";
}
export async function loadNacosConfigLanguage(format: string): Promise<Extension[]> {
switch (normalizeNacosConfigFormat(format)) {
case "json": {
const { json } = await import("@codemirror/lang-json");
return [json()];
}
case "yaml": {
const { yaml } = await import("@codemirror/lang-yaml");
return [yaml()];
}
case "xml": {
const { xml } = await import("@codemirror/lang-xml");
return [xml()];
}
case "html": {
const { html } = await import("@codemirror/lang-html");
return [html({ matchClosingTags: false })];
}
case "properties": {
const { properties } = await import("@codemirror/legacy-modes/mode/properties");
return [StreamLanguage.define(properties)];
}
case "toml": {
const { toml } = await import("@codemirror/legacy-modes/mode/toml");
return [StreamLanguage.define(toml)];
}
default:
return [];
}
}