Added downloading mods from Modrinth

This commit is contained in:
Kallum Jones
2022-08-03 15:00:36 +01:00
parent 7774386385
commit 765920f80c
14 changed files with 681 additions and 11 deletions

8
src/io/download_task.d.ts vendored Normal file
View File

@ -0,0 +1,8 @@
declare global {
type DownloadTask = {
fileName: string,
url: string
}
}
export {}

21
src/io/file_downloder.ts Normal file
View File

@ -0,0 +1,21 @@
import path from "path";
import * as https from "https";
import {createWriteStream} from "fs";
import DownloadError from "../errors/download_error.js";
export default class FileDownloader {
private static readonly MODS_FOLDER_PATH: string = path.join("mods");
static downloadMod(task: DownloadTask): void {
https.get(task.url, res => {
const filePath = path.join(this.MODS_FOLDER_PATH, task.fileName);
const writeStream = createWriteStream(filePath);
res.pipe(writeStream);
writeStream.on("finish", () => writeStream.close());
writeStream.on('error', () => {
throw new DownloadError(`Failed to download ${task.fileName} from ${task.url}`)
})
})
}
}