155 lines
4.4 KiB
JavaScript
155 lines
4.4 KiB
JavaScript
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 = "";
|
|
|
|
if (await vscode.workspace.getConfiguration("dwm-git-simpleuse", 2).get("isEnabled")) {
|
|
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);
|
|
});
|
|
else
|
|
chp.execFile("git", ["pull", "--ff"], (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);
|
|
});
|
|
}
|
|
|
|
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);
|
|
});
|
|
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;
|
|
|
|
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);
|
|
}
|
|
);
|
|
});
|
|
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);
|
|
});
|
|
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";
|
|
|
|
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();
|
|
};
|