安装 DSH 插件

插件从哪里来

DSH 插件通常以 npm 包形式分发,源码托管在 GitHub 上。找插件有三个入口:

  • GitHub 的 dsh-plugin 主题:官方建议插件作者为仓库打上这个主题标签,方便被发现;
  • npm 注册表:发布为 @scope/xxx 形式的包;
  • dsh.plus 插件库:本站收录的插件都附有可直接复制的安装命令。

安装机制:profile

DSH 的每个可启动组合称为一个 profile,位于 $DSH_HOME/profiles/<name> 目录。dsh web 启动的 web profile 就是最常见的目标。dsh plugin 命令负责管理某个 profile 的插件:它把参数转发给 profile 目录内的 pnpm,并在安装后把声明了 dsh.bundle 的组合包追加到该 profile 的 dsh.profile.bundles 列表,成为配置的一层。因此使用 dsh plugin 前需要先装好 pnpm。

安装到 web profile

dsh plugin --profile web add <package>

例如安装一个发布到 npm 的插件:

dsh plugin --profile web add dsh-hello-plugin

也可以直接安装 GitHub 仓库(github:owner/repo 形式,pnpm 支持的 git 规范均可):

dsh plugin --profile web add github:you/hello-plugin

git 安装拉取的是源码而非构建产物,需要作者提供 prepare 脚本在安装时完成构建;pnpm 10 及以上默认拒绝运行 git 依赖的 prepare 脚本,第一次 add 会失败并打印提示——把报错中给出的包键加入该 profile 的 pnpm-workspace.yaml 的 allowBuilds 后重新执行即可:

allowBuilds:
  dsh-hello-plugin: true

请谨慎对待这项授权:它允许该包的代码在安装时于你的机器上执行。只对源码可信的包授权,并尽量锁定 commit(github:you/hello-plugin#<sha>)。

不想走 git 构建流程的话,也可以让作者发布 tarball,直接本地安装:

dsh plugin --profile web add ./hello-plugin-0.1.0.tgz

验证装上了

先不启动,用配置转储确认插件层已经生效:

dsh --profile web --dump-config

输出中会出现以 # == <package> 标注的组合包层。也可以用 why 查看某个包的依赖来源:

dsh plugin --profile web why dsh-hello-plugin

最后启动 dsh web,在 Web UI 中实际使用插件的功能,观察日志与行为即可最终确认。

卸载

dsh plugin --profile web remove dsh-hello-plugin

该命令会同时移除依赖与对应的配置层。如果想临时关掉某个插件的效果而不是卸载,可以编辑 profile 的 cordis.patch.yml,在对应行上禁用或移除配置。

本地开发:patch 覆盖层

开发中的插件不必安装,可以用 --patch 覆盖层直接加载:

pnpm dsh web --patch ./scratch-plugin/cordis.yml

更多细节见《编写第一个 Cordis 插件》与《Cordis 进阶》。