-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
410 lines (361 loc) · 11.8 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
const {
app,
BrowserWindow,
ipcMain,
globalShortcut,
Notification,
dialog,
} = require("electron")
const { autoUpdater } = require("electron-updater")
const { lstatSync, readdirSync, existsSync } = require("fs")
const { platform } = require("os")
const { execSync, spawnSync } = require("child_process")
const { getWindowsBounds, saveBounds } = require("./settings")
const { resolve, join, extname } = require("path")
const isDev = require("electron-is-dev")
if (isDev) require("electron-reload")(__dirname)
let loadingWindow,
mainWindow,
userRecallWindow = true // TODO: need to create option for this
app.whenReady().then(main)
async function main() {
const { x, y, width, height } = getWindowsBounds()
const webPreferences = {
devTools: isDev ? true : false,
nodeIntegration: false, // is default value after Electron v5
contextIsolation: true, // protect against prototype pollution
enableRemoteModule: false, // turn off remote
preload: join(__dirname, "preload.js"), // use a preload script
}
// loadingWindow
loadingWindow = new BrowserWindow({
width: 400,
height: 360,
transparent: true,
alwaysOnTop: true,
autoHideMenuBar: true,
frame: false,
show: false,
backgroundColor: "#181A1B",
webPreferences: webPreferences,
})
loadingWindow.loadFile(join(__dirname, ".", "loadingWindow", "loading.html"))
loadingWindow.webContents.openDevTools()
loadingWindow.on("ready-to-show", async () => {
loadingWindow.show()
})
loadingWindow.on("show", async () => {
const binariesInstalled = await checkForBinaries()
if (!binariesInstalled) await getUserDecisionBinaries()
console.log("\u001b[" + 31 + "m" + "log before checking updates" + "\u001b[0m")
autoUpdater.checkForUpdates()
})
// mainWindow
mainWindow = new BrowserWindow({
width: width,
height: height,
x: userRecallWindow ? x : screen.width / 2 - screen.width,
y: userRecallWindow ? y : screen.height / 2 - screen.height,
show: false, // to show loading window instead
icon: join(
__dirname,
platform() == "darwin"
? "./assets/icons/mac/icon.icns"
: "./assets/icons/win/icon.ico"
),
frame: true, // hide titleBar = false
autoHideMenuBar: true, // hide alt Menu Bar = true
webPreferences: webPreferences,
})
mainWindow.on("resized", () => saveBounds(mainWindow.getBounds()))
mainWindow.on("moved", () => saveBounds(mainWindow.getBounds()))
if (isDev) mainWindow.webContents.openDevTools()
mainWindow.loadFile(join(__dirname, "index.html"))
}
ipcMain.on("showLoadingWindow", (_, error) => {
showLoadingWindow()
})
function showLoadingWindow() {
loadingWindow.show()
autoUpdater.checkForUpdates()
}
// check for Binaries ffmpeg/ffprobe
async function checkForBinaries() {
if (platform() === "darwin") {
const ffmpegPath = existsSync("/usr/local/bin/ffmpeg")
const ffprobePath = existsSync("/usr/local/bin/ffprobe")
return ffmpegPath && ffprobePath
}
const ffmpegPath = existsSync("C:/Program Files (x86)/FFmpeg/bin/ffmpeg.exe")
const ffprobePath = existsSync("C:/Program Files (x86)/FFmpeg/bin/ffprobe.exe")
return ffmpegPath && ffprobePath
}
async function getUserDecisionBinaries() {
const dialogOptions = {
type: "question",
defaultId: 0,
cancelId: 1,
buttons: ["Yes", "Quit"],
noLink: true,
message: `FFmpeg & FFprobe are missing, the application needs them to run. Would you like to install them now?`,
}
await dialog.showMessageBox(loadingWindow, dialogOptions).then(async (userResponse) => {
if (userResponse.response === 0) await awaitDownloadBinaries()
if (userResponse.response === 1) app.exit(0)
})
console.log("end of getUserDecisionBinaries()")
}
async function awaitDownloadBinaries() {
const { downloadBinaries } = require("./loadingWindow/installBinaries.js")
await downloadBinaries(loadingWindow)
// showLoadingWindow()
}
// autoUpdater START
autoUpdater.autoDownload = false // set Download to false
function sendStatusToWindow(text) {
// mainWindow.webContents.send("message", text)
console.log("\u001b[" + 32 + "m" + text + "\u001b[0m")
}
autoUpdater.on("checking-for-update", () => {
sendStatusToWindow("Checking for update...")
})
autoUpdater.on("update-available", (info) => {
sendStatusToWindow("Update available.")
})
autoUpdater.on("update-not-available", (info) => {
sendStatusToWindow("Update not available.")
closeLoaderOpenMainWindow()
})
autoUpdater.on("error", (err) => {
sendStatusToWindow("Error in auto-updater. " + err)
})
autoUpdater.on("update-available", (updateInfo) => {
getUserDecisionWin(updateInfo)
})
async function getUserDecisionWin(updateInfo) {
const { releaseNotes, releaseName, releaseDate, path, files, version, tag } = updateInfo
const dialogOptions = {
type: "question",
defaultId: 1,
cancelId: 1,
buttons: ["Yes", "No", "Quit"],
noLink: true,
message: `${version} - Update is available, would you like to download the update?`,
}
await dialog.showMessageBox(loadingWindow, dialogOptions).then(async (userResponse) => {
if (userResponse.response === 0) await downloadUpdateMac(updateInfo)
if (userResponse.response === 1) closeLoaderOpenMainWindow()
if (userResponse.response === 2) app.exit(0)
})
}
async function downloadUpdateMac(updateInfo) {
const { releaseNotes, releaseName, releaseDate, path, files, version, tag } = updateInfo
const { macUpdater, downloadDMG } = require("./macUpdater")
let options = {
gitUsername: "DMDComposer",
gitRepo: "MW_AudioSplitter",
}
const macDmgLink = await macUpdater(options)
const downloadDmgResults = await downloadDMG(loadingWindow, macDmgLink)
if (downloadDmgResults === true) app.relaunch(), app.exit()
if (downloadDmgResults === false) console.log("something went wrong with install...")
}
autoUpdater.on("download-progress", (progressObj) => {
const { percent } = progressObj
loadingWindow.webContents.send("update/progressBar", percent)
})
autoUpdater.on(
"update-downloaded",
(_, releaseNotes, releaseName, releaseDate, updateURL) => {
const dialogOptions = {
type: "info",
buttons: ["Restart", "Later"],
title: "Application Update",
message: platform() === "win32" ? releaseNotes : releaseName,
detail:
"A new version is been successfully downloaded. Would you like to restart the application to apply the update?",
}
dialog.showMessageBox(loadingWindow, dialogOptions).then((userResponse) => {
console.log(userResponse)
if (userResponse.response === 0) autoUpdater.quitAndInstall()
if (userResponse.response === 1) {
closeLoaderOpenMainWindow()
new Notification({
title: "Update Downloaded",
icon: "./assets/icons/png/icon.png",
sound: resolve(__dirname, ".", "assets", "audio", "tada.wav"),
silent: false,
body: "the update will be installed on the next restart of the application",
timeoutType: "default",
}).show()
}
})
}
)
// autoUpdater END
// set title of Notification
app.setAppUserModelId("MW Audio Splitter")
ipcMain.handle("is-file", async (_, path) => {
return lstatSync(path).isFile()
})
ipcMain.handle("is-folder", async (_, path) => {
try {
return lstatSync(path).isDirectory()
} catch (error) {
// console.log(error)
return false
}
})
ipcMain.handle("pbUpdateListener", async (_, progress) => {
progress >= 1 ? mainWindow.setProgressBar(-1) : mainWindow.setProgressBar(progress)
})
ipcMain.handle("resetProgressBar", async (_, args) => {
mainWindow.setProgressBar(-1)
})
ipcMain.handle("ffmpeg", async (_, args) => {
try {
execSync(args[0], args[1])
} catch (error) {
const err = error.stderr.toString().split("\n")
new Notification({
title: "Audio Splits Error",
icon: "./assets/icons/png/icon.png",
sound: resolve(__dirname, ".", "assets", "audio", "ding.wav"),
silent: false,
body: `Something went wrong: ${err[err.length - 2]}`,
timeoutType: "default",
}).show()
}
})
ipcMain.handle("audioChannelsString", async (_, args) => {
return execSync(args[0], args[1]).toString()
})
ipcMain.handle("getTotalSilence", async (_, args) => {
return spawnSync(args[0], args[1], args[2]).stderr
})
ipcMain.handle("getCountAudioSplits", async (_, destPath) => {
// https://stackoverflow.com/questions/32511789/looping-through-files-in-a-folder-node-js
let filesPrinted = readdirSync(destPath),
totalPrintedStems = 0,
stemsList = [
"2MIX",
"6MIX",
"CLK",
"MX01",
"MX02",
"MX03",
"MX04",
"MX05",
"MX06",
"MX07",
"MX08",
"MX09",
"MX10",
"MX11",
"MX12",
"MX13",
"MX14",
],
results = {}
// console.log(filesPrinted)
for (let value in stemsList) {
let sum = 0
for (let b in filesPrinted) {
console.log(filesPrinted[b])
if (filesPrinted[b].includes(stemsList[value])) sum += 1
}
if (sum == 0) continue
totalPrintedStems += sum
results[value] = sum
}
// console.log("results: " + results)
// console.log("totalPrintedStems: " + totalPrintedStems)
return [results, totalPrintedStems]
})
ipcMain.handle("getTotalFilesInDestPath", async (_, destPath) => {
let sum = 0
const totalFilesInDestPath = readdirSync(destPath)
totalFilesInDestPath.forEach((file) => {
if (extname(file) == ".wav") sum += 1
})
return sum
})
ipcMain.handle("app/close", async (_, error) => {
mainWindow.close()
})
ipcMain.handle("openBrowse", async (_, args) => {
const { openBrowse } = require("./openBrowse")
const openedDir = await openBrowse()
return openedDir
})
ipcMain.handle("newNotification", async (_, args) => {
const countAudioSplits = args?.[0]
const totalFilesInDestPath = args?.[1]
const fileMissingError = args?.[2]
new Notification({
title: "Audio Splits Completed",
icon: "./assets/icons/png/icon.png",
sound: resolve(__dirname, ".", "assets", "audio", "tada.wav"),
silent: false,
body: `Total Number of Stems: ${countAudioSplits} \n ${countAudioSplits} / ${totalFilesInDestPath} ${fileMissingError}`,
timeoutType: "default",
}).show()
})
ipcMain.handle("newDialog", async (_, args) => {
const userResponse = dialog.showMessageBoxSync(
loadingWindow,
args,
(response, checkboxChecked) => {
console.log(response)
console.log(checkboxChecked) //true or false
}
)
return userResponse
})
ipcMain.handle("toggleLoadingWindow", async (_, args) => {
!loadingWindow.isVisible() ? loadingWindow.show() : loadingWindow.hide()
})
ipcMain.handle("skipUpdate", async (_, args) => {
mainWindow.show()
// need to make an updatingWindow & loadingWindow separate.
// if loadingWindow closes than continue, if updatingWindow closes than restart app
try {
loadingWindow.hide()
} catch (error) {
console.log(`\u001b[${35}mloadingWindow: ${error}\u001b[0m`)
}
})
ipcMain.on("app/quit", (_, error) => {
app.exit(0)
})
ipcMain.handle("get/appName", async (_) => {
return app.getName()
})
ipcMain.handle("get/appVersion", async (_) => {
return app.getVersion()
})
async function closeLoaderOpenMainWindow() {
mainWindow.show()
try {
loadingWindow.close()
} catch (error) {
console.log(`\u001b[${35}mloadingWindow: ${error}\u001b[0m`)
}
}
ipcMain.handle("continueDialog/AfterDrop", async (_, args) => {
const dialogOptions = {
type: "question",
defaultId: 1,
cancelId: 1,
buttons: ["Yes", "No"],
noLink: true,
message: `You've only uploaded ${args} audio files, MW stems are always 17,
would you like to continue anyway?`,
}
return await dialog
.showMessageBox(loadingWindow, dialogOptions)
.then(async (userResponse) => {
if (userResponse.response === 0) return true
if (userResponse.response === 1) return false
})
})