56 lines
2.1 KiB
JavaScript
56 lines
2.1 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import test from "node:test";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../..");
|
|
const templateDirectories = fs
|
|
.readdirSync(path.join(root, "packages"))
|
|
.filter(
|
|
(directory) =>
|
|
directory.startsWith("template-") &&
|
|
fs.existsSync(path.join(root, "packages", directory, "template.json")),
|
|
);
|
|
|
|
for (const directory of templateDirectories) {
|
|
const manifest = JSON.parse(
|
|
fs.readFileSync(path.join(root, "packages", directory, "template.json"), "utf8"),
|
|
);
|
|
const rendererPath = path.join(root, "packages", directory, "taro/index.tsx");
|
|
|
|
test(`${manifest.packageKey} preview targets match manifest and renderer`, () => {
|
|
const sections = manifest.admin.sections;
|
|
const targets = sections.flatMap((section) =>
|
|
(section.previewTargets ?? []).map((target) => ({ ...target, page: section.page, section })),
|
|
);
|
|
const keys = targets.map((target) => target.key);
|
|
assert.equal(new Set(keys).size, keys.length, "preview target keys must be unique");
|
|
|
|
for (const { section } of targets) {
|
|
for (const field of section.fields) {
|
|
if (field.previewTarget)
|
|
assert.ok(
|
|
section.previewTargets.some((target) => target.key === field.previewTarget),
|
|
`${section.key}.${field.key} references an undeclared previewTarget`,
|
|
);
|
|
}
|
|
}
|
|
|
|
for (const target of targets)
|
|
assert.ok(
|
|
target.section.fields.some((field) => field.previewTarget === target.key),
|
|
`${target.key} must map to at least one editable field`,
|
|
);
|
|
|
|
if (!targets.length) return;
|
|
assert.ok(fs.existsSync(rendererPath), "templates with preview targets need a Taro renderer");
|
|
const renderer = fs.readFileSync(rendererPath, "utf8");
|
|
for (const target of targets)
|
|
assert.ok(
|
|
renderer.includes(`data-editor-target="${target.key}"`),
|
|
`renderer is missing data-editor-target ${target.key}`,
|
|
);
|
|
});
|
|
}
|