@tbox.cn/app-build 1.0.0-alpha.6 → 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 +5 -2
- package/dist/index.js +105 -15
- package/package.json +1 -1
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
|
-
|
|
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 (
|
|
134
|
-
|
|
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
|
|
348
|
-
throw new AppBuildError("CARD_PAIR_MISSING",
|
|
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
|
|
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",
|
|
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
|
-
|
|
366
|
-
|
|
376
|
+
clientRelativeFile: path5.relative(clientDir, clientFile).split(path5.sep).join("/"),
|
|
377
|
+
view: false,
|
|
378
|
+
serverRelativeFile
|
|
367
379
|
});
|
|
368
380
|
}
|
|
369
381
|
return out;
|
|
@@ -605,8 +617,9 @@ async function scanApp(root) {
|
|
|
605
617
|
|
|
606
618
|
// src/generate.ts
|
|
607
619
|
import path8 from "path";
|
|
608
|
-
import { existsSync as existsSync5 } from "fs";
|
|
620
|
+
import { existsSync as existsSync5, readdirSync } from "fs";
|
|
609
621
|
import { watch as fsWatch } from "fs";
|
|
622
|
+
import ts2 from "typescript";
|
|
610
623
|
function pageDocument(route) {
|
|
611
624
|
if (route === "/") return "index.html";
|
|
612
625
|
if (route === "/_tbox/card-preview") return "_tbox/card-preview.html";
|
|
@@ -692,11 +705,76 @@ function buildServerRegistry(genDir, scan) {
|
|
|
692
705
|
lines.push("");
|
|
693
706
|
return lines.join("\n");
|
|
694
707
|
}
|
|
708
|
+
async function collectObservedCardTypes(scan) {
|
|
709
|
+
const files = [...scan.tools.map((tool) => tool.file), ...scan.actions.map((action) => action.file)];
|
|
710
|
+
for (const mod of scan.modules) {
|
|
711
|
+
for (const sub of ["src/server", "src/client"]) {
|
|
712
|
+
const dir = path8.join(mod.dir, sub);
|
|
713
|
+
if (!existsSync5(dir)) continue;
|
|
714
|
+
for (const entry of readdirSync(dir, { recursive: true, withFileTypes: true })) {
|
|
715
|
+
if (!entry.isFile() || !/\.(ts|tsx)$/.test(entry.name)) continue;
|
|
716
|
+
files.push(path8.join(entry.parentPath ?? dir, entry.name));
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
const observed = /* @__PURE__ */ new Set();
|
|
721
|
+
for (const file of files) {
|
|
722
|
+
let text;
|
|
723
|
+
try {
|
|
724
|
+
text = await readText(file);
|
|
725
|
+
} catch {
|
|
726
|
+
continue;
|
|
727
|
+
}
|
|
728
|
+
const source = parseSource(file, text);
|
|
729
|
+
const visit = (node) => {
|
|
730
|
+
if (ts2.isPropertyAssignment(node) && propertyName(node.name) === "cardType") {
|
|
731
|
+
const init = node.initializer;
|
|
732
|
+
if (ts2.isStringLiteral(init) || ts2.isNoSubstitutionTemplateLiteral(init)) observed.add(init.text);
|
|
733
|
+
}
|
|
734
|
+
ts2.forEachChild(node, visit);
|
|
735
|
+
};
|
|
736
|
+
visit(source);
|
|
737
|
+
}
|
|
738
|
+
return [...observed].sort();
|
|
739
|
+
}
|
|
740
|
+
function buildAppRegistry(scan, observed) {
|
|
741
|
+
const declared = [...new Set(scan.cards.map((card) => card.cardType))].sort();
|
|
742
|
+
const union = declared.length > 0 ? declared.map((type) => `'${type}'`).join(" | ") : "string";
|
|
743
|
+
const lines = [
|
|
744
|
+
"// @generated by @tbox.cn/app-build; do not edit.",
|
|
745
|
+
"",
|
|
746
|
+
`export type AppCardType = ${union};`,
|
|
747
|
+
""
|
|
748
|
+
];
|
|
749
|
+
if (observed.length > 0) {
|
|
750
|
+
lines.push("// C1:源码里出现的 cardType 字面量。任何一项不在 AppCardType 里,类型检查会报错并列出合法取值。");
|
|
751
|
+
lines.push(`const observedCardTypes: AppCardType[] = [${observed.map((type) => `'${type}'`).join(", ")}];`);
|
|
752
|
+
lines.push("void observedCardTypes;");
|
|
753
|
+
lines.push("");
|
|
754
|
+
}
|
|
755
|
+
return lines.join("\n");
|
|
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
|
+
}
|
|
695
773
|
function buildClientRegistry(genDir, scan) {
|
|
696
774
|
const file = path8.join(genDir, "registry.client.ts");
|
|
697
775
|
const lines = ["// @generated by @tbox.cn/app-build; do not edit."];
|
|
698
776
|
const clientModules = scan.modules.filter((mod) => mod.hasClient);
|
|
699
|
-
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)}';`));
|
|
700
778
|
clientModules.forEach((mod, i) => lines.push(`import { clientModule as module${i} } from '${relativePosix(file, path8.join(mod.dir, "src/client/index.ts"))}';`));
|
|
701
779
|
lines.push("");
|
|
702
780
|
lines.push("export const cards = {");
|
|
@@ -763,7 +841,19 @@ async function generateCore(root, shell, scan) {
|
|
|
763
841
|
await writeTextIfChanged(file, text);
|
|
764
842
|
files.push(file);
|
|
765
843
|
};
|
|
844
|
+
const observedCardTypes = await collectObservedCardTypes(scan);
|
|
845
|
+
await write(path8.join(genDir, "app-registry.ts"), buildAppRegistry(scan, observedCardTypes));
|
|
766
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
|
+
}
|
|
767
857
|
await write(path8.join(genDir, "registry.client.ts"), buildClientRegistry(genDir, scan));
|
|
768
858
|
const writeDocuments = async () => {
|
|
769
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.
|
|
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": {
|