-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
197 lines (167 loc) · 4.33 KB
/
index.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict'
const { app, dialog, Menu, ipcMain } = require('electron')
const db = require('./db.js')
const kc = require('./kafclient.js')
const lg = require('./logger.js')
const wins = require('./wins.js')
const plugins = require('./plugins.js')
const users = require('./users.js')
const loc = require('./loc.js')
const util = require('./util.js')
const dbg = require('./dbg.js')
const fs = require('fs')
/* understand/
* main entry point into our program - called
* when electron is ready
*/
function onReady() {
const log = lg(generateName(), process.env.DEBUG)
setupFolders(err => {
if(err) {
dialog.showErrorBox("DB", err.toString())
app.quit()
return
}
db.start(log, err => {
if(err) {
dialog.showErrorBox("DB", err.toString())
app.quit()
} else {
log("app/info", `Logging to ${log.getName()}`)
setupIPC(log)
setupUI()
}
})
})
}
function setupFolders(cb) {
util.ensureExists(loc.cookies(), err => {
if(err) cb(err)
else util.ensureExists(loc.savedCookies(), cb)
})
}
app.whenReady().then(onReady)
app.on('window-all-closed', () => app.quit())
/* understand/
* We need a logfile to hold the messages of our current
* run without interfering with other concurrent runs
*/
function generateName() {
let n = `log-${(new Date()).toISOString()}-${process.pid}`
return n.replace(/[/:\\*&^%$#@!()]/g, "_")
}
function setupIPC(log) {
ipcMain.handle("show-settings", () => {
wins.Settings()
})
ipcMain.handle("trouble", () => {
dbg.xport(log)
})
ipcMain.handle("close-settings", () => {
wins.closeSettings()
})
ipcMain.handle("get-logname", async () => {
return { name: log.getName(), DEBUG: process.env.DEBUG }
})
ipcMain.handle("get-plugins", async (e, url) => {
return plugins.get(url)
})
ipcMain.handle("get-taskname", async (e, action) => {
return plugins.info(action)
})
ipcMain.handle("get-taskchat", async (e,task,status) => {
return plugins.chat(task, status)
})
ipcMain.handle("add-tasks", async (e, tasks) => {
return plugins.add(tasks)
})
ipcMain.handle("add-sent", async (e, tasks) => {
return plugins.sent(tasks)
})
ipcMain.handle("set-users", async (e, uis) => {
return users.set(uis)
})
ipcMain.handle("set-userips", async (e, uips) => {
return users.setips(uips)
})
ipcMain.handle("set-puppetShow", async (e, show) => {
return users.setPuppetShow(show)
})
ipcMain.handle("set-timeout", async (e, t) => {
return users.setTimeout(t)
})
ipcMain.handle("x-cute", async (e, { a, t }) => {
return plugins.perform(a, t)
})
ipcMain.handle("x-cute-again", async (e, task) => {
return plugins.retry(task)
})
ipcMain.handle("x-it", async (e, task) => {
app.quit()
})
ipcMain.handle("clear-browsers", async () => {
users.closeBrowsers()
})
ipcMain.handle("save-usercookie", async (e, info) => {
users.saveCookieFile(info)
})
}
/* way/
* Set up the main menu, load the main window when user clicks the app
* and start by showing the main window
*/
function setupUI() {
setupMenu()
app.on("activate", () => {
if(wins.None()) wins.Main()
})
wins.Main()
}
/* way/
* create a template containing most of the default menu items along
* with our items then set it as our application menu
*/
function setupMenu() {
let template = [
{ role: 'appMenu' },
{ role: 'fileMenu' },
{ role: 'editMenu' },
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forcereload' },
{ role: 'toggledevtools' },
{ type: 'separator' },
{ role: 'togglefullscreen' },
{ type: 'separator' },
{
label: 'Settings',
click: () => wins.Settings()
},
{
label: "User Cookies",
click: () => wins.UserCookie()
}
]
},
{ role: 'windowMenu' },
]
let menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}
/**
+ * understand/
+ * To prevent multiple time application open
+ */
const gotTheLock = app.requestSingleInstanceLock()
if(!gotTheLock) {
app.quit()
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
if(wins.Main()) {
if(wins.Main().isMinimized()) wins.Main().restore()
wins.Main().focus()
}
})
}