soma-core 1.4.2 → 1.5.1

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 (216) hide show
  1. package/bin/component.cjs +240 -0
  2. package/bin/eject.cjs +174 -0
  3. package/bin/migrate.cjs +362 -0
  4. package/core/build/blocks/block-compile.ts +143 -17
  5. package/core/build/blocks/migrate-flat.ts +118 -25
  6. package/core/build/compiler/page-declaration-compiler.ts +149 -5
  7. package/core/build/compiler/route-builder.ts +0 -4
  8. package/core/build/compiler/soma-precompile.js +431 -26
  9. package/core/build/component/component-graph.ts +28 -105
  10. package/core/build/component/package-loader.ts +18 -4
  11. package/core/build/eject/component-eject.ts +8 -2
  12. package/core/build/layout/constraint-solver.ts +131 -16
  13. package/core/build/layout/css-emitter.ts +29 -10
  14. package/core/build/ooui/artifact/artifact.types.ts +275 -0
  15. package/core/build/ooui/artifact/emit-artifact.ts +269 -0
  16. package/core/build/ooui/artifact/emit-html.ts +204 -0
  17. package/core/build/ooui/artifact/index.ts +53 -0
  18. package/core/build/ooui/artifact/spatial-tree.ts +268 -0
  19. package/core/build/ooui/artifact/utility-class.ts +83 -0
  20. package/core/build/ooui/composition/candidate.types.ts +323 -0
  21. package/core/build/ooui/composition/control-model.ts +114 -0
  22. package/core/build/ooui/composition/registry.ts +520 -0
  23. package/core/build/ooui/composition/select.ts +232 -0
  24. package/core/build/ooui/diagnostics/codes.ts +230 -0
  25. package/core/build/ooui/diagnostics/error.ts +235 -0
  26. package/core/build/ooui/diagnostics/format.ts +106 -0
  27. package/core/build/ooui/diagnostics/index.ts +62 -0
  28. package/core/build/ooui/diagnostics/issue.types.ts +112 -0
  29. package/core/build/ooui/engine-slot.ts +184 -0
  30. package/core/build/ooui/index.ts +1427 -0
  31. package/core/build/ooui/intent/derive-intent.ts +450 -0
  32. package/core/build/ooui/intent/field-rules.ts +330 -0
  33. package/core/build/ooui/intent/intent.types.ts +476 -0
  34. package/core/build/ooui/intent/verb-events.ts +285 -0
  35. package/core/build/ooui/page/budget.ts +503 -0
  36. package/core/build/ooui/page/compose-page.ts +736 -0
  37. package/core/build/ooui/page/index.ts +66 -0
  38. package/core/build/ooui/page/page.types.ts +402 -0
  39. package/core/build/ooui/page/region-row.ts +241 -0
  40. package/core/build/ooui/semantic/expand-page-surface.ts +180 -0
  41. package/core/build/ooui/semantic/kernel.types.ts +187 -0
  42. package/core/build/ooui/semantic/normalize.ts +784 -0
  43. package/core/build/ooui/semantic/origin.types.ts +211 -0
  44. package/core/build/ooui/semantic/role-alias.ts +154 -0
  45. package/core/build/ooui/semantic/shorthand.ts +233 -0
  46. package/core/build/ooui/semantic/surface.types.ts +78 -0
  47. package/core/build/ooui/semantic/validate-request.ts +267 -0
  48. package/core/build/ooui/spatial/emit-utility.ts +480 -0
  49. package/core/build/ooui/spatial/ir.types.ts +443 -0
  50. package/core/build/ooui/spatial/registry.ts +565 -0
  51. package/core/build/ooui/spatial/solve.ts +769 -0
  52. package/core/build/ooui/spatial/utility.types.ts +242 -0
  53. package/core/build/ooui/trace/index.ts +40 -0
  54. package/core/build/ooui/trace/render.ts +190 -0
  55. package/core/build/ooui/trace/source.types.ts +69 -0
  56. package/core/build/ooui/trace/trace.types.ts +326 -0
  57. package/core/client/component/component-expander.ts +18 -3
  58. package/core/client/component/component-manager.ts +4 -0
  59. package/core/client/component/example-blocks.ts +1 -7
  60. package/core/client/component/theme-component-tokens.ts +12 -15
  61. package/core/client/component/verify-pack.ts +43 -0
  62. package/core/client/data/data-object-resolver.ts +27 -8
  63. package/core/client/diagnostics/telemetry-context.ts +0 -4
  64. package/core/client/engine/asset-resolver.ts +0 -1
  65. package/core/client/engine/event-engine.ts +7 -3
  66. package/core/client/engine/soma-bootstrap.ts +292 -34
  67. package/core/client/engine/style-binder.ts +0 -1
  68. package/core/client/infra/http/IHttpClient.ts +0 -6
  69. package/core/client/infra/http/SomaHttpService.ts +4 -1
  70. package/core/client/infra/notify/DomNotifyService.ts +1 -1
  71. package/core/client/infra/router/router-path.ts +0 -6
  72. package/core/client/parser/annotation-extractor.ts +1 -1
  73. package/core/client/parser/blocks/namespace.ts +30 -5
  74. package/core/client/parser/blocks/page-parser.ts +44 -1
  75. package/core/client/parser/blocks/use-parser.ts +266 -46
  76. package/core/client/parser/data-config-parser.ts +71 -3
  77. package/core/client/parser/layout-parser.ts +4 -3
  78. package/core/client/parser/reaction-parser.ts +0 -1
  79. package/core/client/parser/ui-config-parser.ts +212 -74
  80. package/core/client/theme/scene-tokens.ts +9 -3
  81. package/core/client/theme/theme-axes.ts +74 -16
  82. package/core/client/theme/theme-engine.ts +341 -70
  83. package/core/client/theme/tone-solver.ts +9 -8
  84. package/core/index.ts +24 -5
  85. package/core/server/api/load-host-service.ts +8 -2
  86. package/core/server/declaration-safety.ts +11 -0
  87. package/core/server/host/page-response-request.ts +0 -2
  88. package/core/server/host/published-page-artifact-cache.ts +244 -0
  89. package/core/server/host/published-page-assets.ts +44 -5
  90. package/core/server/mock/node/cli.mjs +0 -1
  91. package/core/server/mock/node/schema-generator.mjs +0 -1
  92. package/core/server/ooui.ts +34 -0
  93. package/core/server/page-assets.ts +8 -0
  94. package/core/server/page-declaration.ts +18 -0
  95. package/core/server/registry/asset-store.ts +327 -0
  96. package/core/server/registry/declaration-precompiler.ts +49 -6
  97. package/core/server/registry/index.ts +47 -1
  98. package/core/server/registry/page-registry-repository.ts +6 -2
  99. package/core/server/registry/page-registry-store.ts +133 -19
  100. package/core/server/registry/publish-asset-pin.ts +557 -0
  101. package/core/server/registry/services/pages/List.ts +15 -3
  102. package/core/server/registry/validation/compile-gate.ts +180 -8
  103. package/core/server/registry/validation/declaration-safety-scan.ts +113 -23
  104. package/core/shared/component/derive-sample.ts +201 -16
  105. package/core/shared/component/field-lines.ts +98 -8
  106. package/core/shared/container-breakpoints.ts +46 -0
  107. package/core/shared/object-semantics.ts +240 -0
  108. package/core/shared/ooui-declaration.ts +235 -0
  109. package/core/shared/types/component-contract.types.ts +7 -6
  110. package/core/shared/types/data-config.types.ts +95 -0
  111. package/core/shared/types/domain.types.ts +0 -24
  112. package/core/shared/types/engine.types.ts +0 -5
  113. package/core/shared/types/layout-constraint.types.ts +16 -0
  114. package/core/shared/types/ui-config.types.ts +163 -13
  115. package/dist/cli/soma.js +24 -11
  116. package/dist/core/index.cjs +1829 -1028
  117. package/dist/types/build/blocks/block-compile.d.ts +16 -1
  118. package/dist/types/build/blocks/migrate-flat.d.ts +17 -0
  119. package/dist/types/build/compiler/page-declaration-compiler.d.ts +46 -0
  120. package/dist/types/build/compiler/route-builder.d.ts +0 -1
  121. package/dist/types/build/component/component-graph.d.ts +17 -53
  122. package/dist/types/build/component/package-loader.d.ts +8 -1
  123. package/dist/types/build/layout/constraint-solver.d.ts +46 -4
  124. package/dist/types/build/ooui/artifact/artifact.types.d.ts +180 -0
  125. package/dist/types/build/ooui/artifact/emit-artifact.d.ts +58 -0
  126. package/dist/types/build/ooui/artifact/emit-html.d.ts +53 -0
  127. package/dist/types/build/ooui/artifact/index.d.ts +5 -0
  128. package/dist/types/build/ooui/artifact/spatial-tree.d.ts +48 -0
  129. package/dist/types/build/ooui/artifact/utility-class.d.ts +46 -0
  130. package/dist/types/build/ooui/composition/candidate.types.d.ts +206 -0
  131. package/dist/types/build/ooui/composition/control-model.d.ts +37 -0
  132. package/dist/types/build/ooui/composition/registry.d.ts +66 -0
  133. package/dist/types/build/ooui/composition/select.d.ts +63 -0
  134. package/dist/types/build/ooui/diagnostics/codes.d.ts +28 -0
  135. package/dist/types/build/ooui/diagnostics/error.d.ts +135 -0
  136. package/dist/types/build/ooui/diagnostics/format.d.ts +51 -0
  137. package/dist/types/build/ooui/diagnostics/index.d.ts +25 -0
  138. package/dist/types/build/ooui/diagnostics/issue.types.d.ts +56 -0
  139. package/dist/types/build/ooui/engine-slot.d.ts +41 -0
  140. package/dist/types/build/ooui/index.d.ts +286 -0
  141. package/dist/types/build/ooui/intent/derive-intent.d.ts +32 -0
  142. package/dist/types/build/ooui/intent/field-rules.d.ts +99 -0
  143. package/dist/types/build/ooui/intent/intent.types.d.ts +318 -0
  144. package/dist/types/build/ooui/intent/verb-events.d.ts +70 -0
  145. package/dist/types/build/ooui/page/budget.d.ts +28 -0
  146. package/dist/types/build/ooui/page/compose-page.d.ts +41 -0
  147. package/dist/types/build/ooui/page/index.d.ts +4 -0
  148. package/dist/types/build/ooui/page/page.types.d.ts +264 -0
  149. package/dist/types/build/ooui/page/region-row.d.ts +16 -0
  150. package/dist/types/build/ooui/semantic/expand-page-surface.d.ts +33 -0
  151. package/dist/types/build/ooui/semantic/kernel.types.d.ts +125 -0
  152. package/dist/types/build/ooui/semantic/normalize.d.ts +45 -0
  153. package/dist/types/build/ooui/semantic/origin.types.d.ts +95 -0
  154. package/dist/types/build/ooui/semantic/role-alias.d.ts +35 -0
  155. package/dist/types/build/ooui/semantic/shorthand.d.ts +60 -0
  156. package/dist/types/build/ooui/semantic/surface.types.d.ts +16 -0
  157. package/dist/types/build/ooui/semantic/validate-request.d.ts +19 -0
  158. package/dist/types/build/ooui/spatial/emit-utility.d.ts +43 -0
  159. package/dist/types/build/ooui/spatial/ir.types.d.ts +280 -0
  160. package/dist/types/build/ooui/spatial/registry.d.ts +110 -0
  161. package/dist/types/build/ooui/spatial/solve.d.ts +42 -0
  162. package/dist/types/build/ooui/spatial/utility.types.d.ts +191 -0
  163. package/dist/types/build/ooui/trace/index.d.ts +3 -0
  164. package/dist/types/build/ooui/trace/render.d.ts +42 -0
  165. package/dist/types/build/ooui/trace/source.types.d.ts +25 -0
  166. package/dist/types/build/ooui/trace/trace.types.d.ts +143 -0
  167. package/dist/types/client/component/component-expander.d.ts +1 -0
  168. package/dist/types/client/component/example-blocks.d.ts +0 -2
  169. package/dist/types/client/component/theme-component-tokens.d.ts +8 -5
  170. package/dist/types/client/data/data-object-resolver.d.ts +10 -2
  171. package/dist/types/client/diagnostics/telemetry-context.d.ts +0 -1
  172. package/dist/types/client/engine/soma-bootstrap.d.ts +123 -10
  173. package/dist/types/client/infra/http/IHttpClient.d.ts +0 -5
  174. package/dist/types/client/infra/http/SomaHttpService.d.ts +1 -1
  175. package/dist/types/client/infra/router/router-path.d.ts +0 -2
  176. package/dist/types/client/parser/blocks/namespace.d.ts +20 -0
  177. package/dist/types/client/parser/blocks/page-parser.d.ts +9 -0
  178. package/dist/types/client/parser/blocks/use-parser.d.ts +38 -4
  179. package/dist/types/client/parser/data-config-parser.d.ts +13 -0
  180. package/dist/types/client/parser/ui-config-parser.d.ts +1 -11
  181. package/dist/types/client/theme/scene-tokens.d.ts +9 -3
  182. package/dist/types/client/theme/theme-axes.d.ts +64 -1
  183. package/dist/types/client/theme/theme-engine.d.ts +13 -8
  184. package/dist/types/client/theme/tone-solver.d.ts +9 -5
  185. package/dist/types/index.d.ts +4 -6
  186. package/dist/types/server/declaration-safety.d.ts +7 -0
  187. package/dist/types/server/host/page-response-request.d.ts +0 -1
  188. package/dist/types/server/host/published-page-artifact-cache.d.ts +64 -0
  189. package/dist/types/server/ooui.d.ts +18 -0
  190. package/dist/types/server/page-assets.d.ts +7 -0
  191. package/dist/types/server/page-declaration.d.ts +14 -0
  192. package/dist/types/server/registry/asset-store.d.ts +112 -0
  193. package/dist/types/server/registry/declaration-precompiler.d.ts +23 -3
  194. package/dist/types/server/registry/index.d.ts +3 -1
  195. package/dist/types/server/registry/page-registry-store.d.ts +57 -4
  196. package/dist/types/server/registry/publish-asset-pin.d.ts +166 -0
  197. package/dist/types/server/registry/services/pages/List.d.ts +9 -3
  198. package/dist/types/server/registry/validation/compile-gate.d.ts +63 -0
  199. package/dist/types/shared/component/derive-sample.d.ts +14 -0
  200. package/dist/types/shared/container-breakpoints.d.ts +42 -0
  201. package/dist/types/shared/object-semantics.d.ts +42 -0
  202. package/dist/types/shared/ooui-declaration.d.ts +135 -0
  203. package/dist/types/shared/types/component-contract.types.d.ts +7 -6
  204. package/dist/types/shared/types/data-config.types.d.ts +86 -0
  205. package/dist/types/shared/types/domain.types.d.ts +0 -21
  206. package/dist/types/shared/types/engine.types.d.ts +0 -4
  207. package/dist/types/shared/types/layout-constraint.types.d.ts +1 -0
  208. package/dist/types/shared/types/ui-config.types.d.ts +157 -12
  209. package/next.config.js +106 -13
  210. package/package.json +29 -2
  211. package/core/client/host-bridge/index.ts +0 -27
  212. package/core/client/parser/yaml-loader.ts +0 -36
  213. package/core/shared/manifest/manifest-contract.ts +0 -12
  214. package/dist/types/client/host-bridge/index.d.ts +0 -9
  215. package/dist/types/client/parser/yaml-loader.d.ts +0 -21
  216. package/dist/types/shared/manifest/manifest-contract.d.ts +0 -4
