const vscode = require("vscode"); const util = require("node:util"); const chp = require("child_process"); const execP = util.promisify(chp.exec); /** * @param {vscode.ExtensionContext} context */ async function activate(context) { let lastGitCommitMsg = "", isOutPutChannelVisible = false; const logOutputChannel = vscode.window.createOutputChannel("gitDWMSimpleUse", { log: true, logLevel: 3, }); logOutputChannel.hide(); const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 100); statusBarItem.text = "$(remote) GitDWMSU"; statusBarItem.command = "gitDWMSimpleUse.ToggleOutputChannel"; context.subscriptions.push( vscode.commands.registerCommand("gitDWMSimpleUse.ToggleOutputChannel", () => { if (isOutPutChannelVisible) logOutputChannel.hide(); else logOutputChannel.show(true); isOutPutChannelVisible = !isOutPutChannelVisible; }) ); if (await vscode.workspace.getConfiguration("dwm-git-simpleuse", 2).get("isEnabled")) { statusBarItem.show(); try { process.chdir(vscode.workspace.workspaceFolders[0].uri.fsPath); } catch (err) { vscode.window.showErrorMessage(err); } const { stdout } = await execP(`git rev-parse --is-inside-work-tree || echo false`, { cwd: vscode.workspace.workspaceFolders[0].uri.fsPath, }); if (stdout.trim() !== "true") vscode.window .showErrorMessage("Not a git repository", "Disable extension for current workspace") .then((e) => { if (e == undefined) return; vscode.workspace.getConfiguration("dwm-git-simpleuse", 2).update("isEnabled", false); statusBarItem.hide(); }); else { statusBarItem.text = "$(loading~spin) GitDWMSU"; let spawnGP = chp.spawn("git pull --ff 2>&1 || echo 'error'", { cwd: vscode.workspace.workspaceFolders[0].uri.fsPath, shell: true, }); spawnGP.stdout.on("data", (data) => { if (data.toString().trim() == "error") { statusBarItem.backgroundColor = new vscode.ThemeColor("statusBarItem.errorBackground"); setTimeout(() => { statusBarItem.backgroundColor = new vscode.ThemeColor("statusBarItem.background"); }, 5000); vscode.window.showErrorMessage("[openPull] " + "An error occured", "Show").then((e) => { if (e == "Show") { logOutputChannel.show(true); isOutPutChannelVisible = true; } }); return; } logOutputChannel.info("[openPull] " + data.toString()); }); spawnGP.stderr.on("data", (data) => { logOutputChannel.error("[openPull] " + data.toString()); }); spawnGP.on("close", (code) => { logOutputChannel.trace("[openPull] " + `child process exited with code ${code}`); statusBarItem.text = "$(remote) GitDWMSU"; }); } } let disposable = vscode.commands.registerCommand("dwm-git-simpleuse.fullPush", async function () { if (!(await vscode.workspace.getConfiguration("dwm-git-simpleuse", 2).get("isEnabled"))) { vscode.window .showErrorMessage("Extension disabled", "Enable extension for current workspace") .then((e) => { if (e == undefined) return; vscode.workspace.getConfiguration("dwm-git-simpleuse", 2).update("isEnabled", true); statusBarItem.show(); }); return; } const gitCommitMsg = await vscode.window.showInputBox({ placeHolder: "commit message", prompt: "choose your commit message", value: lastGitCommitMsg.incrementSuffixe(), }); if (gitCommitMsg === undefined) return; gitCommitMsg.trim(); if (!gitCommitMsg.length) { vscode.window.showErrorMessage("commit message needed"); return; } lastGitCommitMsg = gitCommitMsg; statusBarItem.text = "$(loading~spin) GitDWMSU"; let spawnFP = chp.spawn( `git add . && git commit -am "${gitCommitMsg.replace( /"/g, '\\"' )}" && git pull --ff && git push 2>&1 || echo "error"`, { cwd: vscode.workspace.workspaceFolders[0].uri.fsPath, shell: true } ); spawnFP.stdout.on("data", (data) => { if (data.toString().trim() == "error") { statusBarItem.backgroundColor = new vscode.ThemeColor("statusBarItem.errorBackground"); setTimeout(() => { statusBarItem.backgroundColor = new vscode.ThemeColor("statusBarItem.background"); }, 5000); vscode.window.showErrorMessage("[fullPush] " + "An error occured", "Show").then((e) => { if (e == "Show") { logOutputChannel.show(true); isOutPutChannelVisible = true; } }); return; } logOutputChannel.info("[fullPush] " + data.toString()); }); spawnFP.stderr.on("data", (data) => { logOutputChannel.error("[fullPush] " + data.toString()); }); spawnFP.on("close", (code) => { logOutputChannel.trace("[fullPush] " + `child process exited with code ${code}`); statusBarItem.text = "$(remote) GitDWMSU"; }); }); context.subscriptions.push(disposable); disposable = vscode.commands.registerCommand("dwm-git-simpleuse.initRepo", async function () { if (!(await vscode.workspace.getConfiguration("dwm-git-simpleuse", 2).get("isEnabled"))) { vscode.window .showErrorMessage("Extension disabled", "Enable extension for current workspace") .then((e) => { if (e == undefined) return; vscode.workspace.getConfiguration("dwm-git-simpleuse", 2).update("isEnabled", true); statusBarItem.show(); }); return; } const gitLink = await vscode.window.showInputBox({ placeHolder: "git repo link", prompt: "Initialize a git repository", value: await vscode.env.clipboard.readText(), }); if (gitLink === undefined) return; gitLink.trim(); if (!gitLink.match(/^https?:\/\/\S+\.git$/).length) { vscode.window.showErrorMessage("Link needed"); return; } let gitBranch = await vscode.window.showInputBox({ placeHolder: "git branch", prompt: "choose wich branch to use (defaults to 'main')", value: "main", }); if (gitBranch === undefined) return; gitBranch.trim(); if (!gitBranch.length) gitBranch = "main"; statusBarItem.text = "$(loading~spin) GitDWMSU"; let spawnIER = chp.spawn( `git init && git remote add origin ${gitLink} && git fetch && git checkout -ft origin/${gitBranch} 2>&1 || echo "error"`, { cwd: vscode.workspace.workspaceFolders[0].uri.fsPath, shell: true } ); spawnIER.stdout.on("data", (data) => { if (data.toString().trim() == "error") { statusBarItem.backgroundColor = new vscode.ThemeColor("statusBarItem.errorBackground"); setTimeout(() => { statusBarItem.backgroundColor = new vscode.ThemeColor("statusBarItem.background"); }, 5000); vscode.window.showErrorMessage("[initRepo] " + "An error occured", "Show").then((e) => { if (e == "Show") { logOutputChannel.show(true); isOutPutChannelVisible = true; } }); return; } logOutputChannel.info("[initRepo] " + data.toString()); }); spawnIER.stderr.on("data", (data) => { logOutputChannel.error("[initRepo] " + data.toString()); }); spawnIER.on("close", (code) => { logOutputChannel.trace("[initRepo] " + `child process exited with code ${code}`); statusBarItem.text = "$(remote) GitDWMSU"; }); }); context.subscriptions.push(disposable); } // This method is called when your extension is deactivated function deactivate() {} module.exports = { activate, deactivate, }; String.prototype.incrementSuffixe = function () { const match = this.match(/\s*\((\d+)\)\s*$/); return match ? this.replace(match[0], ` (${+match[1] + 1})`) : this.trim().length ? this + " (1)" : this.trim(); };