-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
70 lines (56 loc) · 1.73 KB
/
main.ts
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
import * as fs from 'fs';
import * as path from 'path';
import { Server, Socket } from 'socket.io';
import createFactory from './watchers';
import startApp from '.';
import { getAllFolders, openFolder } from './utils';
import logger from './logger';
(async () => {
const server = await startApp();
const io = new Server(server);
const watchers = createFactory(io);
await watchers.resumeLastWork();
async function handleConnection(socket: Socket) {
socket.emit('connection');
socket.on('folders', async () => {
socket.emit(
'folders',
(await getAllFolders()).map((data) => ({
...data,
name: path.basename(data.folderPath),
})),
);
});
socket.on('watch', (args) => {
watchers.watch(socket, args);
});
socket.on('unwatch', (args) => {
watchers.unwatch(args);
});
socket.on('open', function onOpen(folderPath) {
if (!fs.existsSync(folderPath)) {
return socket.emit('error', {
message: 'this path is not found on your device.',
});
}
openFolder(folderPath, (error) => {
if (error) {
if ('message' in error) logger.error(error.message);
else logger.error(JSON.stringify(error));
return socket.emit('error', error);
}
socket.emit('folderOpened', path.basename(folderPath));
});
});
}
function handleDisconnect(socket: Socket) {
socket.emit('disconnect');
}
io.on('connection', handleConnection);
io.on('disconnect', handleDisconnect);
function reportError(reason: Error | unknown) {
logger.error(JSON.stringify(reason));
}
process.on('uncaughtException', reportError);
process.on('unhandledRejection', reportError);
})();