mirror of
https://github.com/Pandipipas/scoreko-electron-dev.git
synced 2026-06-06 05:32:06 +00:00
feat: Implement update management refactor with new dialog and settings handling
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { Readable } from "node:stream";
|
||||
|
||||
import { ReleaseUpdate, sanitizeFileName } from "./update-utils";
|
||||
|
||||
type UpdateDownloadConfig = {
|
||||
tempDirectory: string;
|
||||
};
|
||||
|
||||
export async function downloadInstaller(update: ReleaseUpdate, config: UpdateDownloadConfig): Promise<string> {
|
||||
const safeFileName = sanitizeFileName(update.installer.name);
|
||||
const downloadDirectory = path.join(config.tempDirectory, "scoreko-updates");
|
||||
const targetPath = path.join(downloadDirectory, safeFileName);
|
||||
|
||||
fs.mkdirSync(downloadDirectory, { recursive: true });
|
||||
|
||||
const response = await fetch(update.installer.downloadUrl);
|
||||
if (!response.ok || !response.body) {
|
||||
throw new Error(`Could not download update installer. HTTP ${response.status}.`);
|
||||
}
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const fileStream = fs.createWriteStream(targetPath);
|
||||
const responseStream = Readable.fromWeb(response.body as Parameters<typeof Readable.fromWeb>[0]);
|
||||
|
||||
responseStream.on("error", reject);
|
||||
fileStream.on("error", reject);
|
||||
fileStream.on("finish", resolve);
|
||||
responseStream.pipe(fileStream);
|
||||
});
|
||||
|
||||
return targetPath;
|
||||
}
|
||||
Reference in New Issue
Block a user