Files
obsidian-excalidraw-plugin/scripts/build-script-store.mjs
T

272 lines
8.4 KiB
JavaScript

import { execFileSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const scriptsDir = path.join(root, "ea-scripts");
const catalogPath = path.join(scriptsDir, "script-store.json");
const legacyIndexPath = path.join(scriptsDir, "index-new.md");
const directoryInfoPath = path.join(scriptsDir, "directory-info.json");
const checkOnly = process.argv.includes("--check");
const FEATURED_CATEGORY = "Editors Picks";
const fail = (message) => {
throw new Error(`[script-store] ${message}`);
};
const catalog = JSON.parse(fs.readFileSync(catalogPath, "utf8"));
const escapeHtml = (value) =>
String(value ?? "")
.replaceAll("&", "&")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll('"', "&quot;")
.replaceAll("'", "&#039;");
const validateCatalog = () => {
if (catalog.version !== 1) {
fail(`Unsupported catalog version: ${catalog.version}`);
}
if (!Array.isArray(catalog.categories) || !Array.isArray(catalog.scripts)) {
fail("Catalog must contain categories[] and scripts[].");
}
const categories = new Set();
for (const category of catalog.categories) {
if (!category?.name || categories.has(category.name)) {
fail(`Invalid or duplicate category: ${category?.name ?? "<missing>"}`);
}
categories.add(category.name);
}
const names = new Set();
const files = new Set();
for (const script of catalog.scripts) {
if (!script?.name || !script?.file || !script?.installUrl) {
fail(`Script entry is missing name, file, or installUrl: ${JSON.stringify(script)}`);
}
if (names.has(script.name)) {
fail(`Duplicate script name: ${script.name}`);
}
if (files.has(script.file)) {
fail(`Duplicate script file: ${script.file}`);
}
names.add(script.name);
files.add(script.file);
if (!fs.existsSync(path.join(scriptsDir, script.file))) {
fail(`Missing script file: ea-scripts/${script.file}`);
}
for (const category of script.categories ?? []) {
if (!categories.has(category)) {
fail(`Unknown category "${category}" on ${script.name}`);
}
}
if (
script.featuredRank !== undefined &&
!script.categories?.includes(FEATURED_CATEGORY)
) {
fail(`${script.name} has featuredRank but is not in ${FEATURED_CATEGORY}.`);
}
}
};
const categoryScripts = (categoryName) =>
catalog.scripts
.filter((script) => script.categories?.includes(categoryName))
.sort((a, b) => {
if (categoryName === FEATURED_CATEGORY) {
return (
(a.featuredRank ?? Number.MAX_SAFE_INTEGER) -
(b.featuredRank ?? Number.MAX_SAFE_INTEGER) ||
a.name.localeCompare(b.name)
);
}
return a.name.localeCompare(b.name);
});
const generateLegacyIndex = () => {
const current = fs.readFileSync(legacyIndexPath, "utf8");
const newline = current.includes("\r\n") ? "\r\n" : "\n";
const listMarker = "# List of available scripts";
const generatedMarker =
"<!-- Script catalog below is generated by npm run script-store:build. -->";
const listIndex = current.indexOf(listMarker);
if (listIndex < 0) {
fail(`Could not find "${listMarker}" in ea-scripts/index-new.md`);
}
const existingGeneratedIndex = current.indexOf(generatedMarker);
const introEnd =
existingGeneratedIndex >= 0 && existingGeneratedIndex < listIndex
? existingGeneratedIndex
: listIndex;
const intro = current.slice(0, introEnd).trimEnd();
const lines = [intro, "", generatedMarker, listMarker, ""];
for (const category of catalog.categories) {
lines.push(`## ${category.name}`);
if (category.description?.trim()) {
lines.push(category.description.trim(), "");
}
lines.push("| | |", "|----|-----|");
for (const script of categoryScripts(category.name)) {
lines.push(
`|<div><img src="${script.iconUrl}"/></div>|[[#${script.name}]]|`,
);
}
lines.push("");
}
lines.push("---", "", "# Description and Installation", "");
for (const script of [...catalog.scripts].sort((a, b) =>
a.name.localeCompare(b.name),
)) {
lines.push(
`## ${script.name}`,
"```excalidraw-script-install",
script.installUrl,
"```",
`<table><tr valign='top'><td class="label">Author</td><td class="data"><a href='${escapeHtml(script.authorUrl)}'>${escapeHtml(script.author)}</a></td></tr><tr valign='top'><td class="label">Source</td><td class="data"><a href='${escapeHtml(script.sourceUrl)}'>File on GitHub</a></td></tr><tr valign='top'><td class="label">Description</td><td class="data">${script.descriptionHtml.replace(/\r?\n/g, newline)}</td></tr></table>`,
"",
);
}
return `${lines.join(newline).trimEnd()}${newline}`;
};
const getGitOutput = (args) => {
try {
return execFileSync("git", args, {
cwd: root,
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
});
} catch {
return null;
}
};
const getHeadDirectoryInfo = () => {
const source = getGitOutput(["show", "HEAD:ea-scripts/directory-info.json"]);
if (!source) {
return null;
}
try {
return JSON.parse(source);
} catch {
return null;
}
};
const getChangedManagedFiles = () => {
const status = getGitOutput([
"status",
"--porcelain=v1",
"--untracked-files=all",
"--",
"ea-scripts",
]);
if (status === null) {
return null;
}
const managed = new Set();
for (const script of catalog.scripts) {
managed.add(script.file);
managed.add(script.file.replace(/\.(?:md|js)$/i, ".svg"));
}
const changed = new Set();
for (const line of status.split(/\r?\n/)) {
if (line.length < 4) {
continue;
}
let filePath = line.slice(3).trim();
const renameSeparator = filePath.lastIndexOf(" -> ");
if (renameSeparator >= 0) {
filePath = filePath.slice(renameSeparator + 4);
}
if (filePath.startsWith('"') && filePath.endsWith('"')) {
filePath = filePath.slice(1, -1);
}
const prefix = "ea-scripts/";
if (!filePath.startsWith(prefix)) {
continue;
}
const filename = filePath.slice(prefix.length);
if (managed.has(filename)) {
changed.add(filename);
}
}
return changed;
};
const updateDirectoryInfoForChangedScripts = () => {
const existing = JSON.parse(fs.readFileSync(directoryInfoPath, "utf8"));
const changed = getChangedManagedFiles();
const headEntries = getHeadDirectoryInfo();
// Exported repository ZIPs do not contain .git. In that environment the
// historical mtimes are intentionally left completely untouched.
if (changed === null || headEntries === null || changed.size === 0) {
return null;
}
const headByName = new Map(headEntries.map((entry) => [entry.fname, entry.mtime]));
const currentByName = new Map(existing.map((entry) => [entry.fname, entry]));
let nextMtime = Date.now();
for (const filename of changed) {
const localPath = path.join(scriptsDir, filename);
if (!fs.existsSync(localPath)) {
continue;
}
const current = currentByName.get(filename);
const headMtime = headByName.get(filename);
// If this PR already changed the mtime, keep it. This makes repeated
// script-store:build runs idempotent and prevents timestamp churn.
if (current && headMtime !== undefined && current.mtime !== headMtime) {
continue;
}
const mtime = Math.max(nextMtime++, (current?.mtime ?? headMtime ?? 0) + 1);
if (current) {
current.mtime = mtime;
} else {
const entry = { fname: filename, mtime };
existing.push(entry);
currentByName.set(filename, entry);
}
}
return existing;
};
validateCatalog();
if (checkOnly) {
console.log(
`[script-store] Catalog OK: ${catalog.scripts.length} scripts in ${catalog.categories.length} categories.`,
);
process.exit(0);
}
const legacyIndex = generateLegacyIndex();
if (legacyIndex !== fs.readFileSync(legacyIndexPath, "utf8")) {
fs.writeFileSync(legacyIndexPath, legacyIndex);
}
const directoryInfo = updateDirectoryInfoForChangedScripts();
if (directoryInfo) {
const directoryInfoSource = `${JSON.stringify(directoryInfo, null, 2)}\n`;
if (directoryInfoSource !== fs.readFileSync(directoryInfoPath, "utf8")) {
fs.writeFileSync(directoryInfoPath, directoryInfoSource);
}
}
console.log(
`[script-store] Generated legacy catalog metadata for ${catalog.scripts.length} scripts.`,
);