-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
94 lines (79 loc) · 2.18 KB
/
Copy pathmain.js
File metadata and controls
94 lines (79 loc) · 2.18 KB
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
const { app, Tray, Menu, nativeTheme: { shouldUseDarkColors }, BrowserWindow } = require('electron')
const { exec } = require("child_process");
const path = require('path')
const fs = require('fs');
const { homedir } = require('os')
const icon = shouldUseDarkColors ? 'tray-icon-white.png' : 'tray-icon-dark.png'
const homedirPath = homedir()
let tray = null
const configString = `
[
{
"label": "Script label",
"path": "/path/to/script.sh"
}
]
`
// initLog()
fixPath();
app.dock.hide()
app.on('ready', createApp)
app.setLoginItemSettings({
openAtLogin: true
})
function createApp() {
createConfigFile()
const configJson = readConfigFile()
const menuTemplate = getMenuTemplate(configJson)
const contextMenu = Menu.buildFromTemplate(menuTemplate)
tray = new Tray(path.join(__dirname, icon))
tray.setContextMenu(contextMenu)
}
function createConfigFile() {
const path = `${homedirPath}/bt-config.json`
if (fs.existsSync(path)) {
} else {
fs.writeFileSync(path, configString, function (err) {
if (err) throw err;
});
}
}
function readConfigFile() {
const configFile = path.join(homedirPath, 'bt-config.json')
let rawdata = fs.readFileSync(configFile);
let configJson = JSON.parse(rawdata);
return configJson
}
function getMenuTemplate(configJson) {
const template = configJson.map(obj => (obj.type === 'separator' ? { type: 'separator' } : { label: obj.label, click: () => executeBash(obj.path) }))
template.push(
{ type: 'separator' },
{ label: "Quit", click: () => app.quit() }
)
return template
}
function executeBash(scriptPath) {
exec(scriptPath, execCallback);
}
function execCallback(error, stdout, stderr) {
console.log(error, stdout, stderr)
console.log("DONE")
}
function fixPath() {
const shellPath = require('shell-path');
if (process.platform !== 'darwin') {
return;
}
process.env.PATH = shellPath.sync() || [
'./node_modules/.bin',
'/.nodebrew/current/bin',
'/usr/local/bin',
process.env.PATH
].join(':');
};
function initLog() {
const log = require('electron-log');
Object.assign(console, log.functions);
log.transports.file.file = __dirname + 'log.log';
log.info('Started app');
};