@tbox.cn/app-build 1.0.0-alpha.6 → 1.0.0-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +53 -1
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -605,8 +605,9 @@ async function scanApp(root) {
605
605
 
606
606
  // src/generate.ts
607
607
  import path8 from "path";
608
- import { existsSync as existsSync5 } from "fs";
608
+ import { existsSync as existsSync5, readdirSync } from "fs";
609
609
  import { watch as fsWatch } from "fs";
610
+ import ts2 from "typescript";
610
611
  function pageDocument(route) {
611
612
  if (route === "/") return "index.html";
612
613
  if (route === "/_tbox/card-preview") return "_tbox/card-preview.html";
@@ -692,6 +693,55 @@ function buildServerRegistry(genDir, scan) {
692
693
  lines.push("");
693
694
  return lines.join("\n");
694
695
  }
696
+ async function collectObservedCardTypes(scan) {
697
+ const files = [...scan.tools.map((tool) => tool.file), ...scan.actions.map((action) => action.file)];
698
+ for (const mod of scan.modules) {
699
+ for (const sub of ["src/server", "src/client"]) {
700
+ const dir = path8.join(mod.dir, sub);
701
+ if (!existsSync5(dir)) continue;
702
+ for (const entry of readdirSync(dir, { recursive: true, withFileTypes: true })) {
703
+ if (!entry.isFile() || !/\.(ts|tsx)$/.test(entry.name)) continue;
704
+ files.push(path8.join(entry.parentPath ?? dir, entry.name));
705
+ }
706
+ }
707
+ }
708
+ const observed = /* @__PURE__ */ new Set();
709
+ for (const file of files) {
710
+ let text;
711
+ try {
712
+ text = await readText(file);
713
+ } catch {
714
+ continue;
715
+ }
716
+ const source = parseSource(file, text);
717
+ const visit = (node) => {
718
+ if (ts2.isPropertyAssignment(node) && propertyName(node.name) === "cardType") {
719
+ const init = node.initializer;
720
+ if (ts2.isStringLiteral(init) || ts2.isNoSubstitutionTemplateLiteral(init)) observed.add(init.text);
721
+ }
722
+ ts2.forEachChild(node, visit);
723
+ };
724
+ visit(source);
725
+ }
726
+ return [...observed].sort();
727
+ }
728
+ function buildAppRegistry(scan, observed) {
729
+ const declared = [...new Set(scan.cards.map((card) => card.cardType))].sort();
730
+ const union = declared.length > 0 ? declared.map((type) => `'${type}'`).join(" | ") : "string";
731
+ const lines = [
732
+ "// @generated by @tbox.cn/app-build; do not edit.",
733
+ "",
734
+ `export type AppCardType = ${union};`,
735
+ ""
736
+ ];
737
+ if (observed.length > 0) {
738
+ lines.push("// C1:源码里出现的 cardType 字面量。任何一项不在 AppCardType 里,类型检查会报错并列出合法取值。");
739
+ lines.push(`const observedCardTypes: AppCardType[] = [${observed.map((type) => `'${type}'`).join(", ")}];`);
740
+ lines.push("void observedCardTypes;");
741
+ lines.push("");
742
+ }
743
+ return lines.join("\n");
744
+ }
695
745
  function buildClientRegistry(genDir, scan) {
696
746
  const file = path8.join(genDir, "registry.client.ts");
697
747
  const lines = ["// @generated by @tbox.cn/app-build; do not edit."];
@@ -763,6 +813,8 @@ async function generateCore(root, shell, scan) {
763
813
  await writeTextIfChanged(file, text);
764
814
  files.push(file);
765
815
  };
816
+ const observedCardTypes = await collectObservedCardTypes(scan);
817
+ await write(path8.join(genDir, "app-registry.ts"), buildAppRegistry(scan, observedCardTypes));
766
818
  await write(path8.join(genDir, "registry.server.ts"), buildServerRegistry(genDir, scan));
767
819
  await write(path8.join(genDir, "registry.client.ts"), buildClientRegistry(genDir, scan));
768
820
  const writeDocuments = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tbox.cn/app-build",
3
- "version": "1.0.0-alpha.6",
3
+ "version": "1.0.0-alpha.7",
4
4
  "type": "module",
5
5
  "description": "tbox-app 1.0 build-time leaf: scan apps/ modules/ and generate manifest, registries, documents; Vite plugin and checks.",
6
6
  "exports": {