Moved File to file.ts, made version-utils.ts

This commit is contained in:
Kir_Antipov 2021-12-06 16:07:13 +03:00
parent ffa0520996
commit 07fccebe08
12 changed files with 83 additions and 84 deletions

View file

@ -1,4 +1,4 @@
import { File } from "../../utils/file-utils";
import { File } from "../../utils/file";
import ModPublisher from "../mod-publisher";
import PublisherTarget from "../publisher-target";
import { convertToCurseForgeVersions, uploadFile } from "../../utils/curseforge-utils";

View file

@ -1,7 +1,7 @@
import Publisher from "../publisher";
import PublisherTarget from "../publisher-target";
import * as github from "@actions/github";
import { File } from "../../utils/file-utils";
import { File } from "../../utils/file";
interface GitHubPublisherOptions {
tag?: string;

View file

@ -1,9 +1,10 @@
import { context } from "@actions/github";
import { parseVersionNameFromFileVersion } from "../utils/minecraft-utils";
import { File, getFiles, parseVersionFromFilename, parseVersionTypeFromFilename } from "../utils/file-utils";
import { File } from "../utils/file";
import { getFiles } from "../utils/file-utils";
import Publisher from "./publisher";
import PublisherTarget from "./publisher-target";
import MinecraftVersionResolver from "../utils/minecraft-version-resolver";
import { parseVersionFromName, parseVersionTypeFromName } from "../utils/version-utils";
interface ModPublisherOptions {
id: string;

View file

@ -1,5 +1,5 @@
import { createVersion } from "../../utils/modrinth-utils";
import { File } from "../../utils/file-utils";
import { File } from "../../utils/file";
import ModPublisher from "../mod-publisher";
import PublisherTarget from "../publisher-target";

View file

@ -1,4 +1,4 @@
import { File } from "utils/file-utils";
import { File } from "../utils/file";
import Logger from "../utils/logger";
import { getEmptyLogger } from "../utils/logger-utils";
import PublisherTarget from "./publisher-target";

View file

@ -1,7 +1,7 @@
import fetch from "node-fetch";
import { FormData } from "formdata-node";
import { fileFromPath } from "formdata-node/file-from-path";
import { File } from "../utils/file-utils";
import { File } from "./file";
import { findVersionByName } from "./minecraft-utils";
const baseUrl = "https://minecraft.curseforge.com/api";

View file

@ -1,33 +1,5 @@
import fs from "fs";
import path from "path";
import glob from "fast-glob";
export class File {
public name: string;
public path: string;
public constructor(filePath: string) {
this.name = path.basename(filePath);
this.path = filePath;
Object.freeze(this);
}
public async getBuffer(): Promise<Buffer> {
return new Promise((resolve, reject) => {
fs.readFile(this.path, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data);
}
})
});
}
public equals(file: unknown): boolean {
return file instanceof File && file.path === this.path;
}
}
import { File } from "./file";
export type FileSelector = string | { primary?: string, secondary?: string };
@ -62,20 +34,3 @@ export async function getFiles(files: FileSelector): Promise<File[]> {
}
return results.filter((x, i, self) => self.findIndex(y => x.equals(y)) === i);
}
export function parseVersionFromFilename(filename: string): string {
filename = path.parse(filename).name;
const match = filename.match(/[a-z]{0,2}\d+\.\d+.*/i);
return match ? match[0] : filename;
}
export function parseVersionTypeFromFilename(filename: string): "alpha" | "beta" | "release" {
filename = path.parse(filename).name;
if (filename.match(/[+-_]alpha/i)) {
return "alpha";
} else if (filename.match(/[+-_]beta/i)) {
return "beta";
} else {
return "release";
}
}

29
src/utils/file.ts Normal file
View file

@ -0,0 +1,29 @@
import fs from "fs";
import path from "path";
export class File {
public name: string;
public path: string;
public constructor(filePath: string) {
this.name = path.basename(filePath);
this.path = filePath;
Object.freeze(this);
}
public async getBuffer(): Promise<Buffer> {
return new Promise((resolve, reject) => {
fs.readFile(this.path, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data);
}
})
});
}
public equals(file: unknown): boolean {
return file instanceof File && file.path === this.path;
}
}

View file

