-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
129 lines (104 loc) · 3.95 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
const path = require('path');
const git = require('nodegit');
const _ = require('lodash');
const Tags = {
IGNORE: '[GTI]',
};
async function formatCommits(commits) {
const formattedCommits = await Promise.all(
commits.map(async commit => {
const message = commit.message();
const diffs = await commit.getDiff();
const patches = _.flatten(await Promise.all(diffs.map(diff => diff.patches())));
let files = await Promise.all(
patches.map(async patch => {
const hunks = await patch.hunks();
const lines = _.flatten(await Promise.all(hunks.map(hunk => hunk.lines())));
const diff = lines
.map(line => ({
origin: String.fromCharCode(line.origin()),
content: line.content(),
}))
.filter(line => line.origin === '+' || line.origin === '-' || line.origin === ' ');
return {
isAdded: patch.isAdded(),
newPath: patch.newFile().path(),
oldPath: patch.oldFile().path(),
isRenamed: patch.isRenamed(),
message: message,
diff,
};
})
);
// TODO: for some reason patch.isRenamed() is not working
if (files.length == 2) {
const [src, dest] = files;
if (src.diff.every(({ content }, index) => content === dest.diff[index].content)) {
dest.isRenamed = true;
dest.isAdded = false;
if (dest.isAdded) {
dest.oldPath = src.oldPath;
} else {
dest.newPath = src.newPath;
}
files = [dest];
}
}
return {
message,
files,
};
})
);
return formattedCommits;
}
async function getCommits(history) {
let resolve = () => {};
const p = new Promise(_ => (resolve = _));
const commits = [];
history.on('commit', c => {
commits.unshift(c);
});
history.on('end', () => resolve(commits));
history.start();
return await formatCommits(await p);
}
function formatNewFile(file) {
const ext = path.extname(file.newPath).substr(1);
const text = file.diff.map(line => line.content).join('');
return '```' + ext + '\n' + text + '\n```';
}
function formatDiff(file) {
const text = file.diff.map(line => line.origin + ' ' + line.content).join('');
return '```diff\n' + text + '\n```';
}
exports.render = async function render(pathToRepo) {
const repo = await git.Repository.open(pathToRepo);
const firstCommitOnMaster = await repo.getMasterCommit();
const history = firstCommitOnMaster.history();
const commits = await getCommits(history);
const tutorialCommits = commits.filter(commit => !commit.message.startsWith(Tags.IGNORE));
const markdown = tutorialCommits
.map(commit =>
[
commit.message,
commit.files
.map(file => {
let formatted;
let filename = file.newPath;
if (file.isAdded) {
formatted = formatNewFile(file);
} else if (file.isRenamed) {
filename = `${file.oldPath} => ${file.newPath}`;
formatted = '';
} else {
formatted = formatDiff(file);
}
return `📄 ${filename}\n` + formatted;
})
.join('\n'),
].join('\n')
)
.join('\n');
return markdown;
};