Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

feat(prebuild): support npm-sourced preset plugins

hairyfb3d1e210fb06
diff --git a/scripts/prebuild.ts b/scripts/prebuild.ts
index f333abbb..7a886b3a 100644
--- a/scripts/prebuild.ts
+++ b/scripts/prebuild.ts
@@ -1,15 +1,19 @@
/**
* prebuild:把 `src-tauri/resources/preset-plugins.json` 中标记 `internal: true`
- * 的插件从上游仓库克隆、安装依赖并构建,产物拷入
- * `src-tauri/resources/preset-plugins/<id>/`(随 `bundle.resources` 随安装包分发)。
+ * 的插件制备为随包产物,拷入 `src-tauri/resources/preset-plugins/<id>/`
+ * (随 `bundle.resources` 随安装包分发)。两种来源:
+ *
+ * - `github:owner/repo`:从上游仓库克隆、安装依赖并构建(源码形态的插件);
+ * - npm 包名(`name[@version]`):从 npm registry 拉取已发布产物,跳过构建
+ * (发布包自带 lib/,如 [email protected])。
*
* 由 `pnpm build` 的 prebuild 生命周期自动触发(tauri 的 `beforeBuildCommand` 为
* `pnpm build`,pnpm 先执行 `prebuild` 脚本)。应用启动时(service::plugin::internal)
* 会核对内置插件是否已安装、安装路径是否仍指向该捆绑目录,未满足即强制重装。
*
* 约束:仅用 Node 内置模块(零新增依赖);需要 git 与 pnpm 在 PATH 上;
- * 构建机器需可访问 GitHub 与 npm registry。本文件是「可擦除」TypeScript,
- * Node ≥22.6(--experimental-strip-types)或 ≥23.6(默认启用类型剥离)可直接执行。
+ * 构建机器需可访问 GitHub 与 npm registry。通过 `tsx scripts/prebuild.ts`
+ * 直接运行(TS + ESM),无需预编译。
*/
import { spawnSync } from 'node:child_process'
import {
@@ -20,9 +24,11 @@ import {
readdirSync,
readFileSync,
rmSync,
+ writeFileSync,
} from 'node:fs'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'
+import process from 'node:process'
interface PresetPlugin {
id: string
@@ -66,6 +72,30 @@ function githubUrl(spec: string): string {
return `https://github.com/${repo}.git`
}
+/** `name[@version]`(含 scoped `@scope/name[@version]`)→ 裸包名,用于定位 node_modules。 */
+function npmPackageName(spec: string): string {
+ const at = spec.indexOf('@', spec.startsWith('@') ? spec.indexOf('/') + 1 : 0)
+ return at === -1 ? spec : spec.slice(0, at)
+}
+
+/**
+ * 从 npm registry 拉取已发布产物:临时工程里 `pnpm add <spec>`,产物即
+ * `node_modules/<name>/`(发布包自带 lib/ 等运行必需文件,无需再构建)。
+ * 依赖 pnpm 在 PATH 上(与 git 来源流程同一前提)。
+ */
+function fetchNpmPackage(preset: PresetPlugin, temp: string): string {
+ const project = join(temp, 'project')
+ mkdirSync(project, { recursive: true })
+ writeFileSync(join(project, 'package.json'), JSON.stringify({ private: true }))
+ run('pnpm', ['add', preset.spec, '--ignore-scripts'], project)
+ const pkgDir = join(project, 'node_modules', npmPackageName(preset.spec))
+ if (!existsSync(join(pkgDir, 'package.json'))) {
+ die(`${preset.id}: npm 安装后未找到产物 ${pkgDir}`)
+ }
+ console.log(`[prebuild] ${preset.id}: 来源 npm ${preset.spec}`)
+ return pkgDir
+}
+
/**
* 拷贝构建产物:优先 `files` 白名单(只发运行必需:lib/、patch 文件、README),
* 缺失白名单时拷贝整目录但排除 node_modules/.git 等开发噪声;
@@ -96,31 +126,38 @@ function collectBundle(preset: PresetPlugin, clone: string): void {
cpSync(join(clone, 'package.json'), join(dest, 'package.json'))
}
-/** 构建单个 internal 插件:克隆 → 装依赖 → 构建 → 拷贝产物。 */
+/** 构建单个 internal 插件:git 来源(克隆 → 装依赖 → 构建)或 npm 来源(拉产物)。 */
function buildPlugin(preset: PresetPlugin): void {
const dest = join(BUNDLE_ROOT, preset.id)
rmSync(dest, { recursive: true, force: true })
const temp = mkdtempSync(join(tmpdir(), `dsh-internal-${preset.id}-`))
- const clone = join(temp, preset.id)
- run('git', ['clone', '--depth', '1', '--quiet', githubUrl(preset.spec), clone], temp)
+ let source: string
+ if (preset.spec.startsWith('github:')) {
+ const clone = join(temp, preset.id)
+ run('git', ['clone', '--depth', '1', '--quiet', githubUrl(preset.spec), clone], temp)
- const revision = spawnSync('git', ['-C', clone, 'rev-parse', '--short', 'HEAD'], { encoding: 'utf8' })
- if (revision.status === 0) {
- console.log(`[prebuild] ${preset.id}: 来源修订 ${revision.stdout.trim()}`)
- }
+ const revision = spawnSync('git', ['-C', clone, 'rev-parse', '--short', 'HEAD'], { encoding: 'utf8' })
+ if (revision.status === 0) {
+ console.log(`[prebuild] ${preset.id}: 来源修订 ${revision.stdout.trim()}`)
+ }
- // 注意:pnpm ≥10 默认拦截依赖的构建脚本(esbuild/原生模块需在插件仓库
- // 的 pnpm-workspace.yaml 配 onlyBuiltDependencies 放行);纯 JS/TS 插件不受影响。
- run('pnpm', ['install'], clone)
- const manifest = JSON.parse(readFileSync(join(clone, 'package.json'), 'utf8')) as {
- scripts?: Record<string, string>
+ // 注意:pnpm ≥10 默认拦截依赖的构建脚本(esbuild/原生模块需在插件仓库
+ // 的 pnpm-workspace.yaml 配 onlyBuiltDependencies 放行);纯 JS/TS 插件不受影响。
+ run('pnpm', ['install'], clone)
+ const manifest = JSON.parse(readFileSync(join(clone, 'package.json'), 'utf8')) as {
+ scripts?: Record<string, string>
+ }
+ if (manifest.scripts?.build !== undefined) {
+ run('pnpm', ['run', 'build'], clone)
+ }
+ source = clone
}
- if (manifest.scripts?.build !== undefined) {
- run('pnpm', ['run', 'build'], clone)
+ else {
+ source = fetchNpmPackage(preset, temp)
}
- collectBundle(preset, clone)
+ collectBundle(preset, source)
rmSync(temp, { recursive: true, force: true })
console.log(`[prebuild] ${preset.id}: 产物已就绪 → ${dest}`)
}