@binblink/wasmnest-plugin-sdk-desktop 0.0.0-stage → 0.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/README.md +287 -2
- package/README_en.md +284 -0
- package/bin/cli.js +602 -0
- package/bin/manifest-gen.js +454 -0
- package/bin/manifest-validate.js +432 -0
- package/bin/permission-vocabulary.json +5 -0
- package/bin/plugin-watch.js +331 -0
- package/bin/wasm-hash.js +124 -0
- package/dev-shell/README.md +121 -0
- package/dev-shell/index.html +12 -0
- package/dev-shell/package.json +28 -0
- package/dev-shell/pnpm-workspace.yaml +9 -0
- package/dev-shell/postcss.config.js +6 -0
- package/dev-shell/src/App.vue +90 -0
- package/dev-shell/src/components/LogPanel.vue +90 -0
- package/dev-shell/src/loader.ts +109 -0
- package/dev-shell/src/locale.ts +29 -0
- package/dev-shell/src/locales.ts +50 -0
- package/dev-shell/src/main.ts +46 -0
- package/dev-shell/src/mock/agent-hub-commands.ts +1065 -0
- package/dev-shell/src/mock/agent-hub.ts +42 -0
- package/dev-shell/src/mock/app-env.ts +102 -0
- package/dev-shell/src/mock/commands.ts +54 -0
- package/dev-shell/src/mock/domain-context.ts +46 -0
- package/dev-shell/src/mock/events.ts +16 -0
- package/dev-shell/src/mock/file-transfer.ts +512 -0
- package/dev-shell/src/mock/tauri-ipc.ts +109 -0
- package/dev-shell/src/registry.ts +90 -0
- package/dev-shell/src/shell/components/AppMountSlot.vue +241 -0
- package/dev-shell/src/shell/components/ShellIcon.vue +141 -0
- package/dev-shell/src/shell/components/ShellStatusBar.vue +87 -0
- package/dev-shell/src/shell/components/ShellToolbar.vue +287 -0
- package/dev-shell/src/shell/mount/appSession.ts +45 -0
- package/dev-shell/src/shell/mount/devAppMount.ts +80 -0
- package/dev-shell/src/shell/types.ts +132 -0
- package/dev-shell/src/styles/dev.css +27 -0
- package/dev-shell/src/styles/shell.css +546 -0
- package/dev-shell/src/styles/style.css +1132 -0
- package/dev-shell/src/theme.ts +140 -0
- package/dev-shell/src/vite-env.d.ts +24 -0
- package/dev-shell/tailwind.config.js +98 -0
- package/dev-shell/vite.config.ts +174 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.js +170 -0
- package/dist/types.d.ts +585 -0
- package/dist/types.js +0 -0
- package/dist/vite-plugin.d.ts +25 -0
- package/dist/vite-plugin.js +80 -0
- package/package.json +89 -3
- package/src/config.ts +49 -0
- package/src/global-dialog.ts +169 -0
- package/src/index.ts +80 -0
- package/src/runtime.ts +54 -0
- package/src/types.ts +630 -0
- package/src/ui/MarkdownEditor.vue +520 -0
- package/src/ui/PluginGlobalDialog.vue +401 -0
- package/src/ui/Select.d.ts +36 -0
- package/src/ui/Select.vue +220 -0
- package/src/ui/TerminalInputRail.vue +184 -0
- package/src/ui/apply-select-position.ts +65 -0
- package/src/ui/markdown-editor-syntax.ts +234 -0
- package/src/ui/markdown-editor.d.ts +36 -0
- package/src/ui/plugin-global-dialog.d.ts +12 -0
- package/src/ui/select-position.ts +79 -0
- package/src/ui/terminal-input-rail.d.ts +30 -0
- package/src/ui/zoom-compensation.d.ts +9 -0
- package/src/ui/zoom-compensation.ts +52 -0
- package/src/vite-plugin.ts +124 -0
- package/template/README.md +39 -0
- package/template/package.json +23 -0
- package/template/plugin.json +14 -0
- package/template/rust/Cargo.toml +21 -0
- package/template/rust/src/lib.rs +39 -0
- package/template/src/env.d.ts +24 -0
- package/template/src/index.ts +37 -0
- package/template/tsconfig.json +17 -0
- package/template/vite.config.ts +21 -0
- package/tools/componentize/Cargo.lock +270 -0
- package/tools/componentize/Cargo.toml +15 -0
- package/tools/componentize/src/main.rs +94 -0
package/bin/cli.js
ADDED
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* wasmnest-plugin-desktop — WasmNest 桌面端插件开发工具包命令行
|
|
4
|
+
*
|
|
5
|
+
* 用法:
|
|
6
|
+
* wasmnest-plugin-desktop create <id> <name> [--author <author>] [--dir <dir>] [--rust] [--registry]
|
|
7
|
+
* wasmnest-plugin-desktop build [--resources-dir <dir>] [--frontend-only] [--rust-only]
|
|
8
|
+
* wasmnest-plugin-desktop dev [pluginDir] [--entry <file>] [--port <port>] [--host] [--open]
|
|
9
|
+
* wasmnest-plugin-desktop manifest [--check]
|
|
10
|
+
* wasmnest-plugin-desktop validate [--dir <dir>]
|
|
11
|
+
* wasmnest-plugin-desktop doctor
|
|
12
|
+
*
|
|
13
|
+
* create 从 SDK 内置模板生成插件工程(默认 ts-only;--rust 附带 WASM 后端脚手架);
|
|
14
|
+
* --registry 时引用已发布的 SDK 版本,否则引用本地 SDK 相对路径
|
|
15
|
+
* build 串联 vite build → cargo wasm32(rust-ts 插件);--resources-dir 时复制产物到宿主资源目录,
|
|
16
|
+
* 并把 wasm 字节的 SHA-256 注入**产物** plugin.json 的 wasmHash(源清单不带该键,见 bin/wasm-hash.js)
|
|
17
|
+
* dev 启动浏览器开发环境(dev-shell):vite dev server + HMR;加载应用的挂载包入口
|
|
18
|
+
* (src/entry.ts,导出 mountApp)源码,Rust 后端不在浏览器运行
|
|
19
|
+
* manifest 按插件源码自动填充 plugin.json 的 contributes/permissions;--check 只检查(CI 用)
|
|
20
|
+
* validate 校验 plugin.json 结构与字段合法性(CI 用,exit 1 表示不合法)
|
|
21
|
+
* doctor 环境自检:Node / Rust / wasm32 target / dev-shell 依赖 / SDK 构建产物
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { spawn, spawnSync } from 'node:child_process'
|
|
25
|
+
import {
|
|
26
|
+
copyFileSync,
|
|
27
|
+
cpSync,
|
|
28
|
+
existsSync,
|
|
29
|
+
mkdirSync,
|
|
30
|
+
readFileSync,
|
|
31
|
+
readdirSync,
|
|
32
|
+
rmSync,
|
|
33
|
+
writeFileSync,
|
|
34
|
+
} from 'node:fs'
|
|
35
|
+
import { dirname, join, relative, resolve } from 'node:path'
|
|
36
|
+
import { fileURLToPath } from 'node:url'
|
|
37
|
+
import { generateManifest } from './manifest-gen.js'
|
|
38
|
+
import { validateManifest } from './manifest-validate.js'
|
|
39
|
+
import { injectWasmHash } from './wasm-hash.js'
|
|
40
|
+
|
|
41
|
+
const SDK_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..')
|
|
42
|
+
const TEMPLATE_DIR = join(SDK_ROOT, 'template')
|
|
43
|
+
|
|
44
|
+
// ==================== 工具函数 ====================
|
|
45
|
+
|
|
46
|
+
/** 解析命令行参数:positional 数组 + flags 映射(--flag value / --flag=value / 布尔 --flag) */
|
|
47
|
+
function parseArgs(argv) {
|
|
48
|
+
const positional = []
|
|
49
|
+
const flags = {}
|
|
50
|
+
for (let i = 0; i < argv.length; i++) {
|
|
51
|
+
const arg = argv[i]
|
|
52
|
+
if (arg.startsWith('--')) {
|
|
53
|
+
const eq = arg.indexOf('=')
|
|
54
|
+
if (eq !== -1) {
|
|
55
|
+
flags[arg.slice(2, eq)] = arg.slice(eq + 1)
|
|
56
|
+
} else if (i + 1 < argv.length && !argv[i + 1].startsWith('--')) {
|
|
57
|
+
flags[arg.slice(2)] = argv[++i]
|
|
58
|
+
} else {
|
|
59
|
+
flags[arg.slice(2)] = true
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
positional.push(arg)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return { positional, flags }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function toPosix(p) {
|
|
69
|
+
return p.replace(/\\/g, '/')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function readJson(path, what) {
|
|
73
|
+
try {
|
|
74
|
+
return JSON.parse(readFileSync(path, 'utf-8'))
|
|
75
|
+
} catch (e) {
|
|
76
|
+
console.error(`[wasmnest-plugin-desktop] 读取 ${what} 失败: ${path} — ${e.message}`)
|
|
77
|
+
process.exit(1)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function run(cmd, args, cwd) {
|
|
82
|
+
const r = spawnSync(cmd, args, { cwd, stdio: 'inherit' })
|
|
83
|
+
if (r.status !== 0) {
|
|
84
|
+
console.error(`[wasmnest-plugin-desktop] 命令失败 (${cmd} ${args.join(' ')})`)
|
|
85
|
+
process.exit(1)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 递归收集目录下所有文件 */
|
|
90
|
+
function walk(dir) {
|
|
91
|
+
const out = []
|
|
92
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
93
|
+
const full = join(dir, entry.name)
|
|
94
|
+
if (entry.isDirectory()) out.push(...walk(full))
|
|
95
|
+
else out.push(full)
|
|
96
|
+
}
|
|
97
|
+
return out
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 判断文件是否为文本(含 NUL 字节视为二进制,跳过占位符替换) */
|
|
101
|
+
function isText(file) {
|
|
102
|
+
const buf = readFileSync(file)
|
|
103
|
+
return !buf.includes(0)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** 递归替换文件中的占位符 */
|
|
107
|
+
function fillPlaceholders(files, repl) {
|
|
108
|
+
for (const file of files) {
|
|
109
|
+
if (!isText(file)) continue
|
|
110
|
+
let content = readFileSync(file, 'utf-8')
|
|
111
|
+
let changed = false
|
|
112
|
+
for (const [key, value] of Object.entries(repl)) {
|
|
113
|
+
if (content.includes(key)) {
|
|
114
|
+
content = content.split(key).join(value)
|
|
115
|
+
changed = true
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (changed) writeFileSync(file, content, 'utf-8')
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 复制目录内容(不含顶层目录本身) */
|
|
123
|
+
function copyDirContents(src, dest) {
|
|
124
|
+
for (const entry of readdirSync(src, { withFileTypes: true })) {
|
|
125
|
+
const from = join(src, entry.name)
|
|
126
|
+
const to = join(dest, entry.name)
|
|
127
|
+
if (entry.isDirectory()) {
|
|
128
|
+
mkdirSync(to, { recursive: true })
|
|
129
|
+
copyDirContents(from, to)
|
|
130
|
+
} else {
|
|
131
|
+
copyFileSync(from, to)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** 读取当前目录插件清单(不存在返回 null) */
|
|
137
|
+
function readManifestOrNull(cwd) {
|
|
138
|
+
const path = join(cwd, 'plugin.json')
|
|
139
|
+
if (!existsSync(path)) return null
|
|
140
|
+
try {
|
|
141
|
+
return JSON.parse(readFileSync(path, 'utf-8'))
|
|
142
|
+
} catch {
|
|
143
|
+
return null
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ==================== 命令:create ====================
|
|
148
|
+
|
|
149
|
+
function cmdCreate(positional, flags) {
|
|
150
|
+
const [id, name] = positional
|
|
151
|
+
if (!id || !name) {
|
|
152
|
+
console.error(
|
|
153
|
+
'用法: wasmnest-plugin-desktop create <id> <name> [--author <author>] [--dir <dir>] [--rust]',
|
|
154
|
+
)
|
|
155
|
+
console.error(
|
|
156
|
+
'示例: wasmnest-plugin-desktop create com.example.my-plugin "My Plugin" --author "me" --rust',
|
|
157
|
+
)
|
|
158
|
+
process.exit(1)
|
|
159
|
+
}
|
|
160
|
+
if (!/^[a-zA-Z0-9]+([._-][a-zA-Z0-9]+)*$/.test(id)) {
|
|
161
|
+
console.error(`非法插件 id: "${id}" — 使用反域名风格,如 com.example.my-plugin`)
|
|
162
|
+
process.exit(1)
|
|
163
|
+
}
|
|
164
|
+
if (!id.includes('.')) {
|
|
165
|
+
console.error(`插件 id 应包含域名段(反域名风格): "${id}"`)
|
|
166
|
+
process.exit(1)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const author = flags.author || ''
|
|
170
|
+
const last = id.split('.').pop()
|
|
171
|
+
const outDir = resolve(process.cwd(), flags.dir || last)
|
|
172
|
+
if (existsSync(outDir)) {
|
|
173
|
+
console.error(`目标目录已存在: ${outDir}`)
|
|
174
|
+
process.exit(1)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// 派生身份:crate 名 / 结构体名 / npm 包名
|
|
178
|
+
const crate = `wasmnest_plugin_${last.replace(/[^a-zA-Z0-9_]/g, '_').replace(/^_+|_+$/g, '')}`
|
|
179
|
+
const struct = `${last
|
|
180
|
+
.split(/[^a-zA-Z0-9]/)
|
|
181
|
+
.filter(Boolean)
|
|
182
|
+
.map((seg) => seg.charAt(0).toUpperCase() + seg.slice(1))
|
|
183
|
+
.join('')}Plugin`
|
|
184
|
+
// 桌面端语义(2026-09-25):wasm 插件对外称 wasm 应用,npm 包名用 wasm-app- 前缀;
|
|
185
|
+
// crate 名 / 结构体名是内部实现标识符,保持 wasmnest_plugin_* / *Plugin 不变
|
|
186
|
+
const pkgName = `@wasmnest/wasm-app-${last}`
|
|
187
|
+
|
|
188
|
+
// 插件类型:默认 ts-only;--rust 附带 WASM 后端
|
|
189
|
+
const withRust = flags.rust === true
|
|
190
|
+
const pluginType = withRust ? 'rust-ts' : 'ts-only'
|
|
191
|
+
const rustLibraryLine = withRust ? ` "rustLibrary": "${crate}",\n` : ''
|
|
192
|
+
|
|
193
|
+
// SDK 依赖标识:--registry 引用已发布版本(npm + crates.io),默认引用本地 SDK 相对路径
|
|
194
|
+
const sdkPkg = readJson(join(SDK_ROOT, 'package.json'), 'SDK package.json')
|
|
195
|
+
const sdkJs =
|
|
196
|
+
flags.registry === true ? `^${sdkPkg.version}` : `file:${toPosix(relative(outDir, SDK_ROOT))}`
|
|
197
|
+
const sdkRust =
|
|
198
|
+
flags.registry === true
|
|
199
|
+
? `{ version = "${sdkPkg.version}", features = ["wasm"] }`
|
|
200
|
+
: `{ path = "${toPosix(relative(join(outDir, 'rust'), join(SDK_ROOT, 'rust')))}", features = ["wasm"] }`
|
|
201
|
+
|
|
202
|
+
// 复制模板并填充占位符
|
|
203
|
+
cpSync(TEMPLATE_DIR, outDir, { recursive: true })
|
|
204
|
+
if (!withRust) {
|
|
205
|
+
rmSync(join(outDir, 'rust'), { recursive: true, force: true })
|
|
206
|
+
}
|
|
207
|
+
fillPlaceholders(walk(outDir), {
|
|
208
|
+
'{{ID}}': id,
|
|
209
|
+
'{{NAME}}': name,
|
|
210
|
+
'{{AUTHOR}}': author,
|
|
211
|
+
'{{CRATE}}': crate,
|
|
212
|
+
'{{STRUCT}}': struct,
|
|
213
|
+
'{{PKG_NAME}}': pkgName,
|
|
214
|
+
'{{SDK_JS}}': sdkJs,
|
|
215
|
+
'{{SDK_RUST}}': sdkRust,
|
|
216
|
+
'{{PLUGIN_TYPE}}': pluginType,
|
|
217
|
+
'{{RUST_LIBRARY_LINE}}': rustLibraryLine,
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
console.log(`\n[wasmnest-plugin-desktop] 已生成插件工程: ${outDir}`)
|
|
221
|
+
console.log(` id: ${id}`)
|
|
222
|
+
console.log(` name: ${name}`)
|
|
223
|
+
console.log(` pluginType: ${pluginType}${withRust ? ` (crate: ${crate})` : ''}`)
|
|
224
|
+
console.log(`\n下一步:`)
|
|
225
|
+
console.log(` cd ${toPosix(relative(process.cwd(), outDir)) || '.'}`)
|
|
226
|
+
console.log(` pnpm install`)
|
|
227
|
+
console.log(` pnpm run dev # 浏览器开发环境(HMR,无需真机)`)
|
|
228
|
+
console.log(` pnpm run build # 构建(vite${withRust ? ' + cargo wasm32' : ''})`)
|
|
229
|
+
console.log(` pnpm run build -- --resources-dir <宿主resources/plugins父目录> # 复制产物到宿主`)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// ==================== 命令:build ====================
|
|
233
|
+
|
|
234
|
+
function cmdBuild(flags) {
|
|
235
|
+
const cwd = process.cwd()
|
|
236
|
+
const manifestPath = join(cwd, 'plugin.json')
|
|
237
|
+
if (!existsSync(manifestPath)) {
|
|
238
|
+
console.error(`[wasmnest-plugin-desktop] 当前目录不是插件工程(缺少 plugin.json): ${cwd}`)
|
|
239
|
+
console.error('请先在插件目录内运行,或先用 wasmnest-plugin-desktop create 生成工程')
|
|
240
|
+
process.exit(1)
|
|
241
|
+
}
|
|
242
|
+
const manifest = readJson(manifestPath, 'plugin.json')
|
|
243
|
+
const { id, main, rustLibrary, pluginType } = manifest
|
|
244
|
+
const hasWasm = pluginType === 'rust-ts' && rustLibrary
|
|
245
|
+
const frontendOnly = flags['frontend-only'] === true
|
|
246
|
+
const rustOnly = flags['rust-only'] === true
|
|
247
|
+
const resourcesDir = flags['resources-dir']
|
|
248
|
+
|
|
249
|
+
// 0. 根据源码自动填充 contributes/permissions(构建前同步,保证产物与源码一致;
|
|
250
|
+
// 一致口径不含 wasmHash——它由第 3 步注入产物,源清单不带)
|
|
251
|
+
try {
|
|
252
|
+
const { changed, report } = generateManifest(cwd)
|
|
253
|
+
if (changed) {
|
|
254
|
+
for (const line of report) console.log(`[wasmnest-plugin-desktop] ${line}`)
|
|
255
|
+
console.log('[wasmnest-plugin-desktop] plugin.json 已自动填充')
|
|
256
|
+
}
|
|
257
|
+
} catch (e) {
|
|
258
|
+
console.error(`[wasmnest-plugin-desktop] manifest 自动填充失败: ${e.message}`)
|
|
259
|
+
process.exit(1)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const distMain = join(cwd, 'dist', main || 'index.js')
|
|
263
|
+
const wasmPath = hasWasm
|
|
264
|
+
? join(cwd, 'rust/target/wasm32-unknown-unknown/release', `${rustLibrary}.wasm`)
|
|
265
|
+
: null
|
|
266
|
+
|
|
267
|
+
// 1. 前端构建
|
|
268
|
+
if (!rustOnly) {
|
|
269
|
+
const viteBin = join(cwd, 'node_modules/vite/bin/vite.js')
|
|
270
|
+
if (!existsSync(viteBin)) {
|
|
271
|
+
console.error(`[wasmnest-plugin-desktop] vite 未安装 — 先在插件目录运行 pnpm install`)
|
|
272
|
+
process.exit(1)
|
|
273
|
+
}
|
|
274
|
+
console.log('\n[wasmnest-plugin-desktop] ====== 构建前端 (vite) ======')
|
|
275
|
+
run(process.execPath, [viteBin, 'build'], cwd)
|
|
276
|
+
if (!existsSync(distMain)) {
|
|
277
|
+
console.error(`[wasmnest-plugin-desktop] vite 构建未产出 dist/${main || 'index.js'}`)
|
|
278
|
+
process.exit(1)
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// 2. WASM 构建(rust-ts 插件)
|
|
283
|
+
if (hasWasm && !frontendOnly) {
|
|
284
|
+
console.log('\n[wasmnest-plugin-desktop] ====== 构建 WASM (cargo) ======')
|
|
285
|
+
run(
|
|
286
|
+
'cargo',
|
|
287
|
+
[
|
|
288
|
+
'build',
|
|
289
|
+
'--target',
|
|
290
|
+
'wasm32-unknown-unknown',
|
|
291
|
+
'--no-default-features',
|
|
292
|
+
'--features',
|
|
293
|
+
'wasm',
|
|
294
|
+
'--manifest-path',
|
|
295
|
+
'rust/Cargo.toml',
|
|
296
|
+
'--release',
|
|
297
|
+
],
|
|
298
|
+
cwd,
|
|
299
|
+
)
|
|
300
|
+
if (!existsSync(wasmPath)) {
|
|
301
|
+
console.error(`[wasmnest-plugin-desktop] WASM 构建未产出 ${rustLibrary}.wasm`)
|
|
302
|
+
console.error(' 若提示 target 缺失: rustup target add wasm32-unknown-unknown')
|
|
303
|
+
process.exit(1)
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// 3. 复制产物到宿主资源目录(--resources-dir <父目录>,按插件 id 建子目录)
|
|
308
|
+
if (resourcesDir) {
|
|
309
|
+
const dest = join(resolve(cwd, resourcesDir), id)
|
|
310
|
+
mkdirSync(dest, { recursive: true })
|
|
311
|
+
copyDirContents(join(cwd, 'dist'), dest)
|
|
312
|
+
copyFileSync(manifestPath, join(dest, 'plugin.json'))
|
|
313
|
+
if (hasWasm && !frontendOnly) {
|
|
314
|
+
copyFileSync(wasmPath, join(dest, `${rustLibrary}.wasm`))
|
|
315
|
+
}
|
|
316
|
+
if (hasWasm) {
|
|
317
|
+
// 摘要注入产物清单:源 plugin.json 保持不带 wasmHash(票 14)
|
|
318
|
+
injectWasmHash(dest, { log: (m) => console.log(`[wasmnest-plugin-desktop] ${m}`) })
|
|
319
|
+
}
|
|
320
|
+
console.log(`\n[wasmnest-plugin-desktop] 产物已复制到: ${dest}`)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
console.log('\n[wasmnest-plugin-desktop] 构建完成')
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// ==================== 命令:dev(浏览器开发环境) ====================
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* 解析被调试应用的挂载包入口
|
|
330
|
+
*
|
|
331
|
+
* 桌面 app 是**独立构建的挂载包**(机制 B):入口 = `src/entry.ts`,导出
|
|
332
|
+
* `mountApp(root, env)`,由 dev-shell 的挂载槽装载。`src/index.ts` 是
|
|
333
|
+
* 「dev-shell 兼容出口」(只导出 `devMock`),**不是**挂载入口。
|
|
334
|
+
*
|
|
335
|
+
* 优先取 `src/entry.ts`;没有则回落 `src/index.ts`(老形态工程),并由
|
|
336
|
+
* dev-shell 显性报错「未导出 mountApp」——不静默加载一个空模块。
|
|
337
|
+
*/
|
|
338
|
+
function resolveDevEntry(pluginDir) {
|
|
339
|
+
const mount = join(pluginDir, 'src/entry.ts')
|
|
340
|
+
if (existsSync(mount)) return mount
|
|
341
|
+
const compat = join(pluginDir, 'src/index.ts')
|
|
342
|
+
if (existsSync(compat)) return compat
|
|
343
|
+
return null
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** 启动 dev-shell:缺依赖时自动安装,然后以长驻 vite 进程运行 */
|
|
347
|
+
function cmdDev(positional, flags) {
|
|
348
|
+
const cwd = process.cwd()
|
|
349
|
+
const pluginDir = resolve(cwd, positional[0] || '.')
|
|
350
|
+
const entry = flags.entry ? resolve(cwd, flags.entry) : resolveDevEntry(pluginDir)
|
|
351
|
+
|
|
352
|
+
if (!entry) {
|
|
353
|
+
console.error(
|
|
354
|
+
`[wasmnest-plugin-desktop] 目标不是应用工程(缺少 plugin.json 与 src/entry.ts): ${pluginDir}`,
|
|
355
|
+
)
|
|
356
|
+
console.error(
|
|
357
|
+
'桌面应用是独立挂载包:入口须为 src/entry.ts(导出 mountApp(root, env))',
|
|
358
|
+
)
|
|
359
|
+
console.error(
|
|
360
|
+
'用法: wasmnest-plugin-desktop dev [pluginDir] [--entry <file>] [--port <port>] [--host] [--open]',
|
|
361
|
+
)
|
|
362
|
+
process.exit(1)
|
|
363
|
+
}
|
|
364
|
+
if (!existsSync(join(pluginDir, 'plugin.json')) && !existsSync(entry)) {
|
|
365
|
+
console.error(
|
|
366
|
+
`[wasmnest-plugin-desktop] 目标不是应用工程(缺少 plugin.json 与 ${entry}): ${pluginDir}`,
|
|
367
|
+
)
|
|
368
|
+
console.error(
|
|
369
|
+
'用法: wasmnest-plugin-desktop dev [pluginDir] [--entry <file>] [--port <port>] [--host] [--open]',
|
|
370
|
+
)
|
|
371
|
+
process.exit(1)
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const devShellDir = join(SDK_ROOT, 'dev-shell')
|
|
375
|
+
if (!existsSync(devShellDir)) {
|
|
376
|
+
console.error(`[wasmnest-plugin-desktop] dev-shell 不存在: ${devShellDir}(SDK 包不完整)`)
|
|
377
|
+
process.exit(1)
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// dev-shell 首次运行需要安装自身依赖(vue / vite / tailwind 等)
|
|
381
|
+
const viteBin = join(devShellDir, 'node_modules/vite/bin/vite.js')
|
|
382
|
+
if (!existsSync(viteBin)) {
|
|
383
|
+
console.log('[wasmnest-plugin-desktop] dev-shell 依赖缺失,正在安装(仅首次)…')
|
|
384
|
+
// --ignore-workspace:桌面端 dev-shell 位于宿主 pnpm workspace 内,不带该标志
|
|
385
|
+
// 会触发整个 workspace 安装(装错目录),标准安装即按独立工程处理
|
|
386
|
+
run('pnpm', ['install', '--ignore-workspace', '--no-audit', '--no-fund'], devShellDir)
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const args = [
|
|
390
|
+
viteBin,
|
|
391
|
+
'--config',
|
|
392
|
+
join(devShellDir, 'vite.config.ts'),
|
|
393
|
+
'--port',
|
|
394
|
+
String(flags.port || 5173),
|
|
395
|
+
]
|
|
396
|
+
if (flags.host) args.push('--host', typeof flags.host === 'string' ? flags.host : '0.0.0.0')
|
|
397
|
+
if (flags.open) args.push('--open')
|
|
398
|
+
|
|
399
|
+
console.log(`[wasmnest-plugin-desktop] 启动 dev-shell(应用: ${pluginDir})`)
|
|
400
|
+
if (flags.host) {
|
|
401
|
+
console.log(
|
|
402
|
+
`[wasmnest-plugin-desktop] 已监听局域网 — 手机/平板与电脑同一 WiFi 时,浏览器打开 http://<电脑IP>:${flags.port || 5173}/ 查看`,
|
|
403
|
+
)
|
|
404
|
+
}
|
|
405
|
+
console.log(
|
|
406
|
+
`[wasmnest-plugin-desktop] 浏览器打开 http://localhost:${flags.port || 5173}/ 预览(Ctrl+C 退出)`,
|
|
407
|
+
)
|
|
408
|
+
const child = spawn(process.execPath, args, {
|
|
409
|
+
cwd: devShellDir,
|
|
410
|
+
stdio: 'inherit',
|
|
411
|
+
env: {
|
|
412
|
+
...process.env,
|
|
413
|
+
WASMNEST_DEV_PLUGINS: `${pluginDir}::${entry}`,
|
|
414
|
+
},
|
|
415
|
+
})
|
|
416
|
+
child.on('exit', (code) => {
|
|
417
|
+
process.exit(code ?? 0)
|
|
418
|
+
})
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// ==================== 命令:manifest(自动填充) ====================
|
|
422
|
+
|
|
423
|
+
function cmdManifest(flags) {
|
|
424
|
+
const cwd = process.cwd()
|
|
425
|
+
const check = flags.check === true
|
|
426
|
+
if (!existsSync(join(cwd, 'plugin.json'))) {
|
|
427
|
+
console.error(`[wasmnest-plugin-desktop] 当前目录不是插件工程(缺少 plugin.json): ${cwd}`)
|
|
428
|
+
console.error('请在插件目录内运行,或在插件构建脚本中调用 SDK 的 manifest-gen')
|
|
429
|
+
process.exit(1)
|
|
430
|
+
}
|
|
431
|
+
try {
|
|
432
|
+
const { changed, report } = generateManifest(cwd, { check })
|
|
433
|
+
if (!changed) {
|
|
434
|
+
console.log('[wasmnest-plugin-desktop] plugin.json 已是最新,无需更新')
|
|
435
|
+
return
|
|
436
|
+
}
|
|
437
|
+
for (const line of report) console.log(`[wasmnest-plugin-desktop] ${line}`)
|
|
438
|
+
if (check) {
|
|
439
|
+
console.log('[wasmnest-plugin-desktop] --check 模式:plugin.json 与源码不一致(未写入)')
|
|
440
|
+
process.exit(1)
|
|
441
|
+
}
|
|
442
|
+
console.log('[wasmnest-plugin-desktop] plugin.json 已根据源码自动填充')
|
|
443
|
+
} catch (e) {
|
|
444
|
+
console.error(`[wasmnest-plugin-desktop] manifest 生成失败: ${e.message}`)
|
|
445
|
+
process.exit(1)
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// ==================== 命令:validate(清单校验) ====================
|
|
450
|
+
|
|
451
|
+
function cmdValidate(flags) {
|
|
452
|
+
const dir = resolve(process.cwd(), flags.dir || '.')
|
|
453
|
+
let errors, warnings
|
|
454
|
+
try {
|
|
455
|
+
;({ errors, warnings } = validateManifest(dir))
|
|
456
|
+
} catch (e) {
|
|
457
|
+
console.error(`[wasmnest-plugin-desktop] ${e.message}`)
|
|
458
|
+
process.exit(1)
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
for (const w of warnings) console.log(` ⚠ ${w}`)
|
|
462
|
+
for (const e of errors) console.error(` ✗ ${e}`)
|
|
463
|
+
|
|
464
|
+
if (errors.length) {
|
|
465
|
+
console.error(`\n[wasmnest-plugin-desktop] validate 失败: ${errors.length} 个错误`)
|
|
466
|
+
process.exit(1)
|
|
467
|
+
}
|
|
468
|
+
console.log(
|
|
469
|
+
`\n[wasmnest-plugin-desktop] validate 通过${warnings.length ? `(${warnings.length} 个警告)` : ''}`,
|
|
470
|
+
)
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// ==================== 命令:doctor(环境自检) ====================
|
|
474
|
+
|
|
475
|
+
function cmdDoctor() {
|
|
476
|
+
const checks = []
|
|
477
|
+
const add = (name, ok, detail) => checks.push({ name, ok, detail })
|
|
478
|
+
|
|
479
|
+
// Node 版本
|
|
480
|
+
const nodeMajor = Number(process.versions.node.split('.')[0])
|
|
481
|
+
add('Node.js >= 20', nodeMajor >= 20, process.version)
|
|
482
|
+
|
|
483
|
+
// Rust + wasm32 target
|
|
484
|
+
let rustOk = false
|
|
485
|
+
let rustDetail = '未安装'
|
|
486
|
+
try {
|
|
487
|
+
const r = spawnSync('cargo', ['--version'], { stdio: 'pipe' })
|
|
488
|
+
if (r.status === 0) {
|
|
489
|
+
rustOk = true
|
|
490
|
+
rustDetail = r.stdout.toString().trim()
|
|
491
|
+
}
|
|
492
|
+
} catch {
|
|
493
|
+
// 未安装
|
|
494
|
+
}
|
|
495
|
+
add('Rust (cargo)', rustOk, rustDetail)
|
|
496
|
+
if (rustOk) {
|
|
497
|
+
const t = spawnSync('rustup', ['target', 'list', '--installed'], { stdio: 'pipe' })
|
|
498
|
+
const hasWasm = t.status === 0 && t.stdout.toString().includes('wasm32-unknown-unknown')
|
|
499
|
+
add(
|
|
500
|
+
'wasm32-unknown-unknown target',
|
|
501
|
+
hasWasm,
|
|
502
|
+
hasWasm ? '已安装' : '缺失 — 运行 rustup target add wasm32-unknown-unknown',
|
|
503
|
+
)
|
|
504
|
+
} else {
|
|
505
|
+
add('wasm32-unknown-unknown target', false, '需先安装 Rust')
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// 当前目录是否为插件工程
|
|
509
|
+
const manifest = readManifestOrNull(process.cwd())
|
|
510
|
+
if (manifest) {
|
|
511
|
+
add('当前目录是插件工程', true, `${manifest.id} (${manifest.pluginType})`)
|
|
512
|
+
} else {
|
|
513
|
+
add('当前目录是插件工程', false, '缺少 plugin.json — 在插件目录内运行本命令时检查才有意义')
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// dev-shell 依赖(dev 命令就绪性)
|
|
517
|
+
const devVite = join(SDK_ROOT, 'dev-shell/node_modules/vite/bin/vite.js')
|
|
518
|
+
add(
|
|
519
|
+
'dev-shell 依赖(dev 命令)',
|
|
520
|
+
existsSync(devVite),
|
|
521
|
+
existsSync(devVite) ? '已安装' : '首次运行 dev 命令时自动安装',
|
|
522
|
+
)
|
|
523
|
+
|
|
524
|
+
// SDK dist(file: 依赖的插件需要)
|
|
525
|
+
const sdkDist = join(SDK_ROOT, 'dist/index.js')
|
|
526
|
+
add(
|
|
527
|
+
'SDK 构建产物(file: 依赖)',
|
|
528
|
+
existsSync(sdkDist),
|
|
529
|
+
existsSync(sdkDist) ? '已构建' : '缺失 — 运行 pnpm run build(SDK 目录内)',
|
|
530
|
+
)
|
|
531
|
+
|
|
532
|
+
let failed = 0
|
|
533
|
+
for (const c of checks) {
|
|
534
|
+
const icon = c.ok ? '✓' : '✗'
|
|
535
|
+
if (!c.ok) failed++
|
|
536
|
+
console.log(` ${icon} ${c.name} — ${c.detail}`)
|
|
537
|
+
}
|
|
538
|
+
console.log(
|
|
539
|
+
`\n[wasmnest-plugin-desktop] doctor 完成: ${checks.length - failed}/${checks.length} 通过`,
|
|
540
|
+
)
|
|
541
|
+
if (failed) process.exit(1)
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// ==================== 入口 ====================
|
|
545
|
+
|
|
546
|
+
function main() {
|
|
547
|
+
const { positional, flags } = parseArgs(process.argv.slice(2))
|
|
548
|
+
const [cmd] = positional
|
|
549
|
+
const pkg = readJson(join(SDK_ROOT, 'package.json'), 'SDK package.json')
|
|
550
|
+
|
|
551
|
+
if (flags.version || flags.v) {
|
|
552
|
+
console.log(pkg.version)
|
|
553
|
+
process.exit(0)
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
if (flags.help || flags.h || !cmd) {
|
|
557
|
+
console.log(`wasmnest-plugin-desktop v${pkg.version}`)
|
|
558
|
+
console.log('\nWasmNest 桌面端插件开发工具包\n')
|
|
559
|
+
console.log('用法:')
|
|
560
|
+
console.log(
|
|
561
|
+
' wasmnest-plugin-desktop create <id> <name> [--author <author>] [--dir <dir>] [--rust] [--registry]',
|
|
562
|
+
)
|
|
563
|
+
console.log(
|
|
564
|
+
' wasmnest-plugin-desktop build [--resources-dir <dir>] [--frontend-only] [--rust-only]',
|
|
565
|
+
)
|
|
566
|
+
console.log(
|
|
567
|
+
' wasmnest-plugin-desktop dev [pluginDir] [--entry <file>] [--port <port>] [--host] [--open] # 浏览器开发环境(HMR)',
|
|
568
|
+
)
|
|
569
|
+
console.log(
|
|
570
|
+
' wasmnest-plugin-desktop manifest [--check] # 按源码自动填充 contributes/permissions',
|
|
571
|
+
)
|
|
572
|
+
console.log(' wasmnest-plugin-desktop validate [--dir <dir>] # 校验 plugin.json 结构')
|
|
573
|
+
console.log(' wasmnest-plugin-desktop doctor # 环境自检(Node/Rust/wasm32/dev-shell/SDK)')
|
|
574
|
+
process.exit(0)
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
switch (cmd) {
|
|
578
|
+
case 'create':
|
|
579
|
+
cmdCreate(positional.slice(1), flags)
|
|
580
|
+
break
|
|
581
|
+
case 'build':
|
|
582
|
+
cmdBuild(flags)
|
|
583
|
+
break
|
|
584
|
+
case 'dev':
|
|
585
|
+
cmdDev(positional.slice(1), flags)
|
|
586
|
+
break
|
|
587
|
+
case 'manifest':
|
|
588
|
+
cmdManifest(flags)
|
|
589
|
+
break
|
|
590
|
+
case 'validate':
|
|
591
|
+
cmdValidate(flags)
|
|
592
|
+
break
|
|
593
|
+
case 'doctor':
|
|
594
|
+
cmdDoctor()
|
|
595
|
+
break
|
|
596
|
+
default:
|
|
597
|
+
console.error(`未知命令: ${cmd}(运行 wasmnest-plugin-desktop --help 查看用法)`)
|
|
598
|
+
process.exit(1)
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
main()
|