forked from opista/svn-blamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blamer.js
109 lines (94 loc) · 3.29 KB
/
blamer.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
const vscode = require('vscode');
const fs = require('fs');
const decoration = require('./decoration');
const subversion = require('./subversion');
const blamer = {
editor: '',
extensionPath: '',
files: [],
images: {},
statusBarItem: undefined,
uniqueCommits: [],
init() {
this.destroy();
this.editor = vscode.window.activeTextEditor;
this.extensionPath = vscode.extensions.getExtension('beaugust.blamer-vs').extensionPath;
this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 0);
this.updateStatusBar('Blamer Started');
subversion.init(this.editor)
.then((revisions) => {
this.updateStatusBar(`Preparing ${subversion.name}`);
this.getFiles().then(() => {
this.findUniques(revisions)
.then(() => {
this.updateStatusBar(`Processing complete`);
this.setLines(revisions);
this.statusBarItem.dispose();
})
.catch(error => this.handleError(error));
})
}).catch((error) => this.handleError(error));
},
destroy() {
subversion.destroy();
decoration.destroy();
this.editor = '';
this.extensionPath = '';
this.files = [];
this.images = {};
if (this.statusBarItem) {
this.statusBarItem.dispose();
}
},
findUniques(revisions) {
this.uniqueCommits = Object.values(revisions).reduce((x, y) => x.includes(y) ? x : [...x, y], []);
const promises = this.uniqueCommits.map((unique) => subversion.getLog(unique)
.then((commit) => {
this.updateStatusBar(`Processing revision ${commit.revision}`);
this.images[unique] = {
image: this.randomImage(),
revision: commit.revision,
email: commit.email,
date: commit.date,
message: commit.message,
}
})
.catch(error => this.handleError(error))
);
return Promise.all(promises);
},
setLines(revisions) {
Object.entries(revisions).forEach(([line, revision]) => {
decoration.set(this.editor, line, this.images[revision]);
});
},
getFiles() {
return new Promise((resolve) => {
fs.readdir(`${blamer.extensionPath}/img`, (err, files) => {
this.files = files || [];
resolve();
});
})
},
randomImage() {
const length = this.files.length;
const index = Math.floor(Math.random() * length);
const image = this.files[index];
this.files.splice(index, 1);
return image;
},
updateStatusBar(text) {
if (text) {
this.statusBarItem.text = `$(versions) ${text}`;
this.statusBarItem.show();
} else {
this.statusBarItem.hide();
}
},
handleError(error) {
this.updateStatusBar(`Error`);
vscode.window.showErrorMessage(error);
console.error('Error: ', error);
}
};
module.exports = blamer;