-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·166 lines (135 loc) · 3.88 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const ora = require("ora");
const chalk = require("chalk");
const { execSync } = require("child_process");
const crypto = require("crypto");
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const HASH_SECRET = "crypto";
const COMMANDS = ["from"];
function validationError(message) {
console.error(chalk.bold.red(`✖ ${message}`));
process.exit(1);
}
module.exports = function(command, source, _options) {
const options = Object.assign(
{
secret: HASH_SECRET,
author: null
},
_options
);
if (!command) {
validationError(
`Probably forget ${COMMANDS.map(x => `'${x}'`).join(" or ")} argument`
);
return;
}
if (!COMMANDS.includes(command)) {
validationError(
`Argument '${command}' does not exist. Use ${COMMANDS.map(
x => `'${x}'`
).join(", ")} instead`
);
return;
}
if (!source) {
validationError(`Probably forget to enter a path to existing repository`);
return;
}
function getHistory() {
let author = options.author;
if (!author) {
author = execSync(`cd ${source} && git config user.name`, {
encoding: "utf8"
}).replace(/\n/, "");
}
let authorString = `--author="${author}"`;
if (Array.isArray(author)) {
authorString = author.map(a => `--author="${a}"`).join(" ");
}
return execSync(
`cd ${source} && git log --pretty="%H|%ad" --date=format:"%Y-%m-%d %H:%M:%S" ${authorString} --all`,
{ encoding: "utf8" }
);
}
function getCurrentDirHistory() {
let author = options.author;
if (!author) {
author = execSync(`git config user.name`, { encoding: "utf8" });
}
let authorString = `--author="${author}"`;
if (Array.isArray(author)) {
authorString = author.map(a => `--author="${a}"`).join(" ");
}
return execSync(
`git log --pretty="%s|%ad" --date=format:"%Y-%m-%d %H:%M:%S" ${authorString} --all`,
{ encoding: "utf8" }
);
}
function findMissedCommits() {
const historyArray = getHistory()
.split("\n")
.filter(x => x);
let currentDirHistoryArray = getCurrentDirHistory()
.split("\n")
.filter(x => x);
const missedArray = [];
for (let i = 0; i < historyArray.length; i++) {
const [hash] = historyArray[i].split("|");
let found = false;
for (let j = 0; j < currentDirHistoryArray.length; j++) {
if (!currentDirHistoryArray[j]) {
continue;
}
const [hashCur] = currentDirHistoryArray[j].split("|");
if (makeHash(hash) === hashCur) {
found = true;
delete currentDirHistoryArray[j];
}
}
if (!found) {
missedArray.push(historyArray[i]);
}
}
return missedArray;
}
function makeHash(string) {
return crypto
.createHmac("sha256", options.secret)
.update(string)
.digest("hex");
}
({
async from() {
let lines;
try {
lines = findMissedCommits();
} catch (e) {
console.warn(
chalk.bold.green(`No commits in current directory. Continue`)
);
lines = getHistory()
.split("\n")
.filter(x => x);
}
if (!lines.length) {
console.log(chalk.bold.green(`Nothing to update. It may be ok.`));
return;
}
console.log(chalk.green(`Found ${lines.length} commits`));
const spinner = ora(`Writing history`).start();
await lines.reverse().reduce(async (lastPromise, line) => {
await lastPromise;
const [hash, date] = line.split("|");
const newHash = makeHash(hash);
return exec(
`echo "${newHash}" > commit.md && git add commit.md && git commit --date "${date}" -m "${newHash}"`,
{
encoding: "utf8"
}
);
}, Promise.resolve());
spinner.succeed();
}
}[command]());
};