Skip to content

Commit

Permalink
Change download script structure
Browse files Browse the repository at this point in the history
  • Loading branch information
kristiandrex committed Jul 15, 2021
1 parent 0b8adea commit b2ee3f7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
1 change: 0 additions & 1 deletion public/data/11.14.1.json

This file was deleted.

53 changes: 30 additions & 23 deletions scripts/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,51 @@ const fetch = require("node-fetch");
const core = require("@actions/core");

async function getLatestVersion() {
const VERSIONS_URL = "https://ddragon.leagueoflegends.com/api/versions.json";
const response = await fetch(VERSIONS_URL);
const url = "https://ddragon.leagueoflegends.com/api/versions.json";
const response = await fetch(url);
const versions = await response.json();
return versions[0];
}

getLatestVersion()
.then(async (latestVersion) => {
const filename = `${latestVersion}.json`;
if (!fs.existsSync("public/data/version.txt")) {
return await download(latestVersion);
}

const currentVersion = fs.readFileSync(version).toString();

if (fs.existsSync(`public/data/${filename}`)) {
if (latestVersion === currentVersion) {
core.setOutput("shouldUpdate ", false);
console.log(`Version ${latestVersion} it's already downloaded.`);
return;
}

console.log(`Downloading version ${latestVersion}...`);
await download(latestVersion);
})
.catch((error) => {
console.error("There was an error");
console.error(error);
});

const url = `http://ddragon.leagueoflegends.com/cdn/${latestVersion}/data/es_MX/champion.json`;
const response = await fetch(url);
const json = await response.json();
async function download(version) {
console.log(`Downloading version ${version}...`);

const data = JSON.stringify({
version: json.version,
champions: Object.values(json.data)
});
const url = `http://ddragon.leagueoflegends.com/cdn/${version}/data/es_MX/champion.json`;
const response = await fetch(url);
const json = await response.json();

await fs.promises.writeFile(`public/data/${filename}`, data);
const data = JSON.stringify({
version,
champions: Object.values(json.data)
});

await fs.promises.copyFile(
`public/data/${filename}`,
"public/data/latest.json"
);
await fs.promises.writeFile("public/data/latest.json", data);
await fs.promises.writeFile("public/data/version.txt", version);

// Adding latest version to Github Actions output.
core.setOutput("latestVersion", latestVersion);
core.setOutput("shouldUpdate", true);
// Adding latest version to Github Actions output.
core.setOutput("latestVersion", version);
core.setOutput("shouldUpdate", true);

console.log(`Version ${latestVersion} successfully downloaded.`);
})
.catch((error) => console.error(error));
console.log(`Version ${version} successfully downloaded.`);
}

1 comment on commit b2ee3f7

@vercel
Copy link

@vercel vercel bot commented on b2ee3f7 Jul 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.