-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
132 lines (106 loc) · 3.78 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
import fs from 'fs';
import path from 'path';
import util from 'util';
import process from 'process';
import { app, BrowserWindow, ipcMain, protocol } from 'electron';
import config from './config';
import Client from './modules/Client';
import { Records } from '@ez-trigger/core';
let mainWindow, packageInfo, packagePath, argvStartPosition;
const client = new Client({ ip: config.ip, port: config.port });
const records = new Records();
app.on('activate', () => mainWindow === null && createWindow());
app.on('window-all-closed', () => {
client.stop();
process.platform !== 'darwin' && app.quit();
process.exit(0);
});
ipcMain.on('add-record', (event, arg) => records.add(arg.data));
ipcMain.on('get-record', (event, arg) => event.sender.send('get-records', records.data)); // prettier-ignore
ipcMain.on('export-record', async (event, arg) => {
const filePath = await records.export(arg.filename, config.exportPath);
event.sender.send('exported-record', filePath);
});
ipcMain.on('send-data', (event, arg) => client.send(arg.data));
ipcMain.on('register-command', (event, arg) => {
client.commands.register(arg.command, options => {
event.sender.send('call-command', {command: arg.command, options});
});
}); // prettier-ignore
ipcMain.on('deregister-command', (event, arg) => client.commands.deregister(arg.command)); // prettier-ignore
ipcMain.on('get-argv', (event, arg) => event.sender.send('got-argv', programArgv)); // prettier-ignore
client.commands.register('WHO', ({ options, client }) => {
console.log('i Trying to verify experiment program.');
client.send(`TP ${client.props.type}`);
});
client.commands.register('VERIFIED', (options, client) => {
console.log('i Verified successfuly.');
});
packageInfo = {};
for (let i = 2; i < process.argv.length; i++) {
if (!process.argv[i].startsWith('--')) {
packagePath = process.argv[i];
argvStartPosition = i + 1;
break;
}
}
const programArgv = process.argv.slice(argvStartPosition);
if (packagePath) {
const packageManifestPath = path.normalize(
path.join(packagePath, 'package.json')
);
if (!fs.existsSync(packageManifestPath)) throw TypeError('Path not exist!');
const packageInfoStr = fs.readFileSync(packageManifestPath, 'utf8');
packageInfo = JSON.parse(packageInfoStr);
if (!packageInfo.main)
throw TypeError('Not a standard ezTrigger system package!');
}
const packageMainfile = packagePath
? path.normalize(path.join(packagePath, packageInfo.main))
: './index.html';
if (packageInfo.typeName) {
client.setProps({ type: packageInfo.typeName });
}
const initProgram = async () => {
console.log('i Initializing ezTrigger system.');
protocol.registerFileProtocol(
'ez',
(request, callback) => {
const url = request.url.match('^ez://(.*)$')[1];
callback({
path: path.normalize(`${__dirname}/prog_modules/${url}`)
});
},
err => {
if (err) throw err.message;
}
);
protocol.registerFileProtocol(
'prog',
(request, callback) => {
const url = request.url.match('^prog://(.*)$')[1];
callback({
path: path.normalize(`${packagePath}/${url}`)
});
},
err => {
if (err) throw err.message;
}
);
await client.start();
const windowConfig =
packageInfo && packageInfo.fullscreen
? { fullscreenable: true }
: {
width: packageInfo.width ? packageInfo.width : 800,
height: packageInfo.height ? packageInfo.height : 600
};
mainWindow = new BrowserWindow(windowConfig);
mainWindow.webContents.session.webRequest.onHeadersReceived((_, callback) => {
callback({ responseHeaders: `default-src 'none'` });
});
//mainWindow.setMenu(null);
mainWindow.loadFile(packageMainfile);
mainWindow.on('closed', () => (mainWindow = null));
};
app.on('ready', initProgram);