@macrowing/inbiz-codegen 0.0.0-stage → 1.0.0
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/README.md +592 -2
- package/bin/inbiz-codegen.mjs +7 -0
- package/package.json +32 -4
- package/scripts/prepare-templates.mjs +35 -0
- package/src/backend/init.mjs +170 -0
- package/src/config.mjs +150 -0
- package/src/favicon.mjs +100 -0
- package/src/generate.mjs +935 -0
- package/src/generated-output.mjs +107 -0
- package/src/index.mjs +346 -0
- package/src/menu/build.mjs +252 -0
- package/src/menu/check.mjs +163 -0
- package/src/menu/command.mjs +179 -0
- package/src/migrate-runtime.mjs +106 -0
- package/src/platform/client.mjs +167 -0
- package/src/platform/command.mjs +386 -0
- package/src/platform/pack.mjs +591 -0
- package/src/platform/recover.mjs +236 -0
- package/src/platform/startup.mjs +343 -0
- package/src/platform/state.mjs +116 -0
- package/src/platform/update.mjs +157 -0
- package/src/platform/zip.mjs +134 -0
- package/src/pull.mjs +284 -0
- package/src/scaffold/commands.mjs +349 -0
- package/src/scaffold/init.mjs +370 -0
- package/src/scaffold/template-files.mjs +40 -0
- package/src/scaffold/template-source.mjs +235 -0
- package/src/scaffold/template-sync.mjs +335 -0
- package/src/transform/heaviness.mjs +380 -0
- package/src/transform/preview-props.mjs +108 -0
- package/src/transform/published-page-links.mjs +61 -0
- package/src/transform/published-script.mjs +110 -0
- package/src/transform/registry.mjs +781 -0
- package/src/transform/schema-to-jsx.mjs +1708 -0
- package/src/util/des.mjs +10 -0
- package/templates/README.md +34 -0
- package/templates/backend/Directory.Build.props +19 -0
- package/templates/backend/Directory.Packages.props +49 -0
- package/templates/backend/Macrowing.Scaffold.slnx +13 -0
- package/templates/backend/host/Macrowing.Start/Init/app.json +8 -0
- package/templates/backend/host/Macrowing.Start/Init/menus.json +4 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationDefinition.cs +245 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationEndpoints.cs +57 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationHostedService.cs +63 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationOptions.cs +44 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationRunner.cs +294 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationState.cs +72 -0
- package/templates/backend/host/Macrowing.Start/Initialization/MenuFactory.cs +118 -0
- package/templates/backend/host/Macrowing.Start/Initialization/PackageBuilder.cs +317 -0
- package/templates/backend/host/Macrowing.Start/Initialization/PlatformAppImporter.cs +371 -0
- package/templates/backend/host/Macrowing.Start/Macrowing.Start.csproj +26 -0
- package/templates/backend/host/Macrowing.Start/Program.cs +33 -0
- package/templates/backend/host/Macrowing.Start/StartModule.cs +67 -0
- package/templates/backend/host/Macrowing.Start/appsettings.json +38 -0
- package/templates/backend/modules/Macrowing.Generated/Application/GeneratedApplicationAutoMapperProfile.cs +42 -0
- package/templates/backend/modules/Macrowing.Generated/Domain/Entities/GeneratedEntityBase.cs +72 -0
- package/templates/backend/modules/Macrowing.Generated/Domain/GeneratedConsts.cs +50 -0
- package/templates/backend/modules/Macrowing.Generated/EntityFrameworkCore/GeneratedDbContext.cs +55 -0
- package/templates/backend/modules/Macrowing.Generated/EntityFrameworkCore/GeneratedDbContextModelBuilderExtensions.cs +61 -0
- package/templates/backend/modules/Macrowing.Generated/EntityFrameworkCore/IGeneratedDbContext.cs +32 -0
- package/templates/backend/modules/Macrowing.Generated/GeneratedModule.cs +89 -0
- package/templates/backend/modules/Macrowing.Generated/Macrowing.Generated.csproj +22 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Cms/CmsClient.cs +95 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Cms/CmsModels.cs +81 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Dictionary/DictionaryClient.cs +44 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/InBizSdkModule.cs +16 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/InterfaceEngine/InterfaceEngineClient.cs +50 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Localization/LocalizationClient.cs +28 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Macrowing.InBiz.SDK.csproj +16 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Message/MessageClient.cs +99 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Message/MessageModels.cs +63 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Model/ModelClient.cs +131 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Model/ModelQuery.cs +108 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Model/ModelWrite.cs +44 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Permission/PermissionClient.cs +86 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizPlatformException.cs +42 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizPlatformRegistration.cs +60 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizPlatformTransport.cs +96 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizRequestContext.cs +27 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/README.md +117 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Details/WorkFlowDetailsClient.cs +35 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Details/WorkFlowDetailsQuery.cs +20 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Operations/WorkFlowOperationRequests.cs +105 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Operations/WorkFlowOperationsClient.cs +53 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/README.md +132 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Tracking/WorkFlowTrackingClient.cs +50 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Upgrade/WorkFlowUpgradeClient.cs +78 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Upgrade/WorkFlowUpgradeModels.cs +34 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/WorkFlowContracts.cs +88 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/WorkFlowRegistration.cs +59 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/WorkFlowTransport.cs +163 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Workbench/WorkFlowWorkbenchClient.cs +59 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Workbench/WorkFlowWorkbenchModels.cs +76 -0
- package/templates/backend/modules/Macrowing.OC.SDK/Macrowing.OC.SDK.csproj +20 -0
- package/templates/backend/modules/Macrowing.OC.SDK/OcSdkModule.cs +18 -0
- package/templates/backend/modules/Macrowing.OC.SDK/Protos/README.md +22 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/Macrowing.InBiz.SDK.Tests.csproj +14 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/PlatformContractTests.cs +288 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/PlatformLiveTests.cs +173 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowHttpServer.cs +113 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowLiveTests.cs +269 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowOperationsTests.cs +80 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowQueryTests.cs +163 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowTransportTests.cs +111 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowUpgradeTests.cs +87 -0
- package/templates/frontend/components/README.md +39 -0
- package/templates/frontend/components/app-components/README.md +33 -0
- package/templates/frontend/components/app-components/common/.gitkeep +0 -0
- package/templates/frontend/components/app-components/components/.gitkeep +0 -0
- package/templates/frontend/components/app-components/package.json +20 -0
- package/templates/frontend/mgt/.umirc.ts +574 -0
- package/templates/frontend/mgt/package.json +96 -0
- package/templates/frontend/mgt/src/app-extends.ts +9 -0
- package/templates/frontend/mgt/src/app.tsx +20 -0
- package/templates/frontend/mgt/src/config/app.ts +31 -0
- package/templates/frontend/mgt/src/layout/header.tsx +14 -0
- package/templates/frontend/mgt/src/layout/index.tsx +11 -0
- package/templates/frontend/mgt/src/layout/sidebar.tsx +14 -0
- package/templates/frontend/mgt/src/locales.ts +8 -0
- package/templates/frontend/mgt/src/pages/.gitkeep +0 -0
- package/templates/frontend/mgt/src/root.tsx +12 -0
- package/templates/frontend/mgt/src/route-view.tsx +3 -0
- package/templates/frontend/mgt/src/runtime.ts +21 -0
- package/templates/frontend/mgt/src/styles/app.css +1 -0
- package/templates/frontend/mgt/src/styles/layout.less +79 -0
- package/templates/frontend/mgt/tsconfig.json +65 -0
- package/templates/frontend/mgt/typings.d.ts +57 -0
- package/templates/frontend/wap/.umirc.ts +574 -0
- package/templates/frontend/wap/package.json +73 -0
- package/templates/frontend/wap/src/app-extends.ts +9 -0
- package/templates/frontend/wap/src/app.tsx +28 -0
- package/templates/frontend/wap/src/config/app.ts +31 -0
- package/templates/frontend/wap/src/layout/header.tsx +14 -0
- package/templates/frontend/wap/src/layout/index.tsx +18 -0
- package/templates/frontend/wap/src/layout/sidebar.tsx +14 -0
- package/templates/frontend/wap/src/locales.ts +8 -0
- package/templates/frontend/wap/src/pages/.gitkeep +0 -0
- package/templates/frontend/wap/src/root.tsx +12 -0
- package/templates/frontend/wap/src/route-view.tsx +3 -0
- package/templates/frontend/wap/src/runtime.ts +21 -0
- package/templates/frontend/wap/src/styles/adaptation.less +190 -0
- package/templates/frontend/wap/src/styles/app.css +7 -0
- package/templates/frontend/wap/src/styles/global.less +81 -0
- package/templates/frontend/wap/src/styles/layout.less +92 -0
- package/templates/frontend/wap/tsconfig.json +66 -0
- package/templates/frontend/wap/typings.d.ts +24 -0
- package/templates/frontend/web/.umirc.ts +574 -0
- package/templates/frontend/web/package.json +26 -0
- package/templates/frontend/web/src/app-extends.ts +9 -0
- package/templates/frontend/web/src/app.tsx +25 -0
- package/templates/frontend/web/src/config/app.ts +31 -0
- package/templates/frontend/web/src/layout/header.tsx +14 -0
- package/templates/frontend/web/src/layout/index.tsx +11 -0
- package/templates/frontend/web/src/layout/sidebar.tsx +14 -0
- package/templates/frontend/web/src/locales.ts +8 -0
- package/templates/frontend/web/src/root.tsx +12 -0
- package/templates/frontend/web/src/route-view.tsx +3 -0
- package/templates/frontend/web/src/runtime.ts +21 -0
- package/templates/frontend/web/src/styles/app.css +104 -0
- package/templates/frontend/web/src/styles/layout.less +124 -0
- package/templates/frontend/web/tsconfig.json +91 -0
- package/templates/frontend/web/typings.d.ts +46 -0
- package/templates/package.json +14 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 菜单 spec → 平台菜单数据。
|
|
3
|
+
*
|
|
4
|
+
* ## 口径(重要)
|
|
5
|
+
*
|
|
6
|
+
* 出码之后,线上平台对产物真正有用的只有两样:
|
|
7
|
+
*
|
|
8
|
+
* 1. **应用 id** —— 后台模块(模型/流程/消息/文件)的接口都按应用维度授权;
|
|
9
|
+
* 2. **菜单** —— 产物运行期从 `menu/menuList` 拉菜单树自己渲染。
|
|
10
|
+
*
|
|
11
|
+
* 页面和页面分组**不再往线上建**:页面已经在构建期翻译成 JSX 落到产物里,
|
|
12
|
+
* 线上再建一份只是两份数据要同步。所以这里产出的菜单条目:
|
|
13
|
+
*
|
|
14
|
+
* - `Route` 写页面路由(产物用它导航,`routes.ts` 按同一个 route 找组件);
|
|
15
|
+
* - `PageOutId` / `PageRoute` 留空 —— 线上没有对应页面记录;
|
|
16
|
+
* - `LinkUrl` 留空,`IsInternalLink=true`(站内跳转)。
|
|
17
|
+
*
|
|
18
|
+
* 平台是否接受「没有 PageOutId 的页面菜单」需要实测(见文档 10.7)。
|
|
19
|
+
* 因此 `--with-page-out-id` 保留了口子:若平台后续要求页面必须存在,
|
|
20
|
+
* 传入 route → OutId 的映射即可,其余逻辑不变。
|
|
21
|
+
*
|
|
22
|
+
* ## 幂等
|
|
23
|
+
*
|
|
24
|
+
* `OutId` 由路由派生(`menu_` + 路由的稳定哈希),不用时间戳。
|
|
25
|
+
* 同一份 spec 重复生成得到完全相同的 OutId,这是增量同步的前提:
|
|
26
|
+
* 第二次执行时平台侧按 OutId 命中已有菜单,不会重复创建。
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { createHash } from 'node:crypto';
|
|
30
|
+
|
|
31
|
+
/** 平台菜单条目的默认值(字段含义见 skill 的 menu/SKILL.md §3)。 */
|
|
32
|
+
const DEFAULTS = {
|
|
33
|
+
IsVisible: true,
|
|
34
|
+
IsInternalLink: true,
|
|
35
|
+
IsHaveChild: false,
|
|
36
|
+
IsHomePage: false,
|
|
37
|
+
IsCustomized: false,
|
|
38
|
+
OpenType: 0,
|
|
39
|
+
/**
|
|
40
|
+
* 0 = StandardAdd(标准里新增)。枚举:0 StandardAdd / 1 StandardUpdateToCustom /
|
|
41
|
+
* 2 CustomUpdateFromStandard / 3 CustomAdd。
|
|
42
|
+
*
|
|
43
|
+
* 写 3 创建能过,但**更新会被平台拒**:`MenuImportDataReader` 校验标准根下的
|
|
44
|
+
* 菜单只能取 0 或 1,否则报「定制状态CreateOriginType状态异常」。
|
|
45
|
+
*/
|
|
46
|
+
CreateOriginType: 0,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** 路由 → 稳定 OutId。同一 route 永远得到同一个值。 */
|
|
50
|
+
export function menuOutId(route) {
|
|
51
|
+
const digest = createHash('sha256').update(String(route)).digest('hex').slice(0, 16);
|
|
52
|
+
return `menu_${digest}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** 分组菜单的 Route 前缀:平台用 `xinjiancaidan_` 区分「分组容器」与「页面」。 */
|
|
56
|
+
const GROUP_ROUTE_PREFIX = 'xinjiancaidan_';
|
|
57
|
+
|
|
58
|
+
function nowText(date) {
|
|
59
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
60
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} `
|
|
61
|
+
+ `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @param {object} spec 菜单 spec(见 docs/codegen/10-project-bootstrap.md §10.4)
|
|
66
|
+
* @param {{ now?: Date, pageOutIds?: Record<string, string> }} [options]
|
|
67
|
+
* @returns {{ menus: object[], errors: { code: string, path: string, message: string }[] }}
|
|
68
|
+
*/
|
|
69
|
+
export function buildMenus(spec, options = {}) {
|
|
70
|
+
const now = options.now || new Date();
|
|
71
|
+
const pageOutIds = options.pageOutIds || {};
|
|
72
|
+
const errors = [];
|
|
73
|
+
const items = Array.isArray(spec?.menus) ? spec.menus : [];
|
|
74
|
+
|
|
75
|
+
if (!Array.isArray(spec?.menus)) {
|
|
76
|
+
errors.push({ code: 'MENU_SPEC_INVALID', path: 'menus', message: 'spec 必须是 { "menus": [...] }' });
|
|
77
|
+
return { menus: [], errors };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** route → 声明,用于校验 parent 指向与重复。 */
|
|
81
|
+
const declared = new Map();
|
|
82
|
+
items.forEach((item, index) => {
|
|
83
|
+
const at = `menus[${index}]`;
|
|
84
|
+
const route = typeof item?.route === 'string' ? item.route.trim() : '';
|
|
85
|
+
if (!route) {
|
|
86
|
+
errors.push({ code: 'MENU_ROUTE_REQUIRED', path: `${at}.route`, message: '菜单必须有 route' });
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (declared.has(route)) {
|
|
90
|
+
errors.push({
|
|
91
|
+
code: 'MENU_ROUTE_DUPLICATED',
|
|
92
|
+
path: `${at}.route`,
|
|
93
|
+
message: `route "${route}" 在 spec 里出现了多次(第一次在 ${declared.get(route).at})`,
|
|
94
|
+
});
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (!item?.name) {
|
|
98
|
+
errors.push({ code: 'MENU_NAME_REQUIRED', path: `${at}.name`, message: `route "${route}" 缺少 name` });
|
|
99
|
+
}
|
|
100
|
+
declared.set(route, { item, at, index });
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// 父子关系:parent 必须指向一个分组菜单,不能指向另一个页面菜单。
|
|
104
|
+
for (const [route, { item, at }] of declared) {
|
|
105
|
+
const parent = item.parent ? String(item.parent) : '';
|
|
106
|
+
if (!parent) continue;
|
|
107
|
+
const target = declared.get(parent);
|
|
108
|
+
if (!target) {
|
|
109
|
+
errors.push({
|
|
110
|
+
code: 'MENU_PARENT_NOT_FOUND',
|
|
111
|
+
path: `${at}.parent`,
|
|
112
|
+
message: `parent "${parent}" 在 spec 里不存在`,
|
|
113
|
+
});
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (kindOf(target.item) !== 'group') {
|
|
117
|
+
errors.push({
|
|
118
|
+
code: 'MENU_PARENT_NOT_GROUP',
|
|
119
|
+
path: `${at}.parent`,
|
|
120
|
+
message: `parent "${parent}" 是页面菜单;页面菜单只能挂在分组下`,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 环与深度:最多三级(平台约束),且不能自引用/成环。
|
|
126
|
+
for (const [route, { at }] of declared) {
|
|
127
|
+
const seen = new Set([route]);
|
|
128
|
+
let cursor = declared.get(route)?.item?.parent ? String(declared.get(route).item.parent) : '';
|
|
129
|
+
let depth = 1;
|
|
130
|
+
while (cursor) {
|
|
131
|
+
if (seen.has(cursor)) {
|
|
132
|
+
errors.push({ code: 'MENU_PARENT_CYCLE', path: `${at}.parent`, message: `父子关系成环:${[...seen].join(' → ')}` });
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
seen.add(cursor);
|
|
136
|
+
depth += 1;
|
|
137
|
+
cursor = declared.get(cursor)?.item?.parent ? String(declared.get(cursor).item.parent) : '';
|
|
138
|
+
}
|
|
139
|
+
if (depth > 3) {
|
|
140
|
+
errors.push({ code: 'MENU_DEPTH_EXCEEDED', path: `${at}.parent`, message: `层级 ${depth} 超过平台上限 3 级` });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const hasChild = new Set();
|
|
145
|
+
for (const { item } of declared.values()) {
|
|
146
|
+
if (item.parent && declared.has(String(item.parent))) hasChild.add(String(item.parent));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const creationTime = nowText(now);
|
|
150
|
+
const menus = [];
|
|
151
|
+
// 保持 spec 顺序,但按 sort 稳定排序输出(同 sort 用出现顺序)。
|
|
152
|
+
for (const [route, { item }] of declared) {
|
|
153
|
+
const kind = kindOf(item);
|
|
154
|
+
const parent = item.parent ? String(item.parent) : '';
|
|
155
|
+
const icon = normalizeIcon(item.icon);
|
|
156
|
+
if (item.icon && !icon) {
|
|
157
|
+
errors.push({
|
|
158
|
+
code: 'MENU_ICON_INVALID',
|
|
159
|
+
path: `${declared.get(route).at}.icon`,
|
|
160
|
+
message: 'icon 必须是非空字符串或省略;不要给新菜单凭经验编图标',
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
const isGroup = kind === 'group';
|
|
164
|
+
const pageOutId = pageOutIds[route] || '';
|
|
165
|
+
/**
|
|
166
|
+
* 已有菜单沿用平台下发的 OutId。
|
|
167
|
+
*
|
|
168
|
+
* 从平台拉下来的菜单再重新生成时,如果按 route 重新派生 OutId,
|
|
169
|
+
* 导入后就是「新增一批」而不是「改名字/改图标」。后端初始化
|
|
170
|
+
* (Macrowing.Start/Initialization/MenuFactory)用的是同一条规则。
|
|
171
|
+
*/
|
|
172
|
+
const outId = item.outId ? String(item.outId) : menuOutId(route);
|
|
173
|
+
const parentItem = parent ? declared.get(parent)?.item : undefined;
|
|
174
|
+
const parentOutId = parent
|
|
175
|
+
? (parentItem?.outId ? String(parentItem.outId) : menuOutId(parent))
|
|
176
|
+
: '';
|
|
177
|
+
|
|
178
|
+
menus.push({
|
|
179
|
+
OutId: outId,
|
|
180
|
+
ParentId: parentOutId,
|
|
181
|
+
Name: item.name || route,
|
|
182
|
+
DisplayName: item.displayName || item.name || route,
|
|
183
|
+
IsHaveChild: isGroup ? true : hasChild.has(route),
|
|
184
|
+
Icon: icon,
|
|
185
|
+
IsIconVisible: icon !== '',
|
|
186
|
+
IsVisible: item.visible !== false,
|
|
187
|
+
IsInternalLink: item.external !== true,
|
|
188
|
+
// 站内菜单不写 URL:产物按 Route 找本地页面;外链才需要绝对地址。
|
|
189
|
+
LinkUrl: item.external === true ? String(item.url || '') : '',
|
|
190
|
+
IsHomePage: item.home === true,
|
|
191
|
+
Sort: String(item.order ?? (declared.get(route).index + 1)),
|
|
192
|
+
IsCustomized: DEFAULTS.IsCustomized,
|
|
193
|
+
Route: isGroup ? `${GROUP_ROUTE_PREFIX}${route}` : route,
|
|
194
|
+
// 出码口径:线上不建页面,这两个字段留空。
|
|
195
|
+
PageOutId: isGroup ? '' : pageOutId,
|
|
196
|
+
PageRoute: isGroup ? '' : pageOutId ? route : '',
|
|
197
|
+
CreateOriginType: DEFAULTS.CreateOriginType,
|
|
198
|
+
OpenType: Number(item.openType ?? DEFAULTS.OpenType),
|
|
199
|
+
CreationTime: creationTime,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// 外链必须有 url,否则点不动。
|
|
204
|
+
for (const [route, { item, at }] of declared) {
|
|
205
|
+
if (item.external === true && !String(item.url || '').trim()) {
|
|
206
|
+
errors.push({ code: 'MENU_EXTERNAL_URL_REQUIRED', path: `${at}.url`, message: `外链菜单 "${route}" 缺少 url` });
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return { menus, errors };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function kindOf(item) {
|
|
214
|
+
if (item?.type === 'group' || item?.type === 'folder') return 'group';
|
|
215
|
+
if (Array.isArray(item?.children) && item.children.length) return 'group';
|
|
216
|
+
return 'page';
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* 图标规则来自平台菜单 Skill:新菜单默认必须是空串。
|
|
221
|
+
* 只有显式给了图标才写值 —— 这里不做「用户管理→icon-user」这种映射。
|
|
222
|
+
*/
|
|
223
|
+
function normalizeIcon(icon) {
|
|
224
|
+
if (icon === undefined || icon === null) return '';
|
|
225
|
+
const text = String(icon).trim();
|
|
226
|
+
return text;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* 菜单关系表。平台用它渲染菜单树;与 menu.json 必须同步维护。
|
|
231
|
+
* 出码口径下没有页面 OutId,`PageOutId` 只在显式提供了映射时才有值。
|
|
232
|
+
*/
|
|
233
|
+
export function buildMenuRelation(menus, pageOutIds = {}) {
|
|
234
|
+
/**
|
|
235
|
+
* 只输出**真正绑定到页面**的关系行。
|
|
236
|
+
*
|
|
237
|
+
* 出码口径下线上不建页面,`PageOutId` 为空;这种空关系行会被平台的
|
|
238
|
+
* 菜单关系更新逻辑拿去做 `Except + GetHashCode`,直接空引用
|
|
239
|
+
* (`InBizMenuRelationDataEqualityComparer.GetHashCode` 抛 NRE)。
|
|
240
|
+
* 平台自己导出的包在这种场景下 `menuRelation.json` 就是 `[]`,这里与之一致。
|
|
241
|
+
*/
|
|
242
|
+
return menus
|
|
243
|
+
.filter((menu) => !menu.IsHaveChild || menu.Route.startsWith(GROUP_ROUTE_PREFIX))
|
|
244
|
+
.map((menu) => ({
|
|
245
|
+
ObjectName: menu.Name,
|
|
246
|
+
ObjectValue: menu.OutId,
|
|
247
|
+
PageOutId: menu.PageOutId || pageOutIds[menu.Route] || '',
|
|
248
|
+
}))
|
|
249
|
+
.filter((row) => String(row.PageOutId || '').trim() !== '');
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export { GROUP_ROUTE_PREFIX };
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 菜单只读校验。
|
|
3
|
+
*
|
|
4
|
+
* 对象是**已经落地的 `menu.json` / `menuRelation.json`**(可能来自 pull,
|
|
5
|
+
* 也可能是 build 的产物),不是 spec。规则取自平台菜单 Skill:
|
|
6
|
+
*
|
|
7
|
+
* §2.0 只给主页面建菜单(子页面靠按钮跳转)
|
|
8
|
+
* §2.1 页面菜单只能挂在分组菜单下,最多三级
|
|
9
|
+
* §3 字段:OutId 必填、图标默认空、页面菜单必须成对出现 Route/PageRoute
|
|
10
|
+
* 推送前必查:Route 在同一端下唯一、menuRelation 与 menu.json 同步
|
|
11
|
+
*
|
|
12
|
+
* 出码口径下允许 `PageOutId` 为空(线上不建页面),但**两个字段必须同时为空或
|
|
13
|
+
* 同时有值** —— 只填一个是最容易出的错,平台侧表现为「菜单点不动」。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const GROUP_ROUTE_PREFIX = 'xinjiancaidan_';
|
|
17
|
+
|
|
18
|
+
function issue(code, path, message, level = 'error') {
|
|
19
|
+
return { code, path, message, level };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const isGroup = (menu) => menu?.IsHaveChild === true
|
|
23
|
+
|| (typeof menu?.Route === 'string' && menu.Route.startsWith(GROUP_ROUTE_PREFIX));
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {object[]} menus menu.json 的内容
|
|
27
|
+
* @param {object[]} [relation] menuRelation.json 的内容
|
|
28
|
+
* @param {{ routes?: string[], end?: string }} [options]
|
|
29
|
+
* routes:本次出码实际存在的页面路由;提供时才做「菜单指向不存在页面」的检查
|
|
30
|
+
* @returns {{ ok: boolean, errors: object[], warnings: object[] }}
|
|
31
|
+
*/
|
|
32
|
+
export function checkMenus(menus, relation, options = {}) {
|
|
33
|
+
const errors = [];
|
|
34
|
+
const warnings = [];
|
|
35
|
+
if (!Array.isArray(menus)) {
|
|
36
|
+
return { ok: false, errors: [issue('MENU_FILE_NOT_ARRAY', 'menu.json', 'menu.json 必须是数组')], warnings };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const byOutId = new Map();
|
|
40
|
+
const byRoute = new Map();
|
|
41
|
+
|
|
42
|
+
menus.forEach((menu, index) => {
|
|
43
|
+
const at = `menu[${index}]`;
|
|
44
|
+
if (!menu || typeof menu !== 'object') {
|
|
45
|
+
errors.push(issue('MENU_ITEM_INVALID', at, '菜单项必须是对象'));
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (!menu.OutId) errors.push(issue('MENU_OUT_ID_REQUIRED', `${at}.OutId`, `「${menu.Name || '?'}」缺少 OutId`));
|
|
49
|
+
if (!menu.Name) errors.push(issue('MENU_NAME_REQUIRED', `${at}.Name`, '缺少 Name'));
|
|
50
|
+
if (menu.OutId) {
|
|
51
|
+
if (byOutId.has(menu.OutId)) {
|
|
52
|
+
errors.push(issue('MENU_OUT_ID_DUPLICATED', `${at}.OutId`, `OutId 重复:${menu.OutId}`));
|
|
53
|
+
}
|
|
54
|
+
byOutId.set(menu.OutId, menu);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// 旧格式兜底:Key/Name/Icon/Sort 结构不是平台菜单项
|
|
58
|
+
if (!menu.OutId && menu.Key) {
|
|
59
|
+
errors.push(issue('MENU_LEGACY_SHAPE', at, '检测到 Key/Sort 结构;平台菜单项必须用 OutId/ParentId/Route 结构'));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const group = isGroup(menu);
|
|
63
|
+
if (!group) {
|
|
64
|
+
if (!menu.Route) errors.push(issue('MENU_ROUTE_REQUIRED', `${at}.Route`, `「${menu.Name || '?'}」是页面菜单但没有 Route`));
|
|
65
|
+
if (menu.Route) {
|
|
66
|
+
if (byRoute.has(menu.Route)) {
|
|
67
|
+
errors.push(issue('MENU_ROUTE_DUPLICATED', `${at}.Route`, `同一端下 Route 重复:${menu.Route}`));
|
|
68
|
+
}
|
|
69
|
+
byRoute.set(menu.Route, menu);
|
|
70
|
+
}
|
|
71
|
+
// 页面关联成对:要么都没有(出码口径),要么都有
|
|
72
|
+
const hasPageId = Boolean(menu.PageOutId);
|
|
73
|
+
const hasPageRoute = Boolean(menu.PageRoute);
|
|
74
|
+
if (hasPageId !== hasPageRoute) {
|
|
75
|
+
errors.push(issue(
|
|
76
|
+
'MENU_PAGE_LINK_INCONSISTENT',
|
|
77
|
+
at,
|
|
78
|
+
`PageOutId/PageRoute 必须同时为空或同时有值(当前 PageOutId=${JSON.stringify(menu.PageOutId)}, PageRoute=${JSON.stringify(menu.PageRoute)})`,
|
|
79
|
+
));
|
|
80
|
+
}
|
|
81
|
+
if (!hasPageId && options.routes?.length && menu.Route && !options.routes.includes(menu.Route)) {
|
|
82
|
+
errors.push(issue(
|
|
83
|
+
'MENU_ROUTE_NOT_FOUND',
|
|
84
|
+
`${at}.Route`,
|
|
85
|
+
`菜单指向的页面「${menu.Route}」在本次出码的页面清单里不存在,点进去会 404`,
|
|
86
|
+
));
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
if (menu.PageOutId) errors.push(issue('MENU_GROUP_HAS_PAGE', at, '分组菜单不能有 PageOutId'));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 图标:默认必须空;非空则 IsIconVisible 必须为 true
|
|
93
|
+
if (menu.Icon && menu.IsIconVisible !== true) {
|
|
94
|
+
errors.push(issue('MENU_ICON_VISIBILITY', at, 'Icon 非空但 IsIconVisible 不是 true'));
|
|
95
|
+
}
|
|
96
|
+
if (!menu.Icon && menu.IsIconVisible === true) {
|
|
97
|
+
warnings.push(issue('MENU_ICON_EMPTY_BUT_VISIBLE', at, 'IsIconVisible=true 但 Icon 为空', 'warning'));
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// 父子:ParentId 必须存在,且指向分组;不能成环;最多三级
|
|
102
|
+
menus.forEach((menu, index) => {
|
|
103
|
+
const at = `menu[${index}]`;
|
|
104
|
+
const parentId = menu?.ParentId;
|
|
105
|
+
if (!parentId) return;
|
|
106
|
+
const parent = byOutId.get(parentId);
|
|
107
|
+
if (!parent) {
|
|
108
|
+
errors.push(issue('MENU_PARENT_NOT_FOUND', `${at}.ParentId`, `ParentId ${parentId} 在 menu.json 里不存在`));
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (!isGroup(parent)) {
|
|
112
|
+
errors.push(issue(
|
|
113
|
+
'MENU_PARENT_NOT_GROUP',
|
|
114
|
+
`${at}.ParentId`,
|
|
115
|
+
`「${menu.Name}」挂在了页面菜单「${parent.Name}」下;页面菜单只能挂在分组菜单下`,
|
|
116
|
+
));
|
|
117
|
+
}
|
|
118
|
+
const seen = new Set([menu.OutId]);
|
|
119
|
+
let cursor = parent;
|
|
120
|
+
let depth = 2;
|
|
121
|
+
while (cursor?.ParentId) {
|
|
122
|
+
if (seen.has(cursor.OutId)) {
|
|
123
|
+
errors.push(issue('MENU_PARENT_CYCLE', `${at}.ParentId`, '菜单父子关系成环'));
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
seen.add(cursor.OutId);
|
|
127
|
+
depth += 1;
|
|
128
|
+
cursor = byOutId.get(cursor.ParentId);
|
|
129
|
+
if (depth > 3) {
|
|
130
|
+
errors.push(issue('MENU_DEPTH_EXCEEDED', `${at}.ParentId`, `层级超过 3 级(${menu.Name})`));
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* menuRelation 只覆盖**绑定到页面**的菜单。
|
|
138
|
+
*
|
|
139
|
+
* 出码口径下线上不建页面(PageOutId/PageRoute 都为空),关系表就是空数组 ——
|
|
140
|
+
* 平台自己导出的包也是这样。给空关系行反而会让平台的菜单关系更新逻辑
|
|
141
|
+
* 空引用(`InBizMenuRelationDataEqualityComparer.GetHashCode`)。
|
|
142
|
+
*/
|
|
143
|
+
if (Array.isArray(relation)) {
|
|
144
|
+
const related = new Set(relation.map((item) => item?.ObjectValue).filter(Boolean));
|
|
145
|
+
for (const menu of menus) {
|
|
146
|
+
const bindsPage = Boolean(String(menu?.PageOutId || '').trim() || String(menu?.PageRoute || '').trim());
|
|
147
|
+
if (bindsPage && menu?.OutId && !related.has(menu.OutId)) {
|
|
148
|
+
errors.push(issue('MENU_RELATION_MISSING', `menuRelation`, `菜单「${menu.Name}」(${menu.OutId})缺少关联记录`));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
for (const item of relation) {
|
|
152
|
+
if (item?.ObjectValue && !byOutId.has(item.ObjectValue)) {
|
|
153
|
+
errors.push(issue('MENU_RELATION_ORPHAN', 'menuRelation', `关联记录指向不存在的菜单 ${item.ObjectValue}`));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
} else if (relation !== undefined) {
|
|
157
|
+
errors.push(issue('MENU_RELATION_INVALID', 'menuRelation.json', 'menuRelation.json 必须是数组'));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return { ok: errors.length === 0, errors, warnings };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export { GROUP_ROUTE_PREFIX };
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `menu build` / `menu check`。
|
|
3
|
+
*
|
|
4
|
+
* 输出分两种:给人看的摘要,和 `--json` 的机器可读结果。
|
|
5
|
+
* 退出码是契约的一部分(供 AI 与 CI 判定):
|
|
6
|
+
* 0 成功 / 2 输入或校验错误 / 3 平台拒绝 / 4 网络
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import fs from 'node:fs';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { buildMenus, buildMenuRelation } from './build.mjs';
|
|
12
|
+
import { checkMenus } from './check.mjs';
|
|
13
|
+
|
|
14
|
+
export const EXIT = { OK: 0, INPUT: 2, PLATFORM: 3, NETWORK: 4 };
|
|
15
|
+
|
|
16
|
+
const MENUS_FILE = 'menus.json';
|
|
17
|
+
|
|
18
|
+
export function readSpec(dir) {
|
|
19
|
+
const file = path.join(dir, MENUS_FILE);
|
|
20
|
+
if (!fs.existsSync(file)) {
|
|
21
|
+
throw Object.assign(new Error(`找不到 ${file};菜单 spec 必须放在工程根目录`), { exitCode: EXIT.INPUT });
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
throw Object.assign(new Error(`${file} 不是合法 JSON:${e.message}`), { exitCode: EXIT.INPUT });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 产物里实际存在的页面路由(用于「菜单指向不存在的页面」校验)。
|
|
32
|
+
*
|
|
33
|
+
* 三个来源都要看,缺一个就会误判:
|
|
34
|
+
* 1. `src/pages/routes.ts` —— 出码生成的页面;
|
|
35
|
+
* 2. `src/extra-routes.ts` —— 手写页面(`add page` 登记的地方);
|
|
36
|
+
* 3. `src/pages/**\/index.tsx` —— 磁盘上真实存在的页面目录。
|
|
37
|
+
*
|
|
38
|
+
* 第 3 条是兜底,也是最终判据:前两个文件只是索引,**点进去会不会 404
|
|
39
|
+
* 取决于文件在不在**。只读 routes.ts 时,手写页面会被判成「不存在」,
|
|
40
|
+
* 于是菜单校验形同虚设(实测:只读 routes.ts 时一个指向不存在页面的菜单
|
|
41
|
+
* 竟然校验通过)。
|
|
42
|
+
*
|
|
43
|
+
* @returns {string[] | null} 目录不存在时返回 null(无从判断,跳过这项检查)
|
|
44
|
+
*/
|
|
45
|
+
export function readRouteList(webDir) {
|
|
46
|
+
const pagesDir = path.join(webDir, 'src', 'pages');
|
|
47
|
+
if (!fs.existsSync(pagesDir)) return null;
|
|
48
|
+
|
|
49
|
+
const routes = new Set();
|
|
50
|
+
|
|
51
|
+
const collectFrom = (file, pattern) => {
|
|
52
|
+
if (!fs.existsSync(file)) return;
|
|
53
|
+
const text = fs.readFileSync(file, 'utf8');
|
|
54
|
+
for (const match of text.matchAll(pattern)) routes.add(match[1]);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// 出码页面:routes.ts 里的 "route": "xxx"
|
|
58
|
+
collectFrom(path.join(pagesDir, 'routes.ts'), /^\s*"route":\s*"([^"]+)"/gm);
|
|
59
|
+
// 手写页面:extra-routes.ts 里的 route: "xxx"
|
|
60
|
+
collectFrom(path.join(webDir, 'src', 'extra-routes.ts'), /^\s*route:\s*["']([^"']+)["']/gm);
|
|
61
|
+
|
|
62
|
+
// 磁盘兜底:src/pages/<a>/<b>/index.tsx → a/b
|
|
63
|
+
const walk = (dir, prefix) => {
|
|
64
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
65
|
+
if (entry.name.startsWith('.')) continue;
|
|
66
|
+
const full = path.join(dir, entry.name);
|
|
67
|
+
if (entry.isDirectory()) {
|
|
68
|
+
walk(full, prefix ? `${prefix}/${entry.name}` : entry.name);
|
|
69
|
+
} else if (/^index\.tsx?$/.test(entry.name) && prefix) {
|
|
70
|
+
routes.add(prefix);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
// routes.ts 自身不是页面目录,先跳过它所在层
|
|
75
|
+
for (const entry of fs.readdirSync(pagesDir, { withFileTypes: true })) {
|
|
76
|
+
if (entry.isDirectory() && !entry.name.startsWith('.')) {
|
|
77
|
+
walk(path.join(pagesDir, entry.name), entry.name);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return [...routes];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {{ dir: string, end?: string, json?: boolean, dryRun?: boolean, webDir?: string }} options
|
|
86
|
+
*/
|
|
87
|
+
export function runMenuBuild({ dir, end = 'web', json = false, dryRun = false, webDir }) {
|
|
88
|
+
const spec = readSpec(dir);
|
|
89
|
+
const routes = webDir ? readRouteList(webDir) : null;
|
|
90
|
+
const { menus, errors } = buildMenus(spec);
|
|
91
|
+
|
|
92
|
+
if (errors.length) {
|
|
93
|
+
if (json) {
|
|
94
|
+
console.log(JSON.stringify({ ok: false, command: 'menu build', errors }, null, 2));
|
|
95
|
+
} else {
|
|
96
|
+
console.error('菜单 spec 校验失败:');
|
|
97
|
+
for (const error of errors) console.error(` ✗ ${error.code} ${error.path}: ${error.message}`);
|
|
98
|
+
}
|
|
99
|
+
return EXIT.INPUT;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const relation = buildMenuRelation(menus);
|
|
103
|
+
// 用 spec 里的端覆盖;spec 未声明时用 --end
|
|
104
|
+
const targetEnd = spec.end || end;
|
|
105
|
+
const checked = checkMenus(menus, relation, { routes: routes || undefined });
|
|
106
|
+
|
|
107
|
+
const outDir = path.join(dir, 'standard', targetEnd);
|
|
108
|
+
const menuFile = path.join(outDir, 'menu.json');
|
|
109
|
+
const relationFile = path.join(outDir, 'menuRelation.json');
|
|
110
|
+
|
|
111
|
+
if (!dryRun && checked.ok) {
|
|
112
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
113
|
+
const writeIfChanged = (file, content) => {
|
|
114
|
+
const text = JSON.stringify(content, null, 2) + '\n';
|
|
115
|
+
if (fs.existsSync(file) && fs.readFileSync(file, 'utf8') === text) return false;
|
|
116
|
+
fs.writeFileSync(file, text, 'utf8');
|
|
117
|
+
return true;
|
|
118
|
+
};
|
|
119
|
+
writeIfChanged(menuFile, menus);
|
|
120
|
+
writeIfChanged(relationFile, relation);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (json) {
|
|
124
|
+
console.log(JSON.stringify({
|
|
125
|
+
ok: checked.ok,
|
|
126
|
+
command: 'menu build',
|
|
127
|
+
dryRun,
|
|
128
|
+
end: targetEnd,
|
|
129
|
+
changed: { menus: checked.ok && !dryRun ? menus.length : 0 },
|
|
130
|
+
items: menus.map((m) => ({ kind: m.IsHaveChild ? 'group' : 'page', route: m.Route, outId: m.OutId })),
|
|
131
|
+
errors: checked.errors,
|
|
132
|
+
warnings: checked.warnings,
|
|
133
|
+
files: dryRun ? [] : [path.relative(dir, menuFile), path.relative(dir, relationFile)],
|
|
134
|
+
}, null, 2));
|
|
135
|
+
} else {
|
|
136
|
+
console.log(`${dryRun ? '(dry-run)' : ''}菜单 ${menus.length} 条 → standard/${targetEnd}/menu.json`);
|
|
137
|
+
for (const warning of checked.warnings) console.warn(` ! ${warning.code} ${warning.path}: ${warning.message}`);
|
|
138
|
+
if (!checked.ok) {
|
|
139
|
+
console.error('校验未通过:');
|
|
140
|
+
for (const error of checked.errors) console.error(` ✗ ${error.code} ${error.path}: ${error.message}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return checked.ok ? EXIT.OK : EXIT.INPUT;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @param {{ dir: string, end?: string, json?: boolean, webDir?: string }} options
|
|
148
|
+
*/
|
|
149
|
+
export function runMenuCheck({ dir, end = 'web', json = false, webDir }) {
|
|
150
|
+
const targetEnd = (() => {
|
|
151
|
+
try {
|
|
152
|
+
return readSpec(dir).end || end;
|
|
153
|
+
} catch {
|
|
154
|
+
return end;
|
|
155
|
+
}
|
|
156
|
+
})();
|
|
157
|
+
const menuFile = path.join(dir, 'standard', targetEnd, 'menu.json');
|
|
158
|
+
const relationFile = path.join(dir, 'standard', targetEnd, 'menuRelation.json');
|
|
159
|
+
if (!fs.existsSync(menuFile)) {
|
|
160
|
+
const message = `找不到 ${menuFile}`;
|
|
161
|
+
if (json) console.log(JSON.stringify({ ok: false, command: 'menu check', errors: [{ code: 'MENU_FILE_MISSING', path: menuFile, message }] }));
|
|
162
|
+
else console.error(message);
|
|
163
|
+
return EXIT.INPUT;
|
|
164
|
+
}
|
|
165
|
+
const menus = JSON.parse(fs.readFileSync(menuFile, 'utf8'));
|
|
166
|
+
const relation = fs.existsSync(relationFile) ? JSON.parse(fs.readFileSync(relationFile, 'utf8')) : undefined;
|
|
167
|
+
const routes = webDir ? readRouteList(webDir) : null;
|
|
168
|
+
const result = checkMenus(menus, relation, { routes: routes || undefined, end: targetEnd });
|
|
169
|
+
|
|
170
|
+
if (json) {
|
|
171
|
+
console.log(JSON.stringify({ ok: result.ok, command: 'menu check', end: targetEnd, errors: result.errors, warnings: result.warnings }, null, 2));
|
|
172
|
+
} else {
|
|
173
|
+
console.log(`${result.ok ? '✓' : '✗'} 菜单校验(${targetEnd}):${menus.length} 条,`
|
|
174
|
+
+ `${result.errors.length} 个错误,${result.warnings.length} 个警告`);
|
|
175
|
+
for (const error of result.errors) console.error(` ✗ ${error.code} ${error.path}: ${error.message}`);
|
|
176
|
+
for (const warning of result.warnings) console.warn(` ! ${warning.code} ${warning.path}: ${warning.message}`);
|
|
177
|
+
}
|
|
178
|
+
return result.ok ? EXIT.OK : EXIT.INPUT;
|
|
179
|
+
}
|