ShtukbaseStandard
包文件→ .shtukbase/packages/lenses/kit/bin/shtukbase-lenses.js

kit/bin/shtukbase-lenses.js

96 行, 4.1 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. #!/usr/bin/env node
  2. // shtukbase-lenses - HTML lenses over a Shtukbase base. Works under Bun and Node 20+.
  3. import fs from "node:fs";
  4. import path from "node:path";
  5. import { build, renderOutputs, OUT_DIR, StyleMissing } from "../src/build.js";
  6. import { exportLens } from "../src/export.js";
  7. const HELP = `shtukbase-lenses - HTML lenses over a Shtukbase base
  8. build [--root <base>] build ${OUT_DIR}/ (data, kit, gallery)
  9. check [--root <base>] exit code 1 if ${OUT_DIR}/ is stale or missing
  10. watch [--root <base>] rebuild when .md files and lenses change
  11. list [--root <base>] the lenses of the base: path, template, title
  12. export <lens.html> [-o <file>] [--root <base>]
  13. export a lens as one file (with the data inside)
  14. hook [--root <base>] print a git hook post-checkout/post-merge for the build
  15. The default base is the current folder if it has index.md, otherwise ./shtuks.
  16. A lens is an HTML file in a layer of the base; its settings are YAML in the first HTML comment (type: Lens).`;
  17. function args(argv) {
  18. const o = { _: [] };
  19. for (let i = 0; i < argv.length; i++) {
  20. const a = argv[i];
  21. if (a === "--root") o.root = argv[++i];
  22. else if (a === "-o" || a === "--out") o.out = argv[++i];
  23. else if (a === "-q" || a === "--quiet") o.quiet = true;
  24. else if (a === "-h" || a === "--help") o.help = true;
  25. else o._.push(a);
  26. }
  27. return o;
  28. }
  29. function findRoot(given) {
  30. if (given) return path.resolve(given);
  31. if (fs.existsSync("index.md")) return process.cwd();
  32. if (fs.existsSync("shtuks/index.md")) return path.resolve("shtuks");
  33. return process.cwd();
  34. }
  35. // No kit-style: one clear line instead of a trace.
  36. process.on("uncaughtException", (e) => {
  37. if (e instanceof StyleMissing) { console.error(e.message); process.exit(2); }
  38. console.error(e && e.stack ? e.stack : e);
  39. process.exit(1);
  40. });
  41. const o = args(process.argv.slice(2));
  42. const cmd = o._.shift();
  43. if (!cmd || o.help) { console.log(HELP); process.exit(cmd ? 0 : 1); }
  44. const root = findRoot(o.root);
  45. if (!fs.existsSync(path.join(root, "index.md"))) {
  46. console.error(`Not a Shtukbase base (no index.md): ${root}`);
  47. process.exit(2);
  48. }
  49. if (cmd === "build") {
  50. build(root, { quiet: o.quiet });
  51. } else if (cmd === "check") {
  52. const { files } = renderOutputs(root);
  53. const stale = Object.entries(files).filter(([n, c]) => {
  54. const p = path.join(root, OUT_DIR, n);
  55. return !fs.existsSync(p) || fs.readFileSync(p, "utf8") !== c;
  56. }).map(([n]) => n);
  57. if (stale.length) { console.error(`Lenses are stale: ${stale.join(", ")}. Run: shtukbase-lenses build`); process.exit(1); }
  58. console.log("Lenses are fresh.");
  59. } else if (cmd === "list") {
  60. const { data } = renderOutputs(root);
  61. for (const l of data.lenses) console.log(`${l.path}\t${l.template}\t${l.title}`);
  62. } else if (cmd === "export") {
  63. const lens = o._[0];
  64. if (!lens) { console.error("Name a lens: shtukbase-lenses export tech/architecture.html"); process.exit(1); }
  65. const absLens = path.resolve(fs.existsSync(lens) ? lens : path.join(root, lens));
  66. const relLens = path.relative(root, absLens);
  67. const out = o.out || path.basename(absLens, ".html") + ".standalone.html";
  68. const { size } = exportLens(root, relLens, out);
  69. console.log(`${out}: ${Math.round(size / 1024)} KB`);
  70. } else if (cmd === "watch") {
  71. build(root, { quiet: o.quiet });
  72. let t = null;
  73. fs.watch(root, { recursive: true }, (_, f) => {
  74. if (!f || f.startsWith(OUT_DIR) || f.includes(".shtukbase") || f.startsWith(".git")) return;
  75. if (!/\.(md|html)$/.test(f)) return;
  76. clearTimeout(t);
  77. t = setTimeout(() => { try { build(root, { quiet: o.quiet }); } catch (e) { console.error(e.message); } }, 300);
  78. });
  79. console.log("Watching for changes...");
  80. } else if (cmd === "hook") {
  81. const rel = path.relative(process.cwd(), root) || ".";
  82. console.log(`#!/bin/sh
  83. # Rebuilds the Shtukbase HTML lenses after checkout/merge. Install as .githooks/post-checkout and post-merge.
  84. command -v shtukbase-lenses >/dev/null 2>&1 && shtukbase-lenses build --root "${rel}" -q || true`);
  85. } else {
  86. console.error(`Unknown command: ${cmd}\n\n${HELP}`);
  87. process.exit(1);
  88. }