vscode_dwm-git-simpleuse/extension.js
2024-07-18 16:24:40 +02:00

156 lines
4.7 KiB
JavaScript

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require("vscode");
const chp = require("child_process");
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
let lastGitCommitMsg = "";
try {
process.chdir(vscode.workspace.workspaceFolders[0].uri.fsPath);
} catch (err) {
vscode.window.showErrorMessage(err);
}
chp.execFile("git", ["pull"], (err, stdout, stderr) => {
if (err) {
vscode.window.showErrorMessage("GITPULL : " + err.message);
return;
}
if (stderr) {
vscode.window.showErrorMessage("GITPULL : " + stderr);
return;
}
vscode.window.showInformationMessage("GITPULL : " + stdout);
});
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand("dwm-git-simpleuse.fullPush", async function () {
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;
chp.exec(
`git add . && git commit -am "${gitCommitMsg.replace(/"/g, '\\"')}" && git pull --ff && git push`,
{ cwd: vscode.workspace.workspaceFolders[0].uri.fsPath },
(err, stdout, stderr) => {
if (err) {
vscode.window.showErrorMessage(err.message);
return;
}
if (stderr) {
vscode.window.showErrorMessage(stderr);
return;
}
vscode.window.showInformationMessage(stdout);
}
);
/* try {
process.chdir(vscode.workspace.workspaceFolders[0].uri.fsPath);
} catch (err) {
vscode.window.showErrorMessage(err);
} */
/* chp.execFile("git", ["add", "*"], (err, stdout, stderr) => {
if (err) {
vscode.window.showErrorMessage("GITADD : " + err.message);
return;
}
if (stderr) {
vscode.window.showErrorMessage("GITADD : " + stderr);
return;
}
vscode.window.showInformationMessage("GITADD : " + stdout);
});
chp.execFile(
"git",
["commit", "-m test7", "-a"],
(err, stdout, stderr) => {
if (err) {
vscode.window.showErrorMessage("GITCOM : " + err.message);
return;
}
if (stderr) {
vscode.window.showErrorMessage("GITCOM : " + stderr);
return;
}
vscode.window.showInformationMessage("GITCOM : " + stdout);
}
); */
});
context.subscriptions.push(disposable);
disposable = vscode.commands.registerCommand("dwm-git-simpleuse.initRepo", async function () {
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";
chp.exec(
`git init && git remote add origin ${gitLink} && git fetch && git checkout -ft origin/${gitBranch}`,
{ cwd: vscode.workspace.workspaceFolders[0].uri.fsPath },
(err, stdout, stderr) => {
if (err) {
vscode.window.showErrorMessage(err.message);
return;
}
if (stderr) {
vscode.window.showErrorMessage(stderr);
return;
}
vscode.window.showInformationMessage(stdout);
}
);
});
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();
};