-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgit.js
65 lines (51 loc) · 1.75 KB
/
git.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"use strict";
const fs = require("fs");
const path = require("path");
const { spawn } = require("child_process");
class Git {
constructor(url, username, token, repoFolder, realName, email) {
this._url = `https://${username}:${token}@${url.replace("https://", "")}`;
this._repoFolder = repoFolder;
this._realName = realName;
this._email = email;
}
async clone() {
// Delete an existing source folder and make a new one
if (fs.existsSync(this._repoFolder))
fs.rmdirSync(this._repoFolder, {
recursive: true,
force: true
});
fs.mkdirSync(this._repoFolder);
await this._runGitCommand(["clone", this._url], path.resolve(path.join(this._repoFolder, "..")));
await this._runGitCommand(["config", "user.name", `"${this._realName}"`]);
await this._runGitCommand(["config", "user.email", `"${this._email}"`]);
}
async checkout(branch) {
await this._runGitCommand(["checkout", branch]);
}
async addFile(absolute_file_path) {
await this._runGitCommand(["add", path.relative(this._repoFolder, absolute_file_path)]);
}
async commit(message) {
await this._runGitCommand(["commit", "-m", message]);
}
async pull() {
await this._runGitCommand(["pull"]);
}
async push() {
await this._runGitCommand(["push"]);
}
async _runGitCommand(command, repoFolder = this._repoFolder) {
let child = spawn("git", command, {
cwd: repoFolder
});
for await (let data of child.stdout) {
console.log(`${data}`);
};
for await (let data of child.stderr) {
console.error(`${data}`);
};
}
}
module.exports = Git;