|
| 1 | +import { registerExtension } from '@codingame/monaco-vscode-api/extensions' |
| 2 | +import { ExtensionHostKind } from '@codingame/monaco-vscode-extensions-service-override' |
| 3 | + |
| 4 | +/** |
| 5 | + * Registers Git-related commands in the command palette. |
| 6 | + * In the browser, these use Nodepod's virtual git (shell builtin) |
| 7 | + * via the terminal, since there's no native git binary. |
| 8 | + */ |
| 9 | +export async function registerGitCommands() { |
| 10 | + const { getApi } = registerExtension( |
| 11 | + { |
| 12 | + name: 'xydStudioGit', |
| 13 | + publisher: 'xyd', |
| 14 | + version: '1.0.0', |
| 15 | + engines: { vscode: '*' }, |
| 16 | + contributes: { |
| 17 | + commands: [ |
| 18 | + { command: 'xyd-git.init', title: 'Git: Initialize Repository' }, |
| 19 | + { command: 'xyd-git.clone', title: 'Git: Clone' }, |
| 20 | + { command: 'xyd-git.status', title: 'Git: Status' }, |
| 21 | + { command: 'xyd-git.add', title: 'Git: Stage All Changes' }, |
| 22 | + { command: 'xyd-git.commit', title: 'Git: Commit' }, |
| 23 | + { command: 'xyd-git.log', title: 'Git: Log' }, |
| 24 | + { command: 'xyd-git.diff', title: 'Git: Diff' }, |
| 25 | + { command: 'xyd-git.branch', title: 'Git: Create Branch' }, |
| 26 | + { command: 'xyd-git.checkout', title: 'Git: Checkout to...' }, |
| 27 | + ], |
| 28 | + }, |
| 29 | + }, |
| 30 | + ExtensionHostKind.LocalProcess, |
| 31 | + { system: true } |
| 32 | + ) |
| 33 | + |
| 34 | + const vscodeApi = await getApi() |
| 35 | + |
| 36 | + // Helper: run a command in the terminal |
| 37 | + async function runInTerminal(command: string) { |
| 38 | + const terminal = |
| 39 | + vscodeApi.window.activeTerminal ?? |
| 40 | + vscodeApi.window.createTerminal('Git') |
| 41 | + terminal.show() |
| 42 | + terminal.sendText(command) |
| 43 | + } |
| 44 | + |
| 45 | + vscodeApi.commands.registerCommand('xyd-git.init', async () => { |
| 46 | + await runInTerminal('git init') |
| 47 | + }) |
| 48 | + |
| 49 | + vscodeApi.commands.registerCommand('xyd-git.clone', async () => { |
| 50 | + const url = await vscodeApi.window.showInputBox({ |
| 51 | + prompt: 'Repository URL', |
| 52 | + placeHolder: 'https://github.com/user/repo.git', |
| 53 | + }) |
| 54 | + if (url) { |
| 55 | + await runInTerminal(`git clone ${url}`) |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + vscodeApi.commands.registerCommand('xyd-git.status', async () => { |
| 60 | + await runInTerminal('git status') |
| 61 | + }) |
| 62 | + |
| 63 | + vscodeApi.commands.registerCommand('xyd-git.add', async () => { |
| 64 | + await runInTerminal('git add -A') |
| 65 | + }) |
| 66 | + |
| 67 | + vscodeApi.commands.registerCommand('xyd-git.commit', async () => { |
| 68 | + const message = await vscodeApi.window.showInputBox({ |
| 69 | + prompt: 'Commit message', |
| 70 | + placeHolder: 'feat: my changes', |
| 71 | + }) |
| 72 | + if (message) { |
| 73 | + await runInTerminal(`git commit -m "${message}"`) |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + vscodeApi.commands.registerCommand('xyd-git.log', async () => { |
| 78 | + await runInTerminal('git log --oneline -20') |
| 79 | + }) |
| 80 | + |
| 81 | + vscodeApi.commands.registerCommand('xyd-git.diff', async () => { |
| 82 | + await runInTerminal('git diff') |
| 83 | + }) |
| 84 | + |
| 85 | + vscodeApi.commands.registerCommand('xyd-git.branch', async () => { |
| 86 | + const name = await vscodeApi.window.showInputBox({ |
| 87 | + prompt: 'Branch name', |
| 88 | + placeHolder: 'feat/my-feature', |
| 89 | + }) |
| 90 | + if (name) { |
| 91 | + await runInTerminal(`git checkout -b ${name}`) |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + vscodeApi.commands.registerCommand('xyd-git.checkout', async () => { |
| 96 | + const branch = await vscodeApi.window.showInputBox({ |
| 97 | + prompt: 'Branch to checkout', |
| 98 | + placeHolder: 'main', |
| 99 | + }) |
| 100 | + if (branch) { |
| 101 | + await runInTerminal(`git checkout ${branch}`) |
| 102 | + } |
| 103 | + }) |
| 104 | + |
| 105 | + console.log('[xyd studio] Git commands registered') |
| 106 | +} |
0 commit comments