open-git-cli-lc
v0.0.19
Published
一个支持在命令行中打开git仓库的脚手架chmod
Downloads
233
Maintainers
Readme
open-git-cli-lc
一个命令行脚手架,在终端中一键打开当前 Git 仓库相关的网页与本地文件夹。
提交记录、分支差异、CI 管道、发布页、页面地址、项目文件夹……全都一条命令搞定。
命令名:og
功能特性
- 🌿 打开分支提交历史 —— 当前分支或默认分支的 Git 历史页
- 🔀 打开分支差异 / 创建 PR —— 当前分支与默认分支的差异页(自动拼接成创建 PR 地址)
- 🚀 一键发布 —— 依次打开中转页、管道页、发布页
- 📦 管道 / 发布 / 页面 —— 按地区、环境、前端/后端定位地址
- 📁 打开文件夹 —— 当前项目目录或上级目录
- 💻 跨平台 —— 自动识别 Windows / macOS / Linux 并选用对应的打开命令
- 🔗 GitHub / 仓库 —— 自动识别 GitHub 的 HTTPS、
[email protected]:owner/repo.git和ssh://[email protected]/owner/repo.git地址,保留 TFS 支持
安装
全局安装(推荐):
npm install -g open-git-cli-lc安装后即可在任意 Git 仓库目录下使用 og 命令。
免安装运行:
npx open-git-cli-lc cur本地开发调试:
node bin/openBrowser cur快速开始
进入任意 Git 仓库目录,执行:
og cur # 打开当前分支的提交历史
og curmeg # 打开当前分支与默认分支的差异页(创建 PR)
og fl # 在文件管理器中打开当前项目文件夹命令详解
主命令 og 通过位置参数解析,完整格式:
og <openType> [areaName] [envName] [frontOrBack]| 位置 | 参数 | 说明 | 默认值 |
| --- | --- | --- | --- |
| 1 | openType | 打开类型,见下表 | curmeg |
| 2 | areaName | 地区标识(如 tacheng) | standard |
| 3 | envName | 环境:dev(开发)/ test(测试) | dev |
| 4 | frontOrBack | 端:fd(前端)/ bd(后端) | fd |
areaName/envName/frontOrBack仅在gd/fb/ym/publish这几个依赖配置文件的类型中生效。
openType 取值
| openType | 作用 | 示例 |
| --- | --- | --- |
| cur | 当前分支的提交历史 | og cur |
| def | 默认分支的提交历史 | og def |
| curmeg | 当前分支与默认分支的差异页(创建 PR) | og curmeg |
| pushed | 推送后打开的中转网页 | og pushed |
| gd | CI 管道地址 | og gd tacheng test bd |
| fb | 发布地址 | og fb tacheng |
| ym | 页面地址(不需要 frontOrBack) | og ym tacheng |
| fl | 当前项目文件夹 | og fl |
| flup | 上级文件夹 | og flup |
| publish | 依次打开中转页 + 管道页 + 发布页 | og publish tacheng dev fd |
gd 语法同样适用于 fb、publish;ym 与之类似,但不需要传 frontOrBack。
子命令风格入口
除位置参数外,本包还注册了 og-help 命令(bin/og,基于 commander)提供等价的子命令风格说明,可执行 og-help 查看帮助。
配置文件 openBrowserConfig.js
cur、def、curmeg、pushed、fl、flup 这些类型直接基于当前仓库的 Git 地址拼接,无需配置。
而 gd、fb、ym、publish 需要在项目根目录放置 openBrowserConfig.js,按 openType → areaName → envName → (frontOrBack) 的层级提供地址。结构示例:
// openBrowserConfig.js
module.exports = {
// 管道地址:gd[area][env][front|back]
gd: {
tacheng: {
dev: { fd: 'https://ci.example.com/tacheng/dev/front', bd: 'https://ci.example.com/tacheng/dev/back' },
test: { fd: 'https://ci.example.com/tacheng/test/front', bd: 'https://ci.example.com/tacheng/test/back' },
},
standard: {
dev: { fd: 'https://ci.example.com/standard/dev/front', bd: 'https://ci.example.com/standard/dev/back' },
},
},
// 发布地址:fb[area][env][front|back]
fb: {
tacheng: {
dev: { fd: 'https://deploy.example.com/tacheng/dev/front', bd: 'https://deploy.example.com/tacheng/dev/back' },
},
},
// 页面地址:ym[area][env]
ym: {
tacheng: {
dev: 'https://tacheng-dev.example.com',
test: 'https://tacheng-test.example.com',
},
},
};查找规则:
gd/fb/publish中的管道与发布地址:config[openType][areaName][envName][frontOrBack]ym页面地址:config[openType][areaName][envName]
若项目根目录未提供该文件,程序会给出黄色提示,cur、def、curmeg、fl、flup 等不依赖配置的类型仍可正常使用。
工作原理
- 通过
git rev-parse --abbrev-ref HEAD获取当前分支名,通过git remote get-url origin获取远程地址。 - GitHub(
github.com)的 HTTPS / SSH 地址统一转换为不带.git后缀的 HTTPS 网页地址,仓库名取自远程地址; TFS 保留原有的 SSH 转 HTTP 逻辑。 - GitHub 使用
/commits/<分支>打开提交历史,使用/compare/<默认分支>...<当前分支>?expand=1打开差异 / 创建 PR 页面; TFS 继续使用_git/风格的页面 URL。 - 依据
openType选择要打开的 URL 列表,需要配置的类型再结合openBrowserConfig.js取值。 - 根据操作系统选用打开命令:
win32 → start、darwin → open、linux → xdg-open,逐个用spawn打开。
对可能尚未在远端建立的新分支,
pushed与publish会先打开一个中转页(默认延迟 2 秒)再跳转,避免直接打开差异页 404。
默认分支优先使用 openBrowserConfig.js 的顶层字段 defaultGitBranchName(如 module.exports = { defaultGitBranchName: 'main', ... })。未配置时,GitHub 读取本地缓存的 origin/HEAD,缺失时回退到 main; TFS 仍默认使用 dev。此过程不联网,若远端默认分支已变更,需更新本地 origin/HEAD 或在配置中指定。
平台支持
| 平台 | 打开命令 |
| --- | --- |
| Windows (win32) | start |
| macOS (darwin) | open |
| Linux (linux) | xdg-open |
Windows 下使用 shell: true 调用,文件夹也能正常打开。
目录结构
open-git-cli-lc/
├── bin/
│ ├── openBrowser # 主脚本:og 命令的实际实现,解析位置参数并打开目标地址
│ ├── og # 子命令风格入口(og-help),基于 commander 提供命令说明
│ └── og-cur # 独立脚本(未跟踪),当前内容为 gulpfile 检测逻辑,与本工具主流程无关
├── package.json
├── CHANGELOG.md
└── README.md说明:
bin/og-cur目前内容为检测gulpfile.js并运行mp init,与"打开 Git 仓库页面"的主流程不一致,疑似占位或误置文件,使用时请以bin/openBrowser为准。
本地开发
git clone https://github.com/have-not-BUG/open-git-cli-lc.git
cd open-git-cli-lc
npm install
node bin/openBrowser cur # 直接运行主脚本调试发布前会通过 standard-version 自动生成版本号与变更日志:
npm run prepublishOnly版本记录
详见 CHANGELOG.md。
License
ISC
