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,73 +151,77 @@ 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);
if (!error) {
if (!fs.existsSync(cache)) {
fs.mkdirSync(cache, { recursive: true });
this.client.emit("debug", "[PiMi]: Cache directory created.");
}
fs.writeFile(
path.join(`${cache}/version_manifest.json`),
body,
(err) => {
if (err) return resolve(err);
this.client.emit("debug", "[PiMi]: Cached version_manifest.json");
}
);
}
let parsed; let parsed;
if (error && error.code === "ENOTFOUND") { try {
let response = await axios.get(manifest);
if (!fs.existsSync(cache)) {
fs.mkdirSync(cache, { recursive: true });
this.client.emit("debug", "[PiMi]: Cache directory created.");
}
fs.writeFile(
path.join(`${cache}/version_manifest.json`),
JSON.stringify(response.data),
(err) => {
if (err) return resolve(err);
this.client.emit("debug", "[PiMi]: Cached version_manifest.json");
}
);
parsed = response.data;
} 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(
path.join(`${cache}/${this.options.version.number}.json`),
body,
(err) => {
if (err) return resolve(err);
this.client.emit(
"debug",
`[PiMi]: Cached ${this.options.version.number}.json`
);
}
);
}
fs.writeFile(
path.join(`${cache}/${this.options.version.number}.json`),
JSON.stringify(response.data),
(err) => {
if (err) return resolve(err);
this.client.emit( this.client.emit(
"debug", "debug",
"[PiMi]: Parsed version from version manifest" `[PiMi]: Cached ${this.options.version.number}.json`
); );
if (error && error.code === "ENOTFOUND") {
this.version = JSON.parse(
fs.readFileSync(
`${cache}/${this.options.version.number}.json`
)
);
} else {
this.version = JSON.parse(body);
}
return resolve(this.version);
} }
); );
this.client.emit(
"debug",
"[PiMi]: Parsed version from version manifest"
);
this.version = response.data;
} catch (error) {
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`)
);
}
} }
return resolve(this.version);
} }
}); }
}); });
} }