@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.
Files changed (163) hide show
  1. package/README.md +592 -2
  2. package/bin/inbiz-codegen.mjs +7 -0
  3. package/package.json +32 -4
  4. package/scripts/prepare-templates.mjs +35 -0
  5. package/src/backend/init.mjs +170 -0
  6. package/src/config.mjs +150 -0
  7. package/src/favicon.mjs +100 -0
  8. package/src/generate.mjs +935 -0
  9. package/src/generated-output.mjs +107 -0
  10. package/src/index.mjs +346 -0
  11. package/src/menu/build.mjs +252 -0
  12. package/src/menu/check.mjs +163 -0
  13. package/src/menu/command.mjs +179 -0
  14. package/src/migrate-runtime.mjs +106 -0
  15. package/src/platform/client.mjs +167 -0
  16. package/src/platform/command.mjs +386 -0
  17. package/src/platform/pack.mjs +591 -0
  18. package/src/platform/recover.mjs +236 -0
  19. package/src/platform/startup.mjs +343 -0
  20. package/src/platform/state.mjs +116 -0
  21. package/src/platform/update.mjs +157 -0
  22. package/src/platform/zip.mjs +134 -0
  23. package/src/pull.mjs +284 -0
  24. package/src/scaffold/commands.mjs +349 -0
  25. package/src/scaffold/init.mjs +370 -0
  26. package/src/scaffold/template-files.mjs +40 -0
  27. package/src/scaffold/template-source.mjs +235 -0
  28. package/src/scaffold/template-sync.mjs +335 -0
  29. package/src/transform/heaviness.mjs +380 -0
  30. package/src/transform/preview-props.mjs +108 -0
  31. package/src/transform/published-page-links.mjs +61 -0
  32. package/src/transform/published-script.mjs +110 -0
  33. package/src/transform/registry.mjs +781 -0
  34. package/src/transform/schema-to-jsx.mjs +1708 -0
  35. package/src/util/des.mjs +10 -0
  36. package/templates/README.md +34 -0
  37. package/templates/backend/Directory.Build.props +19 -0
  38. package/templates/backend/Directory.Packages.props +49 -0
  39. package/templates/backend/Macrowing.Scaffold.slnx +13 -0
  40. package/templates/backend/host/Macrowing.Start/Init/app.json +8 -0
  41. package/templates/backend/host/Macrowing.Start/Init/menus.json +4 -0
  42. package/templates/backend/host/Macrowing.Start/Initialization/InitializationDefinition.cs +245 -0
  43. package/templates/backend/host/Macrowing.Start/Initialization/InitializationEndpoints.cs +57 -0
  44. package/templates/backend/host/Macrowing.Start/Initialization/InitializationHostedService.cs +63 -0
  45. package/templates/backend/host/Macrowing.Start/Initialization/InitializationOptions.cs +44 -0
  46. package/templates/backend/host/Macrowing.Start/Initialization/InitializationRunner.cs +294 -0
  47. package/templates/backend/host/Macrowing.Start/Initialization/InitializationState.cs +72 -0
  48. package/templates/backend/host/Macrowing.Start/Initialization/MenuFactory.cs +118 -0
  49. package/templates/backend/host/Macrowing.Start/Initialization/PackageBuilder.cs +317 -0
  50. package/templates/backend/host/Macrowing.Start/Initialization/PlatformAppImporter.cs +371 -0
  51. package/templates/backend/host/Macrowing.Start/Macrowing.Start.csproj +26 -0
  52. package/templates/backend/host/Macrowing.Start/Program.cs +33 -0
  53. package/templates/backend/host/Macrowing.Start/StartModule.cs +67 -0
  54. package/templates/backend/host/Macrowing.Start/appsettings.json +38 -0
  55. package/templates/backend/modules/Macrowing.Generated/Application/GeneratedApplicationAutoMapperProfile.cs +42 -0
  56. package/templates/backend/modules/Macrowing.Generated/Domain/Entities/GeneratedEntityBase.cs +72 -0
  57. package/templates/backend/modules/Macrowing.Generated/Domain/GeneratedConsts.cs +50 -0
  58. package/templates/backend/modules/Macrowing.Generated/EntityFrameworkCore/GeneratedDbContext.cs +55 -0
  59. package/templates/backend/modules/Macrowing.Generated/EntityFrameworkCore/GeneratedDbContextModelBuilderExtensions.cs +61 -0
  60. package/templates/backend/modules/Macrowing.Generated/EntityFrameworkCore/IGeneratedDbContext.cs +32 -0
  61. package/templates/backend/modules/Macrowing.Generated/GeneratedModule.cs +89 -0
  62. package/templates/backend/modules/Macrowing.Generated/Macrowing.Generated.csproj +22 -0
  63. package/templates/backend/modules/Macrowing.InBiz.SDK/Cms/CmsClient.cs +95 -0
  64. package/templates/backend/modules/Macrowing.InBiz.SDK/Cms/CmsModels.cs +81 -0
  65. package/templates/backend/modules/Macrowing.InBiz.SDK/Dictionary/DictionaryClient.cs +44 -0
  66. package/templates/backend/modules/Macrowing.InBiz.SDK/InBizSdkModule.cs +16 -0
  67. package/templates/backend/modules/Macrowing.InBiz.SDK/InterfaceEngine/InterfaceEngineClient.cs +50 -0
  68. package/templates/backend/modules/Macrowing.InBiz.SDK/Localization/LocalizationClient.cs +28 -0
  69. package/templates/backend/modules/Macrowing.InBiz.SDK/Macrowing.InBiz.SDK.csproj +16 -0
  70. package/templates/backend/modules/Macrowing.InBiz.SDK/Message/MessageClient.cs +99 -0
  71. package/templates/backend/modules/Macrowing.InBiz.SDK/Message/MessageModels.cs +63 -0
  72. package/templates/backend/modules/Macrowing.InBiz.SDK/Model/ModelClient.cs +131 -0
  73. package/templates/backend/modules/Macrowing.InBiz.SDK/Model/ModelQuery.cs +108 -0
  74. package/templates/backend/modules/Macrowing.InBiz.SDK/Model/ModelWrite.cs +44 -0
  75. package/templates/backend/modules/Macrowing.InBiz.SDK/Permission/PermissionClient.cs +86 -0
  76. package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizPlatformException.cs +42 -0
  77. package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizPlatformRegistration.cs +60 -0
  78. package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizPlatformTransport.cs +96 -0
  79. package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizRequestContext.cs +27 -0
  80. package/templates/backend/modules/Macrowing.InBiz.SDK/README.md +117 -0
  81. package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Details/WorkFlowDetailsClient.cs +35 -0
  82. package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Details/WorkFlowDetailsQuery.cs +20 -0
  83. package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Operations/WorkFlowOperationRequests.cs +105 -0
  84. package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Operations/WorkFlowOperationsClient.cs +53 -0
  85. package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/README.md +132 -0
  86. package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Tracking/WorkFlowTrackingClient.cs +50 -0
  87. package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Upgrade/WorkFlowUpgradeClient.cs +78 -0
  88. package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Upgrade/WorkFlowUpgradeModels.cs +34 -0
  89. package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/WorkFlowContracts.cs +88 -0
  90. package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/WorkFlowRegistration.cs +59 -0
  91. package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/WorkFlowTransport.cs +163 -0
  92. package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Workbench/WorkFlowWorkbenchClient.cs +59 -0
  93. package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Workbench/WorkFlowWorkbenchModels.cs +76 -0
  94. package/templates/backend/modules/Macrowing.OC.SDK/Macrowing.OC.SDK.csproj +20 -0
  95. package/templates/backend/modules/Macrowing.OC.SDK/OcSdkModule.cs +18 -0
  96. package/templates/backend/modules/Macrowing.OC.SDK/Protos/README.md +22 -0
  97. package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/Macrowing.InBiz.SDK.Tests.csproj +14 -0
  98. package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/PlatformContractTests.cs +288 -0
  99. package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/PlatformLiveTests.cs +173 -0
  100. package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowHttpServer.cs +113 -0
  101. package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowLiveTests.cs +269 -0
  102. package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowOperationsTests.cs +80 -0
  103. package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowQueryTests.cs +163 -0
  104. package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowTransportTests.cs +111 -0
  105. package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowUpgradeTests.cs +87 -0
  106. package/templates/frontend/components/README.md +39 -0
  107. package/templates/frontend/components/app-components/README.md +33 -0
  108. package/templates/frontend/components/app-components/common/.gitkeep +0 -0
  109. package/templates/frontend/components/app-components/components/.gitkeep +0 -0
  110. package/templates/frontend/components/app-components/package.json +20 -0
  111. package/templates/frontend/mgt/.umirc.ts +574 -0
  112. package/templates/frontend/mgt/package.json +96 -0
  113. package/templates/frontend/mgt/src/app-extends.ts +9 -0
  114. package/templates/frontend/mgt/src/app.tsx +20 -0
  115. package/templates/frontend/mgt/src/config/app.ts +31 -0
  116. package/templates/frontend/mgt/src/layout/header.tsx +14 -0
  117. package/templates/frontend/mgt/src/layout/index.tsx +11 -0
  118. package/templates/frontend/mgt/src/layout/sidebar.tsx +14 -0
  119. package/templates/frontend/mgt/src/locales.ts +8 -0
  120. package/templates/frontend/mgt/src/pages/.gitkeep +0 -0
  121. package/templates/frontend/mgt/src/root.tsx +12 -0
  122. package/templates/frontend/mgt/src/route-view.tsx +3 -0
  123. package/templates/frontend/mgt/src/runtime.ts +21 -0
  124. package/templates/frontend/mgt/src/styles/app.css +1 -0
  125. package/templates/frontend/mgt/src/styles/layout.less +79 -0
  126. package/templates/frontend/mgt/tsconfig.json +65 -0
  127. package/templates/frontend/mgt/typings.d.ts +57 -0
  128. package/templates/frontend/wap/.umirc.ts +574 -0
  129. package/templates/frontend/wap/package.json +73 -0
  130. package/templates/frontend/wap/src/app-extends.ts +9 -0
  131. package/templates/frontend/wap/src/app.tsx +28 -0
  132. package/templates/frontend/wap/src/config/app.ts +31 -0
  133. package/templates/frontend/wap/src/layout/header.tsx +14 -0
  134. package/templates/frontend/wap/src/layout/index.tsx +18 -0
  135. package/templates/frontend/wap/src/layout/sidebar.tsx +14 -0
  136. package/templates/frontend/wap/src/locales.ts +8 -0
  137. package/templates/frontend/wap/src/pages/.gitkeep +0 -0
  138. package/templates/frontend/wap/src/root.tsx +12 -0
  139. package/templates/frontend/wap/src/route-view.tsx +3 -0
  140. package/templates/frontend/wap/src/runtime.ts +21 -0
  141. package/templates/frontend/wap/src/styles/adaptation.less +190 -0
  142. package/templates/frontend/wap/src/styles/app.css +7 -0
  143. package/templates/frontend/wap/src/styles/global.less +81 -0
  144. package/templates/frontend/wap/src/styles/layout.less +92 -0
  145. package/templates/frontend/wap/tsconfig.json +66 -0
  146. package/templates/frontend/wap/typings.d.ts +24 -0
  147. package/templates/frontend/web/.umirc.ts +574 -0
  148. package/templates/frontend/web/package.json +26 -0
  149. package/templates/frontend/web/src/app-extends.ts +9 -0
  150. package/templates/frontend/web/src/app.tsx +25 -0
  151. package/templates/frontend/web/src/config/app.ts +31 -0
  152. package/templates/frontend/web/src/layout/header.tsx +14 -0
  153. package/templates/frontend/web/src/layout/index.tsx +11 -0
  154. package/templates/frontend/web/src/layout/sidebar.tsx +14 -0
  155. package/templates/frontend/web/src/locales.ts +8 -0
  156. package/templates/frontend/web/src/root.tsx +12 -0
  157. package/templates/frontend/web/src/route-view.tsx +3 -0
  158. package/templates/frontend/web/src/runtime.ts +21 -0
  159. package/templates/frontend/web/src/styles/app.css +104 -0
  160. package/templates/frontend/web/src/styles/layout.less +124 -0
  161. package/templates/frontend/web/tsconfig.json +91 -0
  162. package/templates/frontend/web/typings.d.ts +46 -0
  163. package/templates/package.json +14 -0
