-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
75 lines (66 loc) · 1.87 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
const { app, shell, nativeImage, Menu, Tray } = require('electron')
const { execFileSync } = require('child_process')
const path = require('path')
const fs = require('fs')
const iconFile = path.join(__dirname, 'icons/png/16x16.png')
const defaultSettings = {"scripts": []}
const DefaultScriptTimeout = 1000;
class ActionScriptsApp {
constructor() {
app.on('ready', () => {
this.tray = new Tray(nativeImage.createFromPath(iconFile))
this.settingsPath = path.join(app.getPath('userData'), 'settings.json')
this.ensureSettingsFile();
this.setMenu();
this.settingsWatcher = fs.watch(this.settingsPath).on('change', () => this.updateMenu())
});
app.on('quit', () => this.settingsWatcher.close())
}
ensureSettingsFile() {
try {
fs.writeFileSync(this.settingsPath, JSON.stringify(defaultSettings), { flag: 'wx' });
}
catch (error) {
if (error.code == 'EEXIST') {
console.log(`Settings file found at ${error.path}`);
}
else {
console.log(error);
}
}
}
updateMenu() {
try {
this.setMenu()
} catch (error) {
console.log(error)
}
}
setMenu() {
const menuTemplate = JSON.parse(fs.readFileSync(this.settingsPath)).scripts.map(this.createMenuItem).concat([
{
type: 'separator'
},
{
label: 'Settings',
click: () => shell.openItem(this.settingsPath)
},
{
label: 'Quit',
click: () => app.quit()
}
]);
this.tray.setContextMenu(Menu.buildFromTemplate(menuTemplate));
}
createMenuItem(settingsElement) {
return {
label: `Run ${settingsElement.label}`,
click: () => console.log(execFileSync(settingsElement.file, {
shell: true,
timeout: settingsElement.timeout || DefaultScriptTimeout,
encoding: 'utf-8'
}))
}
}
}
new ActionScriptsApp()