import { cp, mkdir, readFile, writeFile } from "node:fs/promises"; import { constants } from "node:fs"; import { access } from "node:fs/promises"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; const projectRoot = resolve(dirname(fileURLToPath(import.meta.url)), ".."); const sourceRoot = process.env.TYPORA_THEME_DIR ?? join(process.env.APPDATA ?? "", "Typora", "themes"); const sourceCss = join(sourceRoot, "github.css"); const sourceAssets = join(sourceRoot, "github"); const targetRoot = join(projectRoot, ".local", "themes", "typora-github"); async function assertReadable(path, label) { try { await access(path, constants.R_OK); } catch { throw new Error(`${label}不存在或不可读取:${path}`); } } await assertReadable(sourceCss, "Typora GitHub 主题"); await assertReadable(sourceAssets, "Typora GitHub 主题资源目录"); try { await access(targetRoot, constants.F_OK); throw new Error( `目标目录已存在:${targetRoot}\n请先人工确认并移走旧目录,再重新导入。` ); } catch (error) { if (error instanceof Error && error.message.startsWith("目标目录已存在")) { throw error; } } await mkdir(targetRoot, { recursive: true }); await cp(sourceAssets, join(targetRoot, "github"), { recursive: true, errorOnExist: true, force: false }); await writeFile( join(targetRoot, "github.css"), await readFile(sourceCss, "utf8"), "utf8" ); const manifest = { manifestVersion: 1, id: "typora-github", name: "Typora 默认(GitHub,本地)", version: "local", description: "从本机 Typora 安装导入,仅供当前设备个人使用,不进入 Git、容器或发布包。", author: "Typora", license: "本地个人使用,未随项目分发", entry: "github.css", domPreset: "typora", defaultFontSize: "16px", supportedFeatures: [ "code", "table", "task-list", "footnote", "katex", "mermaid" ], bundled: false }; await writeFile( join(targetRoot, "theme.json"), `${JSON.stringify(manifest, null, 2)}\n`, "utf8" ); console.log(`已导入本地 Typora 主题:${targetRoot}`);