summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTobias Järvelöv <tobias.jarvelov@mullvad.net>2025-02-13 17:46:01 +0100
committerMarkus Pettersson <markus.pettersson@mullvad.net>2025-02-25 12:51:51 +0100
commit738f1788dddbdad0db0389306accb8c15b7f1305 (patch)
tree784e3935c7df5232cc210b8519934e7220822b44
parent36a8f03c55d2bd47379c593b9e35879ab46c8a40 (diff)
downloadmullvadvpn-738f1788dddbdad0db0389306accb8c15b7f1305.tar.xz
mullvadvpn-738f1788dddbdad0db0389306accb8c15b7f1305.zip
Add utilities used for the vite config
See the vite.config.ts file's comments for an explanation of why we need to use these utilities.
-rw-r--r--desktop/packages/mullvad-vpn/vite-utils.ts55
1 files changed, 55 insertions, 0 deletions
diff --git a/desktop/packages/mullvad-vpn/vite-utils.ts b/desktop/packages/mullvad-vpn/vite-utils.ts
new file mode 100644
index 0000000000..76b32cc54f
--- /dev/null
+++ b/desktop/packages/mullvad-vpn/vite-utils.ts
@@ -0,0 +1,55 @@
+// @see vite.config.ts comments for an explanation of why this file exists.
+//
+// These functions are copied from vite-plugin-electron, MIT licensed.
+// https://github.com/electron-vite/vite-plugin-electron/blob/c58fbf9c674f08b020455f01c97b99d3d7b562a9/src/utils.ts
+import cp from 'node:child_process';
+
+export interface PidTree {
+ pid: number;
+ ppid: number;
+ children?: PidTree[];
+}
+
+export function treeKillSync(pid: number) {
+ if (process.platform === 'win32') {
+ cp.execSync(`taskkill /pid ${pid} /T /F`);
+ } else {
+ killTree(pidTree({ pid, ppid: pid }));
+ }
+}
+
+function pidTree(tree: PidTree) {
+ const command =
+ process.platform === 'darwin'
+ ? `pgrep -P ${tree.pid}` // Mac
+ : `ps -o pid --no-headers --ppid ${tree.ppid}`; // Linux
+
+ try {
+ const childs = cp
+ .execSync(command, { encoding: 'utf8' })
+ .match(/\d+/g)
+ ?.map((id) => +id);
+
+ if (childs) {
+ tree.children = childs.map((cid) => pidTree({ pid: cid, ppid: tree.pid }));
+ }
+ } catch {
+ // Do nothing
+ }
+
+ return tree;
+}
+
+function killTree(tree: PidTree) {
+ if (tree.children) {
+ for (const child of tree.children) {
+ killTree(child);
+ }
+ }
+
+ try {
+ process.kill(tree.pid); // #214
+ } catch {
+ /* empty */
+ }
+}