Skip to content

Instantly share code, notes, and snippets.

@GaurangTandon
Last active February 11, 2025 10:17
Show Gist options
  • Save GaurangTandon/f5663544c93b9df39e86067034913a7c to your computer and use it in GitHub Desktop.
Save GaurangTandon/f5663544c93b9df39e86067034913a7c to your computer and use it in GitHub Desktop.
for use with vscode macros extension
const vscode = require('vscode');
const pathModule = require('path');
const gitExt = vscode.extensions.getExtension('vscode.git')?.exports;
const gitAPI = gitExt.getAPI(1);
module.exports.macroCommands = {
OpenContentScript: {
no: 1,
func: () => {
openFile("chrome_extension/src/js/contentScript.js")
}
},
OpenReplacement: {
no: 2,
func: () => {
openFile("chrome_extension/src/js/replacement.js")
}
},
CheckoutMain: {
no: 3,
func: () => checkoutToMain()
},
CheckoutPrevious: {
no: 4,
func: () => checkoutToPreviousBranch()
}
};
function getWorkspaceRoot() {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (workspaceFolders && workspaceFolders.length > 0) {
return workspaceFolders[0].uri.fsPath; // Root path of the first workspace folder
}
return '/Users/blazegt/bono/'; // No workspace opened
}
const workspaceRoot = getWorkspaceRoot();
async function openFile(filename) {
const path = pathModule.join(workspaceRoot, filename);
const document = await vscode.workspace.openTextDocument(path);
await vscode.window.showTextDocument(document);
}
// logging
// let orange = vscode.window.createOutputChannel("Orange" + Math.random());
// orange.appendLine("I am a banana.");
// orange.show();
// git extension developer api reference
// https://stackoverflow.com/questions/59442180/vs-code-git-extension-api
async function checkoutToMain() {
for (const repo of gitAPI.repositories) {
if (repo.rootUri.toString().includes('bono')) {
repo.checkout('main');
}
}
}
async function checkoutToPreviousBranch() {
for (const repo of gitAPI.repositories) {
if (repo.rootUri.toString().includes('bono')) {
repo.checkout('-');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment