-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme-lib.mjs
More file actions
55 lines (47 loc) · 1.75 KB
/
Copy paththeme-lib.mjs
File metadata and controls
55 lines (47 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
const here = path.dirname(fileURLToPath(import.meta.url));
export const skillRoot = path.resolve(here, "..");
const defaultCopy = {
brandTitle: "Codex 自定义皮肤",
brandSubtitle: "AI Crafted Theme ✦",
signature: "Codex ♡",
tagline: "把灵感写进每一天 ♡",
projectPrefix: "选择项目 · ",
projectLabel: "♡ 选择项目",
ribbon: "🎀",
};
function isNamedTheme(value) {
return /^[a-z0-9][a-z0-9_-]*$/i.test(value);
}
export function resolveThemeManifest(theme = "dream") {
if (isNamedTheme(theme)) return path.join(skillRoot, "themes", `${theme}.json`);
return path.resolve(theme);
}
function assertString(value, label) {
if (typeof value !== "string" || !value.trim()) throw new Error(`${label} must be a non-empty string`);
}
export async function loadTheme(theme = "dream") {
const manifestPath = resolveThemeManifest(theme);
const raw = JSON.parse(await fs.readFile(manifestPath, "utf8"));
if (raw.schemaVersion !== 1) throw new Error(`Unsupported theme schemaVersion in ${manifestPath}`);
assertString(raw.id, "theme.id");
assertString(raw.displayName, "theme.displayName");
assertString(raw.version, "theme.version");
assertString(raw.css, "theme.css");
if (!isNamedTheme(raw.id)) throw new Error(`Invalid theme id: ${raw.id}`);
const base = path.dirname(manifestPath);
const cssPath = path.resolve(base, raw.css);
const artPath = raw.art ? path.resolve(base, raw.art) : null;
await fs.access(cssPath);
if (artPath) await fs.access(artPath);
return {
...raw,
manifestPath,
cssPath,
artPath,
copy: { ...defaultCopy, ...(raw.copy ?? {}) },
baseTheme: raw.baseTheme ?? {},
};
}