包文件→ .shtukbase/packages/lenses/kit/bin/shtukbase-lenses.js
kit/bin/shtukbase-lenses.js
96 行, 4.1 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- #!/usr/bin/env node
- // shtukbase-lenses - HTML lenses over a Shtukbase base. Works under Bun and Node 20+.
- import fs from "node:fs";
- import path from "node:path";
- import { build, renderOutputs, OUT_DIR, StyleMissing } from "../src/build.js";
- import { exportLens } from "../src/export.js";
- const HELP = `shtukbase-lenses - HTML lenses over a Shtukbase base
- build [--root <base>] build ${OUT_DIR}/ (data, kit, gallery)
- check [--root <base>] exit code 1 if ${OUT_DIR}/ is stale or missing
- watch [--root <base>] rebuild when .md files and lenses change
- list [--root <base>] the lenses of the base: path, template, title
- export <lens.html> [-o <file>] [--root <base>]
- export a lens as one file (with the data inside)
- hook [--root <base>] print a git hook post-checkout/post-merge for the build
- The default base is the current folder if it has index.md, otherwise ./shtuks.
- A lens is an HTML file in a layer of the base; its settings are YAML in the first HTML comment (type: Lens).`;
- function args(argv) {
- const o = { _: [] };
- for (let i = 0; i < argv.length; i++) {
- const a = argv[i];
- if (a === "--root") o.root = argv[++i];
- else if (a === "-o" || a === "--out") o.out = argv[++i];
- else if (a === "-q" || a === "--quiet") o.quiet = true;
- else if (a === "-h" || a === "--help") o.help = true;
- else o._.push(a);
- }
- return o;
- }
- function findRoot(given) {
- if (given) return path.resolve(given);
- if (fs.existsSync("index.md")) return process.cwd();
- if (fs.existsSync("shtuks/index.md")) return path.resolve("shtuks");
- return process.cwd();
- }
- // No kit-style: one clear line instead of a trace.
- process.on("uncaughtException", (e) => {
- if (e instanceof StyleMissing) { console.error(e.message); process.exit(2); }
- console.error(e && e.stack ? e.stack : e);
- process.exit(1);
- });
- const o = args(process.argv.slice(2));
- const cmd = o._.shift();
- if (!cmd || o.help) { console.log(HELP); process.exit(cmd ? 0 : 1); }
- const root = findRoot(o.root);
- if (!fs.existsSync(path.join(root, "index.md"))) {
- console.error(`Not a Shtukbase base (no index.md): ${root}`);
- process.exit(2);
- }
- if (cmd === "build") {
- build(root, { quiet: o.quiet });
- } else if (cmd === "check") {
- const { files } = renderOutputs(root);
- const stale = Object.entries(files).filter(([n, c]) => {
- const p = path.join(root, OUT_DIR, n);
- return !fs.existsSync(p) || fs.readFileSync(p, "utf8") !== c;
- }).map(([n]) => n);
- if (stale.length) { console.error(`Lenses are stale: ${stale.join(", ")}. Run: shtukbase-lenses build`); process.exit(1); }
- console.log("Lenses are fresh.");
- } else if (cmd === "list") {
- const { data } = renderOutputs(root);
- for (const l of data.lenses) console.log(`${l.path}\t${l.template}\t${l.title}`);
- } else if (cmd === "export") {
- const lens = o._[0];
- if (!lens) { console.error("Name a lens: shtukbase-lenses export tech/architecture.html"); process.exit(1); }
- const absLens = path.resolve(fs.existsSync(lens) ? lens : path.join(root, lens));
- const relLens = path.relative(root, absLens);
- const out = o.out || path.basename(absLens, ".html") + ".standalone.html";
- const { size } = exportLens(root, relLens, out);
- console.log(`${out}: ${Math.round(size / 1024)} KB`);
- } else if (cmd === "watch") {
- build(root, { quiet: o.quiet });
- let t = null;
- fs.watch(root, { recursive: true }, (_, f) => {
- if (!f || f.startsWith(OUT_DIR) || f.includes(".shtukbase") || f.startsWith(".git")) return;
- if (!/\.(md|html)$/.test(f)) return;
- clearTimeout(t);
- t = setTimeout(() => { try { build(root, { quiet: o.quiet }); } catch (e) { console.error(e.message); } }, 300);
- });
- console.log("Watching for changes...");
- } else if (cmd === "hook") {
- const rel = path.relative(process.cwd(), root) || ".";
- console.log(`#!/bin/sh
- # Rebuilds the Shtukbase HTML lenses after checkout/merge. Install as .githooks/post-checkout and post-merge.
- command -v shtukbase-lenses >/dev/null 2>&1 && shtukbase-lenses build --root "${rel}" -q || true`);
- } else {
- console.error(`Unknown command: ${cmd}\n\n${HELP}`);
- process.exit(1);
- }