| 方法 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
| npm link | 修改源码立即生效,最接近真实使用 | 需要两步操作 | 推荐:日常开发测试 |
| 相对路径安装 | 简单直接 | 修改源码需要重新安装 | 一次性测试 |
| 直接运行 | 最简单,无需安装 | 路径较长 | 快速验证功能 |
| 添加到 PATH | 全局可用 | 需要配置环境 | 长期本地使用 |
| package.json 脚本 | 团队共享 | 需要配置 | 团队协作 |
# 进入 git-quick 目录
cd D:\study\git-quick
# 确保已编译
npm run build
# 创建全局链接
npm link预期输出:
npm notice created a symlink: C:\Users\<user>\AppData\Roaming\npm\node_modules\git-quick -> D:\study\git-quick
# 进入你的测试项目(一个有 Git 仓库的项目)
cd D:\your-test-project
# 链接 git-quick
npm link git-quick
# 测试交互式模式
npx git-quick tag
# 测试命令行模式
npx git-quick tag --patch
npx git-quick tag --minor --message "测试版本"
npx git-quick tag --help-
修改 git-quick 源码
cd D:\study\git-quick # 修改 src/ 下的文件
-
重新编译
npm run build # 或使用监听模式 npm run dev -
立即在测试项目中测试
cd D:\your-test-project npx git-quick tag
# 在测试项目中
cd D:\your-test-project
npm unlink git-quick
# 在 git-quick 项目中(可选,如果不再需要全局链接)
cd D:\study\git-quick
npm unlink -g-
环境检查
npx git-quick tag # 应该能检测到 Git 仓库 -
交互式模式
npx git-quick tag # 测试:选择版本类型、添加说明、确认 -
命令行参数
npx git-quick tag --patch npx git-quick tag --minor --message "测试" npx git-quick tag --major --prefix release- npx git-quick tag --patch --no-push -
帮助信息
npx git-quick --help npx git-quick tag --help
-
不在 Git 仓库中
cd D:\temp\not-a-git-repo npx git-quick tag # 应该显示错误:不是 Git 仓库
-
有未提交的更改
# 修改一个文件但不提交 echo "test" >> test.txt npx git-quick tag # 应该提示有未提交的更改
-
没有远程仓库
# 在只有本地 Git 的项目中 git init npx git-quick tag # 应该警告没有远程仓库,但继续执行
-
Tag 已存在
# 创建相同的 tag 两次 git tag v1.0.0 npx git-quick tag # 如果生成 v1.0.0 应该报错
-
没有历史 tag
# 在新仓库中 npx git-quick tag --major # 应该生成 v1.0.0
-
从现有 tag 递增
git tag v1.2.3 npx git-quick tag --patch # 应该生成 v1.2.4 -
不同前缀格式
git tag release-1.0.0 npx git-quick tag --minor # 应该生成 release-1.1.0(保持前缀) -
自定义前缀
npx git-quick tag --major --prefix v # 应该使用 v 前缀
- 验证复制功能
npx git-quick tag --patch # 完成后,Ctrl+V 粘贴,应该是新创建的 tag 名称
解决方案:
# 检查全局 node_modules 路径
npm root -g
# 检查是否在 PATH 中
echo $env:PATH # PowerShell
echo %PATH% # CMD
# 重新链接
npm unlink -g
npm link解决方案:
# 确保重新编译了
cd D:\study\git-quick
npm run build
# 检查 dist/ 目录是否更新
ls -l dist/解决方案:
# 临时允许脚本执行
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
# 或使用 npx
npx git-quick tag解决方案:
# 使用绝对路径
npm install D:\study\git-quick
# 或使用 file: 协议
npm install file:../git-quick创建一个专门的测试项目:
# 创建测试项目
mkdir D:\test-git-quick
cd D:\test-git-quick
# 初始化 Git 仓库
git init
git remote add origin https://github.com/yourname/test-repo.git
# 创建一些提交
echo "# Test Project" > README.md
git add .
git commit -m "Initial commit"
# 创建一些历史 tag
git tag v0.1.0
git tag v0.2.0
git tag v1.0.0
# 链接 git-quick
npm link git-quick
# 开始测试
npx git-quick tag在 git-quick 项目中创建测试脚本:
# 在 package.json 中添加
{
"scripts": {
"test:local": "node test-scripts/local-test.js"
}
}这样就可以自动化执行各种测试场景了。
最佳实践:
- 使用
npm link进行日常开发测试 - 使用
npm run dev保持监听编译 - 在真实的 Git 项目中测试各种场景
- 测试完成后记得
npm unlink
快速命令:
# 在 git-quick 项目
npm run build && npm link
# 在测试项目
npm link git-quick && npx git-quick tag
# 清理
npm unlink git-quick