-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
139 lines (121 loc) · 3.74 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
139
const { BrowserWindow, Menu, app, dialog, ipcMain } = require('electron');
const { opendir, readFile, writeFile } = require('node:fs/promises');
const { exec } = require('node:child_process');
const path = require('path');
Menu.setApplicationMenu(Menu.buildFromTemplate([
{
label: '&File',
submenu: [
{
accelerator: 'CommandOrControl+W',
click: () => {
// FIXME: Ideally we wire this in to the renderer process's logic, but I haven't figured out how to do that, yet.
},
label: 'Close'
},
{
accelerator: 'CommandOrControl+Shift+I',
role: 'toggleDevTools',
label: 'Developer Tools'
},
{
accelerator: 'CommandOrControl+R',
role: 'reload',
label: 'Reload'
}
]
},
{
label: '&Edit',
submenu: [
{ accelerator: 'CommandOrControl+Z', label: 'Undo', role: 'undo' },
{ accelerator: 'CommandOrControl+Shift+Z', label: 'Redo', role: 'redo' },
{ type: 'separator' },
{ accelerator: 'CommandOrControl+X', label: 'Cut', role: 'cut' },
{ accelerator: 'CommandOrControl+C', label: 'Copy', role: 'copy' },
{ accelerator: 'CommandOrControl+V', label: 'Paste', role: 'paste' }
]
}
]));
const gitInspect = (rootPath) => new Promise((resolve) => {
exec('git status --porcelain=v1', {
cwd: rootPath,
encoding: 'utf8'
}, (ignoredError, stdout) => {
resolve((stdout || '').split('\n').reduce((a, line) => {
if (line) {
const [staged, unstaged, repoPath] = (/^(.)(.) (.+)$/g).exec(line).slice(1);
a.push({
path: path.join(rootPath, repoPath),
status: unstaged.trim() || staged.trim()
});
}
return a;
}, []));
});
});
const gitCheckIgnore = (rootPath, ...childPaths) => new Promise((resolve) => {
exec(`git check-ignore ${childPaths.map((it) => JSON.stringify(it)).join(' ')}`, {
cwd: rootPath,
encoding: 'utf8'
}, (ignoredError, stdout) => {
resolve(stdout.split('\n').filter(Boolean).map((path) => ({ path })));
});
});
const traverse = async (rootPath) => {
const parent = { children: [], path: rootPath };
const base = await opendir(rootPath);
let entry = await base.read();
while (entry !== null) {
if (entry.isDirectory()) {
if (entry.name === '.git') {
parent.git = await gitInspect(rootPath);
}
parent.children.push(await traverse(path.join(rootPath, entry.name)));
} else if (entry.isFile()) {
parent.children.push(Object.freeze({ path: path.join(rootPath, entry.name) }));
}
entry = await base.read();
}
await base.close();
if (parent.git) {
parent.ignored = [
{ path: path.join(rootPath, '.git') },
...await gitCheckIgnore(rootPath, ...parent.children.map((it) => it.path))
];
}
parent.children.sort((a, b) => {
if (a.children && !b.children) {
return -1;
}
if (!a.children && b.children) {
return 1;
}
if (a.path < b.path) {
return -1;
}
if (a.path > b.path) {
return 1;
}
return 0;
});
return Object.freeze(parent);
};
app.whenReady().then(() => {
ipcMain.handle('os:env', () => ({ ...process.env }));
ipcMain.handle('file:save', (event, file) => writeFile(file.path, file.data));
ipcMain.handle('file:open', (event, path) => readFile(path, { encoding: 'utf8' }));
ipcMain.handle('file:tree', async (event, rootPath) => traverse(rootPath));
ipcMain.handle('quit', () => app.quit());
const window = new BrowserWindow({
autoHideMenuBar: true,
height: 1080,
icon: path.join(__dirname, 'icon.png'),
title: 'Adam',
webPreferences: {
preload: path.join(__dirname, 'preload.js')
},
width: 1920
});
window.loadFile('index.html');
});