-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
435 lines (353 loc) · 13 KB
/
renderer.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
const ipc = require('electron').ipcRenderer;
const remote = require('electron').remote;
const shell = require('electron').shell;
const fs = require('fs');
const main = remote.require('./main.js');
const path = require('path');
const app = remote.app;
const userPath = app.getPath('userData');
const exec = require('child_process').exec;
let cronTask = null;
let data = {
gitPaths: [],
allClean: true,
isPaid: false,
waitHours: 4
};
class GitPanda{
constructor() {
this.getData().then((response) => {
this.data = response;
rivets.bind(document.getElementById('body'), {
app: this
});
this.startCronJob(this.data.waitHours || data.waitHours);
this.openAtLogin(this.data.openAtLogin || false);
});
this.setEvents();
}
ipcEvents() {
// Add new repo
const selectDirBtn = document.getElementById('btn');
selectDirBtn.addEventListener('click', (event) => {
ipc.send('open-file-dialog');
});
ipc.on('selected-directory', (event, path) => {
// document.getElementById('selected-file').innerHTML = `${path}`
const url = path[0];
const status = this.checkGitStatus({path: url}).then(rsp => {
if (rsp.error) {
new Alert('danger', {
title: 'Something went terribly wrong',
message: 'Well, maybe not terribly....are you sure this is a Git directory ?',
timer: 10
});
return;
}
this.saveDirectory(rsp).then(() => {
this.refreshRepos();
new Alert('success', {
title: 'Success',
message: 'Well done! Your repo has been added to the list'
});
});
});
});
ipc.on('ping', (event, message) => {
if (message.name === 'refresh') {
this.refreshRepos();
}
if(message.name === 'log') {
console.log('main.js log:', message.data);
}
if(message.name === 'close') {
this.closeOrMinimize();
}
});
}
openAtLogin(value) {
app.setLoginItemSettings({
openAtLogin: value,
});
}
onOpenAtLoginChange(a, b) {
const value = a.target.checked;
b.app.openAtLogin(value);
b.app.saveData();
}
setEvents() {
this.ipcEvents();
// Link to olitomas.com
const olitomas = document.getElementById('olitomas');
olitomas.addEventListener('click', (event) => {
shell.openExternal('https://olitomas.com/');
});
// Link to Github
const github = document.getElementById('github');
github.addEventListener('click', (event) => {
shell.openExternal('https://github.com/olitomas/git-panda');
});
// Register
const registerBtn = document.getElementById('registerBtn');
const registeredBtn = document.getElementById('registeredBtn');
registerBtn.addEventListener('click', (event) => {
this.registerDialog();
});
registeredBtn.addEventListener('click', (event) => {
this.registerDialog();
});
}
registerDialog(a, b) {
let bind;
new Modal({
title: 'Register',
message: `
<div id="registerModal">
<div class="ui text-container center">
<p>
Registering is pretty simple, I use the honor system.
</p>
<p>
If you intend to use Git Panda then please donate $5 by clicking the button below.
</p>
<p>
Once you have donated you are free to check the "I have registered" checkbox.
</p>
</div>
<br>
<div class="row">
<div class="center aligned column">
<a id="donate" class="ui huge button">Donate</a>
</div>
</div>
<br>
<div class="ui middle aligned container">
<div class="row">
<div class="ui checkbox">
<input id="isPaid" type="checkbox" rv-checked="app.data.isPaid">
<label>I have registered</label>
</div>
</div>
</div>
</div>
`,
onOpen: () => {
document.getElementById('donate').addEventListener('click', () => {
shell.openExternal('https://paypal.me/olitomas/5');
});
bind = rivets.bind(document.getElementById('registerModal'), {
app: this
});
},
onClose: () => {
this.saveData();
bind.unbind();
}
});
}
closeGitPanda() {
main.closeGitPanda();
}
minimizeGitPanda(a, b) {
b.app.closeOrMinimizeModal.close();
const w = remote.getCurrentWindow();
w.minimize();
}
closeOrMinimize(a, b) {
let bind;
this.closeOrMinimizeModal = new Modal({
title: 'Close Git Panda or minimize ?',
message: `
<div id="closeOrMinimize">
<div class="ui text-container center">
<button rv-on-click="app.closeGitPanda" class="ui secondary button">
Close
</button>
<button rv-on-click="app.minimizeGitPanda" class="ui button">
Minimize
</button>
</div>
</div>
`,
onOpen: () => {
bind = rivets.bind(document.getElementById('closeOrMinimize'), {
app: this
});
},
onClose: () => {
bind.unbind();
}
});
}
checkGitStatus(obj, index) {
const path = obj.path;
const name = obj.name;
return new Promise((resolve, reject) => {
const fs = require('fs');
if (!fs.existsSync(path)) {
new Modal('danger', {
title: 'Something is wrong with this directory: ' + name,
message: 'Path for project cant be found, so it was removed from git-panda...did you move or rename your project ?'
});
if(index){
// Deleting from data since nothing was found in path
console.log('yep');
this.data.gitPaths.splice(index, 1);
this.saveData().then(() => {
this.refreshRepos();
});
}
resolve({error: true});
return;
}
const command = 'cd ' + path + '; git status --porcelain';
exec(command, (error, stdout, stderr) => {
if(error){
resolve({ error: true });
}else{
const pathSplit = path.split(/[\\\/]/);
const projectName = pathSplit[pathSplit.length - 1];
let workingTreeClean = stdout ? false : true;
resolve({
name: projectName,
path: path,
workingTreeClean: workingTreeClean,
error: null
});
}
});
});
}
changeCronJob(a,b) {
const value = parseInt(a.target.value);
if(value){
b.app.startCronJob(value);
document.getElementById('reminderInput').classList.remove('error');
b.app.saveData();
}else{
document.getElementById('reminderInput').classList.add('error');
}
}
startCronJob (cronTime) {
cronTime = cronTime || 4;
const millisec = 3600000 * cronTime;
if(cronTask) clearInterval(cronTask);
cronTask = setInterval(() => {
this.refreshRepos().then(rsp => {
if (!rsp.allClean) {
this.notifyUser(
'You have git repos that need love <3',
rsp.unclean.join(',')
);
}
});
}, millisec);
}
getData() {
return new Promise((resolve) => {
fs.readFile(userPath + '/data.json', 'utf8', (err, d) => {
if(err) new Alert('danger', err);
data = d ? JSON.parse(d) : data;
resolve(data);
});
});
}
checkIfPathExists (arr, path) {
let exists = false;
let index = false;
arr.forEach((value, i) => {
const oldPath = value.path;
if (path === oldPath) {
exists = true;
index = i;
}
});
return { exists: exists, index: index };
}
saveDirectory(obj) {
return new Promise((resolve, reject) => {
const pathTest = this.checkIfPathExists(data.gitPaths, obj.path);
if (!pathTest.exists) {
data.gitPaths.push(obj);
this.saveData().then(() => {
resolve({ success: true });
});
} else {
data.gitPaths[pathTest.index] = obj;
this.saveData().then(() => {
resolve({ success: true });
});
}
});
}
saveData() {
return new Promise((resolve, reject) => {
fs.writeFile((userPath + '/data.json'), JSON.stringify(this.data), 'utf8', (rsp, error) => {
resolve({ success: true });
});
});
}
removeItem (a, b) {
const self = b ? b.$parent.app : this;
const index = b.$index;
self.data.gitPaths.splice(index, 1);
self.saveData().then(() => {
self.refreshRepos();
});
}
openItemFolder (a, b) {
const self = b ? b.$parent.app : this;
const index = b.$index;
const path = self.data.gitPaths[index].path;
shell.showItemInFolder(path);
}
notifyUser (title, message) {
// Do this from the renderer process
const notif = new window.Notification(title, {
body: message
});
// If the user clicks in the Notifications Center, show the app
notif.onclick = () => {
main.showMainWindow();
};
}
refreshRepos(a, b) {
const self = b ? b.app : this;
return new Promise((resolve, reject) => {
let allClean = true;
let unclean = [];
let counter = 0;
let len = data.gitPaths.length;
if (len) {
document.getElementById('refreshIcon').classList.add('loading');
} else {
data.allClean = true;
}
data.gitPaths.forEach((obj, key) => {
self.checkGitStatus(obj, key).then(statusObj => {
if (!statusObj.workingTreeClean){
allClean = false;
const name = statusObj.name;
unclean.push(name);
}
self.saveDirectory(statusObj).then(() => {
counter++;
if (counter === len) {
//Hack to update rivets view
data.allClean = allClean;
data.gitPaths.reverse();
data.gitPaths.reverse();
setTimeout(() => {
document
.getElementById('refreshIcon')
.classList.remove('loading');
}, 500);
resolve({ allClean: allClean, unclean: unclean });
}
});
});
});
});
}
}
new GitPanda();