Preview fix #3

Merged
Sirherobrine23 merged 2 commits from testing into main 2022-11-18 00:25:21 +00:00
2 changed files with 58 additions and 53 deletions

View File

@ -3,6 +3,7 @@ import { httpRequest, httpRequestLarge } from "@the-bds-maneger/core-utils";
export type bedrockSchema = { export type bedrockSchema = {
version: string, version: string,
date: Date, date: Date,
release?: "stable"|"preview",
url: { url: {
[platform in NodeJS.Platform]?: { [platform in NodeJS.Platform]?: {
[arch in NodeJS.Architecture]?: string [arch in NodeJS.Architecture]?: string
@ -10,38 +11,40 @@ export type bedrockSchema = {
} }
}; };
export async function find(): Promise<bedrockSchema|void> { export async function find() {
const minecraftUrls = (await httpRequest.urls("https://www.minecraft.net/en-us/download/server/bedrock")).filter(Link => /bin-.*\.zip/.test(Link) && !(/preview/).test(Link)); const minecraftUrls = (await httpRequest.urls("https://www.minecraft.net/en-us/download/server/bedrock")).filter(Link => /bin-.*\.zip/.test(Link));
const objURLs = minecraftUrls.reduce((mount, url) => { const objURLs = minecraftUrls.reduce((mount, url) => {
// get version
let mcpeVersion = url;
const regReplace = /((^http[s]:\/\/[a-z\.]+\/bin-[a-zA-Z0-9\-]+\/)|(\.zip$)|([a-zA-Z\-]+))/;
while (regReplace.test(mcpeVersion)) mcpeVersion = mcpeVersion.replace(regReplace, "");
if (!mcpeVersion) return mount;
if (!mount[mcpeVersion]) mount[mcpeVersion] = {};
if (/darwin/.test(url)) { if (/darwin/.test(url)) {
if (!mount.darwin) mount.darwin = {}; if (!mount[mcpeVersion].darwin) mount[mcpeVersion].darwin = {};
if (/aarch|arm64/.test(url)) mount.darwin.arm64 = url; if (/aarch|arm64/.test(url)) mount[mcpeVersion].darwin.arm64 = url;
else mount.darwin.x64 = url else mount[mcpeVersion].darwin.x64 = url
} else if (/linux/.test(url)) { } else if (/linux/.test(url)) {
if (!mount.linux) mount.linux = {}; if (!mount[mcpeVersion].linux) mount[mcpeVersion].linux = {};
if (/aarch|arm64/.test(url)) mount.linux.arm64 = url; if (/aarch|arm64/.test(url)) mount[mcpeVersion].linux.arm64 = url;
else mount.linux.x64 = url else mount[mcpeVersion].linux.x64 = url
} else { } else {
if (!mount.win32) mount.win32 = {}; if (!mount[mcpeVersion].win32) mount[mcpeVersion].win32 = {};
if (/aarch|arm64/.test(url)) mount.win32.arm64 = url; if (/aarch|arm64/.test(url)) mount[mcpeVersion].win32.arm64 = url;
else mount.win32.x64 = url else mount[mcpeVersion].win32.x64 = url
} }
return mount; return mount;
}, {} as bedrockSchema["url"]); }, {} as {version: bedrockSchema["url"]});
const data: bedrockSchema[] = [];
// Object file for (const version of Object.keys(objURLs)) {
const anyZip = objURLs.win32?.x64||objURLs.linux?.x64; const versionData = objURLs[version];
if (!anyZip) throw new Error("cannot get url"); const fistUrl = versionData[Object.keys(versionData).at(0)][Object.keys(versionData[Object.keys(versionData).at(0)]).at(0)];
data.push({
// get version version: version,
let mcpeVersion = anyZip; date: (((await httpRequestLarge.zipDownload(fistUrl)).getEntries()).find(file => file.entryName.startsWith("bedrock_server")))?.header?.time||new Date(),
const regReplace = /((^http[s]:\/\/[a-z\.]+\/bin-[a-zA-Z0-9]+\/)|(\.zip$)|([a-zA-Z\-]+))/; release: ((/preview/).test(fistUrl))?"preview":"stable",
while (regReplace.test(mcpeVersion)) mcpeVersion = mcpeVersion.replace(regReplace, ""); url: versionData,
if (!mcpeVersion) throw new Error("No version found"); });
}
return { return data;
version: mcpeVersion,
date: (((await httpRequestLarge.zipDownload(anyZip)).getEntries()).find(file => file.entryName.startsWith("bedrock_server")))?.header?.time||new Date(),
url: objURLs
};
} }

View File

@ -8,13 +8,15 @@ import { createReadStream } from "node:fs";
import path from "node:path"; import path from "node:path";
const allPath = path.join(__dirname, "../versions/all.json"); const allPath = path.join(__dirname, "../versions/all.json");
async function createRelease(tagName: string, secret: string = process.env.GITHUB_SECRET||process.env.GITHUB_TOKEN) { const secret = process.env.GITHUB_SECRET||process.env.GITHUB_TOKEN;
async function createRelease(options: {tagName: string, secret?: string, prerelease?: boolean}) {
options = {secret, prerelease: false, ...options};
const octokit = getOctokit(secret); const octokit = getOctokit(secret);
const releases = (await octokit.rest.repos.listReleases({owner: "The-Bds-Maneger", repo: "BedrockFetch"})).data; const releases = (await octokit.rest.repos.listReleases({owner: "The-Bds-Maneger", repo: "BedrockFetch"})).data;
let release = releases.find(release => release.tag_name === tagName); let release = releases.find(release => release.tag_name === options.tagName);
if (!release) { if (!release) {
console.info("Creating relase!"); console.info("Creating relase!");
release = (await octokit.rest.repos.createRelease({owner: "The-Bds-Maneger", repo: "BedrockFetch", tag_name: tagName})).data; release = (await octokit.rest.repos.createRelease({owner: "The-Bds-Maneger", repo: "BedrockFetch", tag_name: options.tagName, prerelease: options.prerelease||false})).data;
} }
async function uploadFile(filePath: string, name: string = path.basename(filePath)) { async function uploadFile(filePath: string, name: string = path.basename(filePath)) {
const fileExists = (await octokit.rest.repos.listReleaseAssets({release_id: release.id, owner: "The-Bds-Maneger", repo: "BedrockFetch"})).data.find(file => file.name === name); const fileExists = (await octokit.rest.repos.listReleaseAssets({release_id: release.id, owner: "The-Bds-Maneger", repo: "BedrockFetch"})).data.find(file => file.name === name);
@ -48,31 +50,31 @@ async function createRelease(tagName: string, secret: string = process.env.GITHU
main().then(console.log); main().then(console.log);
async function main() { async function main() {
const all: bedrockSchema[] = JSON.parse(await fs.readFile(allPath, "utf8")); const all: bedrockSchema[] = JSON.parse(await fs.readFile(allPath, "utf8"));
const data = await find(); const findData = await find();
if (!data) return null; for (const data of findData) {
// Add to all // Add to all
if (!all.some(version => version.version === data.version)) { if (!all.some(version => version.version === data.version)) {
// Write env // Write env
if (process.env.GITHUB_ENV) { if (process.env.GITHUB_ENV) {
const githubEnv = path.resolve(process.env.GITHUB_ENV); const githubEnv = path.resolve(process.env.GITHUB_ENV);
await fs.writeFile(githubEnv, `VERSION=${data.version}\nUPLOAD=true`); await fs.writeFile(githubEnv, `VERSION=${data.version}\nUPLOAD=true`);
// 'downloadFiles' path // 'downloadFiles' path
const onSave = path.resolve(__dirname, "../downloadFiles"); const onSave = path.resolve(__dirname, "../downloadFiles");
if (!await extendFs.exists(onSave)) await fs.mkdir(onSave, {recursive: true}); if (!await extendFs.exists(onSave)) await fs.mkdir(onSave, {recursive: true});
const rel = await createRelease(data.version); const rel = await createRelease({tagName: data.version, prerelease: data.release === "preview"});
for (const platform of Object.keys(data.url)) { for (const platform of Object.keys(data.url)) {
for (const keyName of Object.keys(data.url[platform])) { for (const keyName of Object.keys(data.url[platform])) {
const downloadData = {url: data.url[platform][keyName], name: `${platform}_${keyName}_${path.basename((new URL(data.url[platform][keyName])).pathname)}`}; const downloadData = {url: data.url[platform][keyName], name: `${platform}_${keyName}_${path.basename((new URL(data.url[platform][keyName])).pathname)}`};
const filePath = await httpRequestLarge.saveFile({url: downloadData.url, filePath: path.join(onSave, downloadData.name)}); const filePath = await httpRequestLarge.saveFile({url: downloadData.url, filePath: path.join(onSave, downloadData.name)});
data.url[platform][keyName] = (await rel.uploadFile(filePath)).browser_download_url; data.url[platform][keyName] = (await rel.uploadFile(filePath)).browser_download_url;
}
} }
} }
all.push(data);
} }
const filePath = path.join(__dirname, "../versions", `${data.version}.json`);
all.push(data); await fs.writeFile(filePath, JSON.stringify(data, null, 2));
} }
const filePath = path.join(__dirname, "../versions", `${data.version}.json`);
await fs.writeFile(filePath, JSON.stringify(data, null, 2));
await fs.writeFile(allPath, JSON.stringify(all.sort((a, b) => compareVersions(a.version, b.version)), null, 2)); await fs.writeFile(allPath, JSON.stringify(all.sort((a, b) => compareVersions(a.version, b.version)), null, 2));
return data; return findData;
} }