feat: add a function to download libraries and its test
This commit is contained in:
parent
aaad782501
commit
58fe602f14
4 changed files with 55 additions and 0 deletions
|
|
@ -1,3 +1,4 @@
|
|||
export * as java from "./java";
|
||||
export * as version from "./version";
|
||||
export * as natives from "./natives";
|
||||
export * as libraries from "./libraries";
|
||||
|
|
|
|||
20
src/core/libraries.ts
Normal file
20
src/core/libraries.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import path from "path";
|
||||
import { parseRule } from "../utils/rules";
|
||||
import { Library, PoolFile, Version } from "../utils/types";
|
||||
import { DownloadPool } from "../utils/fetch";
|
||||
|
||||
export async function download(destination: string, versionManifest: Version) {
|
||||
// Download libraries
|
||||
const files = versionManifest.libraries.map<PoolFile>((library: Library) => {
|
||||
if (!parseRule(library)) {
|
||||
return {
|
||||
url: library.downloads.artifact.url,
|
||||
path: path.join(destination, library.downloads.artifact.path),
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
const dPool = new DownloadPool(files, 5);
|
||||
|
||||
await dPool.run();
|
||||
}
|
||||
|
|
@ -2,6 +2,8 @@ import fs from "fs";
|
|||
import { pipeline } from "stream/promises";
|
||||
import { PoolFile } from "./types";
|
||||
import { Task } from "./task";
|
||||
import { ensureDir } from "./fs";
|
||||
import path from "path";
|
||||
|
||||
export async function fetchJson<T>(url: string): Promise<T> {
|
||||
return await (await fetch(url)).json();
|
||||
|
|
@ -13,6 +15,7 @@ export async function downloadFile(
|
|||
) {
|
||||
try {
|
||||
if (!file) return;
|
||||
await ensureDir(path.dirname(file.path), true);
|
||||
const res = await fetch(file.url, { signal });
|
||||
|
||||
if (!res.ok)
|
||||
|
|
|
|||
31
tests/libraries.test.ts
Normal file
31
tests/libraries.test.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { expect, test } from "vitest";
|
||||
import * as nlk from "../dist/index.js";
|
||||
import path from "path";
|
||||
import fs from "fs/promises";
|
||||
|
||||
const librariesPath = path.join(import.meta.dirname, "temp/libraries");
|
||||
await fs.rm(librariesPath, { recursive: true, force: true });
|
||||
await fs.mkdir(librariesPath, { recursive: true });
|
||||
|
||||
async function exists(path: string) {
|
||||
try {
|
||||
await fs.access(path, fs.constants.F_OK);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
test(
|
||||
"download libraries for 1.21.8 correctly",
|
||||
{ timeout: 10000 },
|
||||
async () => {
|
||||
const versionManifest = await nlk.core.version.getVersionManifest("1.21.8");
|
||||
await nlk.core.libraries.download(librariesPath, versionManifest);
|
||||
|
||||
expect(
|
||||
await exists(path.join(librariesPath, "com/fasterxml/jackson/core/jackson-core/2.13.4/jackson-core-2.13.4.jar")),
|
||||
"com/fasterxml/jackson/core/jackson-core/2.13.4/jackson-core-2.13.4.jar exists",
|
||||
).toBe(true);
|
||||
},
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue