mirror of
https://github.com/Pandipipas/scoreko-electron-dev.git
synced 2026-06-05 21:22:07 +00:00
35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
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;
|
|
}
|