-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
150 lines (136 loc) · 4.88 KB
/
Copy pathextension.js
File metadata and controls
150 lines (136 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const vscode = require('vscode')
const { execFile } = require('child_process')
const fs = require('fs')
const path = require('path')
let cachedCli = null
function detectClaude() {
return new Promise((resolve) => {
const finder = process.platform === 'win32' ? 'where' : 'which'
execFile(finder, ['claude'], { timeout: 5000 }, (err, stdout) => {
if (err || !stdout) return resolve(null)
const lines = String(stdout)
.split(/\r?\n/)
.map((s) => s.trim())
.filter(Boolean)
if (!lines.length) return resolve(null)
if (process.platform !== 'win32') return resolve(lines[0])
// On Windows, npm installs three shims (claude, claude.cmd, claude.ps1).
// The extensionless one is a Unix shell script and the .cmd needs cmd.exe,
// so Node can't execFile them and pipe stdin reliably. Prefer the real exe.
const exe = lines.find((l) => l.toLowerCase().endsWith('.exe'))
if (exe) return resolve(exe)
// Derive claude.exe from any shim's directory (the npm global prefix).
const shim = lines.find((l) => l.toLowerCase().endsWith('.cmd')) || lines[0]
const exePath = path.join(
path.dirname(shim),
'node_modules', '@anthropic-ai', 'claude-code', 'bin', 'claude.exe',
)
if (fs.existsSync(exePath)) return resolve(exePath)
// Fall back to the .cmd shim if present, else whatever we got.
const cmd = lines.find((l) => l.toLowerCase().endsWith('.cmd'))
return resolve(cmd || lines[0])
})
})
}
async function resolveCli() {
const configured = vscode.workspace.getConfiguration('claudeCommitButton').get('cliPath')
if (configured && configured.trim()) return configured.trim()
if (cachedCli && fs.existsSync(cachedCli)) return cachedCli
const found = await detectClaude()
if (found) {
cachedCli = found
return found
}
return 'claude'
}
function activate(context) {
const disposable = vscode.commands.registerCommand('claudeCommitButton.generate', async (arg) => {
const gitExt = vscode.extensions.getExtension('vscode.git')
const git = gitExt && gitExt.exports && gitExt.exports.getAPI(1)
if (!git) {
vscode.window.showErrorMessage('Git extension not found.')
return
}
// Resolve the repository (the scm menu passes the SourceControl as arg)
let repo = null
if (arg && arg.rootUri) {
repo = git.repositories.find((r) => r.rootUri.toString() === arg.rootUri.toString())
}
if (!repo) repo = git.repositories[0]
if (!repo) {
vscode.window.showErrorMessage('No Git repository.')
return
}
// Diff: staged first, fall back to working tree
let diff = await repo.diff(true)
if (!diff || !diff.trim()) diff = await repo.diff(false)
if (!diff || !diff.trim()) {
vscode.window.showWarningMessage('No changes.')
return
}
const MAX = 6000
if (diff.length > MAX) diff = diff.slice(0, MAX) + '\n...[truncated]'
const cfg = vscode.workspace.getConfiguration('claudeCommitButton')
const claudePath = await resolveCli()
const model = cfg.get('model') || 'sonnet'
const effort = cfg.get('effort') || 'low'
const prompt =
'Write a concise git commit message for the diff below. ' +
'Imperative mood, conventional style. ' +
'Reply with the message only (short subject line; blank line and brief body if needed). ' +
'No backticks, no explanation, no surrounding quotes.\n\nDIFF:\n' +
diff
await vscode.window.withProgress(
{ location: vscode.ProgressLocation.SourceControl, title: 'Claude: generating…' },
() =>
new Promise((resolve) => {
const args = [
'-p',
'--model',
model,
'--effort',
effort,
'--no-session-persistence',
'--tools',
'',
'--strict-mcp-config',
'--setting-sources',
'',
'--dangerously-skip-permissions',
]
const child = execFile(
claudePath,
args,
{ maxBuffer: 10 * 1024 * 1024, cwd: repo.rootUri.fsPath },
(err, stdout, stderr) => {
if (err) {
const detail = stderr || err.message
const notFound = /ENOENT/.test(detail)
const hint = notFound
? ' — Claude CLI not found on PATH. Set "claudeCommitButton.cliPath" to your claude executable (on Windows, the claude.exe; if Claude is installed under WSL or Git Bash it won\'t be on the Windows PATH).'
: ''
vscode.window.showErrorMessage('Claude failed: ' + detail + hint)
resolve()
return
}
const msg = (stdout || '').trim()
if (msg) {
repo.inputBox.value = msg
} else {
vscode.window.showWarningMessage('Claude returned empty.')
}
resolve()
},
)
// If the CLI can't be spawned (e.g. wrong path), stdin emits an
// error; swallow it so the execFile callback reports the real cause.
child.stdin.on('error', () => {})
child.stdin.write(prompt)
child.stdin.end()
}),
)
})
context.subscriptions.push(disposable)
}
function deactivate() {}
module.exports = { activate, deactivate }