包文件→ .shtukbase/packages/lenses/actions/packages.mjs
actions/packages.mjs
108 行, 5.5 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // The data of the service lenses "Project packages" and "Project checks": what is installed in the project,
- // read from .shtukbase/packages.lock and the package.md of each installed package, plus the norm pages each
- // package laid. Written to lib/base/packages.data.js as `window.SHTUK_PACKAGES`; the output has no clock in it,
- // so the `check` action can compare it byte for byte.
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
- import { createRequire } from "node:module";
- import { basename, dirname, join } from "node:path";
- import { OWN_KIT } from "./kit.mjs";
- const yaml = createRequire(import.meta.url)(join(OWN_KIT, "vendor", "js-yaml.min.cjs"));
- export const DATA_FILE = join("lib", "base", "packages.data.js");
- const LOCK = join(".shtukbase", "packages.lock");
- /** Page types that are norms; a waiver is not a norm, it is a departure from one. */
- const NORM_TYPES = new Set(["Rule", "Principle", "Default", "Value", "Tenet"]);
- const load = (text) => { try { return yaml.load(text, { schema: yaml.CORE_SCHEMA }) ?? {}; } catch { return null; } };
- const list = (v) => (Array.isArray(v) ? v : v == null || v === "" ? [] : [v]);
- const str = (v) => (v == null ? "" : String(v));
- /** The front matter of a markdown text, or null. */
- function head(text) {
- const m = /^---\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/.exec(text);
- return m ? load(m[1]) : null;
- }
- /** `{id: range}` from either a map or a list of `{package, version}`. */
- function ranges(v) {
- if (Array.isArray(v)) return Object.fromEntries(v.filter((x) => x && x.package).map((x) => [String(x.package), str(x.version)]));
- return v && typeof v === "object" ? Object.fromEntries(Object.entries(v).map(([k, r]) => [k, str(r)])) : {};
- }
- function norm(root, rel, state) {
- const file = join(root, rel);
- if (!existsSync(file)) return null;
- const fm = head(readFileSync(file, "utf8"));
- if (!fm || !NORM_TYPES.has(str(fm.type))) return null;
- return {
- path: "/" + rel, type: str(fm.type),
- id: str(fm.ruleId || fm.normId || fm.id || basename(rel, ".md")),
- title: str(fm.title || fm.requirement), level: str(fm.level), floor: fm.floor === true || fm.floor === "true",
- status: str(fm.status), mode: str(fm.mode),
- // the field is `enforcers`; `heldBy` is its old name, read while packages move over
- enforcers: list(fm.enforcers ?? fm.heldBy).map(str), state,
- };
- }
- /** Everything the lenses show, from the project in `root`; null when the project has no lock. */
- export function packagesData(root) {
- const lockFile = join(root, LOCK);
- if (!existsSync(lockFile)) return null;
- const lock = load(readFileSync(lockFile, "utf8")) || {};
- const packages = [];
- for (const [id, e] of Object.entries(lock.packages || {}).sort(([a], [b]) => (a < b ? -1 : 1))) {
- const mfFile = join(root, ".shtukbase", "packages", id, "package.md");
- const mf = existsSync(mfFile) ? head(readFileSync(mfFile, "utf8")) || {} : {};
- const pages = Object.entries(e.pages || {}).map(([rel, p]) => ({ path: "/" + rel, state: str(p && p.state) || "clean" }))
- .sort((a, b) => (a.path < b.path ? -1 : 1));
- packages.push({
- id, version: str(e.version || mf.version), kind: str(e.kind || "package"),
- title: str(mf.title || id), description: str(mf.description),
- icon: iconOf(root, id, mf.icon),
- source: str(e.source), sha256: str(e.sha256).slice(0, 12), installed: str(e.installed),
- manifest: existsSync(mfFile),
- requires: ranges(mf.requires ?? e.requires), includes: ranges(mf.includes),
- actions: list(mf.actions).filter((a) => a && a.name).map((a) => ({
- name: str(a.name), run: str(a.run), check: a.check === true, norm: a.norm ? str(a.norm) : "", title: str(a.title),
- })),
- pages,
- norms: pages.map((p) => norm(root, p.path.slice(1), p.state)).filter(Boolean),
- });
- }
- return { project: str(lock.project), name: str(lock.name || lock.project), lockVersion: str(lock.lockVersion), packages };
- }
- /** The package's icon as a path from the project root: its `icon:` file (an .svg next to package.md), else "". */
- function iconOf(root, id, icon) {
- const f = typeof icon === "string" && /^[\w.-]+\.svg$/.test(icon) ? icon : "";
- return f && existsSync(join(root, ".shtukbase", "packages", id, f)) ? "/.shtukbase/packages/" + id + "/" + f : "";
- }
- /** The text of lib/base/packages.data.js for the project, or null when it has no lock. */
- export function packagesFile(root) {
- const data = packagesData(root);
- if (!data) return null;
- const json = JSON.stringify(data, null, 1).replace(/<\/(script)/gi, "<\\/$1");
- return "// Built by the build action of the lenses package from .shtukbase/packages.lock and the packages' package.md. Do not edit by hand.\n"
- + "window.SHTUK_PACKAGES = " + json + ";\n";
- }
- /** Writes the data file; returns a line for the person, or null when there is nothing to write. */
- export function writePackages(root) {
- const text = packagesFile(root);
- if (text === null) return null;
- const file = join(root, DATA_FILE);
- mkdirSync(dirname(file), { recursive: true });
- const same = existsSync(file) && readFileSync(file, "utf8") === text;
- if (!same) writeFileSync(file, text);
- return `Project packages → ${DATA_FILE}` + (same ? " (unchanged)" : "");
- }
- /** Null when the data file is fresh (or the project has no lock), else what is wrong. */
- export function stalePackages(root) {
- const text = packagesFile(root);
- if (text === null) return null;
- const file = join(root, DATA_FILE);
- if (!existsSync(file)) return `no ${DATA_FILE}`;
- return readFileSync(file, "utf8") === text ? null : `${DATA_FILE} is stale`;
- }