@binblink/wasmnest-plugin-sdk-mobile 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 +363 -2
- package/README_en.md +353 -0
- package/bin/build.js +269 -0
- package/bin/cli.js +96 -0
- package/bin/lib.js +349 -0
- package/bin/manifest-gen.js +324 -0
- package/bin/package.js +150 -0
- package/bin/scaffold.js +285 -0
- package/dev-shell/README.md +151 -0
- package/dev-shell/index.html +27 -0
- package/dev-shell/package.json +27 -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/apps/mock-terminal/MockTerminalSlotCard.vue +51 -0
- package/dev-shell/src/apps/mock-terminal/MockTerminalView.vue +168 -0
- package/dev-shell/src/apps/mock-terminal/index.ts +50 -0
- package/dev-shell/src/components/DevToolbar.vue +100 -0
- package/dev-shell/src/components/DialogHost.vue +119 -0
- package/dev-shell/src/components/LogPanel.vue +80 -0
- package/dev-shell/src/i18n.ts +22 -0
- package/dev-shell/src/loader.ts +239 -0
- package/dev-shell/src/locale.ts +29 -0
- package/dev-shell/src/locales/en-shell.ts +328 -0
- package/dev-shell/src/locales/index.ts +70 -0
- package/dev-shell/src/locales/shell.ts +328 -0
- package/dev-shell/src/main.ts +58 -0
- package/dev-shell/src/mock/dialog-service.ts +58 -0
- package/dev-shell/src/mock/file-transfer.ts +390 -0
- package/dev-shell/src/mock/mobile-api.ts +369 -0
- package/dev-shell/src/mock/preset-tasks.ts +186 -0
- package/dev-shell/src/mock/session.ts +121 -0
- package/dev-shell/src/mock-context.ts +351 -0
- package/dev-shell/src/registry.ts +151 -0
- package/dev-shell/src/shared-runtime.ts +44 -0
- package/dev-shell/src/shell/ShellView.vue +52 -0
- package/dev-shell/src/shell/adapters/devAppSource.ts +155 -0
- package/dev-shell/src/shell/components/PluginContextHost.vue +40 -0
- package/dev-shell/src/shell/components/ShellAppCell.vue +75 -0
- package/dev-shell/src/shell/components/ShellAppIcon.vue +111 -0
- package/dev-shell/src/shell/components/ShellAppSurface.vue +46 -0
- package/dev-shell/src/shell/components/ShellBrandTile.vue +27 -0
- package/dev-shell/src/shell/components/ShellCapsuleMenu.vue +123 -0
- package/dev-shell/src/shell/components/ShellChip.vue +49 -0
- package/dev-shell/src/shell/components/ShellGroupCard.vue +17 -0
- package/dev-shell/src/shell/components/ShellHost.vue +75 -0
- package/dev-shell/src/shell/components/ShellNotificationShade.vue +496 -0
- package/dev-shell/src/shell/components/ShellPermissionPrompt.vue +127 -0
- package/dev-shell/src/shell/components/ShellPlaceholderSurface.vue +33 -0
- package/dev-shell/src/shell/components/ShellSection.vue +27 -0
- package/dev-shell/src/shell/components/ShellSheet.vue +74 -0
- package/dev-shell/src/shell/components/ShellStatusbar.vue +84 -0
- package/dev-shell/src/shell/components/screens/PluginRoutePage.vue +98 -0
- package/dev-shell/src/shell/components/screens/ShellAppDetailScreen.vue +378 -0
- package/dev-shell/src/shell/components/screens/ShellAppRunScreen.vue +90 -0
- package/dev-shell/src/shell/components/screens/ShellAppsScreen.vue +195 -0
- package/dev-shell/src/shell/components/screens/ShellHomeScreen.vue +316 -0
- package/dev-shell/src/shell/components/screens/ShellPermissionsScreen.vue +376 -0
- package/dev-shell/src/shell/components/screens/ShellSettingsScreen.vue +240 -0
- package/dev-shell/src/shell/components/screens/ShellSwitcherScreen.vue +559 -0
- package/dev-shell/src/shell/components/ui/ConfirmDialog.vue +137 -0
- package/dev-shell/src/shell/components/ui/Toggle.vue +53 -0
- package/dev-shell/src/shell/composables/useShellApps.ts +415 -0
- package/dev-shell/src/shell/composables/useShellNavigation.ts +197 -0
- package/dev-shell/src/shell/composables/useShellNotifications.ts +115 -0
- package/dev-shell/src/shell/composables/useShellOpen.ts +35 -0
- package/dev-shell/src/shell/composables/useShellOverlays.ts +101 -0
- package/dev-shell/src/shell/composables/useShellPreferences.ts +143 -0
- package/dev-shell/src/shell/composables/useShellRecent.ts +84 -0
- package/dev-shell/src/shell/composables/useToast.ts +33 -0
- package/dev-shell/src/shell/index.ts +88 -0
- package/dev-shell/src/shell/logger.ts +45 -0
- package/dev-shell/src/shell/permissions.ts +145 -0
- package/dev-shell/src/shell/registry.ts +220 -0
- package/dev-shell/src/shell/styles/shell.css +841 -0
- package/dev-shell/src/shell/types.ts +137 -0
- package/dev-shell/src/shell/utils.ts +54 -0
- package/dev-shell/src/styles/main.css +153 -0
- package/dev-shell/src/styles/mobile.css +884 -0
- package/dev-shell/src/styles/wallpaper.css +130 -0
- package/dev-shell/src/theme.ts +71 -0
- package/dev-shell/src/vite-env.d.ts +20 -0
- package/dev-shell/tailwind.config.js +101 -0
- package/dev-shell/tsconfig.json +16 -0
- package/dev-shell/vite.config.ts +130 -0
- package/dist/index.d.ts +84 -0
- package/dist/index.js +161 -0
- package/dist/types.d.ts +637 -0
- package/dist/types.js +0 -0
- package/dist/vite-plugin.d.ts +16 -0
- package/dist/vite-plugin.js +95 -0
- package/package.json +80 -3
- package/src/constants.ts +6 -0
- package/src/global-dialog.ts +168 -0
- package/src/index.ts +87 -0
- package/src/runtime.ts +44 -0
- package/src/types.ts +715 -0
- package/src/ui/PluginGlobalDialog.vue +388 -0
- package/src/ui/Select.d.ts +37 -0
- package/src/ui/Select.vue +307 -0
- package/src/ui/plugin-global-dialog.d.ts +11 -0
- package/src/ui/useSwipeTabs.d.ts +20 -0
- package/src/ui/useSwipeTabs.ts +53 -0
- package/src/vite-plugin.ts +132 -0
- package/template/.gitignore +4 -0
- package/template/README.md +46 -0
- package/template/package.json +23 -0
- package/template/plugin.json +13 -0
- package/template/rust/Cargo.toml +19 -0
- package/template/rust/src/lib.rs +33 -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/bin/cli.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* wasmnest-plugin — WasmNest 移动端插件开发工具包命令行
|
|
4
|
+
*
|
|
5
|
+
* 用法:
|
|
6
|
+
* wasmnest-plugin create <id> <name> [--author <author>] [--dir <dir>] [--ts-only] [--registry]
|
|
7
|
+
* wasmnest-plugin build [--resources-dir <dir>] [--frontend-only] [--rust-only] [--watch]
|
|
8
|
+
* wasmnest-plugin package [-o <file>] [--hash]
|
|
9
|
+
* wasmnest-plugin dev [pluginDir] [--entry <file>] [--port <port>] [--host] [--open]
|
|
10
|
+
* wasmnest-plugin manifest [--check]
|
|
11
|
+
* wasmnest-plugin validate [--dir <dir>]
|
|
12
|
+
* wasmnest-plugin doctor
|
|
13
|
+
*
|
|
14
|
+
* create 从 SDK 内置模板生成插件工程(填充 id/name/author/crate 名);
|
|
15
|
+
* --ts-only 生成纯前端插件(无 rust/ 目录);--registry 时引用已发布版本
|
|
16
|
+
* build 串联 vite build → cargo wasm32 构建 → componentize 组件化(Component Model);
|
|
17
|
+
* --resources-dir 时复制产物到宿主资源目录;
|
|
18
|
+
* --watch 时 vite 长驻构建前端,重建后自动复制产物(前端热更)
|
|
19
|
+
* package 将产物打包为 {id}.zip 插件包(分发单元);--hash 时计算并写入 wasmHash
|
|
20
|
+
* dev 启动浏览器开发环境(dev-shell):vite dev server + HMR,插件源码在
|
|
21
|
+
* mock 宿主的移动端骨架中实时预览(WASM 后端不在浏览器运行);
|
|
22
|
+
* --host 时监听局域网,手机浏览器访问 http://<PC-IP>:<port> 可查看页面
|
|
23
|
+
* validate 校验 plugin.json 结构与字段合法性(CI 用,exit 1 表示不合法)
|
|
24
|
+
* doctor 环境自检:Node / Rust / wasm32 target / dev-shell 依赖 / SDK 构建产物
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { spawn, spawnSync } from 'node:child_process'
|
|
28
|
+
|
|
29
|
+
import { join } from 'node:path'
|
|
30
|
+
import { parseArgs, readJson, SDK_ROOT } from './lib.js'
|
|
31
|
+
import { cmdCreate, cmdManifest, cmdValidate } from './scaffold.js'
|
|
32
|
+
import { cmdDev, cmdBuild } from './build.js'
|
|
33
|
+
import { cmdPackage, cmdDoctor } from './package.js'
|
|
34
|
+
|
|
35
|
+
// ==================== 入口 ====================
|
|
36
|
+
|
|
37
|
+
function main() {
|
|
38
|
+
const { positional, flags } = parseArgs(process.argv.slice(2))
|
|
39
|
+
const [cmd] = positional
|
|
40
|
+
const rest = positional.slice(1)
|
|
41
|
+
const pkg = readJson(join(SDK_ROOT, 'package.json'), 'SDK package.json')
|
|
42
|
+
|
|
43
|
+
if (flags.version || flags.v) {
|
|
44
|
+
console.log(pkg.version)
|
|
45
|
+
process.exit(0)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (flags.help || flags.h || !cmd) {
|
|
49
|
+
console.log(`wasmnest-plugin v${pkg.version}`)
|
|
50
|
+
console.log('\nWasmNest 移动端插件开发工具包\n')
|
|
51
|
+
console.log('用法:')
|
|
52
|
+
console.log(
|
|
53
|
+
' wasmnest-plugin create <id> <name> [--author <author>] [--dir <dir>] [--ts-only] [--registry]',
|
|
54
|
+
)
|
|
55
|
+
console.log(
|
|
56
|
+
' wasmnest-plugin build [--resources-dir <dir>] [--frontend-only] [--rust-only] [--watch]',
|
|
57
|
+
)
|
|
58
|
+
console.log(' wasmnest-plugin package [-o <file>] [--hash] # --hash 写入 wasmHash 完整性校验')
|
|
59
|
+
console.log(
|
|
60
|
+
' wasmnest-plugin dev [pluginDir] [--entry <file>] [--port <port>] [--host] [--open] # 浏览器开发环境(HMR;--host 供手机访问)',
|
|
61
|
+
)
|
|
62
|
+
console.log(' wasmnest-plugin manifest [--check] # 按源码自动填充 contributes/permissions')
|
|
63
|
+
console.log(' wasmnest-plugin validate [--dir <dir>] # 校验 plugin.json 结构')
|
|
64
|
+
console.log(' wasmnest-plugin doctor # 环境自检(Node/Rust/wasm32/dev-shell/SDK)')
|
|
65
|
+
process.exit(0)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
switch (cmd) {
|
|
69
|
+
case 'create':
|
|
70
|
+
cmdCreate(rest, flags)
|
|
71
|
+
break
|
|
72
|
+
case 'build':
|
|
73
|
+
cmdBuild(flags)
|
|
74
|
+
break
|
|
75
|
+
case 'package':
|
|
76
|
+
cmdPackage(flags)
|
|
77
|
+
break
|
|
78
|
+
case 'dev':
|
|
79
|
+
cmdDev(rest, flags)
|
|
80
|
+
break
|
|
81
|
+
case 'manifest':
|
|
82
|
+
cmdManifest(flags)
|
|
83
|
+
break
|
|
84
|
+
case 'validate':
|
|
85
|
+
cmdValidate(flags)
|
|
86
|
+
break
|
|
87
|
+
case 'doctor':
|
|
88
|
+
cmdDoctor()
|
|
89
|
+
break
|
|
90
|
+
default:
|
|
91
|
+
console.error(`未知命令: ${cmd}(运行 wasmnest-plugin --help 查看用法)`)
|
|
92
|
+
process.exit(1)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
main()
|
package/bin/lib.js
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* wasmnest-plugin CLI 共享基建:常量 / 工具函数 / 零依赖 ZIP(自 cli.js 拆出)
|
|
4
|
+
*
|
|
5
|
+
* SDK_ROOT 依赖本文件位于 bin/ 下的相对位置,勿移动。
|
|
6
|
+
*/
|
|
7
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs"
|
|
8
|
+
import { crc32 } from "node:zlib"
|
|
9
|
+
import { dirname, join, resolve } from "node:path"
|
|
10
|
+
import { fileURLToPath } from "node:url"
|
|
11
|
+
import { spawnSync } from "node:child_process"
|
|
12
|
+
|
|
13
|
+
const SDK_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..')
|
|
14
|
+
const TEMPLATE_DIR = join(SDK_ROOT, 'template')
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 组件化工具 manifest 的相对定位链(按优先级,逐级向上查找时依次尝试):
|
|
18
|
+
*
|
|
19
|
+
* 1. 桌面 SDK 自带 `tools/componentize`(P1 起的发布落点:平台仓内为
|
|
20
|
+
* `wasmnest-desktop/packages/plugin-sdk-desktop/tools/…`,npm 安装后同相对形状);
|
|
21
|
+
* 2. `node_modules/@binblink/wasmnest-plugin-sdk-desktop/tools/…`(应用工程只装了 npm 包);
|
|
22
|
+
* 3. 旧平台仓级 `tools/componentize`(P1 前的落点,兼容兜底);
|
|
23
|
+
* 4. 三仓兄弟布局 `wasmnest/tools/componentize`(旧布局,兼容兜底)。
|
|
24
|
+
*/
|
|
25
|
+
const COMPONENTIZE_CANDIDATES = [
|
|
26
|
+
join('wasmnest-desktop', 'packages', 'plugin-sdk-desktop', 'tools', 'componentize', 'Cargo.toml'),
|
|
27
|
+
join('node_modules', '@binblink', 'wasmnest-plugin-sdk-desktop', 'tools', 'componentize', 'Cargo.toml'),
|
|
28
|
+
join('tools', 'componentize', 'Cargo.toml'),
|
|
29
|
+
join('wasmnest', 'tools', 'componentize', 'Cargo.toml'),
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
// ==================== 工具函数 ====================
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 定位组件化工具 `tools/componentize/Cargo.toml`。
|
|
36
|
+
*
|
|
37
|
+
* 工具原住仓库级 `packages/wasmnest-componentize`,三仓拆分后迁平台仓 `tools/componentize`,
|
|
38
|
+
* 随后(P1)收进桌面 SDK 的 `tools/` 随 npm 包发布——应用侧不再依赖平台仓工作树。
|
|
39
|
+
* 不能按固定深度从 SDK 根拼绝对路径:SDK 作为依赖安装时(pnpm 把 `file:` 依赖**复制**进
|
|
40
|
+
* `node_modules/.pnpm/…`,node 解析入口脚本不会回落到源码目录),那个深度指向包仓库内部
|
|
41
|
+
* ⇒ manifest 找不到,`cargo run --manifest-path` 直接失败。故按下列顺序定位:
|
|
42
|
+
*
|
|
43
|
+
* 1. `WASMNEST_COMPONENTIZE_MANIFEST`(显式覆盖;已设但不存在则直接报错,不静默回退);
|
|
44
|
+
* 2. 自 SDK 根与插件工作目录向上逐级查找 `COMPONENTIZE_CANDIDATES`
|
|
45
|
+
* (桌面 SDK 落点优先,旧平台仓落点仅作兼容兜底)。
|
|
46
|
+
*
|
|
47
|
+
* 全部未命中时报错并给出获取方式(此时通常意味着插件工程不在三仓布局内)。
|
|
48
|
+
*
|
|
49
|
+
* @param {string} cwd 插件工程目录(构建时的工作目录)
|
|
50
|
+
* @returns {string} 组件化工具 manifest 的绝对路径
|
|
51
|
+
*/
|
|
52
|
+
function resolveComponentizeManifest(cwd) {
|
|
53
|
+
const override = process.env.WASMNEST_COMPONENTIZE_MANIFEST
|
|
54
|
+
if (override) {
|
|
55
|
+
if (!existsSync(override)) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
`WASMNEST_COMPONENTIZE_MANIFEST 指向的 manifest 不存在: ${override}`,
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
return override
|
|
61
|
+
}
|
|
62
|
+
for (const start of [SDK_ROOT, resolve(cwd)]) {
|
|
63
|
+
let dir = start
|
|
64
|
+
for (;;) {
|
|
65
|
+
for (const rel of COMPONENTIZE_CANDIDATES) {
|
|
66
|
+
const candidate = join(dir, rel)
|
|
67
|
+
if (existsSync(candidate)) return candidate
|
|
68
|
+
}
|
|
69
|
+
const parent = dirname(dir)
|
|
70
|
+
if (parent === dir) break
|
|
71
|
+
dir = parent
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
throw new Error(
|
|
75
|
+
`找不到组件化工具(已按 WASMNEST_COMPONENTIZE_MANIFEST、SDK 目录与插件目录 ` +
|
|
76
|
+
`${cwd} 向上查找桌面 SDK 的 tools/componentize 与旧平台仓落点)。` +
|
|
77
|
+
'该工具随 @binblink/wasmnest-plugin-sdk-desktop 发布(tools/componentize):' +
|
|
78
|
+
'插件工程需安装该 SDK,或用 WASMNEST_COMPONENTIZE_MANIFEST 指向其 Cargo.toml。',
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 解析命令行参数:positional 数组 + flags 映射(--flag value / --flag=value / 布尔 --flag) */
|
|
83
|
+
function parseArgs(argv) {
|
|
84
|
+
const positional = []
|
|
85
|
+
const flags = {}
|
|
86
|
+
for (let i = 0; i < argv.length; i++) {
|
|
87
|
+
const arg = argv[i]
|
|
88
|
+
if (arg.startsWith('--')) {
|
|
89
|
+
const eq = arg.indexOf('=')
|
|
90
|
+
if (eq !== -1) {
|
|
91
|
+
flags[arg.slice(2, eq)] = arg.slice(eq + 1)
|
|
92
|
+
} else if (i + 1 < argv.length && !argv[i + 1].startsWith('--')) {
|
|
93
|
+
flags[arg.slice(2)] = argv[++i]
|
|
94
|
+
} else {
|
|
95
|
+
flags[arg.slice(2)] = true
|
|
96
|
+
}
|
|
97
|
+
} else {
|
|
98
|
+
positional.push(arg)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return { positional, flags }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function toPosix(p) {
|
|
105
|
+
return p.replace(/\\/g, '/')
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function readJson(path, what) {
|
|
109
|
+
try {
|
|
110
|
+
return JSON.parse(readFileSync(path, 'utf-8'))
|
|
111
|
+
} catch (e) {
|
|
112
|
+
console.error(`[wasmnest-plugin] 读取 ${what} 失败: ${path} — ${e.message}`)
|
|
113
|
+
process.exit(1)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function run(cmd, args, cwd) {
|
|
118
|
+
const r = spawnSync(cmd, args, { cwd, stdio: 'inherit' })
|
|
119
|
+
if (r.status !== 0) {
|
|
120
|
+
console.error(`[wasmnest-plugin] 命令失败 (${cmd} ${args.join(' ')})`)
|
|
121
|
+
process.exit(1)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 解析 cargo 实际产物目录(`target_directory`)。
|
|
127
|
+
*
|
|
128
|
+
* 应用仓根 `.cargo/config.toml` 的 `[build] target-dir = "target/wasm-apps"` 会把
|
|
129
|
+
* 仓内一切 cargo 调用的产物重定向到共享目录(见该文件注释:避免各 app 重复编译
|
|
130
|
+
* 同一份依赖图)。因此不能写死 `<插件>/rust/target`——旧硬编码成立于插件住平台仓、
|
|
131
|
+
* 尚无该 config 的时期;改写后 cargo 在共享目录产出,此处仍找 `rust/target` 就会
|
|
132
|
+
* 出现「cargo 构建成功但未产出 wasm」的假失败。
|
|
133
|
+
*
|
|
134
|
+
* 以 `cargo metadata` 的 target_directory 为准(cargo 自行计算,覆盖仓内 config、
|
|
135
|
+
* `CARGO_TARGET_DIR` 与 `--target-dir`);metadata 失败时退回 `<插件>/rust/target`,
|
|
136
|
+
* 后续「产物缺失」报错仍是显性失败,不静默挑一个存在的目录。
|
|
137
|
+
*/
|
|
138
|
+
function resolveCargoTargetDir(cwd) {
|
|
139
|
+
const r = spawnSync(
|
|
140
|
+
'cargo',
|
|
141
|
+
['metadata', '--no-deps', '--format-version', '1', '--manifest-path', 'rust/Cargo.toml'],
|
|
142
|
+
{ cwd, encoding: 'utf8' },
|
|
143
|
+
)
|
|
144
|
+
if (r.status === 0) {
|
|
145
|
+
try {
|
|
146
|
+
const dir = JSON.parse(r.stdout).target_directory
|
|
147
|
+
if (dir) return dir
|
|
148
|
+
} catch {
|
|
149
|
+
// 解析失败:走兜底路径
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return join(cwd, 'rust', 'target')
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** wasm 产物路径(按 cargo 实际 target 目录解析,见 resolveCargoTargetDir) */
|
|
156
|
+
function resolveWasmPath(cwd, rustLibrary) {
|
|
157
|
+
return join(
|
|
158
|
+
resolveCargoTargetDir(cwd),
|
|
159
|
+
'wasm32-unknown-unknown',
|
|
160
|
+
'release',
|
|
161
|
+
`${rustLibrary}.wasm`,
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** dev-shell 的 vite 入口(存在即代表该 dev-shell 目录已装好依赖) */
|
|
166
|
+
const DEV_SHELL_VITE = 'node_modules/vite/bin/vite.js'
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 定位可用的 dev-shell 目录:优先已装依赖的仓库内 SDK 检出。
|
|
170
|
+
*
|
|
171
|
+
* 内置插件用 `file:` 说明符依赖 SDK 时,pnpm 把 SDK 硬链注入插件 node_modules:
|
|
172
|
+
* 注入副本带 dev-shell 源码但不带其 node_modules,直接用它等于每次 dev 都重装一遍。
|
|
173
|
+
* 插件 package.json 的 `file:` 说明符能指回仓库内 SDK 检出,检出已装依赖时优先用它。
|
|
174
|
+
* 外部脚手架(npm 发布的 SDK)没有 file: 说明符,走 SDK 自带 dev-shell 自行安装。
|
|
175
|
+
*/
|
|
176
|
+
function resolveDevShellDir(pluginDir) {
|
|
177
|
+
const bundled = join(SDK_ROOT, 'dev-shell')
|
|
178
|
+
const installed = (dir) => existsSync(join(dir, DEV_SHELL_VITE))
|
|
179
|
+
if (installed(bundled)) return bundled
|
|
180
|
+
// 尽力探测:读不到就退回自带 dev-shell,缺失时由调用方报错(此处不能用 readJson,
|
|
181
|
+
// 它遇解析失败直接 process.exit,会把 doctor 这类自检命令一起带走)
|
|
182
|
+
const readPkg = (path) => JSON.parse(readFileSync(path, 'utf-8'))
|
|
183
|
+
try {
|
|
184
|
+
const sdkName = readPkg(join(SDK_ROOT, 'package.json')).name
|
|
185
|
+
const deps = readPkg(join(pluginDir, 'package.json'))
|
|
186
|
+
const spec = deps.dependencies?.[sdkName] ?? deps.devDependencies?.[sdkName]
|
|
187
|
+
if (typeof spec === 'string' && spec.startsWith('file:')) {
|
|
188
|
+
const checkout = join(resolve(pluginDir, spec.slice('file:'.length)), 'dev-shell')
|
|
189
|
+
if (installed(checkout)) return checkout
|
|
190
|
+
}
|
|
191
|
+
} catch {
|
|
192
|
+
// 忽略:非 file: 依赖(npm 发布包)或读不到说明符
|
|
193
|
+
}
|
|
194
|
+
return bundled
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** 递归收集目录下所有文件 */
|
|
198
|
+
function walk(dir) {
|
|
199
|
+
const out = []
|
|
200
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
201
|
+
const full = join(dir, entry.name)
|
|
202
|
+
if (entry.isDirectory()) out.push(...walk(full))
|
|
203
|
+
else out.push(full)
|
|
204
|
+
}
|
|
205
|
+
return out
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** 判断文件是否为文本(含 NUL 字节视为二进制,跳过占位符替换) */
|
|
209
|
+
function isText(file) {
|
|
210
|
+
const buf = readFileSync(file)
|
|
211
|
+
return !buf.includes(0)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** 递归替换文件中的占位符 */
|
|
215
|
+
function fillPlaceholders(files, repl) {
|
|
216
|
+
for (const file of files) {
|
|
217
|
+
if (!isText(file)) continue
|
|
218
|
+
let content = readFileSync(file, 'utf-8')
|
|
219
|
+
let changed = false
|
|
220
|
+
for (const [key, value] of Object.entries(repl)) {
|
|
221
|
+
if (content.includes(key)) {
|
|
222
|
+
content = content.split(key).join(value)
|
|
223
|
+
changed = true
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (changed) writeFileSync(file, content, 'utf-8')
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** 复制目录内容(不含顶层目录本身) */
|
|
231
|
+
function copyDirContents(src, dest) {
|
|
232
|
+
for (const entry of readdirSync(src, { withFileTypes: true })) {
|
|
233
|
+
const from = join(src, entry.name)
|
|
234
|
+
const to = join(dest, entry.name)
|
|
235
|
+
if (entry.isDirectory()) {
|
|
236
|
+
mkdirSync(to, { recursive: true })
|
|
237
|
+
copyDirContents(from, to)
|
|
238
|
+
} else {
|
|
239
|
+
copyFileSync(from, to)
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// ==================== ZIP 打包(store 模式,零依赖) ====================
|
|
245
|
+
|
|
246
|
+
/** 构造 store 模式 zip 字节流 */
|
|
247
|
+
function buildZip(entries) {
|
|
248
|
+
const chunks = []
|
|
249
|
+
const central = []
|
|
250
|
+
let offset = 0
|
|
251
|
+
|
|
252
|
+
// DOS 时间戳(本地时间)
|
|
253
|
+
const now = new Date()
|
|
254
|
+
const dosTime = (now.getHours() << 11) | (now.getMinutes() << 5) | (now.getSeconds() >> 1)
|
|
255
|
+
const dosDate = ((now.getFullYear() - 1980) << 9) | ((now.getMonth() + 1) << 5) | now.getDate()
|
|
256
|
+
|
|
257
|
+
for (const entry of entries) {
|
|
258
|
+
const nameBuf = Buffer.from(entry.name, 'utf-8')
|
|
259
|
+
const data = entry.data
|
|
260
|
+
const crc = crc32(data) >>> 0
|
|
261
|
+
|
|
262
|
+
// 本地文件头
|
|
263
|
+
const lh = Buffer.alloc(30)
|
|
264
|
+
lh.writeUInt32LE(0x04034b50, 0) // signature
|
|
265
|
+
lh.writeUInt16LE(20, 4) // version needed
|
|
266
|
+
lh.writeUInt16LE(0, 6) // flags
|
|
267
|
+
lh.writeUInt16LE(0, 8) // method: store
|
|
268
|
+
lh.writeUInt16LE(dosTime, 10)
|
|
269
|
+
lh.writeUInt16LE(dosDate, 12)
|
|
270
|
+
lh.writeUInt32LE(crc, 14)
|
|
271
|
+
lh.writeUInt32LE(data.length, 18) // compressed size
|
|
272
|
+
lh.writeUInt32LE(data.length, 22) // uncompressed size
|
|
273
|
+
lh.writeUInt16LE(nameBuf.length, 26)
|
|
274
|
+
lh.writeUInt16LE(0, 28) // extra len
|
|
275
|
+
chunks.push(lh, nameBuf, data)
|
|
276
|
+
|
|
277
|
+
// 中央目录项
|
|
278
|
+
const ch = Buffer.alloc(46)
|
|
279
|
+
ch.writeUInt32LE(0x02014b50, 0) // signature
|
|
280
|
+
ch.writeUInt16LE(20, 4) // version made by
|
|
281
|
+
ch.writeUInt16LE(20, 6) // version needed
|
|
282
|
+
ch.writeUInt16LE(0, 8) // flags
|
|
283
|
+
ch.writeUInt16LE(0, 10) // method
|
|
284
|
+
ch.writeUInt16LE(dosTime, 12)
|
|
285
|
+
ch.writeUInt16LE(dosDate, 14)
|
|
286
|
+
ch.writeUInt32LE(crc, 16)
|
|
287
|
+
ch.writeUInt32LE(data.length, 20)
|
|
288
|
+
ch.writeUInt32LE(data.length, 24)
|
|
289
|
+
ch.writeUInt16LE(nameBuf.length, 28)
|
|
290
|
+
ch.writeUInt16LE(0, 30) // extra len
|
|
291
|
+
ch.writeUInt16LE(0, 32) // comment len
|
|
292
|
+
ch.writeUInt16LE(0, 34) // disk start
|
|
293
|
+
ch.writeUInt16LE(0, 36) // internal attrs
|
|
294
|
+
ch.writeUInt32LE(0, 38) // external attrs
|
|
295
|
+
ch.writeUInt32LE(offset, 42) // local header offset
|
|
296
|
+
central.push(ch, nameBuf)
|
|
297
|
+
|
|
298
|
+
offset += lh.length + nameBuf.length + data.length
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const centralSize = central.reduce((sum, b) => sum + b.length, 0)
|
|
302
|
+
|
|
303
|
+
// 中央目录结束记录
|
|
304
|
+
const eocd = Buffer.alloc(22)
|
|
305
|
+
eocd.writeUInt32LE(0x06054b50, 0) // signature
|
|
306
|
+
eocd.writeUInt16LE(0, 4) // disk number
|
|
307
|
+
eocd.writeUInt16LE(0, 6) // cd start disk
|
|
308
|
+
eocd.writeUInt16LE(entries.length, 8)
|
|
309
|
+
eocd.writeUInt16LE(entries.length, 10)
|
|
310
|
+
eocd.writeUInt32LE(centralSize, 12)
|
|
311
|
+
eocd.writeUInt32LE(offset, 16)
|
|
312
|
+
eocd.writeUInt16LE(0, 20) // comment len
|
|
313
|
+
|
|
314
|
+
return Buffer.concat([...chunks, ...central, eocd])
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
function findViteBin(startDir) {
|
|
319
|
+
let dir = startDir
|
|
320
|
+
for (let i = 0; i < 6; i++) {
|
|
321
|
+
const candidate = join(dir, 'node_modules/vite/bin/vite.js')
|
|
322
|
+
if (existsSync(candidate)) return candidate
|
|
323
|
+
const parent = resolve(dir, '..')
|
|
324
|
+
if (parent === dir) break
|
|
325
|
+
dir = parent
|
|
326
|
+
}
|
|
327
|
+
return null
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
export {
|
|
332
|
+
SDK_ROOT,
|
|
333
|
+
TEMPLATE_DIR,
|
|
334
|
+
COMPONENTIZE_CANDIDATES,
|
|
335
|
+
DEV_SHELL_VITE,
|
|
336
|
+
resolveComponentizeManifest,
|
|
337
|
+
parseArgs,
|
|
338
|
+
toPosix,
|
|
339
|
+
readJson,
|
|
340
|
+
run,
|
|
341
|
+
resolveCargoTargetDir,
|
|
342
|
+
resolveWasmPath,
|
|
343
|
+
resolveDevShellDir,
|
|
344
|
+
walk,
|
|
345
|
+
fillPlaceholders,
|
|
346
|
+
copyDirContents,
|
|
347
|
+
buildZip,
|
|
348
|
+
findViteBin,
|
|
349
|
+
}
|