@@ -0,0 +1,107 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { createHash } from 'node:crypto';
4
+ import { ensureDir, writeIfChanged } from './config.mjs';
5
+
6
+ const BINARY_PREFIX = 'bytes:';
7
+ const digest = (content, binary = Buffer.isBuffer(content)) => {
8
+ const bytes = binary ? content : content.toString('utf8').replace(/\r\n/g, '\n');
9
+ return (binary ? BINARY_PREFIX : '') + createHash('sha256').update(bytes).digest('hex');
10
+ };
11
+
12
+ export function createGeneratedOutput(webDir) {
13
+ const root = path.resolve(webDir);
14
+ const manifest = path.join(root, '.inbiz', 'generated-output.json');
15
+ const desired = new Map();
16
+ const fileFor = (key) => {
17
+ if (!/^src\/(?:[^/\\:]+\/)*[^/\\:]+$/.test(key) || key.split('/').some(part => part === '..' || part === '.')) {
18
+ throw new Error(`Invalid generated path: ${key}`);
19
+ }
20
+ return path.join(root, key);
21
+ };
22
+ const read = (file) => {
23
+ let dir = file;
24
+ while (dir !== root) {
25
+ if (fs.lstatSync(dir, { throwIfNoEntry: false })?.isSymbolicLink()) {
26
+ throw new Error(`Generated path is a symbolic link: ${path.relative(root, dir)}`);
27
+ }
28
+ dir = path.dirname(dir);
29
+ }
30
+ return fs.existsSync(file) ? fs.readFileSync(file) : undefined;
31
+ };
32
+
33
+ return {
34
+ add(file, content) {
35
+ const key = path.relative(root, file).split(path.sep).join('/');
36
+ fileFor(key);
37
+ if (desired.has(key)) throw new Error(`Duplicate generated path: ${key}`);
38
+ desired.set(key, content);
39
+ },
40
+ commit({ retire = new Map(), legacy = new Map(), baselineDir } = {}) {
41
+ const manifestBytes = read(manifest);
42
+ const previous = manifestBytes === undefined ? { version: 1, files: {} } : JSON.parse(manifestBytes.toString('utf8'));
43
+ if (previous?.version !== 1 || !previous.files || Array.isArray(previous.files)
44
+ || typeof previous.files !== 'object'
45
+ || Object.values(previous.files).some(hash => typeof hash !== 'string' || !/^(?:bytes:)?[a-f0-9]{64}$/.test(hash))) {
46
+ throw new Error('Invalid .inbiz/generated-output.json');
47
+ }
48
+ const baseline = baselineDir && path.resolve(baselineDir);
49
+ if (baseline && (manifestBytes !== undefined || baseline === root || !fs.statSync(baseline).isDirectory())) {
50
+ throw new Error('Use a separate, clean baseline directory only when adopting a project without generation history');
51
+ }
52
+ const files = Object.create(null);
53
+ const writes = [];
54
+ const removals = [];
55
+ const conflicts = [];
56
+ for (const key of [...new Set([...desired.keys(), ...Object.keys(previous.files), ...retire.keys()])].sort()) {
57
+ const file = fileFor(key);
58
+ const current = read(file);
59
+ const next = desired.get(key);
60
+ const base = Object.hasOwn(previous.files, key) ? previous.files[key] : undefined;
61
+ const currentHashes = new Map();
62
+ const matches = hash => {
63
+ if (current === undefined || hash === undefined) return false;
64
+ const binary = hash.startsWith(BINARY_PREFIX);
65
+ if (!currentHashes.has(binary)) currentHashes.set(binary, digest(current, binary));
66
+ return currentHashes.get(binary) === hash;
67
+ };
68
+ const retired = retire.get(key);
69
+ const isBinary = Buffer.isBuffer(next) || Buffer.isBuffer(retired);
70
+ const matchesBaseline = () => {
71
+ if (!baseline || current === undefined) return false;
72
+ const reference = path.join(baseline, key);
73
+ return fs.existsSync(reference) && matches(digest(fs.readFileSync(reference), isBinary));
74
+ };
75
+ if (next !== undefined) {
76
+ const nextHash = digest(next);
77
+ files[key] = nextHash;
78
+ if (matches(nextHash)) continue;
79
+ if ((current === undefined && base === undefined) || matches(base) ||
80
+ (base === undefined && current !== undefined &&
81
+ (matchesBaseline() || legacy.get(key)?.().some(source => matches(digest(source)))))) {
82
+ writes.push([file, next]);
83
+ }
84
+ else conflicts.push(key);
85
+ } else if (current !== undefined && (matches(base) ||
86
+ (base === undefined && (matchesBaseline() || (retired !== undefined && matches(digest(retired))))))) {
87
+ removals.push(file);
88
+ } else if (current !== undefined) conflicts.push(key);
89
+ }
90
+ if (conflicts.length) {
91
+ const error = new Error(`Generation stopped; reconcile these files or generate into an empty directory:\n${conflicts.join('\n')}`);
92
+ error.conflicts = conflicts;
93
+ throw error;
94
+ }
95
+ for (const [file, content] of writes) {
96
+ if (Buffer.isBuffer(content)) {
97
+ ensureDir(path.dirname(file));
98
+ fs.writeFileSync(file, content);
99
+ } else {
100
+ writeIfChanged(file, content);
101
+ }
102
+ }
103
+ for (const file of removals) fs.unlinkSync(file);
104
+ writeIfChanged(manifest, `${JSON.stringify({ version: 1, files }, null, 2)}\n`);
105
+ },
106
+ };
107
+ }
package/src/index.mjs ADDED
@@ -0,0 +1,346 @@
1
+ import path from 'node:path';
2
+ import fs from 'node:fs';
3
+ import { parseArgs } from 'node:util';
4
+ import { pull } from './pull.mjs';
5
+ import { generate } from './generate.mjs';
6
+ import { runMenuBuild, runMenuCheck, EXIT } from './menu/command.mjs';
7
+ import { runPlatformAppRecover, runPlatformAuthStatus, runPlatformAuthLogin, runDeployStart } from './platform/command.mjs';
8
+ import { runInit, runInitProject } from './scaffold/init.mjs';
9
+ import { resolveTemplatesRoot } from './scaffold/template-source.mjs';
10
+ import { runTemplateStatus, runTemplateUpgrade, runTemplateAdopt } from './scaffold/template-sync.mjs';
11
+ import { runAddPage, runAddComponent, runListComponents, runDoctor } from './scaffold/commands.mjs';
12
+
13
+ const HELP = `
14
+ inbiz-codegen — InBiz 出码脚手架
15
+
16
+ 新建工程:
17
+ inbiz-codegen init --out DIR [--app-id ID] [--site-type 0|1|2]
18
+ [--site-out-id ID] [--data-version-id ID]
19
+ [--server-url URL] [--port 8021] [--base-path /apps/<域名>/]
20
+ [--template-version X.Y.Z] [--template-registry URL] [--offline]
21
+ [--force] [--json]
22
+ 从模板生成一套可开发的工程(web / mgt / wap)。
23
+ 模板默认从 npmjs 的 @macrowing/inbiz-templates 拉取;
24
+ --offline 或拉取失败时回退到 CLI 内置副本。
25
+
26
+ 平台数据 / 出码:
27
+ inbiz-codegen pull [--dir DIR] [--app-id ID] [--site-type N] ...
28
+ 拉取指定应用的布局 / 菜单 / 语言包 / 页面
29
+ inbiz-codegen generate [--out-dir DIR] [--data-dir DIR] [--baseline-dir DIR]
30
+ inbiz-codegen all
31
+
32
+ 菜单(出码口径:线上只保留应用 id + 菜单,页面不再往线上建):
33
+ inbiz-codegen menu build [--dir DIR] [--end web] [--dry-run] [--json]
34
+ inbiz-codegen menu check [--dir DIR] [--end web] [--web-dir DIR] [--json]
35
+
36
+ 开发:
37
+ inbiz-codegen add page --dir DIR --route <route> [--name 名称]
38
+ [--mode plain|runtime] [--force] [--json]
39
+ inbiz-codegen add component --pkg DIR --name <PascalName> [--force] [--json]
40
+ inbiz-codegen components [--json] [--query 关键字]
41
+ inbiz-codegen doctor [--dir DIR] [--json]
42
+
43
+ 模板升级(三方比较,冲突不覆盖):
44
+ inbiz-codegen template status [--dir DIR] [--json]
45
+ inbiz-codegen template adopt [--dir DIR] [--version X.Y.Z] [--offline] [--json]
46
+ inbiz-codegen template upgrade [--dir DIR] [--to X.Y.Z] [--offline] [--force] [--json]
47
+ status 报告结构文件相对模板基线的改动
48
+ adopt 给已有工程补一份基线(不改任何文件)
49
+ upgrade 升级到新版本;冲突写成 <file>.template-new,交人工/AI 合并
50
+
51
+ 部署初始化(容器启动期调用,幂等):
52
+ inbiz-codegen start [--dir DIR] [--init-dir DIR] [--dry-run] [--json]
53
+ 读 init/{boot,menus}.json → 判断线上是否有这个应用
54
+ → 没有则创建(含菜单)→ 有则比对菜单指纹,变了就覆盖导入并发布
55
+ 结果写回 init/state.json;新应用的 APP_ID / SECRET_KEY 写回 .env
56
+
57
+ 平台:
58
+ inbiz-codegen platform auth login [--dir DIR] [--json]
59
+ inbiz-codegen platform auth status [--dir DIR] [--json]
60
+ inbiz-codegen platform app recover --dir DIR --confirm-domain DOMAIN [--dry-run] [--json]
61
+
62
+ 配置来源:<dir>/.env(默认 scaffolds/frontend/web)
63
+ 退出码:0 成功 / 2 输入或校验错误 / 3 平台拒绝 / 4 网络
64
+ `;
65
+
66
+ function defaultDir() {
67
+ const web = path.resolve(import.meta.dirname, '../../scaffolds/frontend/web');
68
+ return fs.existsSync(web) ? web : process.cwd();
69
+ }
70
+
71
+ function parse(argv, options) {
72
+ return parseArgs({ args: argv, options, allowPositionals: true }).values;
73
+ }
74
+
75
+ /** 统一的未知子命令处理。 */
76
+ function unknown(scope) {
77
+ console.error(`未知命令:${scope}`);
78
+ console.log(HELP);
79
+ process.exitCode = EXIT.INPUT;
80
+ }
81
+
82
+ export async function main(argv) {
83
+ const cmd = argv[0] || '--help';
84
+ const rest = argv.slice(1);
85
+
86
+ if (cmd === '--help' || cmd === '-h' || cmd === 'help') {
87
+ console.log(HELP);
88
+ return;
89
+ }
90
+
91
+ if (cmd === 'init') {
92
+ const values = parse(rest, {
93
+ out: { type: 'string' },
94
+ 'app-id': { type: 'string' },
95
+ 'site-type': { type: 'string' },
96
+ 'site-out-id': { type: 'string' },
97
+ 'data-version-id': { type: 'string' },
98
+ 'server-url': { type: 'string' },
99
+ port: { type: 'string' },
100
+ 'base-path': { type: 'string' },
101
+ 'proxy-prefixes': { type: 'string' },
102
+ 'template-version': { type: 'string' },
103
+ 'template-registry': { type: 'string' },
104
+ offline: { type: 'boolean' },
105
+ project: { type: 'boolean' },
106
+ force: { type: 'boolean' },
107
+ json: { type: 'boolean' },
108
+ });
109
+ if (!values.out) return unknown('init(缺少 --out)');
110
+
111
+ // 模板来源:npm 包优先,取不到就回退到 CLI 内置副本(离线/未发布/网络不可达)。
112
+ const templates = await resolveTemplatesRoot({
113
+ version: values['template-version'],
114
+ registry: values['template-registry'],
115
+ offline: !!values.offline,
116
+ });
117
+ const initOptions = {
118
+ out: values.out,
119
+ appId: values['app-id'],
120
+ siteType: values['site-type'],
121
+ siteOutId: values['site-out-id'],
122
+ dataVersionId: values['data-version-id'],
123
+ serverUrl: values['server-url'],
124
+ port: values.port ? Number(values.port) : undefined,
125
+ basePath: values['base-path'],
126
+ proxyPrefixes: values['proxy-prefixes'] ? values['proxy-prefixes'].split(',').filter(Boolean) : undefined,
127
+ templatesRoot: templates?.root,
128
+ templateLabel: templates ? `${templates.source}@${templates.version}` : '',
129
+ templateVersion: templates?.version,
130
+ force: !!values.force,
131
+ json: !!values.json,
132
+ };
133
+ const result = values.project ? runInitProject(initOptions) : runInit(initOptions);
134
+ process.exitCode = result.code;
135
+ return;
136
+ }
137
+
138
+ if (cmd === 'pull') {
139
+ const values = parse(rest, {
140
+ dir: { type: 'string' },
141
+ 'app-id': { type: 'string' },
142
+ 'site-type': { type: 'string' },
143
+ 'site-out-id': { type: 'string' },
144
+ 'data-version-id': { type: 'string' },
145
+ account: { type: 'string' },
146
+ password: { type: 'string' },
147
+ });
148
+ await pull({
149
+ dir: values.dir ? path.resolve(values.dir) : undefined,
150
+ appId: values['app-id'],
151
+ siteType: values['site-type'] !== undefined ? Number(values['site-type']) : undefined,
152
+ siteOutId: values['site-out-id'],
153
+ dataVersionId: values['data-version-id'],
154
+ account: values.account,
155
+ password: values.password,
156
+ });
157
+ return;
158
+ }
159
+
160
+ if (cmd === 'generate') {
161
+ const values = parse(rest, {
162
+ 'out-dir': { type: 'string' },
163
+ 'data-dir': { type: 'string' },
164
+ 'baseline-dir': { type: 'string' },
165
+ });
166
+ await generate({
167
+ webDir: values['out-dir'] ? path.resolve(values['out-dir']) : undefined,
168
+ dataDir: values['data-dir'] ? path.resolve(values['data-dir']) : undefined,
169
+ baselineDir: values['baseline-dir'] ? path.resolve(values['baseline-dir']) : undefined,
170
+ });
171
+ return;
172
+ }
173
+
174
+ if (cmd === 'all') {
175
+ if (rest.length !== 0) throw new Error('Use generate to select output, input, or baseline directories');
176
+ await pull();
177
+ await generate();
178
+ return;
179
+ }
180
+
181
+ if (cmd === 'menu') {
182
+ const sub = rest[0];
183
+ const values = parse(rest.slice(1), {
184
+ dir: { type: 'string' },
185
+ end: { type: 'string' },
186
+ 'web-dir': { type: 'string' },
187
+ json: { type: 'boolean' },
188
+ 'dry-run': { type: 'boolean' },
189
+ });
190
+ const options = {
191
+ dir: values.dir ? path.resolve(values.dir) : defaultDir(),
192
+ end: values.end || 'web',
193
+ json: !!values.json,
194
+ dryRun: !!values['dry-run'],
195
+ webDir: values['web-dir'] ? path.resolve(values['web-dir']) : undefined,
196
+ };
197
+ if (sub === 'build') process.exitCode = runMenuBuild(options);
198
+ else if (sub === 'check') process.exitCode = runMenuCheck(options);
199
+ else unknown(`menu ${sub || ''}`);
200
+ return;
201
+ }
202
+
203
+ if (cmd === 'template') {
204
+ const sub = rest[0];
205
+ const values = parse(rest.slice(1), {
206
+ dir: { type: 'string' },
207
+ to: { type: 'string' },
208
+ version: { type: 'string' },
209
+ registry: { type: 'string' },
210
+ offline: { type: 'boolean' },
211
+ force: { type: 'boolean' },
212
+ json: { type: 'boolean' },
213
+ });
214
+ const dir = values.dir ? path.resolve(values.dir) : process.cwd();
215
+ if (sub === 'status') {
216
+ process.exitCode = runTemplateStatus({ dir, json: !!values.json });
217
+ } else if (sub === 'upgrade') {
218
+ process.exitCode = await runTemplateUpgrade({
219
+ dir,
220
+ to: values.to,
221
+ offline: !!values.offline,
222
+ registry: values.registry,
223
+ force: !!values.force,
224
+ json: !!values.json,
225
+ });
226
+ } else if (sub === 'adopt') {
227
+ process.exitCode = await runTemplateAdopt({
228
+ dir,
229
+ version: values.version,
230
+ offline: !!values.offline,
231
+ registry: values.registry,
232
+ json: !!values.json,
233
+ });
234
+ } else {
235
+ unknown(`template ${sub || ''}`);
236
+ }
237
+ return;
238
+ }
239
+
240
+ if (cmd === 'add') {
241
+ const sub = rest[0];
242
+ const values = parse(rest.slice(1), {
243
+ dir: { type: 'string' },
244
+ pkg: { type: 'string' },
245
+ route: { type: 'string' },
246
+ name: { type: 'string' },
247
+ mode: { type: 'string' },
248
+ force: { type: 'boolean' },
249
+ json: { type: 'boolean' },
250
+ });
251
+ if (sub === 'page') {
252
+ process.exitCode = runAddPage({
253
+ dir: values.dir ? path.resolve(values.dir) : defaultDir(),
254
+ route: values.route,
255
+ name: values.name,
256
+ mode: values.mode || 'plain',
257
+ force: !!values.force,
258
+ json: !!values.json,
259
+ });
260
+ } else if (sub === 'component') {
261
+ if (!values.pkg) return unknown('add component(缺少 --pkg)');
262
+ process.exitCode = runAddComponent({
263
+ pkg: values.pkg,
264
+ name: values.name,
265
+ force: !!values.force,
266
+ json: !!values.json,
267
+ });
268
+ } else {
269
+ unknown(`add ${sub || ''}`);
270
+ }
271
+ return;
272
+ }
273
+
274
+ if (cmd === 'start') {
275
+ const values = parse(rest, {
276
+ dir: { type: 'string' },
277
+ 'init-dir': { type: 'string' },
278
+ json: { type: 'boolean' },
279
+ 'dry-run': { type: 'boolean' },
280
+ });
281
+ process.exitCode = await runDeployStart({
282
+ dir: values.dir ? path.resolve(values.dir) : defaultDir(),
283
+ initDir: values['init-dir'] ? path.resolve(values['init-dir']) : undefined,
284
+ json: !!values.json,
285
+ dryRun: !!values['dry-run'],
286
+ });
287
+ return;
288
+ }
289
+
290
+ if (cmd === 'components') {
291
+ const values = parse(rest, { json: { type: 'boolean' }, query: { type: 'string' } });
292
+ process.exitCode = runListComponents({ json: !!values.json, query: values.query });
293
+ return;
294
+ }
295
+
296
+ if (cmd === 'doctor') {
297
+ const values = parse(rest, { dir: { type: 'string' }, json: { type: 'boolean' } });
298
+ process.exitCode = runDoctor({
299
+ dir: values.dir ? path.resolve(values.dir) : defaultDir(),
300
+ json: !!values.json,
301
+ });
302
+ return;
303
+ }
304
+
305
+ if (cmd === 'platform') {
306
+ const [, resource, action] = argv;
307
+ const values = parse(argv.slice(3), {
308
+ dir: { type: 'string' },
309
+ 'confirm-domain': { type: 'string' },
310
+ 'app-name': { type: 'string' },
311
+ 'table-prefix': { type: 'string' },
312
+ 'keep-archive': { type: 'boolean' },
313
+ ip: { type: 'string' },
314
+ json: { type: 'boolean' },
315
+ 'dry-run': { type: 'boolean' },
316
+ });
317
+ const dir = values.dir ? path.resolve(values.dir) : defaultDir();
318
+
319
+ if (resource === 'auth' && action === 'login') {
320
+ process.exitCode = await runPlatformAuthLogin({ dir, ipAddress: values.ip, json: !!values.json });
321
+ return;
322
+ }
323
+ if (resource === 'auth' && action === 'status') {
324
+ process.exitCode = await runPlatformAuthStatus({ dir, json: !!values.json });
325
+ return;
326
+ }
327
+ if (resource === 'app' && action === 'recover') {
328
+ process.exitCode = await runPlatformAppRecover({
329
+ dir,
330
+ confirmDomain: values['confirm-domain'],
331
+ appName: values['app-name'],
332
+ tablePrefix: values['table-prefix'],
333
+ keepArchive: !!values['keep-archive'],
334
+ json: !!values.json,
335
+ dryRun: !!values['dry-run'],
336
+ });
337
+ return;
338
+ }
339
+ unknown(`platform ${resource || ''} ${action || ''}`.trim());
340
+ return;
341
+ }
342
+
343
+ console.error(`未知命令:${cmd}`);
344
+ console.log(HELP);
345
+ process.exitCode = 1;
346
+ }