forked from kefimochi/sync-contribution-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (37 loc) · 1.45 KB
/
index.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
import { parse } from "node-html-parser";
import axios from "axios";
import fs from "fs";
import shell from "shelljs";
import { buildAxiosRequest } from "./endpoint.js";
// Gathers needed git commands for bash to execute per provided contribution data.
const getCommand = (contribution) => {
return `GIT_AUTHOR_DATE=${contribution.date}T12:00:00 GIT_COMMITER_DATE=${contribution.date}T12:00:00 git commit --allow-empty -m "Rewriting History!" > /dev/null\n`.repeat(
contribution.count
);
};
export default async (answers) => {
// Returns contribution graph html for a full selected year.
const req = buildAxiosRequest(answers)
const res = await axios.get(req.url, req.config);
// Retrieves needed data from the html, loops over green squares with 1+ contributions,
// and produces a multi-line string that can be run as a bash command.
const script = parse(res.data)
.querySelectorAll("[data-count]")
.map((el) => {
return {
date: el.attributes["data-date"],
count: parseInt(el.attributes["data-count"]),
};
})
.filter((contribution) => contribution.count > 0)
.map((contribution) => getCommand(contribution))
.join("")
.concat("git pull origin main\n", "git push -f origin main");
fs.writeFile("script.sh", script, () => {
console.log("\nFile was created successfully.");
if (answers.execute) {
console.log("This might take a moment!\n");
shell.exec("sh ./script.sh");
}
});
};