kill all subprocesses on closing #6

Merged
Anri merged 4 commits from fix-4 into main 2024-05-17 21:24:04 +02:00
2 changed files with 17 additions and 1 deletions
Showing only changes of commit dd62f81e35 - Show all commits

View file

@ -7,6 +7,7 @@ import {
getNewFilename, getNewFilename,
getVideoDuration, getVideoDuration,
printAndDevTool, printAndDevTool,
processes,
} from "./utils/misc"; } from "./utils/misc";
import path = require("path"); import path = require("path");
import ffmpegPath = require("ffmpeg-static"); import ffmpegPath = require("ffmpeg-static");
@ -226,3 +227,11 @@ app.whenReady().then(() => {
ipcMain.handle("exit", () => (error ? {} : app.quit())); ipcMain.handle("exit", () => (error ? {} : app.quit()));
ipcMain.handle("confirmation", (_, text: string) => confirmation(text)); ipcMain.handle("confirmation", (_, text: string) => confirmation(text));
}); });
app.on("window-all-closed", () => {
processes.forEach((proc) => {
proc.kill();
});
app.quit();
});

View file

@ -4,6 +4,8 @@ import path = require("path");
import { BrowserWindow } from "electron"; import { BrowserWindow } from "electron";
import { existsSync, unlink } from "fs"; import { existsSync, unlink } from "fs";
export const processes: child_process.ChildProcess[] = [];
/** Create a new filename from the OG one */ /** Create a new filename from the OG one */
export const getNewFilename = (ogFile: string, part: string) => { export const getNewFilename = (ogFile: string, part: string) => {
const oldFile = path.parse(ogFile); const oldFile = path.parse(ogFile);
@ -28,13 +30,18 @@ export const execute = (
command: string command: string
): Promise<{ stdout: string; stderr: string }> => { ): Promise<{ stdout: string; stderr: string }> => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
child_process.exec(command, (error, stdout, stderr) => { const process = child_process.exec(command, (error, stdout, stderr) => {
if (error) { if (error) {
reject(error); reject(error);
} else { } else {
resolve({ stdout, stderr }); resolve({ stdout, stderr });
} }
}); });
processes.push(process);
process.on("exit", () => {
processes.splice(processes.indexOf(process), 1);
});
}); });
}; };