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

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.
package/dist/index.d.ts CHANGED
@@ -62,9 +62,12 @@ interface ActionModule {
62
62
  interface CardModule {
63
63
  cardType: string;
64
64
  serverFile: string;
65
- clientFile: string;
65
+ /** 视图卡没有客户端文件:客户端 stub 由 generate 派生 */
66
+ clientFile: string | null;
67
+ clientRelativeFile: string | null;
68
+ /** true = 服务端 meta 声明了 view(免成对文件) */
69
+ view: boolean;
66
70
  serverRelativeFile: string;
67
- clientRelativeFile: string;
68
71
  }
69
72
  interface ModuleDescriptor {
70
73
  schemaVersion: 1;
package/dist/index.js CHANGED
@@ -130,8 +130,11 @@ function propertyName(node) {
130
130
  }
131
131
  function getProperty(obj, name) {
132
132
  for (const prop of obj.properties) {
133
- if (!ts.isPropertyAssignment(prop)) continue;
134
- if (propertyName(prop.name) === name) return prop.initializer;
133
+ if (ts.isPropertyAssignment(prop)) {
134
+ if (propertyName(prop.name) === name) return prop.initializer;
135
+ continue;
136
+ }
137
+ if (ts.isShorthandPropertyAssignment(prop) && prop.name.text === name) return prop.name;
135
138
  }
136
139
  return void 0;
137
140
  }
@@ -344,26 +347,35 @@ async function scanCardDirs(serverDir, clientDir) {
344
347
  for (const base of [...bases].sort()) {
345
348
  const serverFile = server.get(base);
346
349
  const clientFile = client.get(base);
347
- if (!serverFile || !clientFile) {
348
- throw new AppBuildError("CARD_PAIR_MISSING", `卡片 ${base} 必须在 server/client 双端成对存在`, {
349
- file: serverFile ?? clientFile
350
- });
350
+ if (!serverFile) {
351
+ throw new AppBuildError("CARD_PAIR_MISSING", "卡片 " + base + " 只有客户端文件,缺少服务端声明", { file: clientFile });
351
352
  }
352
353
  const { expr: serverExpr } = await readExported(serverFile, "card");
354
+ const cardType = stringProperty(serverExpr, "cardType", "card (" + base + ")");
355
+ if (!/^[a-z0-9][a-z0-9._-]*$/i.test(cardType) || cardType.includes("..")) {
356
+ throw new AppBuildError("CARD_TYPE_INVALID", "卡片类型只允许字母/数字/点/下划线/中划线:" + cardType, { file: serverFile });
357
+ }
358
+ const serverRelativeFile = path5.relative(serverDir, serverFile).split(path5.sep).join("/");
359
+ if (!clientFile) {
360
+ const obj = objectFromExpression(serverExpr, "card (" + base + ")");
361
+ if (!getProperty(obj, "view")) {
362
+ throw new AppBuildError("CARD_PAIR_MISSING", "卡片 " + base + " 必须在 server/client 双端成对存在(或用 defineViewCard 声明视图卡)", { file: serverFile });
363
+ }
364
+ out.push({ cardType, serverFile, clientFile: null, clientRelativeFile: null, view: true, serverRelativeFile });
365
+ continue;
366
+ }
353
367
  const { expr: clientExpr } = await readExported(clientFile, "card");
354
- const cardType = stringProperty(serverExpr, "cardType", `card (${base})`);
355
- const clientType = stringProperty(clientExpr, "type", `card (${base})`);
368
+ const clientType = stringProperty(clientExpr, "type", "card (" + base + ")");
356
369
  if (cardType !== clientType) {
357
- throw new AppBuildError("CARD_TYPE_MISMATCH", `卡片 ${base} 双端类型不一致: ${cardType} !== ${clientType}`, {
358
- file: clientFile
359
- });
370
+ throw new AppBuildError("CARD_TYPE_MISMATCH", "卡片 " + base + " 双端类型不一致: " + cardType + " !== " + clientType, { file: clientFile });
360
371
  }
361
372
  out.push({
362
373
  cardType,
363
374
  serverFile,
364
375
  clientFile,
365
- serverRelativeFile: path5.relative(serverDir, serverFile).split(path5.sep).join("/"),
366
- clientRelativeFile: path5.relative(clientDir, clientFile).split(path5.sep).join("/")
376
+ clientRelativeFile: path5.relative(clientDir, clientFile).split(path5.sep).join("/"),
377
+ view: false,
378
+ serverRelativeFile
367
379
  });
368
380
  }
369
381
  return out;
@@ -742,11 +754,27 @@ function buildAppRegistry(scan, observed) {
742
754
  }
743
755
  return lines.join("\n");
744
756
  }
757
+ function buildViewCardStub(cardType) {
758
+ return [
759
+ "// @generated by @tbox.cn/app-build; do not edit.",
760
+ "import type { CardView } from '@tbox.cn/app-contracts';",
761
+ "import { ViewCard, defineCard } from '@tbox.cn/app-agent-ui-kit';",
762
+ "",
763
+ "function View(props: { data: unknown; cardId: string; isHistory: boolean; actionTokens?: Record<string, string> }) {",
764
+ " return (",
765
+ " <ViewCard view={props.data as CardView} cardId={props.cardId} isHistory={props.isHistory} actionTokens={props.actionTokens} />",
766
+ " );",
767
+ "}",
768
+ "",
769
+ "export const card = defineCard({ type: '" + cardType + "', actions: 'self' }, View);",
770
+ ""
771
+ ].join("\n");
772
+ }
745
773
  function buildClientRegistry(genDir, scan) {
746
774
  const file = path8.join(genDir, "registry.client.ts");
747
775
  const lines = ["// @generated by @tbox.cn/app-build; do not edit."];
748
776
  const clientModules = scan.modules.filter((mod) => mod.hasClient);
749
- scan.cards.forEach((card, i) => lines.push(`import { card as card${i} } from '${relativePosix(file, card.clientFile)}';`));
777
+ scan.cards.forEach((card, i) => lines.push(`import { card as card${i} } from '${relativePosix(file, card.view ? path8.join(genDir, "cards", card.cardType + ".tsx") : card.clientFile)}';`));
750
778
  clientModules.forEach((mod, i) => lines.push(`import { clientModule as module${i} } from '${relativePosix(file, path8.join(mod.dir, "src/client/index.ts"))}';`));
751
779
  lines.push("");
752
780
  lines.push("export const cards = {");
@@ -816,6 +844,16 @@ async function generateCore(root, shell, scan) {
816
844
  const observedCardTypes = await collectObservedCardTypes(scan);
817
845
  await write(path8.join(genDir, "app-registry.ts"), buildAppRegistry(scan, observedCardTypes));
818
846
  await write(path8.join(genDir, "registry.server.ts"), buildServerRegistry(genDir, scan));
847
+ const cardsDir = path8.join(genDir, "cards");
848
+ const keepStubs = new Set(scan.cards.filter((item) => item.view).map((item) => item.cardType + ".tsx"));
849
+ if (existsSync5(cardsDir)) {
850
+ for (const name of readdirSync(cardsDir)) {
851
+ if (!keepStubs.has(name)) await removeFile(path8.join(cardsDir, name));
852
+ }
853
+ }
854
+ for (const card of scan.cards.filter((item) => item.view)) {
855
+ await write(path8.join(cardsDir, card.cardType + ".tsx"), buildViewCardStub(card.cardType));
856
+ }
819
857
  await write(path8.join(genDir, "registry.client.ts"), buildClientRegistry(genDir, scan));
820
858
  const writeDocuments = async () => {
821
859
  for (const page of scan.pages) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tbox.cn/app-build",
3
- "version": "1.0.0-alpha.7",
3
+ "version": "1.0.0-alpha.8",
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": {