fix(web): preserve community theme provenance
This commit is contained in:
1 parent
62d130714d
commit
dd8a0db88b
5 files changed
+75
-24
No files matched your search
@@ -10,6 +10,7 @@ import {
|
||||
getCustomThemes,
|
||||
installCustomTheme,
|
||||
removeCustomTheme,
|
||||
updateCustomTheme,
|
||||
type ThemeDefinition,
|
||||
} from "../../themePalette";
|
||||
import { Button } from "../ui/button";
|
||||
@@ -95,26 +96,40 @@ export function ThemeSearchSection({
|
||||
try {
|
||||
const themes = await importOpenVsxThemeExtension(extension, controller.signal);
|
||||
if (controller.signal.aborted) return;
|
||||
const existingIds = new Set(getCustomThemes().map((theme) => theme.id));
|
||||
const missingThemes = themes.filter((theme) => !existingIds.has(theme.id));
|
||||
if (missingThemes.length === 0) {
|
||||
throw new Error(`${extension.name} is already added.`);
|
||||
}
|
||||
|
||||
const installed: ThemeDefinition[] = [];
|
||||
const existingById = new Map(getCustomThemes().map((theme) => [theme.id, theme]));
|
||||
const installedIds: string[] = [];
|
||||
const replaced: ThemeDefinition[] = [];
|
||||
const imported: ThemeDefinition[] = [];
|
||||
try {
|
||||
for (const theme of missingThemes) installed.push(installCustomTheme(theme));
|
||||
for (const theme of themes) {
|
||||
const existing = existingById.get(theme.id);
|
||||
if (existing) {
|
||||
replaced.push(existing);
|
||||
imported.push(updateCustomTheme(theme));
|
||||
} else {
|
||||
const installed = installCustomTheme(theme);
|
||||
installedIds.push(installed.id);
|
||||
imported.push(installed);
|
||||
}
|
||||
}
|
||||
} catch (cause) {
|
||||
for (const theme of installed) {
|
||||
for (const themeId of installedIds) {
|
||||
try {
|
||||
removeCustomTheme(theme.id);
|
||||
removeCustomTheme(themeId);
|
||||
} catch {
|
||||
// Best effort rollback. The original storage failure is clearer.
|
||||
}
|
||||
}
|
||||
for (const theme of replaced) {
|
||||
try {
|
||||
updateCustomTheme(theme);
|
||||
} catch {
|
||||
// Best effort rollback. The original storage failure is clearer.
|
||||
}
|
||||
}
|
||||
throw cause;
|
||||
}
|
||||
onInstalled(installed);
|
||||
onInstalled(imported);
|
||||
} catch (cause) {
|
||||
if (!controller.signal.aborted) {
|
||||
setError(cause instanceof Error ? cause.message : "That theme could not be added.");
|
||||
@@ -161,6 +176,14 @@ export function ThemeSearchSection({
|
||||
</Button>
|
||||
</form>
|
||||
|
||||
<div className="sr-only" role="status">
|
||||
{isSearching
|
||||
? "Finding themes..."
|
||||
: results
|
||||
? `${results.length} supported ${results.length === 1 ? "theme" : "themes"} found.`
|
||||
: ""}
|
||||
</div>
|
||||
|
||||
{error ? (
|
||||
<div
|
||||
aria-live="polite"
|
||||
|
||||
@@ -313,6 +313,12 @@ describe("Open VSX themes", () => {
|
||||
await expect(importOpenVsxThemeExtension(extension)).rejects.toThrow(
|
||||
"does not match its advertised license",
|
||||
);
|
||||
|
||||
Reflect.deleteProperty(packagedManifest, "license");
|
||||
await rebuildPackage();
|
||||
await expect(importOpenVsxThemeExtension(extension)).rejects.toThrow(
|
||||
"does not match its advertised license",
|
||||
);
|
||||
});
|
||||
|
||||
it("stops import work when the request is cancelled", async () => {
|
||||
|
||||
@@ -651,8 +651,8 @@ export async function importOpenVsxThemeExtension(
|
||||
throw new Error("That extension package does not match the selected Open VSX theme.");
|
||||
}
|
||||
if (
|
||||
typeof packagedManifest.license === "string" &&
|
||||
packagedManifest.license.trim() !== extension.license
|
||||
typeof packagedManifest.license !== "string" ||
|
||||
packagedManifest.license.trim().toLowerCase() !== extension.license.toLowerCase()
|
||||
) {
|
||||
throw new Error("That extension package does not match its advertised license.");
|
||||
}
|
||||
|
||||
@@ -195,13 +195,25 @@ describe("theme files", () => {
|
||||
});
|
||||
|
||||
it("serializes a theme back into the importable file shape", () => {
|
||||
const serialized = serializeThemeFile(T3_CHAT_THEME);
|
||||
const theme = {
|
||||
...parseThemeFile({
|
||||
version: THEME_FILE_VERSION,
|
||||
id: "community-demo",
|
||||
name: "Community Demo",
|
||||
appearance: "dark",
|
||||
colors: { canvas: "#111111" },
|
||||
}),
|
||||
collection: { id: "open-vsx:demo.theme", label: "Demo Theme" },
|
||||
};
|
||||
const serialized = serializeThemeFile(theme);
|
||||
expect(JSON.parse(serialized)).toMatchObject({
|
||||
version: THEME_FILE_VERSION,
|
||||
id: T3_CHAT_THEME.id,
|
||||
name: T3_CHAT_THEME.label,
|
||||
appearance: "light",
|
||||
id: theme.id,
|
||||
name: theme.label,
|
||||
appearance: "dark",
|
||||
collection: theme.collection,
|
||||
});
|
||||
expect(parseThemeFile(JSON.parse(serialized)).collection).toEqual(theme.collection);
|
||||
});
|
||||
|
||||
it("keeps sidebar artwork opt-in through theme files", () => {
|
||||
|
||||
@@ -112,6 +112,7 @@ export type ThemeFile = Readonly<{
|
||||
appearance: ThemeAppearance;
|
||||
colors: ThemeColorOverrides;
|
||||
variants?: ThemeVariantOverrides;
|
||||
collection?: ThemeCollection;
|
||||
sidebarArtwork?: boolean;
|
||||
managed?: boolean;
|
||||
}>;
|
||||
@@ -175,6 +176,15 @@ function isThemeLabel(value: unknown): value is string {
|
||||
return typeof value === "string" && value.trim().length > 0 && value.trim().length <= 48;
|
||||
}
|
||||
|
||||
function parseThemeCollection(value: unknown): ThemeCollection | undefined {
|
||||
return isRecord(value) &&
|
||||
typeof value.id === "string" &&
|
||||
/^[a-z0-9][a-z0-9.:-]{0,127}$/i.test(value.id) &&
|
||||
isThemeLabel(value.label)
|
||||
? { id: value.id, label: value.label.trim() }
|
||||
: undefined;
|
||||
}
|
||||
|
||||
function parseStoredThemeColors(value: unknown, appearance: ThemeAppearance): ThemeColors | null {
|
||||
if (!isRecord(value)) return null;
|
||||
|
||||
@@ -219,13 +229,7 @@ function parseStoredTheme(value: unknown): ThemeDefinition | null {
|
||||
if (!colors) return null;
|
||||
const variants = parseStoredThemeVariants(value.variants, value.appearance);
|
||||
if (value.variants !== undefined && variants === null) return null;
|
||||
const collection =
|
||||
isRecord(value.collection) &&
|
||||
typeof value.collection.id === "string" &&
|
||||
/^[a-z0-9][a-z0-9.:-]{0,127}$/i.test(value.collection.id) &&
|
||||
isThemeLabel(value.collection.label)
|
||||
? { id: value.collection.id, label: value.collection.label.trim() }
|
||||
: undefined;
|
||||
const collection = parseThemeCollection(value.collection);
|
||||
|
||||
return {
|
||||
id: value.id,
|
||||
@@ -1547,6 +1551,10 @@ export function parseThemeFile(value: unknown): ThemeDefinition {
|
||||
}
|
||||
|
||||
const overrides = parseThemeColorOverrides(rawColors);
|
||||
const collection = parseThemeCollection(value.collection);
|
||||
if (value.collection !== undefined && !collection) {
|
||||
throw new Error("Theme collections need a valid id and label.");
|
||||
}
|
||||
|
||||
const fallback = getDefaultThemeColors(appearance);
|
||||
const variants: Partial<Record<ThemeAppearance, ThemeColors>> = {};
|
||||
@@ -1573,6 +1581,7 @@ export function parseThemeFile(value: unknown): ThemeDefinition {
|
||||
appearance,
|
||||
colors: { ...fallback, ...overrides },
|
||||
...(Object.keys(variants).length > 0 ? { variants } : {}),
|
||||
...(collection ? { collection } : {}),
|
||||
...(value.sidebarArtwork === true ? { sidebarArtwork: true } : {}),
|
||||
...(value.managed === true ? { managed: true } : {}),
|
||||
};
|
||||
@@ -1586,6 +1595,7 @@ export function serializeThemeFile(theme: ThemeDefinition): string {
|
||||
appearance: theme.appearance,
|
||||
colors: theme.colors,
|
||||
...(theme.variants ? { variants: theme.variants } : {}),
|
||||
...(theme.collection ? { collection: theme.collection } : {}),
|
||||
...(theme.sidebarArtwork ? { sidebarArtwork: true } : {}),
|
||||
...(theme.managed ? { managed: true } : {}),
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user