nx-kv 0.51.0 → 0.53.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/CHANGELOG.md +62 -0
- package/package.json +2 -1
- package/src/core/contract-snapshot.ts +24 -0
- package/src/core/contract-types.ts +106 -0
- package/src/modules/docs/actions/_manifest.generated.ts +3 -0
- package/src/modules/docs/actions/docs-bindings.ts +45 -8
- package/src/modules/docs/actions/docs-gc.ts +32 -0
- package/src/modules/docs/gc.ts +87 -0
- package/src/modules/docs/refs.ts +4 -1
- package/src/modules/docs/service.ts +25 -6
- package/src/modules/localtask/action-render.ts +1 -1
- package/src/modules/localtask/actions/localtask-config.ts +9 -4
- package/src/modules/localtask/actions/localtask-list.ts +39 -0
- package/src/modules/repos/actions/repo-update.ts +1 -1
- package/src/modules/system/actions/_manifest.generated.ts +3 -0
- package/src/modules/system/actions/bootstrap.ts +25 -0
- package/src/modules/system/actions/contract.ts +58 -0
- package/src/modules/tasktool/delivery-template.ts +5 -1
- package/src/modules/tasktool/get.ts +9 -5
- package/src/modules/tasktool/plugins/waitgoal-config.ts +9 -2
- package/src/runtime/action-decorator.ts +20 -2
- package/src/runtime/contract.ts +66 -0
- package/src/runtime/dartgen.ts +347 -0
- package/src/runtime/registry.ts +17 -0
- package/src/runtime/zod-bridge.ts +101 -17
- package/src/shared/prompt-blocks.ts +25 -3
- package/src/web/frontend/App.tsx +15 -12
- package/src/web/frontend/api/cli-tree.json +30 -1
- package/src/web/frontend/api/client.generated.ts +20 -2
- package/src/web/frontend/components/layout/breadcrumb.tsx +44 -35
- package/src/web/frontend/components/layout/crumbs.ts +60 -0
- package/src/web/frontend/components/md/doc-keys.ts +53 -0
- package/src/web/frontend/components/md/md-panes-root.tsx +9 -2
- package/src/web/frontend/components/repos/get-loop-modal.tsx +27 -5
- package/src/web/frontend/components/sidebar/index.tsx +99 -9
- package/src/web/frontend/components/sidebar/model.ts +18 -4
- package/src/web/frontend/components/sidebar/row-menu.tsx +9 -40
- package/src/web/frontend/components/sidebar/task-children.tsx +3 -1
- package/src/web/frontend/components/toast/index.tsx +46 -3
- package/src/web/frontend/modules/md/styles/docs.css +8 -0
- package/src/web/frontend/page/dir/task-actions.ts +20 -2
- package/src/web/frontend/page/doc/api.ts +0 -20
- package/src/web/frontend/page/doc/index.tsx +100 -29
- package/src/web/frontend/page/overview/api.ts +13 -0
- package/src/web/frontend/page/overview/index.tsx +60 -7
- package/src/web/frontend/page/refs/index.tsx +120 -41
- package/src/web/frontend/page/refs/style.css +13 -2
- package/src/web/frontend/page/repos/board-admin.tsx +3 -3
- package/src/web/frontend/page/repos/board-refs.tsx +3 -3
- package/src/web/frontend/page/repos/doc-search-modal.tsx +1 -3
- package/src/web/frontend/page/repos/index.tsx +19 -22
- package/src/web/frontend/page/repos/repo-table.tsx +1 -1
- package/src/web/frontend/page/settings/api.ts +11 -1
- package/src/web/frontend/page/settings/prompt-templates-section.tsx +6 -8
- package/src/web/frontend/refs/api.ts +65 -0
- package/src/web/frontend/refs/follow.ts +91 -0
- package/src/web/frontend/refs/index.ts +15 -0
- package/src/web/frontend/refs/ops.ts +58 -0
- package/src/web/frontend/refs/rows.ts +51 -0
- package/src/web/frontend/repo-data.ts +49 -0
- package/src/web/frontend/styles/repos.css +9 -2
- package/src/web/frontend/styles-v2/dag.css +12 -5
- package/src/web/frontend/styles-v2/sidebar.css +6 -0
- package/src/web/frontend/workspace/layout.ts +38 -0
- package/src/web/frontend/workspace/open.ts +8 -1
- package/src/web/frontend/workspace/panels.ts +42 -1
- package/src/web/public/assets/app-context-B0QwHGOd.js +1 -0
- package/src/web/public/assets/{bar-DoW8R6oG.js → bar-BBGb6Y5_.js} +1 -1
- package/src/web/public/assets/{board-view-aAIEbSQi.js → board-view-BJCwrtHc.js} +1 -1
- package/src/web/public/assets/{composer-launch-CAe2Kg7t.js → composer-launch-CjUpcCqD.js} +1 -1
- package/src/web/public/assets/{composer-panel-BnzPY41l.js → composer-panel-ECutPtRA.js} +1 -1
- package/src/web/public/assets/{dag-Gm4RcvEg.js → dag-DISpxumr.js} +1 -1
- package/src/web/public/assets/dir-B8B1sEfY.js +3 -0
- package/src/web/public/assets/doc-CmGraKDe.js +6 -0
- package/src/web/public/assets/frontmatter-b044qTyx.js +2 -0
- package/src/web/public/assets/group-BA0tiEYP.js +1 -0
- package/src/web/public/assets/index-59ZF4OlE.css +1 -0
- package/src/web/public/assets/index-C2C8mDHD.js +2 -0
- package/src/web/public/assets/md-history-8KUZ9ipc.js +39 -0
- package/src/web/public/assets/{md-parse-CLyUaUto.js → md-parse-Bqb35k5w.js} +1 -1
- package/src/web/public/assets/{member-picker-XT12VRe6.js → member-picker-CQksP35E.js} +1 -1
- package/src/web/public/assets/model-lEeblPg0.js +2 -0
- package/src/web/public/assets/overview-VC4HFUS_.js +1 -0
- package/src/web/public/assets/{progress-CNam3Wuj.js → progress-D0qBwZIV.js} +2 -2
- package/src/web/public/assets/refs-BhgP7Ryy.js +2 -0
- package/src/web/public/assets/refs-IQi1h4-q.css +1 -0
- package/src/web/public/assets/repo-form-modal-B_ruPwr0.js +1 -0
- package/src/web/public/assets/repos-DP6AFrI6.js +14 -0
- package/src/web/public/assets/route-crumb-BHZU-2HE.js +1 -0
- package/src/web/public/assets/settings-C_8mDUqx.js +8 -0
- package/src/web/public/assets/status-tag-DC7mdpkI.js +1 -0
- package/src/web/public/assets/task-IoEm4Lmw.js +11 -0
- package/src/web/public/assets/task-row-3OXcmAJb.js +1 -0
- package/src/web/public/assets/{task-utils-DabL4pWl.js → task-utils-BqIPKFHQ.js} +1 -1
- package/src/web/public/assets/{templates-B5C-04px.js → templates-B2zDwQG0.js} +1 -1
- package/src/web/public/assets/{use-rail-drag-DWh_6gVG.js → use-rail-drag-2mF0AHna.js} +1 -1
- package/src/web/public/assets/workspace-BwpvQUst.js +28 -0
- package/src/web/public/index.html +18 -10
- package/src/web/public/assets/Introduction-Cd1EVmLJ.js +0 -6
- package/src/web/public/assets/app-context-BgpTewyA.js +0 -1
- package/src/web/public/assets/button-xyR2UROd.js +0 -1
- package/src/web/public/assets/dir-DOmboUMw.js +0 -3
- package/src/web/public/assets/group-CRy-GSr6.js +0 -1
- package/src/web/public/assets/index-CUigrOD7.js +0 -2
- package/src/web/public/assets/index-DAz3Dlo_.css +0 -1
- package/src/web/public/assets/md-history-B7SCKnJP.js +0 -39
- package/src/web/public/assets/model-CWJ8_2dN.js +0 -2
- package/src/web/public/assets/overview-Y4aroMeW.js +0 -1
- package/src/web/public/assets/refs-BUfvLix6.js +0 -2
- package/src/web/public/assets/refs-C5idWnUj.css +0 -1
- package/src/web/public/assets/repos-B098Pmd4.js +0 -14
- package/src/web/public/assets/route-crumb-DynMISE4.js +0 -1
- package/src/web/public/assets/settings-DcpAnoPP.js +0 -8
- package/src/web/public/assets/status-tag-nNyC3U7Q.js +0 -1
- package/src/web/public/assets/task-93mVhj0L.js +0 -11
- package/src/web/public/assets/task-row-Bsptt2zF.js +0 -1
- package/src/web/public/assets/task-status-BHF-y9Aq.js +0 -2
- package/src/web/public/assets/ui-Y0aL-bGk.js +0 -1
- package/src/web/public/assets/workspace-D_G-B3TX.js +0 -28
- /package/src/web/frontend/{page → modules}/refs/api.ts +0 -0
- /package/src/web/public/assets/{Introduction-Iuxssa-b.css → doc-Iuxssa-b.css} +0 -0
- /package/src/web/public/assets/{task-status-BZrdOH46.css → frontmatter-BZrdOH46.css} +0 -0
- /package/src/web/public/assets/{button-DA4lNJeC.css → repo-form-modal-DA4lNJeC.css} +0 -0
|
@@ -7,17 +7,20 @@
|
|
|
7
7
|
import type { ActionBase } from '../../../runtime/action-base.js';
|
|
8
8
|
import type { Container } from '../../../runtime/container.js';
|
|
9
9
|
import { BootstrapAction } from './bootstrap.js';
|
|
10
|
+
import { ContractAction } from './contract.js';
|
|
10
11
|
import { HealthAction } from './health.js';
|
|
11
12
|
import { RoutesAction } from './routes.js';
|
|
12
13
|
|
|
13
14
|
/** 本模块的全部 action 类(容器按这个顺序建实例)。 */
|
|
14
15
|
export const ACTIONS: readonly (new (deps: Container) => ActionBase)[] = [
|
|
15
16
|
BootstrapAction,
|
|
17
|
+
ContractAction,
|
|
16
18
|
HealthAction,
|
|
17
19
|
RoutesAction,
|
|
18
20
|
];
|
|
19
21
|
|
|
20
22
|
// 再导出类本身:前端 apiclient 的返回类型从这里 type-only 引用(实现即契约)。
|
|
21
23
|
export { BootstrapAction };
|
|
24
|
+
export { ContractAction };
|
|
22
25
|
export { HealthAction };
|
|
23
26
|
export { RoutesAction };
|
|
@@ -16,6 +16,30 @@ const VERSION: string = JSON.parse(
|
|
|
16
16
|
/** system.bootstrap 的入参契约(CLI 与 HTTP 共用同一份)。 */
|
|
17
17
|
const input = z.object({});
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* system.bootstrap 的**出参视图**:端侧启动只要「我是谁 / 版本 / 命令表」,
|
|
21
|
+
* 不声明 projectRoot 也行——但端侧界面要显示当前宿主目录,所以留着。
|
|
22
|
+
*/
|
|
23
|
+
const output = z.object({
|
|
24
|
+
app: z.object({
|
|
25
|
+
name: z.string().describe('应用标识(面板端口占用时靠它认领)'),
|
|
26
|
+
version: z.string(),
|
|
27
|
+
}),
|
|
28
|
+
version: z.string(),
|
|
29
|
+
defaultPort: z.int().describe('面板默认端口'),
|
|
30
|
+
projectRoot: z.string().describe('面板进程的工作目录'),
|
|
31
|
+
commands: z.array(
|
|
32
|
+
z.object({
|
|
33
|
+
id: z.string(),
|
|
34
|
+
module: z.string(),
|
|
35
|
+
command: z.string().describe('CLI 命令(如 `nx-kv localtask list`)'),
|
|
36
|
+
usage: z.string().describe('完整用法串'),
|
|
37
|
+
http: z.object({ method: z.string(), path: z.string() }).nullable(),
|
|
38
|
+
summary: z.string().optional(),
|
|
39
|
+
}),
|
|
40
|
+
),
|
|
41
|
+
});
|
|
42
|
+
|
|
19
43
|
export class BootstrapAction extends ActionBase<typeof input> {
|
|
20
44
|
@action({
|
|
21
45
|
id: 'system.bootstrap',
|
|
@@ -23,6 +47,7 @@ export class BootstrapAction extends ActionBase<typeof input> {
|
|
|
23
47
|
http: ['GET', '/api/bootstrap'],
|
|
24
48
|
summary: '聚合上下文:版本 / 命令表',
|
|
25
49
|
input,
|
|
50
|
+
output,
|
|
26
51
|
})
|
|
27
52
|
override async run(_ctx: ActionInput<this['_input']>): Promise<{
|
|
28
53
|
app: { name: string; version: string };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// system.contract —— 契约快照端点:`GET /api/contract`(CLI:`nx-kv contract`)。
|
|
2
|
+
//
|
|
3
|
+
// 这是端侧「我这份生成物还认不认得服务端」的唯一依据:
|
|
4
|
+
// · hash 对账:生成物里写死的 contractHash 与这里返回的 hash 比
|
|
5
|
+
// · 结构包含:端侧只做「我声明的字段 ⊆ 服务端声明的字段」判定,不追求完全相等
|
|
6
|
+
// (服务端加字段是兼容变更,不该让所有端侧立刻红)
|
|
7
|
+
//
|
|
8
|
+
// 快照由 runtime/registry 在装配期 publish(core/contract-snapshot.ts),本文件只读:
|
|
9
|
+
// registry → modules → 本文件 → registry 会成环,而模块也够不着 runtime。
|
|
10
|
+
// 这份「发布格子」与 bootstrap 读命令表是同一套模式,代价只是「读快照而非实时算」。
|
|
11
|
+
import { z } from 'zod';
|
|
12
|
+
import { action } from '../../../runtime/action-decorator.js';
|
|
13
|
+
import { ActionBase, type ActionInput } from '../../../runtime/action-base.js';
|
|
14
|
+
import { contractSnapshot } from '../../../core/contract-snapshot.js';
|
|
15
|
+
|
|
16
|
+
/** system.contract 的入参契约。 */
|
|
17
|
+
const input = z.object({});
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 出参视图:**故意写得浅**。
|
|
21
|
+
*
|
|
22
|
+
* 快照里的 `output` 是递归类型树,用 zod 精确描述它需要 z.lazy 自引用,
|
|
23
|
+
* 而 describeOutput 会把 union/lazy 一律降级成 dynamic——写精确了也拿不到类型,
|
|
24
|
+
* 反而让生成物里多一个自指的怪模型。所以这里只声明端侧真正要用的三样:
|
|
25
|
+
* 版本、指纹、action 清单(id + 路由),类型树原样透传成 dynamic。
|
|
26
|
+
*/
|
|
27
|
+
const output = z.object({
|
|
28
|
+
app: z.string(),
|
|
29
|
+
version: z.string(),
|
|
30
|
+
hash: z.string().describe('契约指纹(stableStringify 的 sha256 前 16 位)'),
|
|
31
|
+
actions: z.array(
|
|
32
|
+
z.object({
|
|
33
|
+
id: z.string(),
|
|
34
|
+
http: z.string().nullable().describe('`METHOD /path`;纯 CLI 命令为 null'),
|
|
35
|
+
summary: z.string().optional(),
|
|
36
|
+
output: z.unknown().describe('出参类型树(IR 原样透传,端侧通常不解析)'),
|
|
37
|
+
}),
|
|
38
|
+
),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export class ContractAction extends ActionBase<typeof input> {
|
|
42
|
+
@action({
|
|
43
|
+
id: 'system.contract',
|
|
44
|
+
cli: ['contract'],
|
|
45
|
+
http: ['GET', '/api/contract'],
|
|
46
|
+
summary: '契约快照:带 output 的 action + 指纹(端侧据此对账自己是不是旧契约)',
|
|
47
|
+
input,
|
|
48
|
+
output,
|
|
49
|
+
})
|
|
50
|
+
override async run(_ctx: ActionInput<this['_input']>) {
|
|
51
|
+
const snap = contractSnapshot();
|
|
52
|
+
if (!snap) {
|
|
53
|
+
// 只有「没经过 registry 装配」的裸调用才会走到这里(正常启动/HTTP/CLI 都不会)
|
|
54
|
+
throw new Error('契约快照尚未发布:请通过 registry / 服务启动后再调用 contract');
|
|
55
|
+
}
|
|
56
|
+
return snap;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -129,7 +129,11 @@ export function deliveryCtxOf(groupCtx: any): Record<string, any> {
|
|
|
129
129
|
path: (x.intro && x.intro.path) || '',
|
|
130
130
|
}));
|
|
131
131
|
return {
|
|
132
|
-
|
|
132
|
+
// #104:只要目录**已登记**,repo 就进 ctx(哪怕没写介绍、没绑文档)。
|
|
133
|
+
// 以前要求「有介绍正文或参考仓库」才暴露 → 已登记但没写介绍的仓库(如 nx-kn)
|
|
134
|
+
// 在交付里整段 context 静默消失,用户无法区分「没登记」和「登记了没内容」。
|
|
135
|
+
// 空壳组(group)仍按 #81 过滤,那是另一条口径(组标签刷屏)。
|
|
136
|
+
repo: repo
|
|
133
137
|
? {
|
|
134
138
|
name: repo.name || repo.key || '',
|
|
135
139
|
key: repo.key || '',
|
|
@@ -28,17 +28,21 @@ export async function getTask({ cwd, timeoutSec, open = true, params, instant }:
|
|
|
28
28
|
// 项目开启「持续 get」时,循环指令作为 getloop 区块随其余系统段一起由
|
|
29
29
|
// buildTaskDelivery 统一编排(不再在 get 层后置拼接,避免同一份 instruction
|
|
30
30
|
// 有两个拼装入口)。
|
|
31
|
+
// #112(用户口径):「阻塞 N 分钟」是 **get 命令自己**没任务时阻塞多久(服务端挂住
|
|
32
|
+
// 请求,到点返回 timeout),不是让 agent 睡。窗口配 0 = 不阻塞,没活立即 timeout。
|
|
33
|
+
const minutes = loop.enabled ? Number(loop.waitMinutes) || 0 : 0;
|
|
31
34
|
const r = await waitForTask({
|
|
32
35
|
cwd,
|
|
33
36
|
// 未开启持续 get → instant(waitForTask 内部忽略 timeout 直接快路径);
|
|
34
|
-
//
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
+
// 开启且窗口 >0 → 阻塞这么秒;窗口 = 0 → 同样走 instant(不阻塞)。
|
|
38
|
+
// 显式 --timeout 仍最高优先;instant 显式给定时也最高优先
|
|
39
|
+
// (HTTP 领取不允许挂住请求,见 action-lifecycle 的 claim 路由)。
|
|
40
|
+
timeoutSec: timeoutSec ?? (minutes > 0 ? minutes * 60 : undefined),
|
|
37
41
|
open,
|
|
38
|
-
instant: instant === undefined ? !loop.enabled : !!instant,
|
|
42
|
+
instant: instant === undefined ? !loop.enabled || minutes <= 0 : !!instant,
|
|
39
43
|
params: params || {}, // 外部参数(平面扩展)进交付模板
|
|
40
44
|
getLoopTpl: getLoopInstruction(loop),
|
|
41
|
-
getLoopWaitMinutes:
|
|
45
|
+
getLoopWaitMinutes: minutes,
|
|
42
46
|
});
|
|
43
47
|
if (r.status !== 'ok' || !r.task) return r;
|
|
44
48
|
if (loop.enabled) r.getLoop = true; // 交付里带了持续 get 指令(调试/文档可辨)
|
|
@@ -65,7 +65,10 @@ export function sanitizeGetLoop(raw: unknown): Record<string, unknown> | null {
|
|
|
65
65
|
const out: Record<string, unknown> = {};
|
|
66
66
|
if (decl.enabled !== undefined) out.enabled = decl.enabled === true || decl.enabled === 'on' || decl.enabled === 'true';
|
|
67
67
|
const n = Number(decl.waitMinutes);
|
|
68
|
-
|
|
68
|
+
// #112:0 是合法值(= get 没任务时不阻塞,立即返回 timeout);只挡负数与非数字
|
|
69
|
+
if (decl.waitMinutes !== undefined && decl.waitMinutes !== null && decl.waitMinutes !== '' && Number.isFinite(n) && n >= 0) {
|
|
70
|
+
out.waitMinutes = Math.min(Math.floor(n), 600);
|
|
71
|
+
}
|
|
69
72
|
if (!Object.keys(out).length) return null;
|
|
70
73
|
return out;
|
|
71
74
|
}
|
|
@@ -79,7 +82,11 @@ export async function getLoopForCwd(cwd: unknown): Promise<GetLoopState> {
|
|
|
79
82
|
const repo = k ? s.repos.find((r: any) => normalizePathKey(r.path) === k) : null;
|
|
80
83
|
const loop = repo && repo.getLoop && typeof repo.getLoop === 'object' ? repo.getLoop : null;
|
|
81
84
|
if (!loop || !loop.enabled) return { enabled: false };
|
|
82
|
-
|
|
85
|
+
// #112:显式 0 是合法值(= get 没任务时不阻塞,立即 timeout);只有字段缺失 /
|
|
86
|
+
// 非数字时才回落到默认 10 分钟(老数据兼容)。
|
|
87
|
+
const rawMinutes = loop.waitMinutes;
|
|
88
|
+
const n = rawMinutes === undefined || rawMinutes === null ? NaN : Number(rawMinutes);
|
|
89
|
+
const minutes = Number.isFinite(n) && n >= 0 ? Math.min(Math.floor(n), 600) : GET_LOOP_DEFAULT_MINUTES;
|
|
83
90
|
return { enabled: true, waitMinutes: minutes };
|
|
84
91
|
} catch {
|
|
85
92
|
return { enabled: false }; // 仓库登记坏了绝不拖垮 get
|
|
@@ -23,8 +23,12 @@
|
|
|
23
23
|
// z.input(调用方形状,.default 字段可选)与 z.output(解析后形状,
|
|
24
24
|
// .default 字段必填)别混:handler 拿的是 z.output。
|
|
25
25
|
//
|
|
26
|
-
// (c)
|
|
27
|
-
//
|
|
26
|
+
// (c) 出参默认没有 schema——**实现即契约**:TS 前端 apiclient 的返回类型从 action 类
|
|
27
|
+
// 的方法签名直接推断(codegen type-only 引类),同一信息只声明一次。
|
|
28
|
+
// 但**跨语言客户端(Dart)拿不到 TS 类型**,所以需要跨语言的 action 额外声明
|
|
29
|
+
// `output`:它是**视图**(只列要对端侧暴露的字段,未声明的字段不进生成物),
|
|
30
|
+
// 不是 service 的返回结构。声明了 output 的 action 会被 tests/unit/contract.test.mjs
|
|
31
|
+
// 用 output.safeParse(真实返回) 对账——声明写错,测试当场红。
|
|
28
32
|
import { describeInput, type FieldDesc } from './zod-bridge.js';
|
|
29
33
|
import type { ActionRunMeta } from './action-base.js';
|
|
30
34
|
import type { ZodType } from 'zod';
|
|
@@ -64,6 +68,17 @@ export interface ActionMeta {
|
|
|
64
68
|
restArg?: string;
|
|
65
69
|
/** 输入 schema:CLI 与 HTTP 共用的真相源(运行时校验 + 归一化的依据)。 */
|
|
66
70
|
input: ZodType;
|
|
71
|
+
/**
|
|
72
|
+
* 出参 schema(可选):**只为跨语言客户端存在**。
|
|
73
|
+
*
|
|
74
|
+
* 不声明它,TS 侧一切照旧(返回类型从方法签名推断);声明了它,这个 action 才会
|
|
75
|
+
* 进 contract 快照、才会被生成 Dart 客户端。三条纪律(违反会在生成/测试期报错):
|
|
76
|
+
* 1. 它是**视图**:只列要对端侧暴露的字段;service 多返回的字段不会被生成
|
|
77
|
+
* 2. 必须是 `z.object({...})` 收口:端侧根类型得是个模型
|
|
78
|
+
* 3. 可选性 / 整数 / 枚举即契约:`z.int()` 与 `z.number()` 在 Dart 是 int 与 num,
|
|
79
|
+
* 改一个字符端侧就换个类型(这正是我们想要的编译期信号)
|
|
80
|
+
*/
|
|
81
|
+
output?: ZodType;
|
|
67
82
|
/** CLI 人读渲染(面板不用——面板拿 data 走 JSON)。 */
|
|
68
83
|
// 参数用 any:渲染函数普遍要读结果字段,给 unknown 会让属性访问整片红。
|
|
69
84
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- 见上;eslint 对 any 仅警告
|
|
@@ -128,6 +143,8 @@ export interface ZodBoundAction extends BoundAction {
|
|
|
128
143
|
/** 逐字段说明(收集时用 describeInput 算好一次,CLI/HTTP/usage/codegen 共用) */
|
|
129
144
|
fields: FieldDesc[];
|
|
130
145
|
input: ZodType;
|
|
146
|
+
/** 出参视图(只有跨语言客户端需要的 action 才有;见 ActionMeta.output) */
|
|
147
|
+
output?: ZodType;
|
|
131
148
|
render?: (result: any, ctx?: any) => string | Promise<string>;
|
|
132
149
|
stream?: boolean;
|
|
133
150
|
channel: 'zod';
|
|
@@ -189,6 +206,7 @@ export function zodActionsOf(instance: object): ZodBoundAction[] {
|
|
|
189
206
|
restArg: meta.restArg,
|
|
190
207
|
fields,
|
|
191
208
|
input: meta.input,
|
|
209
|
+
output: meta.output,
|
|
192
210
|
render: meta.render,
|
|
193
211
|
stream: meta.stream,
|
|
194
212
|
channel: 'zod',
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// 契约快照的**构建**(runtime 侧:这里才有 zod)。
|
|
2
|
+
//
|
|
3
|
+
// 数据形状与纯函数(stableStringify / hashOf / diffContracts)在 core/contract-types.ts
|
|
4
|
+
// ——模块要读快照,而模块够不着 runtime。这里只负责「zod action → IR」这一件事。
|
|
5
|
+
//
|
|
6
|
+
// 为什么要有快照(而不是让生成器直接读 zod):
|
|
7
|
+
// 1. **可对账**:快照入库,`gen:dart --check` 重建一遍比对,声明改了没重新生成 → 红。
|
|
8
|
+
// 2. **可协商**:`GET /api/contract` 回的就是它,端侧据此判定自己是不是旧契约。
|
|
9
|
+
// 3. **生成器只认 IR**:Dart 发射器不 import zod,zod 内部结构变化波及不到生成器。
|
|
10
|
+
import type { ZodBoundAction } from './action-decorator.js';
|
|
11
|
+
import { describeOutput } from './zod-bridge.js';
|
|
12
|
+
import {
|
|
13
|
+
hashOf,
|
|
14
|
+
type ContractAction,
|
|
15
|
+
type ContractSnapshot,
|
|
16
|
+
} from '../core/contract-types.js';
|
|
17
|
+
|
|
18
|
+
/** CLI 声明可能是单路径也可能是别名列表:统一成「路径数组的数组」。 */
|
|
19
|
+
function cliPathsOf(cli: ZodBoundAction['cli']): string[][] {
|
|
20
|
+
const first = (cli as readonly unknown[])[0];
|
|
21
|
+
return Array.isArray(first)
|
|
22
|
+
? (cli as readonly (readonly string[])[]).map((p) => [...p])
|
|
23
|
+
: [[...(cli as readonly string[])]];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 根模型名:action 类名去掉 `Action` 后缀 + `Output`。
|
|
28
|
+
* 例:`LocaltaskListAction` → `LocaltaskListOutput`。
|
|
29
|
+
* 用类名而不是 action id:id 允许出现 `.` 和 `-`,类名天然是合法标识符。
|
|
30
|
+
*/
|
|
31
|
+
export function rootModelNameOf(action: ZodBoundAction): string {
|
|
32
|
+
return action.className.replace(/Action$/, '') + 'Output';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 从注册表里的 zod action 构建快照。
|
|
37
|
+
* 只收声明了 `output` 的 action——**不对外就不在契约里**,这是最省事的访问控制。
|
|
38
|
+
*/
|
|
39
|
+
export function buildContractSnapshot(
|
|
40
|
+
actions: readonly ZodBoundAction[],
|
|
41
|
+
meta: { app: string; version: string },
|
|
42
|
+
): ContractSnapshot {
|
|
43
|
+
const picked = actions
|
|
44
|
+
.filter((a) => a.output)
|
|
45
|
+
.map((a) => {
|
|
46
|
+
const out: ContractAction = {
|
|
47
|
+
id: a.id,
|
|
48
|
+
cli: cliPathsOf(a.cli),
|
|
49
|
+
http: a.http ? `${a.http[0]} ${a.http[1]}` : null,
|
|
50
|
+
input: a.fields,
|
|
51
|
+
output: describeOutput(a.output!, rootModelNameOf(a)),
|
|
52
|
+
};
|
|
53
|
+
if (a.summary) out.summary = a.summary;
|
|
54
|
+
return out;
|
|
55
|
+
})
|
|
56
|
+
.sort((x, y) => x.id.localeCompare(y.id));
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
app: meta.app,
|
|
60
|
+
version: meta.version,
|
|
61
|
+
hash: hashOf(meta.app, picked),
|
|
62
|
+
actions: picked,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type { ContractAction, ContractSnapshot } from '../core/contract-types.js';
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
// Dart 发射器:契约快照 → 零依赖 Dart 包。
|
|
2
|
+
//
|
|
3
|
+
// 只认 IR(contract.ts 的 ContractSnapshot),不 import zod——这样 zod 内部结构
|
|
4
|
+
// 变化不会波及生成器,生成的产物也能靠 JSON 快照独立复现。
|
|
5
|
+
//
|
|
6
|
+
// 三条刻意的取舍:
|
|
7
|
+
// 1. **零依赖**:包只有 dart:core(+ 需要序列化数组入参时 dart:convert),
|
|
8
|
+
// 传输层由调用方注入(fr 侧塞 MnWsAppClient,自带应用前缀与设备令牌)。
|
|
9
|
+
// 2. **只做反序列化**:端侧是消费者,不需要 toJson。少一半代码、少一半漂移面。
|
|
10
|
+
// 3. **信封只拆一次**:`{ok,data}` / `{ok,error}` 的拆解写死在生成物里,
|
|
11
|
+
// 调用方永远拿强类型对象,也永远不需要知道 nx-kv 的信封长什么样。
|
|
12
|
+
|
|
13
|
+
import type { ContractSnapshot } from './contract.js';
|
|
14
|
+
import type { FieldDesc, TypeNode } from './zod-bridge.js';
|
|
15
|
+
|
|
16
|
+
/** Dart 保留字(参数名撞上就得加后缀)。 */
|
|
17
|
+
const DART_KEYWORDS = new Set([
|
|
18
|
+
'abstract', 'as', 'assert', 'async', 'await', 'break', 'case', 'catch', 'class', 'const',
|
|
19
|
+
'continue', 'covariant', 'default', 'deferred', 'do', 'dynamic', 'else', 'enum', 'export',
|
|
20
|
+
'extends', 'extension', 'external', 'factory', 'false', 'final', 'finally', 'for', 'function',
|
|
21
|
+
'get', 'hide', 'if', 'implements', 'import', 'in', 'interface', 'is', 'late', 'library',
|
|
22
|
+
'mixin', 'new', 'null', 'on', 'operator', 'part', 'required', 'rethrow', 'return', 'set',
|
|
23
|
+
'show', 'static', 'super', 'switch', 'sync', 'this', 'throw', 'true', 'try', 'typedef', 'var',
|
|
24
|
+
'void', 'while', 'with', 'yield',
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
const camel = (s: string): string => s.replace(/[-_](\w)/g, (_m, c: string) => c.toUpperCase());
|
|
28
|
+
const lowerFirst = (s: string): string => s.charAt(0).toLowerCase() + s.slice(1);
|
|
29
|
+
const dartName = (s: string): string => {
|
|
30
|
+
const n = lowerFirst(camel(s));
|
|
31
|
+
return DART_KEYWORDS.has(n) ? n + 'Value' : n;
|
|
32
|
+
};
|
|
33
|
+
const docComment = (text: string | undefined, indent: string): string[] =>
|
|
34
|
+
text ? [`${indent}/// ${text.replace(/\s+/g, ' ')}`] : [];
|
|
35
|
+
|
|
36
|
+
/** 收集树里所有模型 / 枚举(同名同形只留一份;同名不同形在 describeOutput 已拦)。 */
|
|
37
|
+
function collect(output: TypeNode, models: Map<string, TypeNode & { k: 'model' }>, enums: Map<string, string[]>): void {
|
|
38
|
+
switch (output.k) {
|
|
39
|
+
case 'model':
|
|
40
|
+
if (models.has(output.name)) return;
|
|
41
|
+
models.set(output.name, output);
|
|
42
|
+
for (const f of output.fields) collect(f.type, models, enums);
|
|
43
|
+
return;
|
|
44
|
+
case 'enum':
|
|
45
|
+
enums.set(output.name, output.values);
|
|
46
|
+
return;
|
|
47
|
+
case 'list':
|
|
48
|
+
case 'map':
|
|
49
|
+
collect(output.of, models, enums);
|
|
50
|
+
return;
|
|
51
|
+
default:
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Dart 类型名 */
|
|
57
|
+
function dartType(t: TypeNode): string {
|
|
58
|
+
switch (t.k) {
|
|
59
|
+
case 'string':
|
|
60
|
+
return 'String';
|
|
61
|
+
case 'int':
|
|
62
|
+
return 'int';
|
|
63
|
+
case 'number':
|
|
64
|
+
return 'num';
|
|
65
|
+
case 'bool':
|
|
66
|
+
return 'bool';
|
|
67
|
+
case 'unknown':
|
|
68
|
+
return 'dynamic';
|
|
69
|
+
case 'enum':
|
|
70
|
+
case 'model':
|
|
71
|
+
return t.name;
|
|
72
|
+
case 'list':
|
|
73
|
+
return `List<${dartType(t.of)}>`;
|
|
74
|
+
case 'map':
|
|
75
|
+
return `Map<String, ${dartType(t.of)}>`;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** 把 `src` 处的值读成目标类型(递归) */
|
|
80
|
+
function readExpr(t: TypeNode, src: string): string {
|
|
81
|
+
switch (t.k) {
|
|
82
|
+
case 'string':
|
|
83
|
+
return `${src} as String`;
|
|
84
|
+
case 'int':
|
|
85
|
+
return `(${src} as num).toInt()`;
|
|
86
|
+
case 'number':
|
|
87
|
+
return `${src} as num`;
|
|
88
|
+
case 'bool':
|
|
89
|
+
return `${src} as bool`;
|
|
90
|
+
case 'unknown':
|
|
91
|
+
return src;
|
|
92
|
+
case 'enum':
|
|
93
|
+
return `${t.name}.values.byName(${src} as String)`;
|
|
94
|
+
case 'model':
|
|
95
|
+
return `${t.name}.fromJson(${src} as Map<String, dynamic>)`;
|
|
96
|
+
case 'list':
|
|
97
|
+
return `(${src} as List).map<${dartType(t.of)}>((e) => ${readExpr(t.of, 'e')}).toList()`;
|
|
98
|
+
case 'map':
|
|
99
|
+
return `(${src} as Map).map<String, ${dartType(t.of)}>((k, v) => MapEntry(k as String, ${readExpr(t.of, 'v')}))`;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function emitEnums(enums: Map<string, string[]>): string {
|
|
104
|
+
const out: string[] = [];
|
|
105
|
+
for (const [name, values] of enums) {
|
|
106
|
+
for (const v of values) {
|
|
107
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(v)) {
|
|
108
|
+
throw new Error(
|
|
109
|
+
`枚举 ${name} 的值 ${JSON.stringify(v)} 不是合法 Dart 标识符;` +
|
|
110
|
+
'要么改取值,要么给这个 action 换个出参(生成器不做改名映射,改名会静默漂移)',
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
out.push(`/// 取值:${values.join(' / ')}\nenum ${name} { ${values.join(', ')} }`);
|
|
115
|
+
}
|
|
116
|
+
return out.join('\n\n');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function emitModel(model: TypeNode & { k: 'model' }): string {
|
|
120
|
+
const fields = model.fields;
|
|
121
|
+
const lines: string[] = [];
|
|
122
|
+
lines.push(`class ${model.name} {`);
|
|
123
|
+
const ctorArgs = fields
|
|
124
|
+
.map((f) => `${f.required ? 'required ' : ''}this.${dartName(f.name)}`)
|
|
125
|
+
.join(', ');
|
|
126
|
+
lines.push(` const ${model.name}({${ctorArgs}});`);
|
|
127
|
+
lines.push('');
|
|
128
|
+
for (const f of fields) {
|
|
129
|
+
lines.push(...docComment(f.hint, ' '));
|
|
130
|
+
lines.push(` final ${dartType(f.type)}${f.required ? '' : '?'} ${dartName(f.name)};`);
|
|
131
|
+
}
|
|
132
|
+
lines.push('');
|
|
133
|
+
lines.push(` factory ${model.name}.fromJson(Map<String, dynamic> json) => ${model.name}(`);
|
|
134
|
+
for (const f of fields) {
|
|
135
|
+
const key = `json['${f.name}']`;
|
|
136
|
+
const expr = readExpr(f.type, key);
|
|
137
|
+
lines.push(` ${dartName(f.name)}: ${f.required ? expr : `${key} == null ? null : ${expr}`},`);
|
|
138
|
+
}
|
|
139
|
+
lines.push(' );');
|
|
140
|
+
lines.push('}');
|
|
141
|
+
return lines.join('\n');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** 入参字段 → Dart 形参(GET 走 query,其余走 JSON body) */
|
|
145
|
+
function paramOf(f: FieldDesc): string {
|
|
146
|
+
const type =
|
|
147
|
+
f.kind === 'number'
|
|
148
|
+
? 'num'
|
|
149
|
+
: f.kind === 'boolean'
|
|
150
|
+
? 'bool'
|
|
151
|
+
: f.kind === 'array'
|
|
152
|
+
? 'List<String>'
|
|
153
|
+
: f.kind === 'kv' || f.kind === 'raw'
|
|
154
|
+
? 'Map<String, dynamic>'
|
|
155
|
+
: f.kind === 'enum'
|
|
156
|
+
? 'String'
|
|
157
|
+
: 'String';
|
|
158
|
+
return `${f.required ? 'required ' : ''}${type}${f.required ? '' : '?'} ${dartName(f.name)}`;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function methodNameOf(id: string, used: Set<string>): string {
|
|
162
|
+
const parts = id.split('.');
|
|
163
|
+
// 用**整条 id** 取名(localtask.list → localtaskList):只取末段会让
|
|
164
|
+
// localtask.list 与 docs.list 都叫 list,端侧天天撞名(设计里已把这条写成硬规则)
|
|
165
|
+
const base = lowerFirst(camel(parts.join('-')));
|
|
166
|
+
let name = base;
|
|
167
|
+
if (used.has(name)) {
|
|
168
|
+
const module = parts.length > 1 ? lowerFirst(camel(parts[parts.length - 2]!)) : 'action';
|
|
169
|
+
name = `${module}${base.charAt(0).toUpperCase()}${base.slice(1)}`;
|
|
170
|
+
}
|
|
171
|
+
let n = 2;
|
|
172
|
+
while (used.has(name)) name = `${base}${n++}`;
|
|
173
|
+
used.add(name);
|
|
174
|
+
return name;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Dart 包的文件集合(相对 contract/dart/ 的路径 → 内容)。 */
|
|
178
|
+
export function emitDartPackage(snapshot: ContractSnapshot): Record<string, string> {
|
|
179
|
+
const models = new Map<string, TypeNode & { k: 'model' }>();
|
|
180
|
+
const enums = new Map<string, string[]>();
|
|
181
|
+
for (const a of snapshot.actions) collect(a.output, models, enums);
|
|
182
|
+
|
|
183
|
+
const pkg = snapshot.app.replace(/[^a-z0-9]+/gi, '_').toLowerCase() + '_client';
|
|
184
|
+
const rootModels = new Map(snapshot.actions.map((a) => [a.id, a.output] as const));
|
|
185
|
+
|
|
186
|
+
// ---- models.dart ----
|
|
187
|
+
const modelSrc = [
|
|
188
|
+
'// GENERATED by nx-kv scripts/gen-dart-client.mjs —— 勿手改。',
|
|
189
|
+
`// 契约指纹:${snapshot.hash}(nx-kv ${snapshot.version})`,
|
|
190
|
+
'',
|
|
191
|
+
'// ignore_for_file: unnecessary_cast',
|
|
192
|
+
'',
|
|
193
|
+
emitEnums(enums),
|
|
194
|
+
[...models.values()].map(emitModel).join('\n\n'),
|
|
195
|
+
]
|
|
196
|
+
.filter((s) => s !== '')
|
|
197
|
+
.join('\n') + '\n';
|
|
198
|
+
|
|
199
|
+
// ---- api.dart ----
|
|
200
|
+
const used = new Set<string>();
|
|
201
|
+
const methodLines: string[] = [];
|
|
202
|
+
for (const a of snapshot.actions) {
|
|
203
|
+
const root = rootModels.get(a.id)!;
|
|
204
|
+
if (root.k !== 'model') throw new Error(`action ${a.id} 的出参不是模型`);
|
|
205
|
+
const [method, path] = (a.http ?? 'GET ').split(' ') as [string, string];
|
|
206
|
+
const params = a.input.map(paramOf).join(', ');
|
|
207
|
+
const isGet = method === 'GET' || method === 'HEAD';
|
|
208
|
+
const body = isGet ? 'null' : bodyMap(a.input);
|
|
209
|
+
const query = isGet ? queryMap(a.input) : 'const <String, String>{}';
|
|
210
|
+
methodLines.push(
|
|
211
|
+
[
|
|
212
|
+
...docComment(a.summary, ' '),
|
|
213
|
+
` /// action: ${a.id} · 认证/前缀由注入的 transport 负责`,
|
|
214
|
+
` Future<${root.name}> ${methodNameOf(a.id, used)}(${params}) async {`,
|
|
215
|
+
` final query = ${query};`,
|
|
216
|
+
` final body = ${body};`,
|
|
217
|
+
` final data = _unwrap(await _send('${method}', '${path}', query, body));`,
|
|
218
|
+
` return ${root.name}.fromJson(data);`,
|
|
219
|
+
' }',
|
|
220
|
+
].join('\n'),
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const needsConvert = snapshot.actions.some(
|
|
225
|
+
(a) => (a.http ?? 'GET').startsWith('GET') && a.input.some((f) => f.kind === 'array' || f.kind === 'kv'),
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
const apiSrc = [
|
|
229
|
+
'// GENERATED by nx-kv scripts/gen-dart-client.mjs —— 勿手改。',
|
|
230
|
+
`// 契约指纹:${snapshot.hash}(nx-kv ${snapshot.version})`,
|
|
231
|
+
'',
|
|
232
|
+
needsConvert ? "import 'dart:convert';" : '',
|
|
233
|
+
"import 'models.dart';",
|
|
234
|
+
'',
|
|
235
|
+
'// 指纹对不上就是「端侧跑在旧契约上」的硬信号:拿它跟 GET /api/contract 的 hash 比。',
|
|
236
|
+
`const String contractHash = '${snapshot.hash}';`,
|
|
237
|
+
`const String contractVersion = '${snapshot.version}';`,
|
|
238
|
+
'',
|
|
239
|
+
'/// 传输层契约(由调用方注入):返回**完整信封**(`{ok,data}` / `{ok,error}`)的 JSON。',
|
|
240
|
+
'///',
|
|
241
|
+
'/// fr 侧的实现只有一行:把 method/path/query/body 交给 MnWsAppClient,应用前缀与',
|
|
242
|
+
'/// 设备令牌都由它负责——生成物不知道 nx-kv 挂在哪、也不需要知道。',
|
|
243
|
+
'typedef NxKvTransport = Future<dynamic> Function(',
|
|
244
|
+
' String method,',
|
|
245
|
+
' String path,',
|
|
246
|
+
' Map<String, String> query,',
|
|
247
|
+
' Object? body,',
|
|
248
|
+
');',
|
|
249
|
+
'',
|
|
250
|
+
'/// 服务端信封里的业务错误。',
|
|
251
|
+
'class NxKvApiException implements Exception {',
|
|
252
|
+
' NxKvApiException(this.message);',
|
|
253
|
+
'',
|
|
254
|
+
' final String message;',
|
|
255
|
+
'',
|
|
256
|
+
' @override',
|
|
257
|
+
' String toString() => \'NxKvApiException: $message\';',
|
|
258
|
+
'}',
|
|
259
|
+
'',
|
|
260
|
+
'Map<String, dynamic> _unwrap(dynamic raw) {',
|
|
261
|
+
' if (raw is! Map) throw NxKvApiException(\'响应不是 JSON 对象:$raw\');',
|
|
262
|
+
' if (raw[\'ok\'] != true) throw NxKvApiException(\'\${raw["error"] ?? raw}\');',
|
|
263
|
+
' final data = raw[\'data\'];',
|
|
264
|
+
' if (data is! Map) throw NxKvApiException(\'data 不是对象:$data\');',
|
|
265
|
+
' return data.cast<String, dynamic>();',
|
|
266
|
+
'}',
|
|
267
|
+
'',
|
|
268
|
+
`class ${pascalOf(pkg)}Api {`,
|
|
269
|
+
` ${pascalOf(pkg)}Api(this._send);`,
|
|
270
|
+
'',
|
|
271
|
+
' final NxKvTransport _send;',
|
|
272
|
+
'',
|
|
273
|
+
methodLines.join('\n\n'),
|
|
274
|
+
'}',
|
|
275
|
+
'',
|
|
276
|
+
].join('\n');
|
|
277
|
+
|
|
278
|
+
const entry = [
|
|
279
|
+
'// GENERATED by nx-kv scripts/gen-dart-client.mjs —— 勿手改。',
|
|
280
|
+
"export 'src/api.dart';",
|
|
281
|
+
"export 'src/models.dart';",
|
|
282
|
+
'',
|
|
283
|
+
].join('\n');
|
|
284
|
+
|
|
285
|
+
return {
|
|
286
|
+
// pubspec 的 description 只能是 ASCII —— 不是洁癖,是踩出来的:
|
|
287
|
+
// pub 读 **git 源**的 pubspec 时按系统 ANSI 码页解码(Windows 中文 = GBK),
|
|
288
|
+
// UTF-8 的多字节尾巴会把行尾 `\n` 一起吃进残缺的 GBK 字符里,于是
|
|
289
|
+
// description 与 version 被拼成一行,报「Mapping values are not allowed here」,
|
|
290
|
+
// 而错误信息指向的是 version 的冒号 —— 看着像缩进问题。
|
|
291
|
+
// 同一个包用 path 引入却完全正常(path 源走 UTF-8 读),所以这个坑只在
|
|
292
|
+
// 「锁 sha 的 git 依赖」这条真实消费路径上出现。
|
|
293
|
+
'pubspec.yaml': [
|
|
294
|
+
`name: ${pkg}`,
|
|
295
|
+
`description: nx-kv contract client (${snapshot.hash}) - generated, zero deps.`,
|
|
296
|
+
`version: ${snapshot.version}`,
|
|
297
|
+
'publish_to: none',
|
|
298
|
+
'environment:',
|
|
299
|
+
" sdk: '>=3.0.0 <4.0.0'",
|
|
300
|
+
'',
|
|
301
|
+
].join('\n'),
|
|
302
|
+
'.gitignore': [
|
|
303
|
+
'# 生成器不产这两样;它们属于消费者,入库只会打架',
|
|
304
|
+
'.dart_tool/',
|
|
305
|
+
'pubspec.lock',
|
|
306
|
+
'',
|
|
307
|
+
].join('\n'),
|
|
308
|
+
'lib/nx_kv_client.dart': entry,
|
|
309
|
+
'lib/src/models.dart': modelSrc,
|
|
310
|
+
'lib/src/api.dart': apiSrc,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function queryMap(input: FieldDesc[]): string {
|
|
315
|
+
if (!input.length) return 'const <String, String>{}';
|
|
316
|
+
const lines = input.map((f) => {
|
|
317
|
+
const v = dartName(f.name);
|
|
318
|
+
if (f.kind === 'array') {
|
|
319
|
+
return ` if (${v} != null) '${f.name}': (${v} as List).join(','),`;
|
|
320
|
+
}
|
|
321
|
+
if (f.kind === 'kv' || f.kind === 'raw') {
|
|
322
|
+
return ` if (${v} != null) '${f.name}': jsonEncode(${v}),`;
|
|
323
|
+
}
|
|
324
|
+
// 生成 Dart 的 '${x}' 插值:这里用拼接写,避免在模板串里嵌套插值把自己绕晕
|
|
325
|
+
return ' if (' + v + " != null) '" + f.name + "': '${" + v + "}',";
|
|
326
|
+
});
|
|
327
|
+
return ['<String, String>{', ...lines, ' }'].join('\n');
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function bodyMap(input: FieldDesc[]): string {
|
|
331
|
+
if (!input.length) return 'const <String, dynamic>{}';
|
|
332
|
+
const lines = input.map((f) => {
|
|
333
|
+
const v = dartName(f.name);
|
|
334
|
+
return f.required
|
|
335
|
+
? ` '${f.name}': ${v},`
|
|
336
|
+
: ` if (${v} != null) '${f.name}': ${v},`;
|
|
337
|
+
});
|
|
338
|
+
return ['<String, dynamic>{', ...lines, ' }'].join('\n');
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function pascalOf(s: string): string {
|
|
342
|
+
return s
|
|
343
|
+
.split(/[_\-]+/)
|
|
344
|
+
.filter(Boolean)
|
|
345
|
+
.map((p) => p.charAt(0).toUpperCase() + p.slice(1))
|
|
346
|
+
.join('');
|
|
347
|
+
}
|