@ -1,7 +1,7 @@
import { FormData } from "formdata-node";
import { fileFromPath } from "formdata-node/file-from-path";
import fetch from "node-fetch";
import { File } from "./file-utils";
import { File } from "./file";
export async function createVersion(id: string, data: Record<string, any>, files: File[], token: string): Promise<string> {
data = {

View file

@ -0,0 +1,14 @@
export function parseVersionFromName(name: string): string {
const match = name.match(/[a-z]{0,2}\d+\.\d+.*/i);
return match ? match[0] : name;
}
export function parseVersionTypeFromName(name: string): "alpha" | "beta" | "release" {
if (name.match(/[+-_]alpha/i)) {
return "alpha";
} else if (name.match(/[+-_]beta/i)) {
return "beta";
} else {
return "release";
}
}

View file

@ -1,5 +1,5 @@
import { describe, test, expect } from "@jest/globals";
import { getFiles, getRequiredFiles, parseVersionFromFilename, parseVersionTypeFromFilename } from "../src/utils/file-utils";
import { getFiles, getRequiredFiles } from "../src/utils/file-utils";
describe("getFiles", () => {
test("all files matching the given pattern are returned", async () => {
@ -26,32 +26,3 @@ describe("getRequiredFiles", () => {
await expect(getRequiredFiles("FOO.md")).rejects.toBeInstanceOf(Error);
});
});
describe("parseVersionFromFilename", () => {
test("file version is correctly extracted from the filename", () => {
expect(parseVersionFromFilename("sodium-fabric-mc1.17.1-0.3.2+build.7.jar")).toStrictEqual("mc1.17.1-0.3.2+build.7");
expect(parseVersionFromFilename("build/libs/sodium-fabric-mc1.17.1-0.3.2+build.7.jar")).toStrictEqual("mc1.17.1-0.3.2+build.7");
expect(parseVersionFromFilename("fabric-api-0.40.1+1.18_experimental.jar")).toStrictEqual("0.40.1+1.18_experimental");
expect(parseVersionFromFilename("TechReborn-5.0.8-beta+build.111.jar")).toStrictEqual("5.0.8-beta+build.111");
expect(parseVersionFromFilename("TechReborn-1.17-5.0.1-beta+build.29.jar")).toStrictEqual("1.17-5.0.1-beta+build.29");
expect(parseVersionFromFilename("Terra-forge-5.3.3-BETA+ec3b0e5d.jar")).toStrictEqual("5.3.3-BETA+ec3b0e5d");
expect(parseVersionFromFilename("modmenu-2.0.12.jar")).toStrictEqual("2.0.12");
expect(parseVersionFromFilename("enhancedblockentities-0.5+1.17.jar")).toStrictEqual("0.5+1.17");
expect(parseVersionFromFilename("sync-mc1.17.x-1.2.jar")).toStrictEqual("mc1.17.x-1.2");
});
});
describe("parseVersionTypeFromFilename", () => {
test("version type is correctly extracted from the filename", () => {
expect(parseVersionTypeFromFilename("sodium-fabric-mc1.17.1-0.3.2+build.7.jar")).toStrictEqual("release");
expect(parseVersionTypeFromFilename("build/libs/sodium-fabric-mc1.17.1-0.3.2+build.7.jar")).toStrictEqual("release");
expect(parseVersionTypeFromFilename("fabric-api-0.40.1+1.18_experimental.jar")).toStrictEqual("release");
expect(parseVersionTypeFromFilename("TechReborn-5.0.8-beta+build.111.jar")).toStrictEqual("beta");
expect(parseVersionTypeFromFilename("TechReborn-1.17-5.0.1-beta+build.29.jar")).toStrictEqual("beta");
expect(parseVersionTypeFromFilename("Terra-forge-5.3.3-BETA+ec3b0e5d.jar")).toStrictEqual("beta");
expect(parseVersionTypeFromFilename("Terra-forge-5.3.3-alpha+ec3b0e5d.jar")).toStrictEqual("alpha");
expect(parseVersionTypeFromFilename("modmenu-2.0.12.jar")).toStrictEqual("release");
expect(parseVersionTypeFromFilename("enhancedblockentities-0.5+1.17.jar")).toStrictEqual("release");
expect(parseVersionTypeFromFilename("sync-mc1.17.x-1.2.jar")).toStrictEqual("release");
});
});

View file

@ -0,0 +1,29 @@
import { describe, test, expect } from "@jest/globals";
import { parseVersionFromName, parseVersionTypeFromName } from "../src/utils/version-utils";
describe("parseVersionFromName", () => {
test("file version is correctly extracted from the filename", () => {
expect(parseVersionFromName("sodium-fabric-mc1.17.1-0.3.2+build.7")).toStrictEqual("mc1.17.1-0.3.2+build.7");
expect(parseVersionFromName("fabric-api-0.40.1+1.18_experimental")).toStrictEqual("0.40.1+1.18_experimental");
expect(parseVersionFromName("TechReborn-5.0.8-beta+build.111")).toStrictEqual("5.0.8-beta+build.111");
expect(parseVersionFromName("TechReborn-1.17-5.0.1-beta+build.29")).toStrictEqual("1.17-5.0.1-beta+build.29");
expect(parseVersionFromName("Terra-forge-5.3.3-BETA+ec3b0e5d")).toStrictEqual("5.3.3-BETA+ec3b0e5d");
expect(parseVersionFromName("modmenu-2.0.12")).toStrictEqual("2.0.12");
expect(parseVersionFromName("enhancedblockentities-0.5+1.17")).toStrictEqual("0.5+1.17");
expect(parseVersionFromName("sync-mc1.17.x-1.2")).toStrictEqual("mc1.17.x-1.2");
});
});
describe("parseVersionTypeFromName", () => {
test("version type is correctly extracted from the filename", () => {
expect(parseVersionTypeFromName("sodium-fabric-mc1.17.1-0.3.2+build.7")).toStrictEqual("release");
expect(parseVersionTypeFromName("fabric-api-0.40.1+1.18_experimental")).toStrictEqual("release");
expect(parseVersionTypeFromName("TechReborn-5.0.8-beta+build.111")).toStrictEqual("beta");
expect(parseVersionTypeFromName("TechReborn-1.17-5.0.1-beta+build.29")).toStrictEqual("beta");
expect(parseVersionTypeFromName("Terra-forge-5.3.3-BETA+ec3b0e5d")).toStrictEqual("beta");
expect(parseVersionTypeFromName("Terra-forge-5.3.3-alpha+ec3b0e5d")).toStrictEqual("alpha");
expect(parseVersionTypeFromName("modmenu-2.0.12")).toStrictEqual("release");
expect(parseVersionTypeFromName("enhancedblockentities-0.5+1.17")).toStrictEqual("release");
expect(parseVersionTypeFromName("sync-mc1.17.x-1.2")).toStrictEqual("release");
});
});