opencode-diff-viewer 1.0.1 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +79 -39
  2. package/dist/index.js +28 -31
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -12,26 +12,69 @@
12
12
  - 🔧 **智能终端适配** - 自动检测操作系统,打开新终端窗口展示 diff
13
13
  - 🤖 **LLM 工具集成** - LLM 可自动调用 `view_diff` 工具
14
14
 
15
- ## 安装
15
+ ## 前置条件
16
+
17
+ ### 1. 安装 lumen
18
+
19
+ 插件会自动尝试安装 lumen,如果自动安装失败,需要手动安装:
20
+
21
+ **macOS / Linux (Homebrew)**:
22
+ ```bash
23
+ brew install jnsahaj/lumen/lumen
24
+ ```
25
+
26
+ **macOS / Linux (Bun)**:
27
+ ```bash
28
+ bun install jnsahaj/lumen/lumen
29
+ ```
30
+
31
+ **Cargo (Rust)**:
32
+ ```bash
33
+ cargo install lumen
34
+ ```
35
+
36
+ **Windows**:
37
+ 下载 [lumen releases](https://github.com/jnsahaj/lumen/releases) 并添加到 PATH
38
+
39
+ ### 2. Git 仓库
40
+
41
+ 确保项目是 git 仓库,并且有修改的文件:
42
+ ```bash
43
+ git status # 查看修改的文件
44
+ ```
45
+
46
+ ## 安装(全局配置)
16
47
 
17
48
  ### 1. 安装 npm 包
18
49
 
19
50
  ```bash
20
- npm install opencode-diff-viewer
21
- #
22
- pnpm add opencode-diff-viewer
23
- #
24
- yarn add opencode-diff-viewer
51
+ # npm
52
+ npm install -g opencode-diff-viewer
53
+
54
+ # pnpm
55
+ pnpm add -g opencode-diff-viewer
56
+
57
+ # bun
58
+ bun add -g opencode-diff-viewer
25
59
  ```
26
60
 
27
- ### 2. 配置 opencode.json
61
+ ### 2. 配置全局 opencode.json
28
62
 
29
- 在项目的 `opencode.json` 中添加插件:
63
+ 创建或编辑 `~/.config/opencode/opencode.json`:
30
64
 
31
- ```json
65
+ ```bash
66
+ mkdir -p ~/.config/opencode
67
+ cat > ~/.config/opencode/opencode.json << 'EOF'
32
68
  {
69
+ "command": {
70
+ "diff": {
71
+ "template": "View git diff using lumen TUI.",
72
+ "description": "View diff of modified files using lumen TUI"
73
+ }
74
+ },
33
75
  "plugin": ["opencode-diff-viewer"]
34
76
  }
77
+ EOF
35
78
  ```
36
79
 
37
80
  ### 3. 重启 OpenCode
@@ -47,7 +90,6 @@ yarn add opencode-diff-viewer
47
90
  ```bash
48
91
  /diff # 查看所有修改文件的 diff
49
92
  /diff src/app.ts # 查看指定文件的 diff
50
- /diff src/ # 查看目录下所有文件的 diff
51
93
  ```
52
94
 
53
95
  ### 通过 LLM
@@ -66,32 +108,6 @@ LLM 可以自动调用 `view_diff` 工具来展示代码变更。无需手动操
66
108
  | `e` | 在编辑器中打开文件 |
67
109
  | `q` | 退出 |
68
110
 
69
- ## 前置条件
70
-
71
- ### lumen 安装
72
-
73
- 插件会自动尝试安装 lumen,如果自动安装失败,需要手动安装:
74
-
75
- **macOS / Linux (Homebrew)**:
76
- ```bash
77
- brew install jnsahaj/lumen/lumen
78
- ```
79
-
80
- **Cargo (Rust)**:
81
- ```bash
82
- cargo install lumen
83
- ```
84
-
85
- **Windows**:
86
- 下载 [lumen releases](https://github.com/jnsahaj/lumen/releases) 并添加到 PATH
87
-
88
- ### Git 仓库
89
-
90
- 确保项目是 git 仓库,并且有修改的文件:
91
- ```bash
92
- git status # 查看修改的文件
93
- ```
94
-
95
111
  ## 故障排除
96
112
 
97
113
  ### 1. lumen 未安装
@@ -119,6 +135,26 @@ git add .
119
135
  - macOS: Terminal.app
120
136
  - Linux: gnome-terminal 或 xterm
121
137
 
138
+ ### 4. 插件未加载
139
+
140
+ 检查全局配置文件是否正确:
141
+ ```bash
142
+ cat ~/.config/opencode/opencode.json
143
+ ```
144
+
145
+ 确保配置正确,包含 `command` 和 `plugin` 两部分:
146
+ ```json
147
+ {
148
+ "command": {
149
+ "diff": {
150
+ "template": "View git diff using lumen TUI.",
151
+ "description": "View diff of modified files using lumen TUI"
152
+ }
153
+ },
154
+ "plugin": ["opencode-diff-viewer"]
155
+ }
156
+ ```
157
+
122
158
  ## 工作原理
123
159
 
124
160
  1. **检测修改文件** - 插件使用 `git diff` 获取已暂存和未暂存的修改
@@ -132,7 +168,7 @@ opencode-diff-viewer/
132
168
  ├── src/
133
169
  │ ├── index.ts # 插件主逻辑
134
170
  │ └── command-diff.md # /diff 命令定义
135
- ├── dist/ # 编译输出
171
+ ├── dist/ # 编译输出(发布用)
136
172
  ├── package.json # npm 配置
137
173
  └── tsconfig.json # TypeScript 配置
138
174
  ```
@@ -148,6 +184,10 @@ cd opencode-diff-viewer
148
184
 
149
185
  # 安装依赖
150
186
  npm install
187
+ # 或
188
+ pnpm install
189
+ # 或
190
+ bun install
151
191
 
152
192
  # 构建
153
193
  npm run build
@@ -155,8 +195,8 @@ npm run build
155
195
  # 链接本地包
156
196
  npm link
157
197
 
158
- # 在测试项目中
159
- npm link opencode-diff-viewer
198
+ # 在全局使用
199
+ npm link -g opencode-diff-viewer
160
200
  ```
161
201
 
162
202
  ### 发布新版本
package/dist/index.js CHANGED
@@ -66,7 +66,7 @@ export const DiffViewerPlugin = async ({ project, client, $, directory, worktree
66
66
  }
67
67
  return { installed: false, message: `❌ ${result.error}` };
68
68
  };
69
- const initResult = await ensureLumenInstalled();
69
+ await ensureLumenInstalled();
70
70
  const getModifiedFiles = async () => {
71
71
  try {
72
72
  const unstaged = await $ `git diff --name-only`.text();
@@ -81,41 +81,38 @@ export const DiffViewerPlugin = async ({ project, client, $, directory, worktree
81
81
  return [];
82
82
  }
83
83
  };
84
- const openDiffInNewTerminal = async (fileArgs) => {
85
- const platform = process.platform;
86
- if (platform === 'darwin') {
87
- await $ `osascript -e 'tell application "Terminal" to do script "cd ${directory} && lumen diff ${fileArgs}; exit"'`;
88
- return;
89
- }
90
- if (platform === 'linux') {
91
- try {
92
- await $ `which gnome-terminal && gnome-terminal -- bash -c "cd ${directory} && lumen diff ${fileArgs}; read -p 'Press Enter to close...'"`;
93
- return;
94
- }
95
- catch {
96
- try {
97
- await $ `which xterm && xterm -e "cd ${directory} && lumen diff ${fileArgs}; read -p 'Press Enter to close...'"`;
98
- return;
99
- }
100
- catch { }
101
- }
102
- }
103
- await $ `lumen diff ${fileArgs}`;
104
- };
105
84
  const launchDiffViewer = async (files) => {
106
- const lumenCheck = await ensureLumenInstalled();
107
- if (!lumenCheck.installed) {
108
- return lumenCheck.message || "❌ lumen is not installed and auto-install failed.";
85
+ if (!await isLumenInstalled()) {
86
+ return "❌ lumen is not installed.\n\nTo install:\n brew install jnsahaj/lumen/lumen\n # or\n cargo install lumen";
109
87
  }
110
88
  const modifiedFiles = files && files.length > 0 ? files : await getModifiedFiles();
111
89
  if (modifiedFiles.length === 0) {
112
- return "📝 No modified files to show diff for.";
90
+ return "📝 No modified files to show diff for.\n\nRun `git add .` to stage changes first.";
113
91
  }
92
+ const platform = process.platform;
93
+ const fileArgs = modifiedFiles.map(f => `"${f}"`).join(' ');
94
+ const cmd = `cd "${directory}" && lumen diff ${fileArgs}`;
114
95
  try {
115
- const fileArgs = modifiedFiles.map(f => `--file "${f}"`).join(' ');
116
- await openDiffInNewTerminal(fileArgs);
117
- const prefix = lumenCheck.message ? `${lumenCheck.message}\n\n` : "";
118
- return `${prefix}✅ Opened lumen diff viewer for ${modifiedFiles.length} file(s):
96
+ if (platform === 'darwin') {
97
+ await $ `osascript -e 'tell application "Terminal" to do script "${cmd}; exit"'`;
98
+ }
99
+ else if (platform === 'linux') {
100
+ try {
101
+ await $ `which gnome-terminal && gnome-terminal -- bash -c "${cmd}; read -p 'Press Enter to close...'" `;
102
+ }
103
+ catch {
104
+ try {
105
+ await $ `which xterm && xterm -e "bash -c '${cmd}; read -p Press Enter to close...'" `;
106
+ }
107
+ catch {
108
+ return `❌ No terminal emulator found (gnome-terminal/xterm).\n\nPlease run manually:\n ${cmd}`;
109
+ }
110
+ }
111
+ }
112
+ else {
113
+ await $ `${cmd}`;
114
+ }
115
+ return `✅ Opened lumen diff viewer for ${modifiedFiles.length} file(s):
119
116
  ${modifiedFiles.map(f => ` • ${f}`).join('\n')}
120
117
 
121
118
  Keybindings:
@@ -124,7 +121,7 @@ Keybindings:
124
121
  q: Quit`;
125
122
  }
126
123
  catch (error) {
127
- return `❌ Failed to launch diff viewer: ${error}`;
124
+ return `❌ Failed to launch diff viewer: ${error}\n\nTry running manually:\n ${cmd}`;
128
125
  }
129
126
  };
130
127
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-diff-viewer",
3
- "version": "1.0.1",
3
+ "version": "1.0.4",
4
4
  "description": "OpenCode plugin for viewing git diffs using lumen TUI",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",