-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
138 lines (115 loc) · 3.49 KB
/
main.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
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const url = require('url');
const nodeGit = require('nodegit');
const isDevBuild = process.env.NODE_ENV === 'development';
const uuid = require('uuid');
let mainWindow;
const repos = { };
function generateWindowUrl(fileName) {
const generatedUrl = isDevBuild ?
url.format({
protocol: 'http:',
host: 'localhost:8080',
pathname: fileName,
slashes: true
}) :
url.format({
protocol: 'file:',
pathname: path.join(__dirname, 'dist', fileName),
slashes: true
});
return generatedUrl;
}
function createWindow() {
mainWindow = new BrowserWindow({
width: 1024, height: 768, show: false
});
mainWindow.loadURL(generateWindowUrl('index.html'));
mainWindow.once('ready-to-show', () => {
mainWindow.show();
if (isDevBuild) {
mainWindow.webContents.openDevTools();
}
});
mainWindow.on('closed', function() {
mainWindow = null;
});
}
ipcMain.on('repo-list-commits', (evt, repoUuid) => {
const repo = repos[repoUuid];
if (!repo) {
evt.sender.send('invalid-repo-uuid', repoUuid);
return;
}
repo.getMasterCommit()
.then(function(firstCommitOnMaster) {
var history = firstCommitOnMaster.history();
const commits = [];
history.on('commit', function(commit) {
commits.push({
sha: commit.sha(),
author: commit.author().name(),
email: commit.author().email(),
date: commit.date(),
message: commit.message()
});
});
history.on('end', function() {
evt.sender.send('repo-commit-list', commits);
});
history.start();
});
});
ipcMain.on('repo-select', (evt, filepath) => {
nodeGit.Repository
.open(filepath)
.then(repo => {
const projectName = filepath.split(path.sep).pop();
const repoUuid = uuid();
repos[repoUuid] = repo;
evt.sender.send('valid-repo-selected', {
id: repoUuid,
name: projectName
});
})
.catch(err => {
evt.sender.send('invalid-repo-selected', path);
});
});
ipcMain.on('repo-status', (evt, repoUuid) => {
const repo = repos[repoUuid];
if (!repo) {
evt.sender.send('invalid-repo-uuid', repoUuid);
return;
}
repo.getStatus()
.then(statuses => {
evt.sender.send('repo-status-result', statuses.map(status => ({
fileName: path.basename(status.path()),
path: status.path(),
inIndex: !!status.inIndex(),
isModified: !!status.isModified(),
isConflicted: !!status.isConflicted(),
isDeleted: !!status.isDeleted(),
isNew: !!status.isNew(),
isRenamed: !!status.isRenamed(),
isTypechange: !!status.isTypechange()
})));
})
.catch(e => {
// TODO
console.log(e)
});
});
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (mainWindow === null) {
createWindow();
}
});