89 lines
2.7 KiB
JavaScript
89 lines
2.7 KiB
JavaScript
// Optional audit of cached documentation data against the pinned, read-only source.
|
|
// Not part of build/runtime; no React is imported or executed.
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import assert from "node:assert/strict";
|
|
import { execFileSync } from "node:child_process";
|
|
import ts from "typescript";
|
|
const root = path.resolve(import.meta.dirname, "..");
|
|
const option = process.argv.indexOf("--reference");
|
|
const reference = path.resolve(
|
|
root,
|
|
option < 0 ? "../heroui" : process.argv[option + 1],
|
|
);
|
|
const raw = execFileSync(
|
|
"git",
|
|
[
|
|
"show",
|
|
"ce90ea3ab49d58f6c428f71bfd6fe8ae2a87b960:apps/docs/content/docs/cn/react/getting-started/(handbook)/colors.mdx",
|
|
],
|
|
{ cwd: reference, encoding: "utf8" },
|
|
);
|
|
const tags = [...raw.matchAll(/<ColorSection\w+\b[\s\S]*?\/>/g)].map(
|
|
(m) => m[0],
|
|
);
|
|
function literal(node) {
|
|
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
|
|
return node.text;
|
|
if (node.kind === ts.SyntaxKind.TrueKeyword) return true;
|
|
if (node.kind === ts.SyntaxKind.FalseKeyword) return false;
|
|
if (ts.isArrayLiteralExpression(node)) return node.elements.map(literal);
|
|
if (ts.isObjectLiteralExpression(node))
|
|
return Object.fromEntries(
|
|
node.properties.map((p) => {
|
|
assert(ts.isPropertyAssignment(p), "Expected static data property");
|
|
return [p.name.text, literal(p.initializer)];
|
|
}),
|
|
);
|
|
throw Error("Non-literal source: " + ts.SyntaxKind[node.kind]);
|
|
}
|
|
const source = ts.createSourceFile(
|
|
"source.tsx",
|
|
"const sections = <>" + tags.join("\n") + "</>;",
|
|
ts.ScriptTarget.Latest,
|
|
true,
|
|
ts.ScriptKind.TSX,
|
|
);
|
|
const original = [];
|
|
function collect(node) {
|
|
if (ts.isJsxSelfClosingElement(node))
|
|
original.push({
|
|
kind: node.tagName.getText(source),
|
|
...Object.fromEntries(
|
|
node.attributes.properties.map((p) => [
|
|
p.name.text,
|
|
ts.isStringLiteral(p.initializer)
|
|
? p.initializer.text
|
|
: literal(p.initializer.expression),
|
|
]),
|
|
),
|
|
});
|
|
ts.forEachChild(node, collect);
|
|
}
|
|
collect(source);
|
|
const cachedSource = ts.createSourceFile(
|
|
"cached.ts",
|
|
fs.readFileSync(
|
|
path.join(root, "apps/docs/.vitepress/theme/color-sections.ts"),
|
|
"utf8",
|
|
),
|
|
ts.ScriptTarget.Latest,
|
|
true,
|
|
);
|
|
let cached;
|
|
function find(node) {
|
|
if (
|
|
ts.isVariableDeclaration(node) &&
|
|
node.name.getText(cachedSource) === "colorSections"
|
|
)
|
|
cached = literal(node.initializer);
|
|
ts.forEachChild(node, find);
|
|
}
|
|
find(cachedSource);
|
|
assert(cached);
|
|
assert.equal(original.length, 12);
|
|
assert.deepEqual(Object.values(cached), original);
|
|
console.log(
|
|
"12/12 color sections match pinned source: order, layout, labels, variables, explicit formulas and tooltip source data.",
|
|
);
|