Skip to content

安装

安装

dsh-git-guard 以 DSH 插件形式发布,同一份代码提供三个包源:

包源 包名 说明
CNB 制品库 @dtapp/dsh-git-guard 无需 token
GitHub Packages @dtapps/dsh-git-guard 需要具备 read:packages 的 token
npm 公共源 @dtapp/dsh-git-guard 公开,无需额外配置

三条通道的插件内容一致,只是 registry 与包名不同。选择其中一种即可。

方式一:CNB 制品库(推荐)

1. 配置 profile 的 .npmrc

本项目的包发布在 CNB 制品库。profile 的 pnpm 默认源若不是 CNB(例如 npmmirror 等镜像),安装与依赖校验会去那里找这个包,找不到或版本滞后就会报 ERR_PNPM_FETCH_404 / Lockfile failed supply-chain policy check。这是 pnpm 校验 lockfile 时按包名回源取证,不是网络故障,也不是需要登录。

在 profile 的 .npmrc 里给 @dtapp scope 指定 CNB 地址(只影响本 scope,不动其他依赖):

@dtapp:registry=https://npm.cnb.cool/dtapp/dsh/-/packages/

文件位置:$DSH_HOME/profiles/你的profile名/.npmrc(如 web、desktop),DSH_HOME 默认取 ~/.dsh。

注意:.npmrc 里的 registry= 是全局默认源;只有 @dtapp:registry= 会让 pnpm 在校验/更新时也把 @dtapp 包指向 CNB。若 ~/.npmrc 或 profile .npmrc 里已有指向别处的 @dtapp:registry,先改正或删掉那一行(npm config get @dtapp:registry 可检查)。

2. 安装到 profile

在终端执行(--profile 后面换成你的 profile 名):

dsh plugin --profile 你的profile名 add @dtapp/dsh-git-guard

这条命令把参数原样转发给该 profile 目录里的 pnpm,等价于在 $DSH_HOME/profiles/你的profile名 下执行 pnpm add @dtapp/dsh-git-guard,依赖和 lockfile 都记在 profile 里。装完重启该 profile(dsh 你的profile名),guard 即对整个 Harness 生效。

方式二:GitHub Packages

1. 准备 GitHub token

拉取 GitHub Packages 的 npm 包需要 token:

  • classic PAT:勾选 read:packages
  • fine-grained token:给 dtapps/dsh-git-guard 的 Packages: Read 权限

不要把 token 提交进仓库。

2. 配置 profile 的 .npmrc

文件位置:$DSH_HOME/profiles/你的profile名/.npmrc。写入两行,把 ghp_你的Token 换成你的 token:

@dtapps:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=ghp_你的Token

只有写进 profile .npmrc 的 @dtapps:registry 和 _authToken 会让 pnpm 在更新与 lockfile 供应链校验时也回源到 GitHub Packages。若 ~/.npmrc 或 profile .npmrc 里已有指向别处的 @dtapps:registry,先改掉。

3. 安装到 profile

dsh plugin --profile 你的profile名 add @dtapps/dsh-git-guard

安装与生效方式与 CNB 一致,装完重启该 profile 即可。

方式三:npm 公共源

包已发布在 npm 公共源,使用默认 registry 即可,不需要修改 .npmrc:

dsh plugin --profile 你的profile名 add @dtapp/dsh-git-guard

安装与生效方式与其他通道一致,装完重启该 profile 即可。

在发现页安装

三个制品库各提供一份 DSH format 1 索引,由本站的 market/ 目录直接提供,可在 插件 → 发现 → 索引源 中添加(索引源属于 profile,每个 profile 需单独添加一次)。下面的地址会根据当前访问的站点自动补全域名:

CNB:

/market/index.json

GitHub:

/market/index.github.json

npm:

/market/index.npm.json

添加后在发现页搜索 dsh-git-guard,点击安装时索引里的 source.registry 会指向对应的制品库。索引地址必须直接返回 2xx、不能重定向,加载限时 15 秒。

辅助脚本:配置/撤销包源映射

不想手写 profile 的 .npmrc 可以直接运行仓库里的辅助脚本(POSIX sh,无需 Node)。脚本只改 $DSH_HOME/profiles/你的profile名/.npmrc 中对应的 scope 行,幂等,带 --undo 可撤销;不挂 postinstall、不碰 npm 全局配置。

脚本会自动检测 profile:~/.dsh/profiles 下只有一个目录时直接用它;有多个时会在终端列出列表让你选(输入序号或名称,通过 curl | sh 执行也一样),也可以用 --profile 指定。CI 等无终端环境需设置 DSH_NON_INTERACTIVE=1。

CNB(scripts/setup-registry.sh):

# 本地已克隆仓库
./scripts/setup-registry.sh
./scripts/setup-registry.sh --undo

# 没有仓库时直接拉取执行
curl -fsSL https://cnb.cool/dtapp/dsh-git-guard/-/git/raw/main/scripts/setup-registry.sh | sh -s --            # 配置
curl -fsSL https://cnb.cool/dtapp/dsh-git-guard/-/git/raw/main/scripts/setup-registry.sh | sh -s -- --undo     # 撤销

GitHub(scripts/setup-github-registry.sh,需要 read:packages token):

GITHUB_TOKEN=你的token ./scripts/setup-github-registry.sh
GITHUB_TOKEN=你的token ./scripts/setup-github-registry.sh --undo

curl -fsSL https://raw.githubusercontent.com/dtapps/dsh-git-guard/main/scripts/setup-github-registry.sh | GITHUB_TOKEN=你的token sh -s --            # 配置
curl -fsSL https://raw.githubusercontent.com/dtapps/dsh-git-guard/main/scripts/setup-github-registry.sh | GITHUB_TOKEN=你的token sh -s -- --undo     # 撤销

curl ... | sh 会直接执行远端脚本,介意的话可先下载查看,或把 main 换成固定 tag/commit 避免内容漂移。输出语言按 LC_ALL → LC_MESSAGES → LANG 自动选择中文或英文,也可用 DSH_LANG=zh / DSH_LANG=en 显式覆盖。

卸载

# CNB 与 npm 公共源(包名相同)
dsh plugin --profile 你的profile名 remove @dtapp/dsh-git-guard

# GitHub Packages
dsh plugin --profile 你的profile名 remove @dtapps/dsh-git-guard

常见问题

  • ERR_PNPM_FETCH_404 或 Lockfile failed supply-chain policy check:profile 的 .npmrc 没有把对应 scope 指向正确的制品库,按上面的说明补齐对应的 @dtapp:registry 或 @dtapps:registry。
  • 发现页搜不到插件:确认索引源地址能直接返回 2xx 且不重定向,并确认该索引源添加在当前 profile 下。
  • 装完没有生效:需要重启 profile(dsh 你的profile名),guard 从插件注册时开始接管工具调用。
  • GitHub Packages 401/403:token 缺少 read:packages 权限,或 .npmrc 中的 _authToken 未写入当前 profile。