@@ -0,0 +1,240 @@
1
+ // `soma component eject|diff|upgrade` 与 `soma pack init` 的 CLI 外壳
2
+ // (REQ-009 EC-1/2/3 + REQ-010 F-45)。
3
+ //
4
+ // 真正的逻辑在 core/build/{eject/component-eject,pack/pack-init}.ts(可测的纯模块),
5
+ // 这里只做参数解析与打印——与页面 eject 同一分工。
6
+
7
+ const fs = require('node:fs');
8
+ const path = require('node:path');
9
+
10
+ /**
11
+ * 规划器是 TS 源码,用 jiti 现编译加载——与 `soma db` / `soma eject` / `soma migrate` 同一条路。
12
+ *
13
+ * 曾经是"先 dist/cli、回退 tsx/cjs":dist/cli 下从来不产这几个模块(tsconfig.cli.json
14
+ * 只编 bin/soma.ts),于是永远走 tsx;而 tsx 只在 devDependencies 里——仓内跑得通、
15
+ * 装出去的包里一跑就 "Cannot find module 'tsx/cjs'"。jiti 是运行时依赖。
16
+ */
17
+ function loadBuildModule(relative) {
18
+ const createJiti = require('jiti');
19
+ const jiti = createJiti(__filename, { interopDefault: true });
20
+ return jiti(path.resolve(__dirname, '../core/build', `${relative}.ts`));
21
+ }
22
+
23
+ function usage() {
24
+ console.error([
25
+ 'Usage:',
26
+ ' soma component eject <组件名> [选项] 把官方组件的源码交给宿主维护',
27
+ ' soma component diff <文件路径> 看上游从 eject 那天起改了什么',
28
+ ' soma component upgrade <文件路径> 把上游的改动三方合并进你的副本',
29
+ ' soma pack init <包名> [--dir <目录>] 生成一个组件包骨架',
30
+ '',
31
+ 'eject 选项:',
32
+ ' --to <目录> 目标组件包目录(缺省 ./src/soma-packs)',
33
+ ' --ui <目录> soma-ui 包根(缺省从 node_modules 解析)',
34
+ ' --force 覆盖同名既有文件(默认撞名即中止)',
35
+ ' --dry-run 只打印计划,不落盘',
36
+ '',
37
+ 'upgrade 说明:',
38
+ ' 三方合并(基线 / 你的副本 / 上游新版)。上游改 A 行、你改 B 行 → 自动合并;',
39
+ ' 同一行两边都改 → 写 git 形状的冲突标记,并且**不更新基线**',
40
+ ' (基线一旦提前推进,这次没解完的冲突下次就再没有共同祖先可依了)。',
41
+ ].join('\n'));
42
+ }
43
+
44
+ function resolveUiRoot(explicit) {
45
+ if (explicit) return path.resolve(explicit);
46
+ try {
47
+ return path.dirname(require.resolve('soma-ui/package.json', { paths: [process.cwd()] }));
48
+ } catch {
49
+ const local = path.resolve(process.cwd(), 'soma-ui');
50
+ if (fs.existsSync(path.join(local, 'package.json'))) return local;
51
+ throw new Error(
52
+ '找不到 soma-ui 包根。请用 --ui <目录> 指出它,'
53
+ + '或在装了 soma-ui 的工程目录里运行本命令。',
54
+ );
55
+ }
56
+ }
57
+
58
+ function parseFlags(rest) {
59
+ const flags = { positional: [], to: '', ui: '', dir: '', force: false, dryRun: false };
60
+ for (let index = 0; index < rest.length; index += 1) {
61
+ const token = rest[index];
62
+ if (token === '--force') flags.force = true;
63
+ else if (token === '--dry-run') flags.dryRun = true;
64
+ else if (token === '--to') flags.to = rest[++index] || '';
65
+ else if (token === '--ui') flags.ui = rest[++index] || '';
66
+ else if (token === '--dir') flags.dir = rest[++index] || '';
67
+ else if (token.startsWith('--')) {
68
+ console.error(`[soma] 无法识别的参数:${token}`);
69
+ usage();
70
+ process.exit(1);
71
+ } else flags.positional.push(token);
72
+ }
73
+ return flags;
74
+ }
75
+
76
+ function runComponentEject(flags) {
77
+ const { planComponentEject, applyComponentEject } = loadBuildModule('eject/component-eject');
78
+ const name = flags.positional[0];
79
+ if (!name) { usage(); process.exit(1); }
80
+
81
+ const uiRoot = resolveUiRoot(flags.ui);
82
+ const to = path.resolve(flags.to || path.join(process.cwd(), 'src', 'soma-packs'));
83
+ const plan = planComponentEject({ name, to, uiRoot });
84
+
85
+ console.log(`[soma component eject] ${name} ← ${plan.provenance.from}@${plan.provenance.version}`);
86
+ for (const rewrite of plan.rewrittenImports) {
87
+ console.log(` 导入改写 ${rewrite.from} → ${rewrite.to}`);
88
+ }
89
+ console.log(` 共享底座 ${plan.requiredShared.join('、') || '(无)'}(不复制,运行期从 soma-ui 取)`);
90
+ console.log(
91
+ ` 组件依赖 ${(plan.requiredComponents || []).join('、') || '(无)'}`
92
+ + '(仍从 soma-ui 取;它们要在你的注册表里可解析)',
93
+ );
94
+ for (const file of plan.files) console.log(` 产出 ${path.join(to, file.path)}`);
95
+
96
+ if (flags.dryRun) {
97
+ console.log('\n--dry-run:没有落盘。去掉它即写入。');
98
+ return;
99
+ }
100
+ const result = applyComponentEject(plan, { to, force: flags.force });
101
+ console.log(`\n已写入 ${result.written.length} 个文件。`);
102
+ console.log([
103
+ '下一步:',
104
+ ` 1. 把 ${name} 注册进你的组件包(src/index.ts 的 components 表);`,
105
+ ' 2. 跑一次 verifyComponentPack —— 产物仍要过裸色值 / RTL / ai 段等闸门;',
106
+ ' 3. 合成时它会以 overrides: soma-ui 替掉官方同名组件,并强制过契约兼容检查。',
107
+ ].join('\n'));
108
+ }
109
+
110
+ function runComponentDiff(flags) {
111
+ const { diffEjectedComponent } = loadBuildModule('eject/component-eject');
112
+ const target = flags.positional[0];
113
+ if (!target) { usage(); process.exit(1); }
114
+
115
+ const result = diffEjectedComponent(path.resolve(target), resolveUiRoot(flags.ui));
116
+ if (!result.changed) {
117
+ console.log(
118
+ `[soma component diff] ${result.componentName}:上游自 `
119
+ + `${result.ejectedFrom.from}@${result.ejectedFrom.version} 起没有改动。`,
120
+ );
121
+ return;
122
+ }
123
+ console.log(
124
+ `[soma component diff] ${result.componentName}:上游已改动`
125
+ + `(eject 自 ${result.ejectedFrom.version},当前 ${result.currentVersion})。\n`,
126
+ );
127
+ if (!result.hasBaseline) {
128
+ console.log(
129
+ '找不到 eject 时落盘的基线(.soma-eject-base/),只能判定"变了",出不了逐行 diff。'
130
+ + '\n重新 eject 一次即可补上基线(--dry-run 先看计划,或 --force 覆盖)。',
131
+ );
132
+ return;
133
+ }
134
+ console.log(result.unified);
135
+ console.log(
136
+ '\n以上是**上游**从你 eject 那天起的变化,不是你本地的改动。'
137
+ + '\n要不要把它合回你的副本,由你决定。',
138
+ );
139
+ }
140
+
141
+ function runComponentUpgrade(flags) {
142
+ const { upgradeEjectedComponent } = loadBuildModule('eject/component-upgrade');
143
+ const target = flags.positional[0];
144
+ if (!target) { usage(); process.exit(1); }
145
+
146
+ const result = upgradeEjectedComponent({
147
+ file: path.resolve(target),
148
+ uiRoot: resolveUiRoot(flags.ui),
149
+ dryRun: flags.dryRun,
150
+ });
151
+
152
+ if (result.upToDate) {
153
+ console.log(
154
+ `[soma component upgrade] ${result.componentName}:上游自 `
155
+ + `${result.ejectedFrom.from}@${result.ejectedFrom.version} 起没有改动,无需升级。`,
156
+ );
157
+ return;
158
+ }
159
+
160
+ console.log(
161
+ `[soma component upgrade] ${result.componentName}:`
162
+ + `${result.ejectedFrom.version} → ${result.currentVersion}\n`
163
+ + ` 自动合入上游改动 ${result.applied} 行\n`
164
+ + ` 冲突 ${result.conflicts} 处`,
165
+ );
166
+
167
+ if (flags.dryRun) {
168
+ console.log('\n--dry-run:没有落盘。以下是上游这段时间的改动,供你先看一眼:\n');
169
+ console.log(result.upstreamDiff);
170
+ return;
171
+ }
172
+
173
+ console.log(`\n已写入 ${result.written.length} 个文件。`);
174
+ if (result.conflicts > 0) {
175
+ console.log([
176
+ '',
177
+ `有 ${result.conflicts} 处冲突需要你处理(文件里已用 <<<<<<< / ======= / >>>>>>> 标出)。`,
178
+ '解完之后**必须**再跑一次本命令,让它把基线推进到当前上游版本——',
179
+ '基线没推进的话,下一次升级会把你这次已经解过的冲突再报一遍。',
180
+ '',
181
+ '解完还要过两道闸门:',
182
+ ' 1. verifyComponentPack —— 裸色值 / RTL / ai 段等仍要通过;',
183
+ ' 2. 契约兼容检查 —— 合成时它会以 overrides 替掉官方同名组件。',
184
+ ].join('\n'));
185
+ } else {
186
+ console.log('基线已推进到当前上游版本,下一次 upgrade 会从这里继续。');
187
+ }
188
+ }
189
+
190
+ function runPackInit(flags) {
191
+ const { planPackInit } = loadBuildModule('pack/pack-init');
192
+ const name = flags.positional[0];
193
+ if (!name) { usage(); process.exit(1); }
194
+
195
+ const dir = path.resolve(flags.dir || path.join(process.cwd(), name));
196
+ const files = planPackInit(name);
197
+
198
+ if (flags.dryRun) {
199
+ console.log(`[soma pack init] ${name} → ${dir}(--dry-run)`);
200
+ for (const file of files) console.log(` ${file.path}`);
201
+ return;
202
+ }
203
+ const existing = files
204
+ .map((file) => path.join(dir, file.path))
205
+ .filter((filePath) => fs.existsSync(filePath));
206
+ if (existing.length > 0 && !flags.force) {
207
+ console.error(`[soma pack init] 目标已存在:${existing.join('、')}\n 要覆盖请加 --force`);
208
+ process.exit(1);
209
+ }
210
+ for (const file of files) {
211
+ const target = path.join(dir, file.path);
212
+ fs.mkdirSync(path.dirname(target), { recursive: true });
213
+ fs.writeFileSync(target, file.content, 'utf8');
214
+ console.log(` 写入 ${target}`);
215
+ }
216
+ console.log(`\n[soma pack init] ${name} 就绪。先跑一次它自带的自检:\n cd ${dir} && npm test`);
217
+ }
218
+
219
+ function main(argv) {
220
+ const [group, action, ...rest] = argv;
221
+ const flags = parseFlags(rest);
222
+ try {
223
+ if (group === 'component' && action === 'eject') return runComponentEject(flags);
224
+ if (group === 'component' && action === 'diff') return runComponentDiff(flags);
225
+ if (group === 'component' && action === 'upgrade') return runComponentUpgrade(flags);
226
+ if (group === 'pack' && action === 'init') return runPackInit(flags);
227
+ usage();
228
+ process.exit(1);
229
+ } catch (error) {
230
+ console.error(error instanceof Error ? error.message : String(error));
231
+ process.exit(1);
232
+ }
233
+ }
234
+
235
+ module.exports = { main };
236
+
237
+ // 发布入口(dist/cli/soma.js)以独立进程调本文件:`node component.cjs <component|pack> <动作> ...`
238
+ if (require.main === module) {
239
+ main(process.argv.slice(2));
240
+ }
package/bin/eject.cjs ADDED
@@ -0,0 +1,174 @@
1
+ // `soma eject <页面名>` —— 把运行态注册的页面固化成开发态源码。
2
+ //
3
+ // 这是两态模型里唯一的单向门:运行态页面按名寻址、不占 URL;固化之后它有了
4
+ // 自己的文件与 Domain 类,走文件路由、进 git、受全仓闸门看守——从此归开发态管辖。
5
+ //
6
+ // 三条刻意的默认值(都可以显式推翻,但推翻要写出来):
7
+ // 1. **不覆盖**:目标文件已存在就整体中止。宿主目录里的东西是人写的,默认不动它;
8
+ // 2. **不提交**:写完只打印下一步的 git 命令。替开发者提交是越界——
9
+ // 他可能还想改 Domain 类、还想看看 diff。`--commit` 明确要求了才提交;
10
+ // 3. **校验不过就回滚**:`--verify "<命令>"` 给了就跑,不过则把这次写的全部撤掉,
11
+ // 宿主目录回到固化之前的样子(半新半旧的目录比失败本身更难收拾)。
12
+ //
13
+ // 取材直接读运行时态文件(data/soma-runtime/pages/<名>.json),不要求服务器在跑。
14
+
15
+ const fs = require('node:fs');
16
+ const path = require('node:path');
17
+ const { spawnSync } = require('node:child_process');
18
+
19
+ function usage() {
20
+ console.error([
21
+ 'Usage: soma eject <页面名> [选项]',
22
+ '',
23
+ ' 把注册中心里的运行态页面固化成宿主源码(html + _ui.yaml + Domain 类)。',
24
+ '',
25
+ '选项:',
26
+ ' --version <N> 固化指定版本(缺省:最新 published 版本)',
27
+ ' --registry <目录> 运行时态页面目录(缺省 data/soma-runtime/pages)',
28
+ ' --host <目录> 宿主根目录(缺省当前目录)',
29
+ ' --force 覆盖同名既有文件(默认撞名即中止)',
30
+ ' --verify "<命令>" 落盘后跑一条校验命令,不过则整体回滚',
31
+ ' --commit 校验通过后 git add + commit(默认只打印命令)',
32
+ ].join('\n'));
33
+ }
34
+
35
+ function parseArgs(argv) {
36
+ const options = { name: '', version: undefined, registryDir: '', hostDir: '', force: false, verify: '', commit: false };
37
+ const rest = argv.slice(3);
38
+ for (let index = 0; index < rest.length; index += 1) {
39
+ const token = rest[index];
40
+ if (token === '--force') options.force = true;
41
+ else if (token === '--commit') options.commit = true;
42
+ else if (token === '--version') options.version = Number(rest[++index]);
43
+ else if (token === '--registry') options.registryDir = rest[++index] || '';
44
+ else if (token === '--host') options.hostDir = rest[++index] || '';
45
+ else if (token === '--verify') options.verify = rest[++index] || '';
46
+ else if (!token.startsWith('--') && !options.name) options.name = token;
47
+ else {
48
+ console.error(`[soma eject] 无法识别的参数:${token}`);
49
+ usage();
50
+ process.exit(1);
51
+ }
52
+ }
53
+ return options;
54
+ }
55
+
56
+ /** 从运行时态文件里挑出要固化的那一版 */
57
+ function readDeclaration(registryDir, name, version) {
58
+ const file = path.join(registryDir, `${name}.json`);
59
+ if (!fs.existsSync(file)) {
60
+ throw new Error(`注册中心里没有页面 "${name}"(找不到 ${file})`);
61
+ }
62
+ const versions = JSON.parse(fs.readFileSync(file, 'utf8'));
63
+ if (!Array.isArray(versions) || versions.length === 0) {
64
+ throw new Error(`页面 "${name}" 的记录为空或损坏:${file}`);
65
+ }
66
+ if (version !== undefined) {
67
+ if (!Number.isInteger(version) || version < 1) throw new Error('--version 必须是正整数');
68
+ const picked = versions.find((record) => record.version === version);
69
+ if (!picked) throw new Error(`页面 "${name}" 没有版本 ${version}`);
70
+ return picked;
71
+ }
72
+ for (let index = versions.length - 1; index >= 0; index -= 1) {
73
+ if (versions[index].status === 'archived') {
74
+ throw new Error(`页面 "${name}" 已注销;要固化历史版本请显式 --version <N>`);
75
+ }
76
+ if (versions[index].status === 'published') return versions[index];
77
+ }
78
+ throw new Error(`页面 "${name}" 没有已发布版本;草稿请显式 --version <N>`);
79
+ }
80
+
81
+ function runEject(argv) {
82
+ const options = parseArgs(argv);
83
+ if (!options.name) {
84
+ usage();
85
+ process.exit(1);
86
+ }
87
+ // SOMA_INVOKE_CWD:经 soma CLI 委派进来时,调用方的原始 cwd(委派本身以包根为 cwd)
88
+ const hostDir = path.resolve(options.hostDir || process.env.SOMA_INVOKE_CWD || process.cwd());
89
+ const registryDir = path.resolve(
90
+ options.registryDir
91
+ || process.env.SOMA_PAGE_REGISTRY_DIR
92
+ || path.join(hostDir, 'data', 'soma-runtime', 'pages'),
93
+ );
94
+
95
+ // 规划器与落盘器是 TS 源码,用 jiti 现编译加载(与宿主服务加载器同一条路)
96
+ const createJiti = require('jiti');
97
+ const jiti = createJiti(__filename, { interopDefault: true });
98
+ const packageRoot = path.resolve(__dirname, '..');
99
+ const { planEject } = jiti(path.join(packageRoot, 'core/build/eject/plan-eject.ts'));
100
+ const { applyEject } = jiti(path.join(packageRoot, 'core/build/eject/apply-eject.ts'));
101
+ const { dryRunCompile } = jiti(path.join(packageRoot, 'core/server/registry/validation/compile-gate.ts'));
102
+
103
+ let record;
104
+ try {
105
+ record = readDeclaration(registryDir, options.name, options.version);
106
+ } catch (error) {
107
+ console.error(`[soma eject] ${error.message}`);
108
+ process.exit(1);
109
+ }
110
+
111
+ // 编不过的声明不许固化:它进了源码目录就是全仓的问题,而不再只是一条注册记录
112
+ const uncompilable = dryRunCompile(record.declaration);
113
+ if (uncompilable.length > 0) {
114
+ console.error(`[soma eject] 声明编译不过,拒绝固化:\n ${uncompilable.join('\n ')}`);
115
+ process.exit(1);
116
+ }
117
+
118
+ let plan;
119
+ let applied;
120
+ try {
121
+ plan = planEject(options.name, record.declaration);
122
+ applied = applyEject(plan, { hostDir, force: options.force });
123
+ } catch (error) {
124
+ console.error(`[soma eject] ${error.message}`);
125
+ process.exit(1);
126
+ }
127
+
128
+ console.log(`[soma eject] ${options.name} v${record.version} → 开发态`);
129
+ for (const file of applied.written) {
130
+ console.log(` 写入 ${file}${applied.overwritten.includes(file) ? '(覆盖)' : ''}`);
131
+ }
132
+
133
+ if (options.verify) {
134
+ console.log(`[soma eject] 校验:${options.verify}`);
135
+ const verified = spawnSync(options.verify, { cwd: hostDir, stdio: 'inherit', shell: true });
136
+ if (verified.status !== 0) {
137
+ applied.rollback();
138
+ console.error('[soma eject] 校验未通过,已整体回滚——宿主目录回到固化之前的样子。');
139
+ process.exit(1);
140
+ }
141
+ }
142
+
143
+ const paths = applied.written.join(' ');
144
+ if (options.commit) {
145
+ const add = spawnSync('git', ['add', ...applied.written], { cwd: hostDir, stdio: 'inherit' });
146
+ if (add.status !== 0) {
147
+ console.error('[soma eject] git add 失败;文件已写入,未提交。');
148
+ process.exit(1);
149
+ }
150
+ const message = `feat: 固化页面 ${options.name}(运行态 v${record.version} → 开发态)`;
151
+ const commit = spawnSync('git', ['commit', '-m', message], { cwd: hostDir, stdio: 'inherit' });
152
+ if (commit.status !== 0) {
153
+ console.error('[soma eject] git commit 失败;文件已写入并暂存,未提交。');
154
+ process.exit(1);
155
+ }
156
+ console.log('[soma eject] 已提交。');
157
+ } else {
158
+ console.log([
159
+ '',
160
+ `[soma eject] 页面固化完毕,访问路径 ${plan.url}`,
161
+ ' 下一步(未替你执行——提交是你的决定):',
162
+ ` git add ${paths}`,
163
+ ` git commit -m "feat: 固化页面 ${options.name}"`,
164
+ ` 注册中心里的 ${options.name} 仍在;确认固化版可用后可注销它:`,
165
+ ` curl -X POST <host>/api/soma-registry/pages/unregister -H 'Content-Type: application/json' -d '{"name":"${options.name}"}'`,
166
+ ].join('\n'));
167
+ }
168
+ }
169
+
170
+ module.exports = { runEject, readDeclaration, parseArgs };
171
+
172
+ if (require.main === module) {
173
+ runEject(process.argv);
174
+ }