diff --git a/src/find.ts b/src/find.ts index 66bf80c..1c2cfa7 100644 --- a/src/find.ts +++ b/src/find.ts @@ -3,6 +3,7 @@ import { httpRequest, httpRequestLarge } from "@the-bds-maneger/core-utils"; export type bedrockSchema = { version: string, date: Date, + release?: "stable"|"preview", url: { [platform in NodeJS.Platform]?: { [arch in NodeJS.Architecture]?: string @@ -10,38 +11,40 @@ export type bedrockSchema = { } }; -export async function find(): Promise { - const minecraftUrls = (await httpRequest.urls("https://www.minecraft.net/en-us/download/server/bedrock")).filter(Link => /bin-.*\.zip/.test(Link) && !(/preview/).test(Link)); +export async function find() { + 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) => { + // 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 (!mount.darwin) mount.darwin = {}; - if (/aarch|arm64/.test(url)) mount.darwin.arm64 = url; - else mount.darwin.x64 = url + if (!mount[mcpeVersion].darwin) mount[mcpeVersion].darwin = {}; + if (/aarch|arm64/.test(url)) mount[mcpeVersion].darwin.arm64 = url; + else mount[mcpeVersion].darwin.x64 = url } else if (/linux/.test(url)) { - if (!mount.linux) mount.linux = {}; - if (/aarch|arm64/.test(url)) mount.linux.arm64 = url; - else mount.linux.x64 = url + if (!mount[mcpeVersion].linux) mount[mcpeVersion].linux = {}; + if (/aarch|arm64/.test(url)) mount[mcpeVersion].linux.arm64 = url; + else mount[mcpeVersion].linux.x64 = url } else { - if (!mount.win32) mount.win32 = {}; - if (/aarch|arm64/.test(url)) mount.win32.arm64 = url; - else mount.win32.x64 = url + if (!mount[mcpeVersion].win32) mount[mcpeVersion].win32 = {}; + if (/aarch|arm64/.test(url)) mount[mcpeVersion].win32.arm64 = url; + else mount[mcpeVersion].win32.x64 = url } return mount; - }, {} as bedrockSchema["url"]); - - // Object file - const anyZip = objURLs.win32?.x64||objURLs.linux?.x64; - if (!anyZip) throw new Error("cannot get url"); - - // get version - let mcpeVersion = anyZip; - 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) throw new Error("No version found"); - - return { - version: mcpeVersion, - date: (((await httpRequestLarge.zipDownload(anyZip)).getEntries()).find(file => file.entryName.startsWith("bedrock_server")))?.header?.time||new Date(), - url: objURLs - }; + }, {} as {version: bedrockSchema["url"]}); + const data: bedrockSchema[] = []; + for (const version of Object.keys(objURLs)) { + const versionData = objURLs[version]; + const fistUrl = versionData[Object.keys(versionData).at(0)][Object.keys(versionData[Object.keys(versionData).at(0)]).at(0)]; + data.push({ + version: version, + date: (((await httpRequestLarge.zipDownload(fistUrl)).getEntries()).find(file => file.entryName.startsWith("bedrock_server")))?.header?.time||new Date(), + release: ((/preview/).test(fistUrl))?"preview":"stable", + url: versionData, + }); + } + return data; } \ No newline at end of file diff --git a/src/update.ts b/src/update.ts index 60cc83b..d502bea 100644 --- a/src/update.ts +++ b/src/update.ts @@ -8,13 +8,15 @@ import { createReadStream } from "node:fs"; import path from "node:path"; 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 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) { 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)) { 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); async function main() { const all: bedrockSchema[] = JSON.parse(await fs.readFile(allPath, "utf8")); - const data = await find(); - if (!data) return null; - // Add to all - if (!all.some(version => version.version === data.version)) { - // Write env - if (process.env.GITHUB_ENV) { - const githubEnv = path.resolve(process.env.GITHUB_ENV); - await fs.writeFile(githubEnv, `VERSION=${data.version}\nUPLOAD=true`); - // 'downloadFiles' path - const onSave = path.resolve(__dirname, "../downloadFiles"); - if (!await extendFs.exists(onSave)) await fs.mkdir(onSave, {recursive: true}); - const rel = await createRelease(data.version); - for (const platform of Object.keys(data.url)) { - 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 filePath = await httpRequestLarge.saveFile({url: downloadData.url, filePath: path.join(onSave, downloadData.name)}); - data.url[platform][keyName] = (await rel.uploadFile(filePath)).browser_download_url; + const findData = await find(); + for (const data of findData) { + // Add to all + if (!all.some(version => version.version === data.version)) { + // Write env + if (process.env.GITHUB_ENV) { + const githubEnv = path.resolve(process.env.GITHUB_ENV); + await fs.writeFile(githubEnv, `VERSION=${data.version}\nUPLOAD=true`); + // 'downloadFiles' path + const onSave = path.resolve(__dirname, "../downloadFiles"); + if (!await extendFs.exists(onSave)) await fs.mkdir(onSave, {recursive: true}); + const rel = await createRelease({tagName: data.version, prerelease: data.release === "preview"}); + for (const platform of Object.keys(data.url)) { + 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 filePath = await httpRequestLarge.saveFile({url: downloadData.url, filePath: path.join(onSave, downloadData.name)}); + data.url[platform][keyName] = (await rel.uploadFile(filePath)).browser_download_url; + } } } + all.push(data); } - - all.push(data); + const filePath = path.join(__dirname, "../versions", `${data.version}.json`); + 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)); - return data; + return findData; } \ No newline at end of file