-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdateManager.js
More file actions
139 lines (122 loc) · 4.49 KB
/
updateManager.js
File metadata and controls
139 lines (122 loc) · 4.49 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
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
const { dialog, app, shell } = require("electron");
const https = require("https");
const path = require("path");
const { exec } = require("child_process");
const GITHUB_OWNER = "kabir-coderex";
const GITHUB_REPO = "CopyList";
const CURRENT_VERSION = app?.getVersion() || "1.0.0"; // Default version if app is not defined
async function getLatestRelease() {
return new Promise((resolve, reject) => {
https.get(
`https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/releases/latest`,
{ headers: { "User-Agent": "Electron-App" } },
(res) => {
let data = "";
res.on("data", (chunk) => (data += chunk));
res.on("end", () => resolve(JSON.parse(data)));
}
).on("error", reject);
});
}
function forceQuitApp(appName = "Copy List") {
return new Promise((resolve, reject) => {
exec(`ps -ax | grep "${appName}.app" | grep -v grep`, (err, stdout) => {
if (err || !stdout) {
return reject("App not running or could not find PID.");
}
const lines = stdout.trim().split("\n");
const pids = lines.map(line => line.trim().split(/\s+/)[0]);
if (pids.length === 0) {
return reject("PID not found.");
}
// Kill all matched PIDs
exec(`kill -9 ${pids.join(" ")}`, (killErr) => {
if (killErr) {
return reject("Failed to kill the app.");
}
resolve("App killed successfully.");
});
});
});
}
async function checkForUpdates() {
try {
const latestRelease = await getLatestRelease();
const latestVersion = latestRelease.tag_name.replace("v", "");
if (latestVersion === CURRENT_VERSION) {
dialog.showMessageBox({
type: "info",
title: "Update Check",
message: "You are already using the latest version.",
});
return;
}
const updateAsset = latestRelease.assets.find(asset =>
asset.name.endsWith("-universal.dmg")
);
if (!updateAsset) {
dialog.showMessageBox({
type: "error",
title: "Update Error",
message: "No valid update file found.",
});
return;
}
const updateUrl = updateAsset.browser_download_url;
const downloadPath = path.join(app.getPath("downloads"), updateAsset.name);
dialog.showMessageBox({
type: "info",
title: "Update Available",
message: `New version ${latestVersion} is available. Do you want to update now?`,
buttons: ["Yes", "No"],
}).then(async (result) => {
if (result.response === 0) {
// Step 1: Download file using curl
const curlCommand = `curl -L --fail --silent --show-error --output "${downloadPath}" "${updateUrl}"`;
exec(curlCommand, async (err) => {
if (err) {
dialog.showErrorBox("Download Failed", "Unable to download update file.");
return;
}
// Step 2: Start fswatch observer in Terminal
const monitorScript = `
osascript -e 'tell application "Terminal"
do script "fswatch -0 /Applications/Copy\\\\ List.app | while IFS= read -r -d \\"\\" event; do
echo \\"Copy List.app has been updated or replaced.\\"
xattr -cr /Applications/Copy\\\\ List.app
echo \\"xattr command executed. Stopping observer.\\"
osascript -e \\"tell application \\\\\\"Terminal\\\\\\" to close (every window whose name contains \\\\\\"monitor\\\\\\")\\" 2>/dev/null
break
done"
set custom title of front window to "monitor"
end tell'
`;
exec(monitorScript);
// Step 3: Show downloaded file and open DMG
shell.showItemInFolder(downloadPath);
exec(`open "${downloadPath}"`);
// Step 4: Force kill the app
try {
const result = await forceQuitApp("Copy List");
console.log(result);
} catch (quitErr) {
console.error("Force quit failed:", quitErr);
dialog.showMessageBox({
type: "warning",
title: "Quit App Manually",
message: "Could not quit Copy List automatically. Please quit it manually before installing the update.",
});
}
});
}
});
} catch (error) {
console.error("Update Check Failed:", error);
dialog.showMessageBox({
type: "error",
title: "Update Error",
message: "Could not check for updates. Please try again later.",
});
}
}
module.exports = { checkForUpdates };