fix: full axios migration in handler

This commit is contained in:
Artemy 2023-06-21 17:11:56 +03:00
parent 6cce71b998
commit 523ea1e7ce

View file

@ -1,6 +1,5 @@
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const request = require("request");
const checksum = require("checksum"); const checksum = require("checksum");
const Zip = require("adm-zip"); const Zip = require("adm-zip");
const child = require("child_process"); const child = require("child_process");
@ -136,7 +135,7 @@ class Handler {
} }
getVersion() { getVersion() {
return new Promise((resolve) => { return new Promise(async (resolve) => {
const versionJsonPath = const versionJsonPath =
this.options.overrides.versionJson || this.options.overrides.versionJson ||
path.join( path.join(
@ -152,44 +151,45 @@ class Handler {
const cache = this.options.cache const cache = this.options.cache
? `${this.options.cache}/json` ? `${this.options.cache}/json`
: `${this.options.root}/cache/json`; : `${this.options.root}/cache/json`;
request.get(manifest, (error, response, body) => {
if (error && error.code !== "ENOTFOUND") return resolve(error); let parsed;
if (!error) { try {
let response = await axios.get(manifest);
if (!fs.existsSync(cache)) { if (!fs.existsSync(cache)) {
fs.mkdirSync(cache, { recursive: true }); fs.mkdirSync(cache, { recursive: true });
this.client.emit("debug", "[PiMi]: Cache directory created."); this.client.emit("debug", "[PiMi]: Cache directory created.");
} }
fs.writeFile( fs.writeFile(
path.join(`${cache}/version_manifest.json`), path.join(`${cache}/version_manifest.json`),
body, JSON.stringify(response.data),
(err) => { (err) => {
if (err) return resolve(err); if (err) return resolve(err);
this.client.emit("debug", "[PiMi]: Cached version_manifest.json"); this.client.emit("debug", "[PiMi]: Cached version_manifest.json");
} }
); );
}
let parsed; parsed = response.data;
if (error && error.code === "ENOTFOUND") { } catch (error) {
if (error.code !== "ENOTFOUND") {
return resolve(error);
} else {
parsed = JSON.parse( parsed = JSON.parse(
fs.readFileSync(`${cache}/version_manifest.json`) fs.readFileSync(`${cache}/version_manifest.json`)
); );
} else { }
parsed = JSON.parse(body);
} }
for (const desiredVersion in parsed.versions) { for (const desiredVersion in parsed.versions) {
if ( if (
parsed.versions[desiredVersion].id === this.options.version.number parsed.versions[desiredVersion].id === this.options.version.number
) { ) {
request.get( try {
parsed.versions[desiredVersion].url, let response = await axios.get(parsed.versions[desiredVersion].url);
(error, response, body) => {
if (error && error.code !== "ENOTFOUND") return resolve(error);
if (!error) {
fs.writeFile( fs.writeFile(
path.join(`${cache}/${this.options.version.number}.json`), path.join(`${cache}/${this.options.version.number}.json`),
body, JSON.stringify(response.data),
(err) => { (err) => {
if (err) return resolve(err); if (err) return resolve(err);
this.client.emit( this.client.emit(
@ -198,27 +198,30 @@ class Handler {
); );
} }
); );
}
this.client.emit( this.client.emit(
"debug", "debug",
"[PiMi]: Parsed version from version manifest" "[PiMi]: Parsed version from version manifest"
); );
if (error && error.code === "ENOTFOUND") {
this.version = JSON.parse( this.version = response.data;
fs.readFileSync( } catch (error) {
`${cache}/${this.options.version.number}.json` if (error.code !== "ENOTFOUND") return resolve(error);
) else {
this.client.emit(
"debug",
"[PiMi]: Parsed version from version manifest"
);
this.version = JSON.parse(
fs.readFileSync(`${cache}/${this.options.version.number}.json`)
); );
} else {
this.version = JSON.parse(body);
} }
}
return resolve(this.version); return resolve(this.version);
} }
);
} }
}
});
}); });
} }