@capyclaw/plugin-generator 1.0.0 → 1.0.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.
- package/bin/create-doc-model.mjs +128 -0
- package/bin/create-plugin.mjs +15 -6
- package/bin/pack-plugin.mjs +26 -19
- package/package.json +5 -5
- package/templates/doc-model/README.md.tmpl +51 -0
- package/templates/doc-model/schema.mjs.tmpl +69 -0
- package/templates/doc-model/test.mjs.tmpl +92 -0
- package/templates/doc-model/tools.mjs.tmpl +169 -0
- package/templates/plugin/server/relay.mjs.tmpl +2 -2
- package/bin/cli.mjs +0 -78
- package/lib/index.mjs +0 -250
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 文档模型脚手架:从「一份领域文档的形态」直接生成可用的读/定位/改/校验四件套。
|
|
4
|
+
*
|
|
5
|
+
* 生成内容(不依赖具体宿主,可直接放进任意 workbench 的 server/ 下):
|
|
6
|
+
* model/schema.mjs 唯一需要按领域改的文件(怎么分页、ID 在哪、文本在哪)
|
|
7
|
+
* model/tools.mjs <ns>_read / <ns>_edit / <ns>_validate 三个工具工厂
|
|
8
|
+
* model/model.test.mjs 定位 → 打补丁 → 校验 的回归测试
|
|
9
|
+
* model/README.md 接入步骤
|
|
10
|
+
*
|
|
11
|
+
* 用法:
|
|
12
|
+
* node tools/capyclaw-plugin-generator/bin/create-doc-model.mjs \
|
|
13
|
+
* --ns sheet --title "表格工作台" --out ./my-plugins/sheet
|
|
14
|
+
*/
|
|
15
|
+
import fs from 'node:fs'
|
|
16
|
+
import path from 'node:path'
|
|
17
|
+
import { fileURLToPath } from 'node:url'
|
|
18
|
+
|
|
19
|
+
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
|
20
|
+
const TEMPLATES = path.join(HERE, '..', 'templates', 'doc-model')
|
|
21
|
+
|
|
22
|
+
function printHelp() {
|
|
23
|
+
console.log(`
|
|
24
|
+
文档模型脚手架
|
|
25
|
+
|
|
26
|
+
用法:
|
|
27
|
+
node create-doc-model.mjs --ns sheet --title "表格工作台" --out ./out/sheet
|
|
28
|
+
|
|
29
|
+
参数:
|
|
30
|
+
--ns <kebab> 命名空间/工具前缀(必填,如 sheet → sheet_read)
|
|
31
|
+
--title <text> 显示标题(默认取 ns)
|
|
32
|
+
--out <dir> 输出目录(必填)
|
|
33
|
+
--force 覆盖已存在的文件
|
|
34
|
+
--help, -h 显示帮助
|
|
35
|
+
|
|
36
|
+
生成:
|
|
37
|
+
<out>/model/schema.mjs
|
|
38
|
+
<out>/model/tools.mjs
|
|
39
|
+
<out>/model/model.test.mjs
|
|
40
|
+
<out>/model/README.md
|
|
41
|
+
`)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function parseArgs(argv) {
|
|
45
|
+
const options = {}
|
|
46
|
+
for (let i = 0; i < argv.length; i++) {
|
|
47
|
+
const arg = argv[i]
|
|
48
|
+
const next = () => {
|
|
49
|
+
const value = argv[++i]
|
|
50
|
+
if (value === undefined) throw new Error(`参数缺少值: ${arg}`)
|
|
51
|
+
return value
|
|
52
|
+
}
|
|
53
|
+
if (arg === '--help' || arg === '-h') options.help = true
|
|
54
|
+
else if (arg === '--force') options.force = true
|
|
55
|
+
else if (arg === '--ns') options.ns = next()
|
|
56
|
+
else if (arg.startsWith('--ns=')) options.ns = arg.slice(5)
|
|
57
|
+
else if (arg === '--title') options.title = next()
|
|
58
|
+
else if (arg.startsWith('--title=')) options.title = arg.slice(8)
|
|
59
|
+
else if (arg === '--out') options.out = next()
|
|
60
|
+
else if (arg.startsWith('--out=')) options.out = arg.slice(6)
|
|
61
|
+
else throw new Error(`未知参数: ${arg}`)
|
|
62
|
+
}
|
|
63
|
+
return options
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function slugOf(value) {
|
|
67
|
+
return String(value || '').trim().toLowerCase().replace(/[^a-z0-9-]+/g, '-').replace(/^-+|-+$/g, '')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function render(templateRel, tokens) {
|
|
71
|
+
const source = fs.readFileSync(path.join(TEMPLATES, templateRel), 'utf8')
|
|
72
|
+
return source.replace(/\{\{([A-Z_]+)\}\}/g, (match, key) => (key in tokens ? tokens[key] : match))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function writeFile(file, content, force) {
|
|
76
|
+
fs.mkdirSync(path.dirname(file), { recursive: true })
|
|
77
|
+
if (fs.existsSync(file) && !force) throw new Error(`已存在,加 --force 覆盖: ${file}`)
|
|
78
|
+
fs.writeFileSync(file, content, 'utf8')
|
|
79
|
+
console.log('+ ' + path.relative(process.cwd(), file))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function main() {
|
|
83
|
+
let options
|
|
84
|
+
try {
|
|
85
|
+
options = parseArgs(process.argv.slice(2))
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.error(`✗ ${error.message}`)
|
|
88
|
+
process.exit(1)
|
|
89
|
+
}
|
|
90
|
+
if (options.help) return printHelp()
|
|
91
|
+
|
|
92
|
+
const ns = slugOf(options.ns)
|
|
93
|
+
if (!ns) {
|
|
94
|
+
console.error('✗ 缺少 --ns(命名空间,如 sheet)')
|
|
95
|
+
process.exit(1)
|
|
96
|
+
}
|
|
97
|
+
const out = options.out ? path.resolve(options.out) : null
|
|
98
|
+
if (!out) {
|
|
99
|
+
console.error('✗ 缺少 --out(输出目录)')
|
|
100
|
+
process.exit(1)
|
|
101
|
+
}
|
|
102
|
+
const title = options.title || options.ns
|
|
103
|
+
const tokens = {
|
|
104
|
+
NS: ns,
|
|
105
|
+
TITLE: title,
|
|
106
|
+
NS_LABEL: title,
|
|
107
|
+
CONST_PREFIX: ns.toUpperCase().replace(/-/g, '_'),
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const files = [
|
|
111
|
+
['schema.mjs.tmpl', 'model/schema.mjs'],
|
|
112
|
+
['tools.mjs.tmpl', 'model/tools.mjs'],
|
|
113
|
+
['test.mjs.tmpl', 'model/model.test.mjs'],
|
|
114
|
+
['README.md.tmpl', 'model/README.md'],
|
|
115
|
+
]
|
|
116
|
+
try {
|
|
117
|
+
for (const [template, dest] of files) {
|
|
118
|
+
writeFile(path.join(out, dest), render(template, tokens), options.force)
|
|
119
|
+
}
|
|
120
|
+
} catch (error) {
|
|
121
|
+
console.error(`✗ ${error.message}`)
|
|
122
|
+
process.exit(1)
|
|
123
|
+
}
|
|
124
|
+
console.log(`\n✓ 已生成 ${ns} 文档模型(4 个文件)`)
|
|
125
|
+
console.log(` 下一步:改 model/schema.mjs 的适配函数 → node ${path.join(path.relative(process.cwd(), out), 'model', 'model.test.mjs')}`)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
main()
|
package/bin/create-plugin.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
3
|
* DSH 插件脚手架(标准版)。
|
|
4
4
|
*
|
|
@@ -14,7 +14,7 @@ import path from 'node:path'
|
|
|
14
14
|
import readline from 'node:readline'
|
|
15
15
|
import { fileURLToPath } from 'node:url'
|
|
16
16
|
import { renderCordisPatch } from '@capyclaw/suite-kit/compose'
|
|
17
|
-
import { syncSharedDir } from '
|
|
17
|
+
import { syncSharedDir } from '../../../../A/build/sync-shared.mjs'
|
|
18
18
|
|
|
19
19
|
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
|
20
20
|
const TEMPLATES = path.join(HERE, '..', 'templates')
|
|
@@ -128,6 +128,12 @@ function makePackageJson({ name, title, description, type, version }) {
|
|
|
128
128
|
},
|
|
129
129
|
dependencies: {
|
|
130
130
|
'@capyclaw/plugin-kit': '^0.2.0',
|
|
131
|
+
/* server/shared/*.mjs 是包依赖转发(sync-shared 生成),实现来自这些包:
|
|
132
|
+
* 新增插件必须声明依赖,否则装完即用时解析不到。 */
|
|
133
|
+
'@capyclaw/service-host': '^0.1.3',
|
|
134
|
+
'@capyclaw/tool-host': '^0.1.3',
|
|
135
|
+
'@capyclaw/asset-service': '^0.1.3',
|
|
136
|
+
'@capyclaw/editor-host': '^0.1.3',
|
|
131
137
|
},
|
|
132
138
|
engines: {
|
|
133
139
|
dsh: '^0.2.0-rc.2',
|
|
@@ -381,7 +387,7 @@ ${type === 'ui' ? `启动后浏览器打开:http://127.0.0.1:${options.port}/\
|
|
|
381
387
|
fs.mkdirSync(path.join(suiteDir, 'lib'), { recursive: true })
|
|
382
388
|
fs.writeFileSync(path.join(suiteDir, 'cordis.patch.yml'), renderCordisPatch(suite.rows, { header: true }))
|
|
383
389
|
fs.writeFileSync(path.join(suiteDir, 'lib', 'index.mjs'), `export const name = '${suiteName}'\n\nexport function apply() {}\n`)
|
|
384
|
-
fs.writeFileSync(path.join(suiteDir, 'README.md'), `# ${suiteName}\n\n安装:\n\n\`\`\`bash\nnode packages/capyclaw-suite-kit/bin/dsh-suite.mjs install ${suiteDir.replace(/\\/g, '/')} --profile web\n\`\`\`\n`)
|
|
390
|
+
fs.writeFileSync(path.join(suiteDir, 'README.md'), `# ${suiteName}\n\n安装:\n\n\`\`\`bash\nnode C/packages/capyclaw-suite-kit/bin/dsh-suite.mjs install ${suiteDir.replace(/\\/g, '/')} --profile web\n\`\`\`\n`)
|
|
385
391
|
|
|
386
392
|
writeJson(path.join(domainDir, 'dsh-domain.json'), {
|
|
387
393
|
name: domainName,
|
|
@@ -424,7 +430,10 @@ ${type === 'ui' ? `启动后浏览器打开:http://127.0.0.1:${options.port}/\
|
|
|
424
430
|
},
|
|
425
431
|
ports: {
|
|
426
432
|
harness: { preferred: 3080, range: 100 },
|
|
427
|
-
|
|
433
|
+
/* 脚手架生成的插件只声明「可接受的端口范围」,不继承任何既有插件的端口:
|
|
434
|
+
* 曾经这里写死过某个工作台的固定端口,于是生成的插件会一起去抢同一个号。
|
|
435
|
+
* 要固定就用 --port(默认 DEFAULT_PORT)。 */
|
|
436
|
+
content: { preferred: options.port || DEFAULT_PORT, range: 100 },
|
|
428
437
|
},
|
|
429
438
|
},
|
|
430
439
|
removeRows: suite.rows.map((row) => row.id),
|
|
@@ -440,8 +449,8 @@ ${type === 'ui' ? `启动后浏览器打开:http://127.0.0.1:${options.port}/\
|
|
|
440
449
|
console.log('\n下一步:')
|
|
441
450
|
console.log(` npm run validate-plugin -- ${pluginDir.replace(/\\/g, '/')}`)
|
|
442
451
|
if (withSuite) {
|
|
443
|
-
console.log(` node packages/capyclaw-suite-kit/bin/dsh-suite.mjs validate ${suiteDir}`)
|
|
444
|
-
console.log(` node packages/capyclaw-suite-kit/bin/dsh-domain.mjs validate ${domainDir}`)
|
|
452
|
+
console.log(` node C/packages/capyclaw-suite-kit/bin/dsh-suite.mjs validate ${suiteDir}`)
|
|
453
|
+
console.log(` node C/packages/capyclaw-suite-kit/bin/dsh-domain.mjs validate ${domainDir}`)
|
|
445
454
|
}
|
|
446
455
|
}
|
|
447
456
|
|
package/bin/pack-plugin.mjs
CHANGED
|
@@ -1,36 +1,42 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
* DSH
|
|
3
|
+
* DSH 插件独立打包 CLI(单插件 → 可分发的 zip)。
|
|
4
4
|
*
|
|
5
5
|
* 用法:
|
|
6
6
|
* node pack-plugin.mjs <插件目录> [--out <目录>]
|
|
7
7
|
*
|
|
8
8
|
* 输出:
|
|
9
|
-
* <out>/<name
|
|
9
|
+
* <out>/<A|B|C>/<name>-<version>.zip(默认 <out> = 仓根 dist/;类别由分发面决定,
|
|
10
|
+
* 仓外插件按独立类 B 落盘)
|
|
11
|
+
*
|
|
12
|
+
* 这是 `dsh-release release <目录>` 的薄封装:打包实现只有一份
|
|
13
|
+
* (@capyclaw/plugin-kit 的 packPlugin),闸门也只有一份(验证器 + 发布闸 +
|
|
14
|
+
* vendor 依赖内置)。不再有绕过发布闸、把 zip 写到 dist/ 之外的第二条路径。
|
|
10
15
|
*/
|
|
11
16
|
import fs from 'node:fs'
|
|
12
17
|
import path from 'node:path'
|
|
13
|
-
import { fileURLToPath } from 'node:url'
|
|
14
|
-
import { packPlugin } from '@capyclaw/plugin-kit/packaging/pack'
|
|
15
18
|
|
|
16
|
-
|
|
19
|
+
import { buildIndex, DEFAULT_OUT, releasePlugin } from '../../capyclaw-plugin-release/lib/index.mjs'
|
|
20
|
+
import { distributionSurface } from '../../capyclaw-plugin-release/lib/surface.mjs'
|
|
17
21
|
|
|
18
22
|
function usage() {
|
|
19
23
|
console.log(`
|
|
20
|
-
DSH
|
|
24
|
+
DSH 插件打包工具(与 dsh-release 同一实现)
|
|
21
25
|
|
|
22
26
|
用法:
|
|
23
|
-
node pack-plugin.mjs <插件目录> [--out
|
|
27
|
+
node pack-plugin.mjs <插件目录> [--out <源目录>]
|
|
24
28
|
|
|
25
29
|
说明:
|
|
30
|
+
- 默认输出到仓根 dist/,产物落 dist/<A|B|C>/<name>-<version>.zip
|
|
31
|
+
- 打包前过两道闸:插件规范验证(dsh-release 同款)+ 发布闸
|
|
26
32
|
- 始终包含 package.json、README、LICENSE、THIRD_PARTY_NOTICES(存在时)
|
|
27
|
-
-
|
|
28
|
-
-
|
|
33
|
+
- 按 package.json files 收集,支持目录、文件与 ! 排除项
|
|
34
|
+
- 打包后重建 <out>/index.json,保证索引与 zip 同步
|
|
29
35
|
`)
|
|
30
36
|
}
|
|
31
37
|
|
|
32
38
|
function parseArgs(argv) {
|
|
33
|
-
const options = { src: '', out:
|
|
39
|
+
const options = { src: '', out: DEFAULT_OUT }
|
|
34
40
|
for (let i = 0; i < argv.length; i++) {
|
|
35
41
|
const arg = argv[i]
|
|
36
42
|
if (arg === '--help' || arg === '-h') options.help = true
|
|
@@ -42,7 +48,7 @@ function parseArgs(argv) {
|
|
|
42
48
|
return options
|
|
43
49
|
}
|
|
44
50
|
|
|
45
|
-
function main() {
|
|
51
|
+
async function main() {
|
|
46
52
|
let options
|
|
47
53
|
try {
|
|
48
54
|
options = parseArgs(process.argv.slice(2))
|
|
@@ -63,17 +69,18 @@ function main() {
|
|
|
63
69
|
}
|
|
64
70
|
|
|
65
71
|
try {
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
})
|
|
72
|
+
const out = path.resolve(options.out)
|
|
73
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(srcDir, 'package.json'), 'utf8'))
|
|
74
|
+
const entry = distributionSurface({ out }).plugins.find((item) => item.name === pkg.name)
|
|
75
|
+
const result = await releasePlugin(srcDir, { out, className: entry?.cls })
|
|
76
|
+
const index = buildIndex(out)
|
|
70
77
|
console.log(`✓ 已打包: ${result.outFile}`)
|
|
71
|
-
console.log(` 文件数: ${result.
|
|
72
|
-
|
|
78
|
+
console.log(` 文件数: ${result.files.length}${result.vendored?.length ? `;内置依赖: ${result.vendored.join(', ')}` : ''}`)
|
|
79
|
+
console.log(` 源索引: ${path.join(path.resolve(options.out), 'index.json')}(${index.plugins.length} 条)`)
|
|
73
80
|
console.log(' 安装:')
|
|
74
81
|
console.log(' 1) 解压到任意目录')
|
|
75
82
|
if (result.pkg.dsh?.suite) {
|
|
76
|
-
console.log(' 2) node packages/capyclaw-suite-kit/bin/dsh-suite.mjs install <解压后的目录> --profile web')
|
|
83
|
+
console.log(' 2) node C/packages/capyclaw-suite-kit/bin/dsh-suite.mjs install <解压后的目录> --profile web')
|
|
77
84
|
} else {
|
|
78
85
|
console.log(' 2) dsh plugin --profile web add <解压后的目录>')
|
|
79
86
|
}
|
|
@@ -83,4 +90,4 @@ function main() {
|
|
|
83
90
|
}
|
|
84
91
|
}
|
|
85
92
|
|
|
86
|
-
main()
|
|
93
|
+
await main()
|
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capyclaw/plugin-generator",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "DSH 插件脚手架生成器:快速创建符合规范的插件骨架",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./lib/index.mjs",
|
|
7
6
|
"bin": {
|
|
8
|
-
"create-plugin": "./bin/
|
|
7
|
+
"create-plugin": "./bin/create-plugin.mjs",
|
|
8
|
+
"create-doc-model": "./bin/create-doc-model.mjs"
|
|
9
9
|
},
|
|
10
10
|
"scripts": {},
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
14
14
|
"url": "git+https://github.com/Maple496/capyclaw.git",
|
|
15
|
-
"directory": "tools/capyclaw-plugin-generator"
|
|
15
|
+
"directory": "C/tools/capyclaw-plugin-generator"
|
|
16
16
|
},
|
|
17
17
|
"bugs": {
|
|
18
18
|
"url": "https://github.com/Maple496/capyclaw/issues"
|
|
19
19
|
},
|
|
20
|
-
"homepage": "https://github.com/Maple496/capyclaw/tree/main/tools/capyclaw-plugin-generator#readme"
|
|
20
|
+
"homepage": "https://github.com/Maple496/capyclaw/tree/main/C/tools/capyclaw-plugin-generator#readme"
|
|
21
21
|
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# {{TITLE}} · 文档模型
|
|
2
|
+
|
|
3
|
+
由 `create-doc-model.mjs` 生成。这套文件把「PPT 那种一次读就能按元素 ID 精确修改」
|
|
4
|
+
的路径变成可复用件——你只需要描述**这份文档长什么样**。
|
|
5
|
+
|
|
6
|
+
## 三层
|
|
7
|
+
|
|
8
|
+
| 层 | 文件 | 说明 |
|
|
9
|
+
|---|---|---|
|
|
10
|
+
| 领域适配 | `schema.mjs` | 唯一需要改的文件:怎么分页、节点在哪、ID/文本/几何怎么取 |
|
|
11
|
+
| 模型 | `@capyclaw/document-workbench/model` | 稳定 ID、清单投影、定位器、编辑计划、校验(已复用,不要改) |
|
|
12
|
+
| 工具 | `tools.mjs` | `{{NS}}_read` / `{{NS}}_edit` / `{{NS}}_validate` 三个 MCP 工具 |
|
|
13
|
+
|
|
14
|
+
## 接入(约 20 行)
|
|
15
|
+
|
|
16
|
+
在你的 workbench 里接上两个 I/O,然后把 capability 加进工具注册表:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import { createModelTools } from './model/tools.mjs'
|
|
20
|
+
|
|
21
|
+
const { capability, tools } = createModelTools({
|
|
22
|
+
schema,
|
|
23
|
+
readDoc: () => bridge.getDoc(), // 读当前文档树
|
|
24
|
+
writeDoc: (doc, title) => bridge.push([{ // 编辑器权威桥:一条 setValue
|
|
25
|
+
cmd: 'setValue', args: [doc],
|
|
26
|
+
}], title),
|
|
27
|
+
defineTool, defineCapability,
|
|
28
|
+
ns: '{{NS}}',
|
|
29
|
+
})
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 用法(模型视角)
|
|
33
|
+
|
|
34
|
+
1. `{{NS}}_read` → 拿到每个元素的 `id / 路径 / 槽位 / 几何 / 文本`
|
|
35
|
+
2. `{{NS}}_edit` → `edits: [{ locate: { page: 1, role: 'title' }, patch: { text: '新标题' } }]`
|
|
36
|
+
3. `{{NS}}_validate` → 重复 ID、空页、越界、超长文本
|
|
37
|
+
|
|
38
|
+
定位器支持 `id / path / page / role / text / type / index`,多条件取交集;
|
|
39
|
+
不唯一时返回候选列表,不猜。
|
|
40
|
+
|
|
41
|
+
## 两条硬约定
|
|
42
|
+
|
|
43
|
+
1. **生成时就写 ID**:用 `slotId('title', pageIndex, index)`,以后移动/改样式都不变。
|
|
44
|
+
没有 ID 的元素只能按路径寻址,重排后就会指错。
|
|
45
|
+
2. **一次读、一次改**:不要边读边改、也不要按截图猜坐标;改完再读一次确认即可。
|
|
46
|
+
|
|
47
|
+
## 测试
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
node model/model.test.mjs
|
|
51
|
+
```
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文档模型适配({{TITLE}}):唯一需要按领域填写的文件。
|
|
3
|
+
*
|
|
4
|
+
* 模型层只问 6 个问题,回答完就有了「按 ID 精确编辑」的能力:
|
|
5
|
+
* 1. 怎么分页 pages(doc)
|
|
6
|
+
* 2. 每页有哪些节点 nodes(page, pageIndex)
|
|
7
|
+
* 3. 节点 ID 在哪 nodeId(node, index, pageIndex)
|
|
8
|
+
* 4. 节点是什么类型 nodeType(node)
|
|
9
|
+
* 5. 语义槽位(可选) role(node) title/body/card/value/label/foot
|
|
10
|
+
* 6. 几何与文本在哪 box(node) / text(node)
|
|
11
|
+
*
|
|
12
|
+
* 关键约定:**ID 必须在生成时就写进文档**,且后续移动/改样式不变。
|
|
13
|
+
* 生成时用 slotId('<槽位>', pageIndex, index) 即可,例如:
|
|
14
|
+
* { id: slotId('title', i), type: 'text', ... }
|
|
15
|
+
* 没有 ID 的元素只能按路径寻址,一旦重排就会指错。
|
|
16
|
+
*/
|
|
17
|
+
import { slotId } from '@capyclaw/document-workbench/model'
|
|
18
|
+
|
|
19
|
+
export const schema = {
|
|
20
|
+
pages: (doc) => (Array.isArray(doc && doc.pages) ? doc.pages : []),
|
|
21
|
+
pageId: (page, index) => (page && page.id) || `p${index}`,
|
|
22
|
+
nodes: (page) => (page && Array.isArray(page.elements) ? page.elements : []),
|
|
23
|
+
nodeId: (node) => (node && node.id) || null,
|
|
24
|
+
nodeType: (node) => (node && node.type) || 'unknown',
|
|
25
|
+
role: (node) => (node && node.role) || null,
|
|
26
|
+
box: (node) => {
|
|
27
|
+
if (!node) return null
|
|
28
|
+
const { left, top, width, height } = node
|
|
29
|
+
if (![left, top, width, height].every((n) => Number.isFinite(n))) return null
|
|
30
|
+
return { x: left, y: top, w: width, h: height }
|
|
31
|
+
},
|
|
32
|
+
text: (node) => (node && (node.text ?? node.value ?? node.content)) || '',
|
|
33
|
+
pageSize: () => null, // 已知页面尺寸时返回 { w, h },可启用越界校验
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 空文档形态:写入前的最小骨架。 */
|
|
37
|
+
export function emptyDoc() {
|
|
38
|
+
return { pages: [], rev: 0 }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 示例文档:脚手架自带一份可跑的 fixture,替换成你的真实形态后测试仍然有效。
|
|
43
|
+
* 注意所有元素都带稳定 ID——这是后续能被精确修改的前提。
|
|
44
|
+
*/
|
|
45
|
+
export function sampleDoc() {
|
|
46
|
+
return {
|
|
47
|
+
rev: 1,
|
|
48
|
+
pages: [
|
|
49
|
+
{
|
|
50
|
+
id: 'p0',
|
|
51
|
+
elements: [
|
|
52
|
+
{ id: slotId('title', 0), type: 'text', role: 'title', left: 60, top: 60, width: 880, height: 80, text: '示例标题' },
|
|
53
|
+
{ id: slotId('body', 0), type: 'text', role: 'body', left: 60, top: 170, width: 880, height: 260, text: '示例正文' },
|
|
54
|
+
],
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
id: 'p1',
|
|
58
|
+
elements: [
|
|
59
|
+
{ id: slotId('title', 1), type: 'text', role: 'title', left: 60, top: 60, width: 880, height: 80, text: '第二页' },
|
|
60
|
+
{ id: slotId('value', 1, 0), type: 'text', role: 'value', left: 60, top: 200, width: 240, height: 120, text: '70%' },
|
|
61
|
+
{ id: slotId('value', 1, 1), type: 'text', role: 'value', left: 360, top: 200, width: 240, height: 120, text: '3.5x' },
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** 生成节点时可用的 ID 助手(与 PT 的 lt_stat_value_0_1 同风格)。 */
|
|
69
|
+
export { slotId }
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {{TITLE}} 模型回归:定位 → 打补丁 → 校验,以及三个工具的端到端链路。
|
|
3
|
+
*
|
|
4
|
+
* 这些测试不需要编辑器/服务:模型层是纯函数,工具通过注入的 readDoc/writeDoc 工作。
|
|
5
|
+
*/
|
|
6
|
+
import test from 'node:test'
|
|
7
|
+
import assert from 'node:assert/strict'
|
|
8
|
+
|
|
9
|
+
import { applyPlan, buildManifest, locate, planEdit, validateManifest } from '@capyclaw/document-workbench/model'
|
|
10
|
+
import { sampleDoc, schema } from './schema.mjs'
|
|
11
|
+
import { createModelTools } from './tools.mjs'
|
|
12
|
+
|
|
13
|
+
const defineTool = (def) => ({ ...def })
|
|
14
|
+
const defineCapability = (def) => ({ ...def })
|
|
15
|
+
|
|
16
|
+
test('清单:每个元素都有稳定 ID 与路径', () => {
|
|
17
|
+
const manifest = buildManifest(sampleDoc(), schema)
|
|
18
|
+
assert.equal(manifest.pages.length, 2)
|
|
19
|
+
assert.ok(manifest.nodes.length >= 4)
|
|
20
|
+
for (const node of manifest.nodes) {
|
|
21
|
+
assert.match(node.id, /^[A-Za-z0-9._:-]+$/, `非法 ID: ${node.id}`)
|
|
22
|
+
assert.match(node.path, /^p\d+\/n\d+/)
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('定位:按语义槽位精确命中,且不唯一时报候选', () => {
|
|
27
|
+
const manifest = buildManifest(sampleDoc(), schema)
|
|
28
|
+
const title = locate(manifest, { page: 0, role: 'title' })
|
|
29
|
+
assert.equal(title.page, 0)
|
|
30
|
+
assert.ok(title.text.includes('示例标题'))
|
|
31
|
+
assert.throws(() => locate(manifest, { role: 'value' }), (error) => {
|
|
32
|
+
assert.equal(error.code, 'LOCATOR_AMBIGUOUS')
|
|
33
|
+
assert.equal(error.candidates.length, 2)
|
|
34
|
+
return true
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
test('编辑:定位 + 补丁 → 纯函数改写,原文档不变', () => {
|
|
39
|
+
const doc = sampleDoc()
|
|
40
|
+
const manifest = buildManifest(doc, schema)
|
|
41
|
+
const plan = planEdit(manifest, [
|
|
42
|
+
{ locate: { page: 1, role: 'title' }, patch: { text: '改过的标题' } },
|
|
43
|
+
{ locate: { page: 1, role: 'value' }, patch: { style: { color: '#e3b23c' } } },
|
|
44
|
+
].map((edit) => (edit.locate.role === 'value' ? { ...edit, locate: { page: 1, role: 'value', index: 1 } } : edit)))
|
|
45
|
+
const { doc: next, applied } = applyPlan(doc, plan, schema)
|
|
46
|
+
assert.equal(applied.length, 2)
|
|
47
|
+
assert.equal(next.pages[1].elements[0].text, '改过的标题')
|
|
48
|
+
assert.equal(next.pages[1].elements[1].color, '#e3b23c')
|
|
49
|
+
assert.equal(doc.pages[1].elements[0].text, '第二页', '原文档不能被就地修改')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('工具端到端:{{NS}}_read → {{NS}}_edit → {{NS}}_validate', async () => {
|
|
53
|
+
let doc = sampleDoc()
|
|
54
|
+
const { tools } = createModelTools({
|
|
55
|
+
schema,
|
|
56
|
+
defineTool,
|
|
57
|
+
defineCapability,
|
|
58
|
+
readDoc: () => doc,
|
|
59
|
+
writeDoc: (next) => { doc = next },
|
|
60
|
+
ns: '{{NS}}',
|
|
61
|
+
})
|
|
62
|
+
const byName = Object.fromEntries(tools.map((tool) => [tool.name, tool]))
|
|
63
|
+
assert.deepEqual(Object.keys(byName).sort(), ['{{NS}}_edit', '{{NS}}_read', '{{NS}}_validate'])
|
|
64
|
+
|
|
65
|
+
const read = await byName['{{NS}}_read'].handler({})
|
|
66
|
+
assert.match(read.content[0].text, /第 1 页/)
|
|
67
|
+
assert.match(read.content[0].text, /title_0/)
|
|
68
|
+
|
|
69
|
+
const edited = await byName['{{NS}}_edit'].handler({
|
|
70
|
+
edits: [{ locate: { page: 1, role: 'title' }, patch: { text: '工具改的标题' } }],
|
|
71
|
+
})
|
|
72
|
+
assert.match(edited.content[0].text, /已修改 1 个元素/)
|
|
73
|
+
assert.equal(doc.pages[1].elements[0].text, '工具改的标题')
|
|
74
|
+
|
|
75
|
+
const validated = await byName['{{NS}}_validate'].handler({})
|
|
76
|
+
assert.equal(validated.isError, false)
|
|
77
|
+
assert.match(validated.content[0].text, /校验通过/)
|
|
78
|
+
|
|
79
|
+
const miss = await byName['{{NS}}_edit'].handler({
|
|
80
|
+
edits: [{ locate: { text: '不存在的文本' }, patch: { text: 'x' } }],
|
|
81
|
+
})
|
|
82
|
+
assert.equal(miss.isError, true)
|
|
83
|
+
assert.match(miss.content[0].text, /定位失败/)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('校验能抓出重复 ID(最常见的生成缺陷)', () => {
|
|
87
|
+
const doc = sampleDoc()
|
|
88
|
+
doc.pages[1].elements[1].id = doc.pages[1].elements[0].id
|
|
89
|
+
const result = validateManifest(buildManifest(doc, schema))
|
|
90
|
+
assert.equal(result.ok, false)
|
|
91
|
+
assert.ok(result.issues.some((issue) => issue.code === 'ID_DUPLICATE'))
|
|
92
|
+
})
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文档模型工具({{TITLE}}):读清单 → 按定位器精确改 → 校验,三件套。
|
|
3
|
+
*
|
|
4
|
+
* 特点(这就是 PPT 那条快路径的通用形态):
|
|
5
|
+
* - 读一次拿到每个元素的稳定 ID / 路径 / 语义槽位 / 几何 / 文本;
|
|
6
|
+
* - 改的时候只给「定位器 + 补丁」,模型不需要截图、不需要猜坐标;
|
|
7
|
+
* - 找不到或不唯一会明确报错并给候选,不会静默改错元素。
|
|
8
|
+
*
|
|
9
|
+
* 宿主只需注入两个 I/O:
|
|
10
|
+
* readDoc() → 当前文档(领域树)
|
|
11
|
+
* writeDoc(doc, title) → 把整棵树写回(编辑器权威桥里通常是 setValue 一条命令)
|
|
12
|
+
*/
|
|
13
|
+
import {
|
|
14
|
+
applyPlan, buildManifest, describeIssues, locateAll, planEdit, validateManifest,
|
|
15
|
+
} from '@capyclaw/document-workbench/model'
|
|
16
|
+
import { schema as defaultSchema } from './schema.mjs'
|
|
17
|
+
|
|
18
|
+
const NS = '{{NS}}'
|
|
19
|
+
|
|
20
|
+
function manifestText(manifest) {
|
|
21
|
+
const lines = []
|
|
22
|
+
for (const page of manifest.pages) {
|
|
23
|
+
lines.push(`# 第 ${page.index + 1} 页${page.title ? ' · ' + page.title : ''}(${page.count} 个元素)`)
|
|
24
|
+
for (const node of manifest.nodes.filter((n) => n.page === page.index)) {
|
|
25
|
+
const box = node.box ? ` @(${Math.round(node.box.x)},${Math.round(node.box.y)}) ${Math.round(node.box.w)}×${Math.round(node.box.h)}` : ''
|
|
26
|
+
const text = node.text ? ` 「${node.text.slice(0, 40)}${node.text.length > 40 ? '…' : ''}」` : ''
|
|
27
|
+
lines.push(`- p${node.page}/n${node.index} ${node.id} · ${node.role || node.type}${box}${text}`)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return lines.join('\n')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {{
|
|
35
|
+
* schema?: object,
|
|
36
|
+
* readDoc: () => Promise<object>|object,
|
|
37
|
+
* writeDoc: (doc: object, title?: string) => Promise<any>,
|
|
38
|
+
* defineTool: Function,
|
|
39
|
+
* defineCapability: Function,
|
|
40
|
+
* ns?: string,
|
|
41
|
+
* }} options
|
|
42
|
+
*/
|
|
43
|
+
export function createModelTools(options = {}) {
|
|
44
|
+
const {
|
|
45
|
+
schema = defaultSchema,
|
|
46
|
+
readDoc,
|
|
47
|
+
writeDoc,
|
|
48
|
+
defineTool,
|
|
49
|
+
defineCapability,
|
|
50
|
+
ns = NS,
|
|
51
|
+
} = options
|
|
52
|
+
if (typeof readDoc !== 'function') throw new Error('createModelTools 需要 readDoc')
|
|
53
|
+
if (typeof writeDoc !== 'function') throw new Error('createModelTools 需要 writeDoc')
|
|
54
|
+
|
|
55
|
+
const manifestOf = async () => buildManifest(await readDoc(), schema)
|
|
56
|
+
|
|
57
|
+
const readTool = defineTool({
|
|
58
|
+
name: `${ns}_read`,
|
|
59
|
+
description: '读取文档清单:每个元素的稳定 ID、路径、语义槽位、几何与文本。改文档前先读一次,后续按 ID 精确定位。',
|
|
60
|
+
inputSchema: {
|
|
61
|
+
type: 'object',
|
|
62
|
+
properties: {
|
|
63
|
+
mode: { type: 'string', enum: ['manifest', 'json', 'node'], description: 'manifest(默认,模型可读文本)/ json(原始清单)/ node(单个元素)' },
|
|
64
|
+
locate: { type: 'object', description: 'mode=node 时的定位器:{ id | path | page | role | text | type | index }' },
|
|
65
|
+
page: { type: 'number', description: '只读某一页(0 起)' },
|
|
66
|
+
},
|
|
67
|
+
additionalProperties: false,
|
|
68
|
+
},
|
|
69
|
+
kind: 'read',
|
|
70
|
+
handler: async (args = {}) => {
|
|
71
|
+
const manifest = await manifestOf()
|
|
72
|
+
if (args.mode === 'node') {
|
|
73
|
+
const hits = locateAll(manifest, args.locate || {})
|
|
74
|
+
if (!hits.length) return { content: [{ type: 'text', text: '没有匹配的元素' }] }
|
|
75
|
+
return { content: [{ type: 'text', text: JSON.stringify(hits, null, 2) }] }
|
|
76
|
+
}
|
|
77
|
+
if (args.mode === 'json') {
|
|
78
|
+
const view = Number.isInteger(args.page)
|
|
79
|
+
? { ...manifest, nodes: manifest.nodes.filter((n) => n.page === args.page) }
|
|
80
|
+
: manifest
|
|
81
|
+
return { content: [{ type: 'text', text: JSON.stringify(view, null, 2) }] }
|
|
82
|
+
}
|
|
83
|
+
const scoped = Number.isInteger(args.page)
|
|
84
|
+
? { ...manifest, pages: manifest.pages.filter((p) => p.index === args.page), nodes: manifest.nodes.filter((n) => n.page === args.page) }
|
|
85
|
+
: manifest
|
|
86
|
+
return { content: [{ type: 'text', text: manifestText(scoped) }] }
|
|
87
|
+
},
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
const editTool = defineTool({
|
|
91
|
+
name: `${ns}_edit`,
|
|
92
|
+
description: '按定位器修改元素:edits = [{ locate, patch }]。locate 支持 id/path/page/role/text/type/index;patch 支持 text / box{x,y,w,h} / style / attrs。定位不唯一会返回候选。',
|
|
93
|
+
inputSchema: {
|
|
94
|
+
type: 'object',
|
|
95
|
+
properties: {
|
|
96
|
+
edits: {
|
|
97
|
+
type: 'array',
|
|
98
|
+
items: {
|
|
99
|
+
type: 'object',
|
|
100
|
+
properties: {
|
|
101
|
+
locate: { type: 'object', description: '定位器' },
|
|
102
|
+
patch: { type: 'object', description: '要改的字段:text / box / style / attrs' },
|
|
103
|
+
op: { type: 'string', enum: ['update', 'remove', 'insert', 'move'] },
|
|
104
|
+
page: { type: 'number' },
|
|
105
|
+
index: { type: 'number' },
|
|
106
|
+
},
|
|
107
|
+
required: ['locate'],
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
title: { type: 'string', description: '批次说明(进 undo 历史)' },
|
|
111
|
+
},
|
|
112
|
+
required: ['edits'],
|
|
113
|
+
additionalProperties: false,
|
|
114
|
+
},
|
|
115
|
+
kind: 'write',
|
|
116
|
+
handler: async (args = {}) => {
|
|
117
|
+
const doc = await readDoc()
|
|
118
|
+
const manifest = buildManifest(doc, schema)
|
|
119
|
+
let plan
|
|
120
|
+
try {
|
|
121
|
+
plan = planEdit(manifest, args.edits || [])
|
|
122
|
+
} catch (error) {
|
|
123
|
+
return { content: [{ type: 'text', text: `定位失败:${error.message}` }], isError: true }
|
|
124
|
+
}
|
|
125
|
+
const { doc: next, applied, skipped } = applyPlan(doc, plan, schema)
|
|
126
|
+
if (!applied.length) {
|
|
127
|
+
return { content: [{ type: 'text', text: `没有可应用的修改(${skipped.map((s) => s.reason).join(',') || '空计划'})` }], isError: true }
|
|
128
|
+
}
|
|
129
|
+
await writeDoc(next, args.title || '模型编辑')
|
|
130
|
+
const who = applied.map((op) => `${op.target ? op.target.id : 'insert@' + op.page}`).join('、')
|
|
131
|
+
return {
|
|
132
|
+
content: [{
|
|
133
|
+
type: 'text',
|
|
134
|
+
text: `已修改 ${applied.length} 个元素:${who}${skipped.length ? `(跳过 ${skipped.length})` : ''}`,
|
|
135
|
+
}],
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
const validateTool = defineTool({
|
|
141
|
+
name: `${ns}_validate`,
|
|
142
|
+
description: '校验文档模型:重复/缺失 ID、空页、越界、超长文本。写入后建议跑一次。',
|
|
143
|
+
inputSchema: {
|
|
144
|
+
type: 'object',
|
|
145
|
+
properties: {
|
|
146
|
+
maxChars: { type: 'number', description: '单元素文本上限(默认不检查)' },
|
|
147
|
+
},
|
|
148
|
+
additionalProperties: false,
|
|
149
|
+
},
|
|
150
|
+
kind: 'read',
|
|
151
|
+
handler: async (args = {}) => {
|
|
152
|
+
const manifest = await manifestOf()
|
|
153
|
+
const pageSize = typeof schema.pageSize === 'function' ? schema.pageSize : undefined
|
|
154
|
+
const result = validateManifest(manifest, { maxChars: args.maxChars, pageSize })
|
|
155
|
+
return { content: [{ type: 'text', text: describeIssues(result) }], isError: !result.ok }
|
|
156
|
+
},
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
const capability = defineCapability({
|
|
160
|
+
id: `${ns}-model`,
|
|
161
|
+
title: '{{TITLE}} · 文档模型',
|
|
162
|
+
description: '结构化读 + 稳定 ID 寻址 + 按 ID 精确修改 + 模型校验。',
|
|
163
|
+
tags: ['content'],
|
|
164
|
+
features: ['doc.read', 'doc.write', 'doc.validate'],
|
|
165
|
+
tools: [readTool, editTool, validateTool],
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
return { capability, tools: [readTool, editTool, validateTool] }
|
|
169
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* {{TITLE}} 服务组合根。
|
|
3
3
|
*
|
|
4
|
-
* 宿主(端口/锁/复用/健康/指标/静态/关闭)由
|
|
5
|
-
* 工具面(能力 DSL/注册表/校验/契约/MCP)由
|
|
4
|
+
* 宿主(端口/锁/复用/健康/指标/静态/关闭)由 @capyclaw/service-host 提供;
|
|
5
|
+
* 工具面(能力 DSL/注册表/校验/契约/MCP)由 @capyclaw/tool-host 提供;
|
|
6
6
|
* 本文件只保留业务装配。
|
|
7
7
|
*/
|
|
8
8
|
import fs from 'node:fs'
|
package/bin/cli.mjs
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* DSH 插件生成器 CLI
|
|
4
|
-
* 用法: create-plugin <plugin-name> [options]
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { fileURLToPath } from 'node:url'
|
|
8
|
-
import { dirname } from 'node:path'
|
|
9
|
-
import { generatePlugin } from '../lib/index.mjs'
|
|
10
|
-
|
|
11
|
-
const __filename = fileURLToPath(import.meta.url)
|
|
12
|
-
const __dirname = dirname(__filename)
|
|
13
|
-
|
|
14
|
-
const args = process.argv.slice(2)
|
|
15
|
-
const pluginName = args[0]
|
|
16
|
-
|
|
17
|
-
if (!pluginName || pluginName.startsWith('-')) {
|
|
18
|
-
console.log('用法: create-plugin <plugin-name> [options]')
|
|
19
|
-
console.log('')
|
|
20
|
-
console.log('示例:')
|
|
21
|
-
console.log(' create-plugin my-plugin --tier 0 # 纯客户端插件')
|
|
22
|
-
console.log(' create-plugin my-service --tier 1 # 标准服务插件')
|
|
23
|
-
console.log(' create-plugin my-suite --tier suite # 插件套件')
|
|
24
|
-
console.log('')
|
|
25
|
-
console.log('选项:')
|
|
26
|
-
console.log(' --tier <0|1|2|suite> 插件层级 (默认: 0)')
|
|
27
|
-
console.log(' --category <ui|service|bridge|tool|suite> 插件分类 (默认: ui)')
|
|
28
|
-
console.log(' --platform <web|desktop|both> 支持平台 (默认: web)')
|
|
29
|
-
console.log(' --output <path> 输出目录 (默认: extensions/official/)')
|
|
30
|
-
process.exit(1)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// 解析选项
|
|
34
|
-
const options = {
|
|
35
|
-
tier: 0,
|
|
36
|
-
category: 'ui',
|
|
37
|
-
platform: 'web',
|
|
38
|
-
output: null
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
for (let i = 1; i < args.length; i++) {
|
|
42
|
-
const arg = args[i]
|
|
43
|
-
if (arg === '--tier' && args[i + 1]) {
|
|
44
|
-
options.tier = args[i + 1] === 'suite' ? 'suite' : parseInt(args[i + 1], 10)
|
|
45
|
-
i++
|
|
46
|
-
} else if (arg === '--category' && args[i + 1]) {
|
|
47
|
-
options.category = args[i + 1]
|
|
48
|
-
i++
|
|
49
|
-
} else if (arg === '--platform' && args[i + 1]) {
|
|
50
|
-
options.platform = args[i + 1]
|
|
51
|
-
i++
|
|
52
|
-
} else if (arg === '--output' && args[i + 1]) {
|
|
53
|
-
options.output = args[i + 1]
|
|
54
|
-
i++
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
console.log('🚀 创建 DSH 插件:', pluginName)
|
|
59
|
-
console.log('━'.repeat(60))
|
|
60
|
-
console.log('配置:')
|
|
61
|
-
console.log(' 层级:', options.tier)
|
|
62
|
-
console.log(' 分类:', options.category)
|
|
63
|
-
console.log(' 平台:', options.platform)
|
|
64
|
-
console.log('')
|
|
65
|
-
|
|
66
|
-
try {
|
|
67
|
-
const result = await generatePlugin(pluginName, options)
|
|
68
|
-
console.log('\n✅ 插件创建成功!')
|
|
69
|
-
console.log('\n插件位置:', result.path)
|
|
70
|
-
console.log('\n下一步:')
|
|
71
|
-
console.log(` cd ${result.path}`)
|
|
72
|
-
console.log(' npm install')
|
|
73
|
-
console.log(` npm run validate-plugin -- ${result.path}`)
|
|
74
|
-
console.log('')
|
|
75
|
-
} catch (error) {
|
|
76
|
-
console.error('\n❌ 创建失败:', error.message)
|
|
77
|
-
process.exit(1)
|
|
78
|
-
}
|
package/lib/index.mjs
DELETED
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DSH 插件生成器核心
|
|
3
|
-
* 自动生成符合规范的插件骨架
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { mkdirSync, writeFileSync, existsSync } from 'node:fs'
|
|
7
|
-
import { join, resolve } from 'node:path'
|
|
8
|
-
import { fileURLToPath } from 'node:url'
|
|
9
|
-
import { dirname } from 'node:path'
|
|
10
|
-
|
|
11
|
-
const __filename = fileURLToPath(import.meta.url)
|
|
12
|
-
const __dirname = dirname(__filename)
|
|
13
|
-
const rootDir = resolve(__dirname, '../../..')
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* 生成插件
|
|
17
|
-
*/
|
|
18
|
-
export async function generatePlugin(pluginName, options = {}) {
|
|
19
|
-
const {
|
|
20
|
-
tier = 0,
|
|
21
|
-
category = 'ui',
|
|
22
|
-
platform = 'web',
|
|
23
|
-
output = null
|
|
24
|
-
} = options
|
|
25
|
-
|
|
26
|
-
// 验证插件名
|
|
27
|
-
if (!/^[a-z0-9-]+$/.test(pluginName)) {
|
|
28
|
-
throw new Error('插件名只能包含小写字母、数字和连字符')
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const fullName = `dsh-${pluginName}`
|
|
32
|
-
|
|
33
|
-
// 确定输出目录
|
|
34
|
-
let pluginDir
|
|
35
|
-
if (output) {
|
|
36
|
-
pluginDir = resolve(process.cwd(), output, fullName)
|
|
37
|
-
} else {
|
|
38
|
-
const baseDir = tier === 'suite' ? 'extensions/suites' : 'extensions/official'
|
|
39
|
-
pluginDir = join(rootDir, baseDir, fullName)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// 检查是否已存在
|
|
43
|
-
if (existsSync(pluginDir)) {
|
|
44
|
-
throw new Error(`插件目录已存在: ${pluginDir}`)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// 创建目录结构
|
|
48
|
-
mkdirSync(pluginDir, { recursive: true })
|
|
49
|
-
mkdirSync(join(pluginDir, 'lib'), { recursive: true })
|
|
50
|
-
|
|
51
|
-
// 生成 package.json
|
|
52
|
-
const packageJson = generatePackageJson(fullName, tier, category, platform)
|
|
53
|
-
writeFileSync(join(pluginDir, 'package.json'), JSON.stringify(packageJson, null, 2))
|
|
54
|
-
|
|
55
|
-
// 生成主入口
|
|
56
|
-
const mainEntry = generateMainEntry(fullName, tier)
|
|
57
|
-
writeFileSync(join(pluginDir, 'lib/index.mjs'), mainEntry)
|
|
58
|
-
|
|
59
|
-
// 生成 README
|
|
60
|
-
const readme = generateReadme(fullName, category, platform)
|
|
61
|
-
writeFileSync(join(pluginDir, 'README.md'), readme)
|
|
62
|
-
|
|
63
|
-
// 如果需要客户端代码,创建 client/
|
|
64
|
-
if (category === 'ui' || tier >= 1) {
|
|
65
|
-
mkdirSync(join(pluginDir, 'client'), { recursive: true })
|
|
66
|
-
const clientCode = generateClientCode(fullName)
|
|
67
|
-
writeFileSync(join(pluginDir, 'client/client.js'), clientCode)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// 生成 cordis.patch.yml
|
|
71
|
-
const cordisPatch = generateCordisPatch(fullName, category)
|
|
72
|
-
writeFileSync(join(pluginDir, 'cordis.patch.yml'), cordisPatch)
|
|
73
|
-
|
|
74
|
-
return { path: pluginDir, name: fullName }
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* 生成 package.json
|
|
79
|
-
*/
|
|
80
|
-
function generatePackageJson(name, tier, category, platform) {
|
|
81
|
-
const pkg = {
|
|
82
|
-
name,
|
|
83
|
-
version: '1.0.0',
|
|
84
|
-
description: `DSH ${category} 插件`,
|
|
85
|
-
type: 'module',
|
|
86
|
-
main: './lib/index.mjs',
|
|
87
|
-
exports: {
|
|
88
|
-
'.': './lib/index.mjs',
|
|
89
|
-
'./package.json': './package.json'
|
|
90
|
-
},
|
|
91
|
-
dependencies: {},
|
|
92
|
-
dsh: {
|
|
93
|
-
bundle: {
|
|
94
|
-
patch: './cordis.patch.yml'
|
|
95
|
-
},
|
|
96
|
-
category,
|
|
97
|
-
platform
|
|
98
|
-
},
|
|
99
|
-
engines: {
|
|
100
|
-
dsh: '^0.2.0-rc.2'
|
|
101
|
-
},
|
|
102
|
-
meta: {
|
|
103
|
-
title: name.replace('dsh-', '').split('-').map(w => w[0].toUpperCase() + w.slice(1)).join(' '),
|
|
104
|
-
description: `${category} 插件,支持 ${platform} 平台`
|
|
105
|
-
},
|
|
106
|
-
files: [
|
|
107
|
-
'lib/',
|
|
108
|
-
'cordis.patch.yml',
|
|
109
|
-
'README.md'
|
|
110
|
-
],
|
|
111
|
-
private: true
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// 根据层级添加依赖
|
|
115
|
-
if (tier === 'suite') {
|
|
116
|
-
pkg.dependencies['@capyclaw/suite-kit'] = '^0.1.0'
|
|
117
|
-
pkg.dsh.suite = './dsh-suite.json'
|
|
118
|
-
} else {
|
|
119
|
-
pkg.dependencies['@capyclaw/plugin-kit'] = '^0.2.0'
|
|
120
|
-
|
|
121
|
-
if (tier >= 1) {
|
|
122
|
-
pkg.dependencies['dsh-service-host'] = '^0.1.0'
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (tier >= 2) {
|
|
126
|
-
pkg.dependencies['dsh-tool-host'] = '^0.1.0'
|
|
127
|
-
pkg.dependencies['dsh-asset-service'] = '^0.1.0'
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// 如果有客户端代码,添加到 exports 和 files
|
|
132
|
-
if (category === 'ui' || tier >= 1) {
|
|
133
|
-
pkg.exports['./client'] = './client/client.js'
|
|
134
|
-
pkg.dsh.client = {
|
|
135
|
-
platform,
|
|
136
|
-
immediately: true
|
|
137
|
-
}
|
|
138
|
-
pkg.files.push('client/')
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return pkg
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* 生成主入口代码
|
|
146
|
-
*/
|
|
147
|
-
function generateMainEntry(name, tier) {
|
|
148
|
-
if (tier === 'suite') {
|
|
149
|
-
return `/**
|
|
150
|
-
* ${name} - DSH 插件套件
|
|
151
|
-
*/
|
|
152
|
-
|
|
153
|
-
export const name = '${name}'
|
|
154
|
-
|
|
155
|
-
export function apply(ctx) {
|
|
156
|
-
ctx.logger.info('${name} 套件已加载')
|
|
157
|
-
}
|
|
158
|
-
`
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return `/**
|
|
162
|
-
* ${name} - DSH 插件主入口
|
|
163
|
-
*/
|
|
164
|
-
|
|
165
|
-
export const name = '${name}'
|
|
166
|
-
|
|
167
|
-
export function apply(ctx, config) {
|
|
168
|
-
ctx.logger.info('${name} 插件已加载', config)
|
|
169
|
-
|
|
170
|
-
// TODO: 实现插件功能
|
|
171
|
-
}
|
|
172
|
-
`
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* 生成 README
|
|
177
|
-
*/
|
|
178
|
-
function generateReadme(name, category, platform) {
|
|
179
|
-
return `# ${name}
|
|
180
|
-
|
|
181
|
-
DSH ${category} 插件
|
|
182
|
-
|
|
183
|
-
## 功能
|
|
184
|
-
|
|
185
|
-
TODO: 描述插件功能
|
|
186
|
-
|
|
187
|
-
## 安装
|
|
188
|
-
|
|
189
|
-
\`\`\`bash
|
|
190
|
-
# 在 DSH profile 中安装
|
|
191
|
-
npm install ${name}
|
|
192
|
-
\`\`\`
|
|
193
|
-
|
|
194
|
-
## 配置
|
|
195
|
-
|
|
196
|
-
\`\`\`yaml
|
|
197
|
-
# cordis.patch.yml
|
|
198
|
-
- id: ${name}
|
|
199
|
-
name: ${name}
|
|
200
|
-
config:
|
|
201
|
-
# TODO: 配置项
|
|
202
|
-
\`\`\`
|
|
203
|
-
|
|
204
|
-
## 开发
|
|
205
|
-
|
|
206
|
-
\`\`\`bash
|
|
207
|
-
npm run validate-plugin -- <插件目录>
|
|
208
|
-
\`\`\`
|
|
209
|
-
|
|
210
|
-
## 平台支持
|
|
211
|
-
|
|
212
|
-
- ${platform}
|
|
213
|
-
|
|
214
|
-
## License
|
|
215
|
-
|
|
216
|
-
MIT
|
|
217
|
-
`
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* 生成客户端代码
|
|
222
|
-
*/
|
|
223
|
-
function generateClientCode(name) {
|
|
224
|
-
return `/**
|
|
225
|
-
* ${name} - 客户端代码
|
|
226
|
-
* 在浏览器环境中运行
|
|
227
|
-
*/
|
|
228
|
-
|
|
229
|
-
;(function() {
|
|
230
|
-
'use strict'
|
|
231
|
-
|
|
232
|
-
console.log('[${name}] 客户端代码已加载')
|
|
233
|
-
|
|
234
|
-
// TODO: 实现客户端功能
|
|
235
|
-
})()
|
|
236
|
-
`
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* 生成 cordis.patch.yml
|
|
241
|
-
*/
|
|
242
|
-
function generateCordisPatch(name, category) {
|
|
243
|
-
return `# ${name} Cordis 配置
|
|
244
|
-
|
|
245
|
-
- id: ${name}
|
|
246
|
-
name: ${name}
|
|
247
|
-
config:
|
|
248
|
-
# TODO: 添加配置项
|
|
249
|
-
`
|
|
250
|
-
}
|