soma-kit 0.1.14 → 0.1.15
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/package.json +3 -3
- package/packs/finance/reconcile-desk/compile.ts +1 -1
- package/packs/finance/reconcile-desk/shell.ts +1 -1
- package/spec/generated/kit-app.schema.json +4 -2
- package/spec/generated/kit-page.schema.json +72 -2
- package/spec/kit-grammar.json +163 -57
- package/spec/kit-grammar.schema.json +0 -24
- package/src/app/app-config.ts +1 -1
- package/src/app/component-gate.ts +2 -2
- package/src/app/compose-apps.ts +5 -2
- package/src/app/define-kit-actions.ts +82 -10
- package/src/app/define-kit-app.ts +6 -5
- package/src/app/define-kit-view.ts +1 -1
- package/src/app/index.ts +7 -7
- package/src/app/kit-view-context.ts +11 -2
- package/src/app/theme-endpoint.ts +1 -1
- package/src/app/theme-runtime.ts +7 -2
- package/src/base/alert.ts +23 -20
- package/src/base/anchored-layer.ts +1 -1
- package/src/base/button.ts +30 -15
- package/src/base/cascader.ts +8 -4
- package/src/base/check-core.ts +9 -6
- package/src/base/color-picker.ts +10 -6
- package/src/base/command-palette.ts +10 -4
- package/src/base/context-menu.ts +5 -2
- package/src/base/date-range.ts +9 -6
- package/src/base/date.ts +10 -6
- package/src/base/delta.ts +4 -2
- package/src/base/divider.ts +15 -66
- package/src/base/fab.ts +11 -6
- package/src/base/gap-value.ts +1 -1
- package/src/base/hint-tips.ts +1 -1
- package/src/base/index.ts +15 -4
- package/src/base/inline-error.ts +84 -0
- package/src/base/input.ts +54 -18
- package/src/base/kbd.ts +4 -3
- package/src/base/link.ts +1 -1
- package/src/base/listbox-core.ts +70 -22
- package/src/base/mentions.ts +6 -3
- package/src/base/menu-button.ts +63 -27
- package/src/base/menu-core.ts +9 -2
- package/src/base/motion/hover-3d.ts +4 -1
- package/src/base/motion/hover-gallery.ts +4 -1
- package/src/base/otp.ts +7 -3
- package/src/base/overlay-runtime.ts +3 -2
- package/src/base/overlay-shell.ts +141 -0
- package/src/base/popover.ts +2 -2
- package/src/base/popup.ts +47 -41
- package/src/base/qty-stepper.ts +14 -9
- package/src/base/reason-tip.ts +2 -2
- package/src/{frames → base}/resize-handle.ts +1 -1
- package/src/base/segmented.ts +35 -36
- package/src/base/select.ts +1 -0
- package/src/base/shared.ts +43 -7
- package/src/base/status-tone.ts +1 -1
- package/src/base/tag-input.ts +13 -8
- package/src/base/tag.ts +53 -59
- package/src/base/text.ts +37 -13
- package/src/base/textarea.ts +1 -1
- package/src/base/time-picker.ts +9 -5
- package/src/base/tree-select.ts +12 -9
- package/src/base/upload.ts +21 -13
- package/src/biz/approval-desk/compile.ts +28 -1
- package/src/biz/approval-desk/shell.ts +2 -2
- package/src/biz/auth-panel/compile.ts +1 -1
- package/src/biz/auth-panel/shell.ts +2 -2
- package/src/biz/category-crud/compile.ts +17 -4
- package/src/biz/category-crud/shell.ts +7 -5
- package/src/biz/checkout-page/compile.ts +1 -1
- package/src/biz/checkout-page/shell.ts +7 -6
- package/src/biz/compose.ts +3 -3
- package/src/biz/define.ts +1 -1
- package/src/biz/import-wizard/compile.ts +1 -1
- package/src/biz/import-wizard/shell.ts +6 -4
- package/src/biz/index.ts +4 -3
- package/src/biz/landing-page/compile.ts +1 -1
- package/src/biz/landing-page/shell.ts +5 -4
- package/src/biz/link/shared.ts +8 -1
- package/src/biz/master-sub/compile.ts +162 -19
- package/src/biz/master-sub/detail-head.ts +7 -6
- package/src/biz/master-sub/index.ts +3 -3
- package/src/biz/master-sub/shell.ts +90 -53
- package/src/biz/multi-board/compile.ts +3 -3
- package/src/biz/multi-board/index.ts +24 -9
- package/src/biz/multi-board/prose-fold.ts +2 -2
- package/src/biz/multi-board/shell.ts +17 -13
- package/src/biz/names.ts +1 -0
- package/src/biz/notification-center/compile.ts +9 -2
- package/src/biz/notification-center/shell.ts +9 -2
- package/src/biz/page-templates/domain-templates.ts +6 -2
- package/src/biz/page-templates/result-page/compile.ts +1 -1
- package/src/biz/page-templates/result-page/shell.ts +4 -3
- package/src/biz/page-templates/shared.ts +1 -1
- package/src/biz/page-templates/sign-in-page/compile.ts +1 -1
- package/src/biz/page-templates/sign-in-page/shell.ts +4 -4
- package/src/biz/record-detail/compile.ts +5 -2
- package/src/biz/record-detail/shell.ts +4 -4
- package/src/biz/settings-center/compile.ts +1 -1
- package/src/biz/settings-center/shell.ts +8 -5
- package/src/biz/shared/action-host.ts +12 -5
- package/src/biz/shared/compile-helpers.ts +4 -4
- package/src/biz/shared/coordinator.ts +104 -21
- package/src/biz/shared/overlay-compile.ts +48 -18
- package/src/biz/shared/overlay-shell.ts +63 -112
- package/src/biz/single-form/compile.ts +2 -2
- package/src/biz/single-form/shell.ts +8 -4
- package/src/biz/standard-crud/compile.ts +11 -4
- package/src/biz/standard-crud/shell.ts +26 -9
- package/src/biz/standard-crud/summary.ts +3 -1
- package/src/biz/step-form/compile.ts +1 -1
- package/src/biz/step-form/hub.ts +4 -0
- package/src/biz/step-form/shell.ts +6 -4
- package/src/biz/types.ts +11 -4
- package/src/cli/app-layer-check.ts +375 -0
- package/src/cli/kit-cli.ts +35 -8
- package/src/cli/kit-init.ts +149 -0
- package/src/compile/access-gate-attach.ts +1 -1
- package/src/compile/action-host-attach.ts +1 -1
- package/src/compile/compile-page.ts +29 -234
- package/src/compile/component-candidates.ts +2 -1
- package/src/compile/hints.ts +5 -58
- package/src/compile/index.ts +2 -2
- package/src/compile/layout-lint/index.ts +4 -1
- package/src/compile/layout-lint/rules-composition.ts +43 -13
- package/src/compile/page-composition.ts +33 -31
- package/src/compile/redirect.ts +1 -1
- package/src/compile/stages.ts +169 -0
- package/src/compile/styles.ts +70 -32
- package/src/compile/types.ts +2 -2
- package/src/data/amount-breakdown.ts +4 -2
- package/src/data/anchor-nav.ts +4 -1
- package/src/data/approval-flow.ts +9 -4
- package/src/data/assignment-picker.ts +28 -18
- package/src/data/async-result-panel.ts +6 -2
- package/src/data/breadcrumbs.ts +13 -9
- package/src/data/calendar.ts +4 -3
- package/src/data/carousel.ts +11 -5
- package/src/data/chart.ts +153 -23
- package/src/data/checklist.ts +5 -4
- package/src/data/code-view.ts +220 -26
- package/src/data/comment-thread.ts +7 -2
- package/src/data/confirm-dialog.ts +56 -13
- package/src/data/diff.ts +4 -1
- package/src/data/dock.ts +5 -1
- package/src/data/embed.ts +1 -1
- package/src/data/engine-ports.ts +126 -0
- package/src/data/field-mapper.ts +12 -6
- package/src/data/flow-editor.ts +105 -29
- package/src/data/footer.ts +6 -2
- package/src/data/gantt.ts +11 -8
- package/src/data/hero.ts +6 -2
- package/src/data/image-viewer.ts +7 -3
- package/src/data/index.ts +12 -5
- package/src/data/item-list.ts +6 -5
- package/src/data/json-view.ts +844 -0
- package/src/data/kanban.ts +11 -8
- package/src/data/markdown-view.ts +487 -0
- package/src/data/matrix.ts +23 -14
- package/src/data/media-grid.ts +5 -2
- package/src/data/mega-menu.ts +4 -3
- package/src/data/menu-dom.ts +9 -8
- package/src/data/menu.ts +15 -9
- package/src/data/mockup.ts +9 -5
- package/src/data/modal-dialog.ts +39 -88
- package/src/data/navbar.ts +11 -7
- package/src/data/notice-list.ts +1 -1
- package/src/data/notify-renderer.ts +21 -10
- package/src/data/object-form.ts +129 -48
- package/src/data/pager.ts +12 -8
- package/src/data/panel-head.ts +1 -1
- package/src/data/permission-matrix.ts +15 -8
- package/src/data/permission-tree.ts +24 -11
- package/src/data/pricing-table.ts +8 -3
- package/src/data/reaction-bar.ts +6 -3
- package/src/data/record-card.ts +13 -9
- package/src/data/record-picker.ts +8 -4
- package/src/data/redirect.ts +1 -1
- package/src/data/request-console.ts +20 -7
- package/src/data/resident.ts +0 -12
- package/src/data/result-panel.ts +2 -2
- package/src/data/rich-text.ts +58 -96
- package/src/data/row-editor.ts +10 -5
- package/src/data/rule-builder.ts +14 -8
- package/src/data/shape-util.ts +14 -3
- package/src/data/stat-group.ts +63 -22
- package/src/data/static-lazy.ts +3 -0
- package/src/data/static-registry.ts +8 -2
- package/src/data/steps.ts +6 -2
- package/src/data/survey.ts +9 -3
- package/src/data/system-switcher.ts +15 -6
- package/src/data/text-diff.ts +663 -0
- package/src/data/timeline.ts +1 -1
- package/src/data/tour-guide.ts +6 -3
- package/src/data/transfer.ts +19 -10
- package/src/data/tree-table.ts +77 -12
- package/src/data/tree.ts +5 -2
- package/src/data/virtual-rows.ts +17 -7
- package/src/data/wrap.ts +2 -2
- package/src/frames/index.ts +3 -2
- package/src/frames/inherited-shell.ts +178 -0
- package/src/frames/list-summary.ts +59 -36
- package/src/frames/page-bar.ts +4 -2
- package/src/frames/resident-components.ts +20 -0
- package/src/frames/shared.ts +18 -14
- package/src/frames/split-frame/shell.ts +17 -11
- package/src/frames/split-frame/wiring.ts +2 -2
- package/src/frames/tabs-frame/shell.ts +12 -6
- package/src/frames/types.ts +3 -2
- package/src/frames/workspace-frame/extras.ts +19 -5
- package/src/frames/workspace-frame/shell.ts +2 -1
- package/src/frames/workspace-frame/state.ts +8 -2
- package/src/frames/workspace-frame/view.ts +125 -76
- package/src/generated/kit-grammar-runtime.ts +61 -39
- package/src/i18n/kit-text.ts +114 -2
- package/src/i18n/resolve-text-refs.ts +1 -1
- package/src/object-renderer/action-group.ts +31 -1
- package/src/object-renderer/action-more.ts +28 -12
- package/src/object-renderer/arrange.ts +37 -7
- package/src/object-renderer/collection-parts.ts +38 -7
- package/src/object-renderer/collection-table.ts +68 -23
- package/src/object-renderer/collection.ts +63 -30
- package/src/object-renderer/compile-data.ts +1 -1
- package/src/object-renderer/defaults.ts +8 -0
- package/src/object-renderer/emit-events.ts +1 -1
- package/src/object-renderer/emit-object.ts +3 -0
- package/src/object-renderer/emit-static.ts +140 -37
- package/src/object-renderer/field-value.ts +12 -239
- package/src/object-renderer/filter-toolbar.ts +103 -0
- package/src/{compile → object-renderer}/fragment.ts +2 -2
- package/src/object-renderer/index.ts +1 -1
- package/src/object-renderer/layout-facts.ts +37 -5
- package/src/object-renderer/overlay-decide.ts +82 -0
- package/src/object-renderer/render-object.ts +1 -1
- package/src/object-renderer/table-actions.ts +70 -23
- package/src/object-renderer/table-tools.ts +12 -5
- package/src/object-renderer/types.ts +9 -15
- package/src/recipes/app-blocks.ts +66 -0
- package/src/recipes/code-view.ts +56 -0
- package/src/recipes/compact-group.ts +31 -0
- package/src/recipes/container.ts +25 -15
- package/src/recipes/control.ts +21 -3
- package/src/recipes/desc-list.ts +14 -0
- package/src/recipes/divider.ts +53 -0
- package/src/recipes/emphasis.ts +12 -8
- package/src/recipes/frame-bleed.ts +3 -3
- package/src/recipes/index.ts +11 -1
- package/src/recipes/inherited-shell.ts +37 -0
- package/src/recipes/json-view.ts +49 -0
- package/src/recipes/markdown.ts +86 -0
- package/src/recipes/overlay.ts +112 -26
- package/src/recipes/page-width.ts +20 -0
- package/src/recipes/placeholder.ts +1 -1
- package/src/recipes/stat-state.ts +1 -1
- package/src/recipes/state.ts +10 -8
- package/src/recipes/status.ts +13 -12
- package/src/recipes/strip.ts +72 -0
- package/src/recipes/surface.ts +10 -6
- package/src/recipes/table-gap.ts +1 -1
- package/src/recipes/table-native-edit.ts +4 -4
- package/src/recipes/table-row.ts +11 -8
- package/src/recipes/tabs.ts +108 -31
- package/src/recipes/text-diff.ts +90 -0
- package/src/recipes/title-row.ts +4 -2
- package/src/recipes/typography.ts +2 -2
- package/src/shared/diagnostics.ts +16 -0
- package/src/shared/envelope.ts +1 -1
- package/src/shared/field-value-format.ts +246 -0
- package/src/shared/focus-ring.ts +8 -0
- package/src/shared/hints.ts +56 -0
- package/src/{base → shared}/icons.ts +12 -0
- package/src/shared/kit-error.ts +1 -1
- package/src/{object-renderer → shared}/merge-class.ts +3 -2
- package/src/shared/path-guard.ts +1 -1
- package/src/shared/primitives-types.ts +8 -0
- package/src/shared/theme-runtime-shared.ts +19 -0
- package/src/theme/kit-theme-text.ts +1 -1
- package/src/theme/kit-theme.css +38 -14
- package/src/theme/kit-theme.tpl +38 -14
- package/src/theme/nine-dim-structure.ts +66 -41
- package/src/theme/surface-layers.ts +11 -3
- package/src/theme/theme-tokens.ts +5 -1
- package/src/app/envelope.ts +0 -1
- package/src/app/notify-renderer.ts +0 -1
- package/src/app/paged-list-adapter.ts +0 -1
- package/src/app/theme-runtime-shared.ts +0 -1
- package/src/compile/kit-error.ts +0 -1
- /package/src/{base → shared}/class-candidates.ts +0 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
// 应用代码双平面与四层分层边界检查(`soma kit check` -> KIT-APP-015,对齐《代码四层分层规范》§5 R-APP-1/2/3):
|
|
2
|
+
// 1. R-APP-1 双面物理隔离:
|
|
3
|
+
// - 客户端面(web/、soma.app.*、views/、actions/、components/、lib/)严禁引用服务端面(bff/、api/、server/、config/、soma.server.*)
|
|
4
|
+
// - 服务端面(bff/、api/、server/)严禁反向引用客户端面(web/、soma.app.*、views/、actions/、components/)
|
|
5
|
+
// - 契约共享面(shared/)严禁引用 web/、bff/、api/、server/、config/、soma.server.* 或 Node.js 内置模块(node:* 等)
|
|
6
|
+
// - soma.server.* 作为服务端动态入口,允许桥接同应用的 soma.app.* 与服务端面,但不得被客户端面反向引用
|
|
7
|
+
// 2. R-APP-2 BFF 四层单向依赖:
|
|
8
|
+
// - foundation/ 严禁反向引用 domain/、application/、api/
|
|
9
|
+
// - domain/(当同级存在 application/ 或 foundation/ 四层目录时)严禁反向引用 application/、api/
|
|
10
|
+
// 3. R-APP-3 页面视图横向隔离:
|
|
11
|
+
// - views/*.view.* 严禁横向直接引用其它 views/*.view.*(跨页共享逻辑须下沉至 actions/、lib/ 或 shared/)
|
|
12
|
+
//
|
|
13
|
+
// 采用 TypeScript AST 解析所有静态 import / import type / export ... from / 动态 import() / require(),
|
|
14
|
+
// 天然忽略注释与普通字符串字面量;跳过 node_modules、tests/、test/、mock/、dist/、build/ 与隐藏目录。
|
|
15
|
+
|
|
16
|
+
import ts from 'typescript';
|
|
17
|
+
import type { KitDiagnostic } from '../object-renderer/types';
|
|
18
|
+
|
|
19
|
+
const SKIP_DIRS = new Set(['node_modules', 'tests', 'test', 'mock', 'mocks', 'dist', 'build', 'coverage', 'scripts', 'prisma']);
|
|
20
|
+
const SOURCE_EXTS = ['.ts', '.tsx', '.mts', '.mjs', '.js', '.jsx', '.cjs'];
|
|
21
|
+
const NODE_BUILTIN_RE =
|
|
22
|
+
/^(?:node:|(?:fs|path|http|https|http2|net|tls|dgram|dns|crypto|child_process|cluster|os|stream|zlib|worker_threads|v8|vm|readline|inspector|async_hooks|perf_hooks)(?:\/|$))/;
|
|
23
|
+
|
|
24
|
+
type Plane = 'web' | 'bff' | 'shared' | 'server-entry' | 'neutral';
|
|
25
|
+
type BffLayer = 'foundation' | 'domain' | 'application' | 'contract';
|
|
26
|
+
|
|
27
|
+
const LAYER_RANK: Readonly<Record<BffLayer, number>> = {
|
|
28
|
+
foundation: 0,
|
|
29
|
+
domain: 1,
|
|
30
|
+
application: 2,
|
|
31
|
+
contract: 3,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
interface SpecRef {
|
|
35
|
+
readonly spec: string;
|
|
36
|
+
readonly line: number;
|
|
37
|
+
readonly column: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface WorkspaceLayout {
|
|
41
|
+
readonly projectRoot: string;
|
|
42
|
+
readonly webRootRel: string | null;
|
|
43
|
+
readonly bffRootRel: string | null;
|
|
44
|
+
readonly hasFourLayerDomain: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const toPosix = (p: string): string => p.split('\\').join('/');
|
|
48
|
+
|
|
49
|
+
function extractModuleSpecifiers(sourceText: string, fileName: string): SpecRef[] {
|
|
50
|
+
const sf = ts.createSourceFile(fileName, sourceText, ts.ScriptTarget.Latest, false);
|
|
51
|
+
const out: SpecRef[] = [];
|
|
52
|
+
const pushNode = (lit: ts.StringLiteralLike): void => {
|
|
53
|
+
const pos = sf.getLineAndCharacterOfPosition(lit.getStart(sf));
|
|
54
|
+
out.push({ spec: lit.text, line: pos.line + 1, column: pos.character + 1 });
|
|
55
|
+
};
|
|
56
|
+
const visit = (node: ts.Node): void => {
|
|
57
|
+
if (ts.isImportDeclaration(node) && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) {
|
|
58
|
+
pushNode(node.moduleSpecifier);
|
|
59
|
+
} else if (ts.isExportDeclaration(node) && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) {
|
|
60
|
+
pushNode(node.moduleSpecifier);
|
|
61
|
+
} else if (
|
|
62
|
+
ts.isImportEqualsDeclaration(node) &&
|
|
63
|
+
ts.isExternalModuleReference(node.moduleReference) &&
|
|
64
|
+
ts.isStringLiteral(node.moduleReference.expression)
|
|
65
|
+
) {
|
|
66
|
+
pushNode(node.moduleReference.expression);
|
|
67
|
+
} else if (ts.isImportTypeNode(node) && ts.isLiteralTypeNode(node.argument) && ts.isStringLiteral(node.argument.literal)) {
|
|
68
|
+
pushNode(node.argument.literal);
|
|
69
|
+
} else if (ts.isCallExpression(node)) {
|
|
70
|
+
const expr = node.expression;
|
|
71
|
+
const isDynImport = expr.kind === ts.SyntaxKind.ImportKeyword;
|
|
72
|
+
const isRequire = ts.isIdentifier(expr) && expr.text === 'require';
|
|
73
|
+
if ((isDynImport || isRequire) && node.arguments.length > 0 && ts.isStringLiteral(node.arguments[0]!)) {
|
|
74
|
+
pushNode(node.arguments[0]!);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
ts.forEachChild(node, visit);
|
|
78
|
+
};
|
|
79
|
+
visit(sf);
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function resolveRelativeTarget(
|
|
84
|
+
fs: typeof import('node:fs'),
|
|
85
|
+
path: typeof import('node:path'),
|
|
86
|
+
fromFull: string,
|
|
87
|
+
spec: string,
|
|
88
|
+
): string | null {
|
|
89
|
+
const raw = path.resolve(path.dirname(fromFull), spec);
|
|
90
|
+
const candidates: string[] = [raw];
|
|
91
|
+
if (raw.endsWith('.js')) candidates.push(`${raw.slice(0, -3)}.ts`, `${raw.slice(0, -3)}.tsx`);
|
|
92
|
+
if (raw.endsWith('.mjs')) candidates.push(`${raw.slice(0, -4)}.mts`);
|
|
93
|
+
if (raw.endsWith('.cjs')) candidates.push(`${raw.slice(0, -4)}.cts`);
|
|
94
|
+
for (const ext of SOURCE_EXTS) candidates.push(`${raw}${ext}`);
|
|
95
|
+
for (const ext of SOURCE_EXTS) candidates.push(path.join(raw, `index${ext}`));
|
|
96
|
+
for (const c of candidates) {
|
|
97
|
+
try {
|
|
98
|
+
if (fs.statSync(c).isFile()) return c;
|
|
99
|
+
} catch {
|
|
100
|
+
// candidate absent
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function isServerEntryFile(relPosix: string): boolean {
|
|
107
|
+
const base = relPosix.split('/').pop() ?? '';
|
|
108
|
+
return /^soma\.server\.[cm]?[jt]s$/.test(base);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function isClientEntryFile(relPosix: string): boolean {
|
|
112
|
+
const base = relPosix.split('/').pop() ?? '';
|
|
113
|
+
return /^soma\.app(?:\.standalone)?\.[cm]?[jt]s$/.test(base);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function isViewFile(relPosix: string): boolean {
|
|
117
|
+
const segs = relPosix.split('/');
|
|
118
|
+
const base = segs[segs.length - 1] ?? '';
|
|
119
|
+
return segs.includes('views') && /\.view\.[cm]?[jt]sx?$/.test(base);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function detectLayout(fs: typeof import('node:fs'), path: typeof import('node:path'), appDir: string): WorkspaceLayout {
|
|
123
|
+
const resolved = path.resolve(appDir);
|
|
124
|
+
const normRel = (root: string, sub: unknown): string | null => {
|
|
125
|
+
if (typeof sub !== 'string' || sub.trim() === '') return null;
|
|
126
|
+
const abs = path.resolve(root, sub.trim());
|
|
127
|
+
const rel = toPosix(path.relative(root, abs));
|
|
128
|
+
return rel === '' || rel.startsWith('..') ? null : rel;
|
|
129
|
+
};
|
|
130
|
+
const hasDir = (p: string): boolean => {
|
|
131
|
+
try {
|
|
132
|
+
return fs.statSync(p).isDirectory();
|
|
133
|
+
} catch {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
let projectRoot = resolved;
|
|
139
|
+
let webRootRel: string | null = null;
|
|
140
|
+
let bffRootRel: string | null = null;
|
|
141
|
+
|
|
142
|
+
// Walk up at most 2 levels so `soma kit check src/web` or `soma kit check src/kit` also sees projectRoot's package.json#soma
|
|
143
|
+
let probe = resolved;
|
|
144
|
+
for (let depth = 0; depth <= 2; depth++) {
|
|
145
|
+
const pkgFile = path.join(probe, 'package.json');
|
|
146
|
+
try {
|
|
147
|
+
const pkg = JSON.parse(fs.readFileSync(pkgFile, 'utf8')) as { soma?: { webRoot?: unknown; apiRoot?: unknown; serviceRoot?: unknown } };
|
|
148
|
+
const wr = normRel(probe, pkg.soma?.webRoot);
|
|
149
|
+
const ar = normRel(probe, pkg.soma?.apiRoot ?? pkg.soma?.serviceRoot);
|
|
150
|
+
if (wr !== null || ar !== null) {
|
|
151
|
+
if (probe === resolved || (wr !== null && path.resolve(probe, wr) === resolved)) {
|
|
152
|
+
projectRoot = probe;
|
|
153
|
+
webRootRel = wr;
|
|
154
|
+
bffRootRel = ar;
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (probe === resolved) break;
|
|
159
|
+
} catch {
|
|
160
|
+
// no package.json at this level
|
|
161
|
+
}
|
|
162
|
+
const parent = path.dirname(probe);
|
|
163
|
+
if (parent === probe) break;
|
|
164
|
+
probe = parent;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (webRootRel === null && hasDir(path.join(projectRoot, 'src', 'web'))) webRootRel = 'src/web';
|
|
168
|
+
if (bffRootRel === null && hasDir(path.join(projectRoot, 'src', 'bff'))) bffRootRel = 'src/bff';
|
|
169
|
+
|
|
170
|
+
const checkFourLayerBase = bffRootRel !== null ? path.join(projectRoot, bffRootRel) : projectRoot;
|
|
171
|
+
const hasFourLayerDomain =
|
|
172
|
+
hasDir(path.join(checkFourLayerBase, 'application')) || hasDir(path.join(checkFourLayerBase, 'foundation'));
|
|
173
|
+
|
|
174
|
+
return { projectRoot, webRootRel, bffRootRel, hasFourLayerDomain };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function classifyPlane(relPosix: string, layout: WorkspaceLayout): Plane {
|
|
178
|
+
if (isServerEntryFile(relPosix)) return 'server-entry';
|
|
179
|
+
if (relPosix.startsWith('src/shared/') || relPosix.startsWith('shared/')) return 'shared';
|
|
180
|
+
|
|
181
|
+
if (layout.webRootRel !== null && (relPosix === layout.webRootRel || relPosix.startsWith(`${layout.webRootRel}/`))) {
|
|
182
|
+
return 'web';
|
|
183
|
+
}
|
|
184
|
+
if (
|
|
185
|
+
layout.bffRootRel !== null &&
|
|
186
|
+
layout.bffRootRel !== 'src' &&
|
|
187
|
+
(relPosix === layout.bffRootRel || relPosix.startsWith(`${layout.bffRootRel}/`))
|
|
188
|
+
) {
|
|
189
|
+
return 'bff';
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Standard directory conventions (applicable to single-root or `apiRoot: "src"` layouts)
|
|
193
|
+
const stripped = relPosix.startsWith('src/') ? relPosix.slice(4) : relPosix;
|
|
194
|
+
if (isClientEntryFile(stripped)) return 'web';
|
|
195
|
+
if (/^(?:web|kit|views|actions|components)\//.test(stripped)) return 'web';
|
|
196
|
+
if (/^(?:bff|api|server|config|foundation|application)\//.test(stripped)) return 'bff';
|
|
197
|
+
return 'neutral';
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function classifyBffLayer(relPosix: string, layout: WorkspaceLayout): BffLayer | null {
|
|
201
|
+
let sub = relPosix;
|
|
202
|
+
if (layout.bffRootRel !== null && sub.startsWith(`${layout.bffRootRel}/`)) {
|
|
203
|
+
sub = sub.slice(layout.bffRootRel.length + 1);
|
|
204
|
+
} else if (sub.startsWith('src/bff/')) {
|
|
205
|
+
sub = sub.slice('src/bff/'.length);
|
|
206
|
+
} else if (sub.startsWith('bff/')) {
|
|
207
|
+
sub = sub.slice('bff/'.length);
|
|
208
|
+
} else if (sub.startsWith('src/server/')) {
|
|
209
|
+
sub = sub.slice('src/server/'.length);
|
|
210
|
+
} else if (sub.startsWith('src/')) {
|
|
211
|
+
sub = sub.slice('src/'.length);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (sub.startsWith('api/service/') || sub.startsWith('api/')) return 'contract';
|
|
215
|
+
if (sub.startsWith('application/')) return 'application';
|
|
216
|
+
if (sub.startsWith('foundation/')) return 'foundation';
|
|
217
|
+
if (sub.startsWith('domain/') && layout.hasFourLayerDomain) return 'domain';
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* 扫描应用源码目录,核对双平面隔离(web ↔ bff ↔ shared)、BFF 四层依赖方向与页面视图横向隔离;
|
|
223
|
+
* 返回 KIT-APP-015 诊断清单(按文件路径与行号稳定排序)。
|
|
224
|
+
*/
|
|
225
|
+
export async function scanKitAppLayering(appDir: string): Promise<KitDiagnostic[]> {
|
|
226
|
+
const path = await import(/* webpackIgnore: true */ 'node:path');
|
|
227
|
+
const fs = await import(/* webpackIgnore: true */ 'node:fs');
|
|
228
|
+
const layout = detectLayout(fs, path, appDir);
|
|
229
|
+
const root = layout.projectRoot;
|
|
230
|
+
const displayBase = path.resolve(appDir);
|
|
231
|
+
const out: KitDiagnostic[] = [];
|
|
232
|
+
|
|
233
|
+
const formatFile = (absFile: string): string => {
|
|
234
|
+
const rel = toPosix(path.relative(displayBase, absFile));
|
|
235
|
+
return rel.startsWith('..') ? toPosix(path.relative(root, absFile)) : rel;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const files: string[] = [];
|
|
239
|
+
const walk = (dir: string): void => {
|
|
240
|
+
let entries: import('node:fs').Dirent[];
|
|
241
|
+
try {
|
|
242
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
243
|
+
} catch {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
for (const e of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
247
|
+
if (e.name.startsWith('.') || SKIP_DIRS.has(e.name)) continue;
|
|
248
|
+
const full = path.join(dir, e.name);
|
|
249
|
+
if (e.isDirectory()) {
|
|
250
|
+
walk(full);
|
|
251
|
+
} else if (
|
|
252
|
+
e.isFile() &&
|
|
253
|
+
SOURCE_EXTS.some((ext) => e.name.endsWith(ext)) &&
|
|
254
|
+
!/\.d\.[cm]?ts$/.test(e.name) &&
|
|
255
|
+
!/\.(?:test|spec)\.[cm]?[jt]sx?$/.test(e.name)
|
|
256
|
+
) {
|
|
257
|
+
files.push(full);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
walk(root);
|
|
262
|
+
|
|
263
|
+
for (const fromFull of files) {
|
|
264
|
+
let text: string;
|
|
265
|
+
try {
|
|
266
|
+
text = fs.readFileSync(fromFull, 'utf8');
|
|
267
|
+
} catch {
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
const fromRel = toPosix(path.relative(root, fromFull));
|
|
271
|
+
const fromDisplay = formatFile(fromFull);
|
|
272
|
+
const fromPlane = classifyPlane(fromRel, layout);
|
|
273
|
+
const fromLayer = classifyBffLayer(fromRel, layout);
|
|
274
|
+
const fromIsView = isViewFile(fromRel);
|
|
275
|
+
const refs = extractModuleSpecifiers(text, fromRel);
|
|
276
|
+
|
|
277
|
+
for (const ref of refs) {
|
|
278
|
+
// R-APP-1c: shared/ 纯净度——禁止依赖 Node.js 内置模块
|
|
279
|
+
if (fromPlane === 'shared' && NODE_BUILTIN_RE.test(ref.spec)) {
|
|
280
|
+
out.push({
|
|
281
|
+
code: 'KIT-APP-015',
|
|
282
|
+
path: `${fromDisplay}:${ref.line}`,
|
|
283
|
+
file: fromDisplay,
|
|
284
|
+
line: ref.line,
|
|
285
|
+
column: ref.column,
|
|
286
|
+
message: `shared 共享契约模块 '${fromDisplay}' 引用了 Node 内置模块 '${ref.spec}'(R-APP-1:shared 须保持零运行时 IO 纯净)`,
|
|
287
|
+
hint: `将涉及文件 / 网络 / 进程的实现移入 bff/foundation/,shared/ 仅保留纯类型与静态枚举`,
|
|
288
|
+
});
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (!ref.spec.startsWith('.')) continue;
|
|
293
|
+
const targetFull = resolveRelativeTarget(fs, path, fromFull, ref.spec);
|
|
294
|
+
if (targetFull === null) continue;
|
|
295
|
+
const targetRel = toPosix(path.relative(root, targetFull));
|
|
296
|
+
if (targetRel.startsWith('..')) continue;
|
|
297
|
+
const targetDisplay = formatFile(targetFull);
|
|
298
|
+
const toPlane = classifyPlane(targetRel, layout);
|
|
299
|
+
|
|
300
|
+
// R-APP-1a: 客户端面禁止引用服务端面或 soma.server.*
|
|
301
|
+
if (fromPlane === 'web' && (toPlane === 'bff' || toPlane === 'server-entry')) {
|
|
302
|
+
out.push({
|
|
303
|
+
code: 'KIT-APP-015',
|
|
304
|
+
path: `${fromDisplay}:${ref.line}`,
|
|
305
|
+
file: fromDisplay,
|
|
306
|
+
line: ref.line,
|
|
307
|
+
column: ref.column,
|
|
308
|
+
message: `客户端模块 '${fromDisplay}' 越界引用了服务端模块 '${targetDisplay}'(R-APP-1:客户端面严禁静态依赖 BFF / api / config / soma.server)`,
|
|
309
|
+
hint: `两端共用的 DTO 类型与枚举请提取到 shared/;服务端逻辑请通过 /api/* HTTP 接口调用`,
|
|
310
|
+
});
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// R-APP-1b: 服务端面禁止反向引用客户端面
|
|
315
|
+
if (fromPlane === 'bff' && toPlane === 'web') {
|
|
316
|
+
out.push({
|
|
317
|
+
code: 'KIT-APP-015',
|
|
318
|
+
path: `${fromDisplay}:${ref.line}`,
|
|
319
|
+
file: fromDisplay,
|
|
320
|
+
line: ref.line,
|
|
321
|
+
column: ref.column,
|
|
322
|
+
message: `服务端模块 '${fromDisplay}' 反向引用了客户端模块 '${targetDisplay}'(R-APP-1:BFF 严禁依赖浏览器端视图 / 动作 / 组件)`,
|
|
323
|
+
hint: `将两端共用的纯类型或纯常量下沉至 shared/`,
|
|
324
|
+
});
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// R-APP-1c: shared/ 禁止引用客户端面或服务端面
|
|
329
|
+
if (fromPlane === 'shared' && (toPlane === 'web' || toPlane === 'bff' || toPlane === 'server-entry')) {
|
|
330
|
+
out.push({
|
|
331
|
+
code: 'KIT-APP-015',
|
|
332
|
+
path: `${fromDisplay}:${ref.line}`,
|
|
333
|
+
file: fromDisplay,
|
|
334
|
+
line: ref.line,
|
|
335
|
+
column: ref.column,
|
|
336
|
+
message: `共享契约模块 '${fromDisplay}' 反向引用了 '${targetDisplay}'(R-APP-1:shared 处于最底层,严禁引用 web 或 bff)`,
|
|
337
|
+
hint: `保持 shared/ 单向被 web/ 与 bff/ 消费,不要从 shared/ 反向 import 业务实现`,
|
|
338
|
+
});
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// R-APP-3: 页面视图禁止横向互引 views/a.view.ts -> views/b.view.ts
|
|
343
|
+
if (fromIsView && isViewFile(targetRel) && fromRel !== targetRel) {
|
|
344
|
+
out.push({
|
|
345
|
+
code: 'KIT-APP-015',
|
|
346
|
+
path: `${fromDisplay}:${ref.line}`,
|
|
347
|
+
file: fromDisplay,
|
|
348
|
+
line: ref.line,
|
|
349
|
+
column: ref.column,
|
|
350
|
+
message: `页面视图 '${fromDisplay}' 直接横向引用了另一页面视图 '${targetDisplay}'(R-APP-3:严禁跨页视图直连)`,
|
|
351
|
+
hint: `将跨页面复用的辅助逻辑提取到 views/ 非 .view 辅助模块、actions/、lib/ 或 shared/`,
|
|
352
|
+
});
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// R-APP-2: BFF 四层自上而下(api/service[3] -> application[2] -> domain[1] -> foundation[0])
|
|
357
|
+
if (fromLayer !== null) {
|
|
358
|
+
const toLayer = classifyBffLayer(targetRel, layout);
|
|
359
|
+
if (toLayer !== null && LAYER_RANK[toLayer] > LAYER_RANK[fromLayer]) {
|
|
360
|
+
out.push({
|
|
361
|
+
code: 'KIT-APP-015',
|
|
362
|
+
path: `${fromDisplay}:${ref.line}`,
|
|
363
|
+
file: fromDisplay,
|
|
364
|
+
line: ref.line,
|
|
365
|
+
column: ref.column,
|
|
366
|
+
message: `下层模块 '${fromDisplay}'(${fromLayer})反向引用了上层模块 '${targetDisplay}'(${toLayer})(R-APP-2:四层依赖只许自上而下)`,
|
|
367
|
+
hint: `将公共类型 / 纯函数下沉至更低层(${fromLayer} 或 shared/),或由上层通过参数注入依赖`,
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return out;
|
|
375
|
+
}
|
package/src/cli/kit-cli.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { closestOf, formatKitDiagnostic, kitDiagnosticFooter } from '../compile/
|
|
|
5
5
|
import type { KitDiagnostic } from '../object-renderer/types';
|
|
6
6
|
import { formatKitThemeDegradation, type NineDimDegradation } from '../theme/theme-tokens';
|
|
7
7
|
import { scanKitCoreImports } from './core-imports';
|
|
8
|
+
import { scanKitAppLayering } from './app-layer-check';
|
|
8
9
|
import { scanKitViewWrites } from './view-writes';
|
|
9
10
|
import { packageNameOf, scaffoldKitApp } from './kit-init';
|
|
10
11
|
import { formatServiceEndpointReport, runServiceEndpointCheck, type ServiceEndpointWarning } from './service-endpoint-check';
|
|
@@ -34,9 +35,9 @@ export interface KitCliIo {
|
|
|
34
35
|
|
|
35
36
|
export const KIT_CLI_USAGE = [
|
|
36
37
|
'Usage:',
|
|
37
|
-
' soma-kit init <目录>
|
|
38
|
+
' soma-kit init <目录> [--bff] 生成最小可跑的 soma-kit 应用(--bff 生成 src/{shared,web,bff} 双平面四层标准骨架;已有同名文件则整体拒绝)',
|
|
38
39
|
' soma kit explain <诊断码> 打印诊断的完整说明、常见原因与正反示例',
|
|
39
|
-
' soma kit check <应用目录> [--json] 编译应用全部页面并扫描源码是否直接 import soma-core*(KIT-APP-013)、展示对象方法是否对数组原地变异(KIT-VIEW-008),打印带修改建议的诊断;--json 在标准输出打印机器可读 JSON',
|
|
40
|
+
' soma kit check <应用目录> [--json] 编译应用全部页面并扫描源码是否直接 import soma-core*(KIT-APP-013)、是否违反双平面与四层边界(KIT-APP-015)、展示对象方法是否对数组原地变异(KIT-VIEW-008),打印带修改建议的诊断;--json 在标准输出打印机器可读 JSON',
|
|
40
41
|
' 另核对页面里 /api/... 地址是否有对应服务文件与方法,不符只打印告警,不影响退出码',
|
|
41
42
|
' 退出码:0 全部通过;1 有诊断;2 用法错误(缺参数 / 未知选项)',
|
|
42
43
|
' soma kit check <应用目录> [--strict] [--profile ai|human] [--score]',
|
|
@@ -84,8 +85,23 @@ async function checkApp(
|
|
|
84
85
|
): Promise<{ pages: number; diagnostics: KitDiagnostic[]; degradations: readonly NineDimDegradation[]; layoutReports?: ReadonlyMap<string, KitLayoutReport>; deprecations?: readonly KitAdvisory[]; root: string }> {
|
|
85
86
|
const path = await import(/* webpackIgnore: true */ 'node:path');
|
|
86
87
|
const fs = await import(/* webpackIgnore: true */ 'node:fs');
|
|
87
|
-
|
|
88
|
-
|
|
88
|
+
let root = path.resolve(appDir);
|
|
89
|
+
let file = path.join(root, 'soma.app.ts');
|
|
90
|
+
if (!fs.existsSync(file)) {
|
|
91
|
+
try {
|
|
92
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8')) as { soma?: { webRoot?: unknown } };
|
|
93
|
+
if (typeof pkg.soma?.webRoot === 'string' && pkg.soma.webRoot.trim() !== '') {
|
|
94
|
+
const candidateRoot = path.resolve(root, pkg.soma.webRoot.trim());
|
|
95
|
+
const candidateFile = path.join(candidateRoot, 'soma.app.ts');
|
|
96
|
+
if (!path.relative(root, candidateRoot).startsWith('..') && fs.existsSync(candidateFile)) {
|
|
97
|
+
root = candidateRoot;
|
|
98
|
+
file = candidateFile;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
} catch {
|
|
102
|
+
// 无 package.json 或解析失败时保持原目录报错口径
|
|
103
|
+
}
|
|
104
|
+
}
|
|
89
105
|
if (!fs.existsSync(file)) {
|
|
90
106
|
return {
|
|
91
107
|
pages: 0,
|
|
@@ -219,13 +235,19 @@ export async function runKitCli(args: readonly string[], io: KitCliIo, load: (fi
|
|
|
219
235
|
return runKitMigrate(rest, io);
|
|
220
236
|
}
|
|
221
237
|
if (sub === 'init') {
|
|
222
|
-
const
|
|
238
|
+
const flags = rest.filter((x) => x.startsWith('--'));
|
|
239
|
+
const unknown = flags.filter((x) => x !== '--bff');
|
|
240
|
+
if (unknown.length > 0) {
|
|
241
|
+
io.err(`[soma-kit init] 未知选项 ${unknown.join(' ')}\n${KIT_CLI_USAGE}`);
|
|
242
|
+
return 2;
|
|
243
|
+
}
|
|
244
|
+
const dir = rest.find((x) => !x.startsWith('--'));
|
|
223
245
|
if (dir === undefined) {
|
|
224
246
|
io.err(`[soma-kit init] 缺目录\n${KIT_CLI_USAGE}`);
|
|
225
247
|
return 1;
|
|
226
248
|
}
|
|
227
249
|
const path = await import(/* webpackIgnore: true */ 'node:path');
|
|
228
|
-
const result = await scaffoldKitApp(dir, { name: packageNameOf(path.basename(path.resolve(dir))) });
|
|
250
|
+
const result = await scaffoldKitApp(dir, { name: packageNameOf(path.basename(path.resolve(dir))), bff: flags.includes('--bff') });
|
|
229
251
|
if (result.conflicts.length > 0) {
|
|
230
252
|
io.err(`[soma-kit init] ${result.root} 下已有同名文件,未写入任何内容:\n${result.conflicts.map((f) => ` ${f}`).join('\n')}\n`);
|
|
231
253
|
return 1;
|
|
@@ -259,8 +281,13 @@ export async function runKitCli(args: readonly string[], io: KitCliIo, load: (fi
|
|
|
259
281
|
const deprecations: readonly KitAdvisory[] = checked.deprecations ?? [];
|
|
260
282
|
const strictFailed = parsed.strict && strictFails([...advisories, ...deprecations]);
|
|
261
283
|
const deprecationText = deprecations.length === 0 ? '' : `弃用提示 ${deprecations.length} 条(仍生效但应迁移;不影响 ok,--strict 时算失败)\n${deprecations.map(formatAdvisory).join('\n')}\n`;
|
|
262
|
-
// AP-1:应用源码不直接 import soma-core*(KIT-APP-013
|
|
263
|
-
const diagnostics = [
|
|
284
|
+
// AP-1:应用源码不直接 import soma-core*(KIT-APP-013)、双平面与四层边界(KIT-APP-015)、展示对象方法写入与对象/动作引用核对,与装配结果合并输出
|
|
285
|
+
const diagnostics = [
|
|
286
|
+
...(await scanKitCoreImports(dir)),
|
|
287
|
+
...(await scanKitAppLayering(dir)),
|
|
288
|
+
...(await scanKitViewWrites(checked.root, load)),
|
|
289
|
+
...checked.diagnostics,
|
|
290
|
+
];
|
|
264
291
|
const degradations = checked.degradations;
|
|
265
292
|
// 接口落点核对:只出告警,不进诊断、不改退出码;编译通过或失败都跑
|
|
266
293
|
const endpoints = await runServiceEndpointCheck(dir);
|
package/src/cli/kit-init.ts
CHANGED
|
@@ -11,6 +11,8 @@ export interface KitInitOptions {
|
|
|
11
11
|
name: string;
|
|
12
12
|
/** 写进 package.json 依赖的 soma-kit 版本范围;缺省取本包版本的 ^ 范围 */
|
|
13
13
|
kitRange?: string;
|
|
14
|
+
/** 是否生成 src/{shared,web,bff} 双平面四层标准骨架(--bff) */
|
|
15
|
+
bff?: boolean;
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
/** 目录名 -> npm 包名:小写字母 / 数字 / 连字符,其余换成连字符;空则回落 my-kit-app */
|
|
@@ -180,9 +182,156 @@ const GITIGNORE = `node_modules/
|
|
|
180
182
|
.soma-runtime/
|
|
181
183
|
`;
|
|
182
184
|
|
|
185
|
+
const BFF_SHARED_CONTRACT = `// [两端共享契约面 L4] 零运行时 IO / DOM / Node 依赖,供 src/web 与 src/bff 共同消费。
|
|
186
|
+
export interface OrderDto {
|
|
187
|
+
orderNo: string;
|
|
188
|
+
customer: string;
|
|
189
|
+
region: string;
|
|
190
|
+
status: string;
|
|
191
|
+
amount: number;
|
|
192
|
+
createdAt: string;
|
|
193
|
+
}
|
|
194
|
+
`;
|
|
195
|
+
|
|
196
|
+
const BFF_REPOSITORY = `// [BFF L4 基础支撑] 订单仓储实现(后续对接数据库或下游微服务只改此处,不触碰上层)。
|
|
197
|
+
import type { OrderDto } from '../../../shared/contracts/orders';
|
|
198
|
+
|
|
199
|
+
const rows: OrderDto[] = [
|
|
200
|
+
{ orderNo: 'SO-0001', customer: '星河科技', region: 'east', status: 'paid', amount: 12800, createdAt: '2026-01-05' },
|
|
201
|
+
{ orderNo: 'SO-0002', customer: '北辰贸易', region: 'north', status: 'pending', amount: 3600, createdAt: '2026-01-08' },
|
|
202
|
+
{ orderNo: 'SO-0003', customer: '南湾物流', region: 'south', status: 'done', amount: 25400, createdAt: '2026-01-12' },
|
|
203
|
+
];
|
|
204
|
+
|
|
205
|
+
export function listAllOrders(): OrderDto[] {
|
|
206
|
+
return rows;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function upsertOrderRecord(next: OrderDto): OrderDto {
|
|
210
|
+
const existing = rows.find((r) => r.orderNo === next.orderNo);
|
|
211
|
+
if (existing !== undefined) {
|
|
212
|
+
Object.assign(existing, next);
|
|
213
|
+
return existing;
|
|
214
|
+
}
|
|
215
|
+
rows.push(next);
|
|
216
|
+
return next;
|
|
217
|
+
}
|
|
218
|
+
`;
|
|
219
|
+
|
|
220
|
+
const BFF_DOMAIN = `// [BFF L3 业务领域] 纯业务计算与默认值归一化(零网络 / 存储依赖)。
|
|
221
|
+
import type { OrderDto } from '../../../shared/contracts/orders';
|
|
222
|
+
|
|
223
|
+
export function buildNewOrder(body: Partial<OrderDto>, seq: number, today: string): OrderDto {
|
|
224
|
+
return {
|
|
225
|
+
orderNo: \`SO-\${String(seq).padStart(4, '0')}\`,
|
|
226
|
+
customer: (body.customer ?? '').trim(),
|
|
227
|
+
region: body.region ?? 'east',
|
|
228
|
+
status: body.status ?? 'pending',
|
|
229
|
+
amount: Number(body.amount) || 0,
|
|
230
|
+
createdAt: today,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
`;
|
|
234
|
+
|
|
235
|
+
const BFF_APPLICATION = `// [BFF L2 业务编排] 面向前端页面的用例编排与分页整形。
|
|
236
|
+
import type { OrderDto } from '../../../shared/contracts/orders';
|
|
237
|
+
import { buildNewOrder } from '../../domain/orders/orderRules';
|
|
238
|
+
import { listAllOrders, upsertOrderRecord } from '../../foundation/repositories/orderRepo';
|
|
239
|
+
|
|
240
|
+
export function queryOrdersPage(pageNo: number, pageSize: number): { rows: OrderDto[]; total: number; pageNo: number; pageSize: number } {
|
|
241
|
+
const all = listAllOrders();
|
|
242
|
+
const safeSize = Math.max(1, pageSize || 10);
|
|
243
|
+
const safePage = Math.max(1, pageNo || 1);
|
|
244
|
+
return {
|
|
245
|
+
rows: all.slice((safePage - 1) * safeSize, safePage * safeSize),
|
|
246
|
+
total: all.length,
|
|
247
|
+
pageNo: safePage,
|
|
248
|
+
pageSize: safeSize,
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function saveOrderUseCase(body: Partial<OrderDto>): OrderDto {
|
|
253
|
+
const all = listAllOrders();
|
|
254
|
+
const found = all.find((r) => r.orderNo === body.orderNo);
|
|
255
|
+
if (found !== undefined) {
|
|
256
|
+
return upsertOrderRecord({ ...found, ...body });
|
|
257
|
+
}
|
|
258
|
+
const created = buildNewOrder(body, all.length + 1, new Date().toISOString().slice(0, 10));
|
|
259
|
+
return upsertOrderRecord(created);
|
|
260
|
+
}
|
|
261
|
+
`;
|
|
262
|
+
|
|
263
|
+
const BFF_LIST_SERVICE = `// [BFF L1 契约与入口] 薄壳路由入口:解析请求参数 -> 调 application 用例 -> 返回统一信封。
|
|
264
|
+
import { queryOrdersPage } from '../../../application/orders/orderUseCases';
|
|
265
|
+
|
|
266
|
+
export default class OrdersListService {
|
|
267
|
+
async get(req: Request): Promise<Response> {
|
|
268
|
+
const q = new URL(req.url).searchParams;
|
|
269
|
+
const data = queryOrdersPage(Number(q.get('pageNo')) || 1, Number(q.get('pageSize')) || 10);
|
|
270
|
+
return Response.json({ success: true, data });
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
`;
|
|
274
|
+
|
|
275
|
+
const BFF_SAVE_SERVICE = `// [BFF L1 契约与入口] 薄壳路由入口:新建 POST / 编辑 PUT 共用 saveOrderUseCase。
|
|
276
|
+
import type { OrderDto } from '../../../../shared/contracts/orders';
|
|
277
|
+
import { saveOrderUseCase } from '../../../application/orders/orderUseCases';
|
|
278
|
+
|
|
279
|
+
export default class OrdersSaveService {
|
|
280
|
+
async post(req: Request): Promise<Response> {
|
|
281
|
+
return this.save(req);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async put(req: Request): Promise<Response> {
|
|
285
|
+
return this.save(req);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
private async save(req: Request): Promise<Response> {
|
|
289
|
+
const body = (await req.json()) as Partial<OrderDto>;
|
|
290
|
+
return Response.json({ success: true, data: saveOrderUseCase(body) });
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
`;
|
|
294
|
+
|
|
295
|
+
const BFF_SOMA_APP = `// [客户端面 L1] 应用装配入口:与服务端 src/bff 物理分目录(通过 package.json#soma 配置 webRoot / apiRoot)。
|
|
296
|
+
import { defineKitApp } from 'soma-kit/app';
|
|
297
|
+
|
|
298
|
+
export default defineKitApp({
|
|
299
|
+
app: 'kit.app.yaml',
|
|
300
|
+
appName: '__TITLE__',
|
|
301
|
+
title: '__TITLE__',
|
|
302
|
+
locales: ['zh-CN'],
|
|
303
|
+
defaultLocale: 'zh-CN',
|
|
304
|
+
services: () => ({ kind: 'directory', dir: '../bff/api/service' }),
|
|
305
|
+
});
|
|
306
|
+
`;
|
|
307
|
+
|
|
183
308
|
/** 生成物清单(纯函数)。 */
|
|
184
309
|
export function kitInitFiles(options: KitInitOptions): KitInitFiles {
|
|
185
310
|
const kitRange = options.kitRange ?? `^${(pkgJson as { version: string }).version}`;
|
|
311
|
+
if (options.bff === true) {
|
|
312
|
+
const packageJson = {
|
|
313
|
+
name: options.name,
|
|
314
|
+
private: true,
|
|
315
|
+
type: 'module',
|
|
316
|
+
soma: { webRoot: 'src/web', apiRoot: 'src/bff', configRoot: 'config' },
|
|
317
|
+
scripts: { dev: 'soma dev', build: 'soma build', start: 'soma start', check: 'soma-kit check .' },
|
|
318
|
+
dependencies: { 'soma-kit': kitRange },
|
|
319
|
+
};
|
|
320
|
+
return {
|
|
321
|
+
'package.json': `${JSON.stringify(packageJson, null, 2)}\n`,
|
|
322
|
+
'src/shared/contracts/orders.ts': BFF_SHARED_CONTRACT,
|
|
323
|
+
'src/web/soma.app.ts': BFF_SOMA_APP.replace(/__TITLE__/g, options.name),
|
|
324
|
+
'src/web/kit.app.yaml': APP_YAML,
|
|
325
|
+
'src/web/pages/orders.kit.yaml': PAGE,
|
|
326
|
+
'src/bff/api/service/orders/List.ts': BFF_LIST_SERVICE,
|
|
327
|
+
'src/bff/api/service/orders/Save.ts': BFF_SAVE_SERVICE,
|
|
328
|
+
'src/bff/application/orders/orderUseCases.ts': BFF_APPLICATION,
|
|
329
|
+
'src/bff/domain/orders/orderRules.ts': BFF_DOMAIN,
|
|
330
|
+
'src/bff/foundation/repositories/orderRepo.ts': BFF_REPOSITORY,
|
|
331
|
+
'.vscode/settings.json': VSCODE_SETTINGS,
|
|
332
|
+
'.gitignore': GITIGNORE,
|
|
333
|
+
};
|
|
334
|
+
}
|
|
186
335
|
const packageJson = {
|
|
187
336
|
name: options.name,
|
|
188
337
|
private: true,
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// 任一读取返回 403 → 整页收成一个「无权访问」状态(组件在运行期按数据对象的读状态判,见 data/access-gate.ts)。
|
|
3
3
|
// 只补一个宿主实例(html + show props:objects);没有读接口的页面不补。纯函数,就地改 framed。
|
|
4
4
|
|
|
5
|
-
import type { KitFragment } from '
|
|
5
|
+
import type { KitFragment } from '../object-renderer/fragment';
|
|
6
6
|
|
|
7
7
|
/** 宿主组件名与锚点(运行期组件在 data/access-gate.ts) */
|
|
8
8
|
export const KIT_ACCESS_GATE = 'KitAccessGate';
|
|
@@ -5,7 +5,7 @@ import { KIT_ACT_HOST, KIT_ACT_SCOPE, requestWriteObjects, type KitRequestSpec }
|
|
|
5
5
|
import { resolveSliceReloadObjects, type KitSliceReload } from '../object-renderer/filter-reload';
|
|
6
6
|
import type { KitDiagnostic } from '../object-renderer/types';
|
|
7
7
|
import { getCompileEnvelope } from '../shared/envelope';
|
|
8
|
-
import type { KitFragment } from '
|
|
8
|
+
import type { KitFragment } from '../object-renderer/fragment';
|
|
9
9
|
|
|
10
10
|
const HOST_ANCHOR = 'kit-act-host';
|
|
11
11
|
|