diff --git a/.editorconfig b/.editorconfig index cee4596c..78b66ce6 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,4 +8,4 @@ charset = utf-8 trim_trailing_whitespace = false insert_final_newline = true indent_style = space -indent_size = 2 +indent_size = 4 diff --git a/.gitignore b/.gitignore index 56965cab..b280f3e5 100644 --- a/.gitignore +++ b/.gitignore @@ -45,7 +45,4 @@ public/styles/css/* #Ignore environment .env -#Ignore link.json -link.json - public/js/*.js diff --git a/express.js b/express.js index b2fd2269..a0ce3900 100644 --- a/express.js +++ b/express.js @@ -29,7 +29,6 @@ process.env.SESSION_SECRET_KEY = process.env.SESSION_SECRET_KEY || '12345'; //Execute Middleware app.use(middleware.initLocals); -app.use(middleware.getLatestClientRelease); app.use(middleware.clientChecks); //Set static public directory path @@ -292,7 +291,7 @@ app.get(`/${process.env.CALLBACK}`, passport.authenticate('faforever', { }); // Run scripts initially on startup -let requireRunArray = ['extractor', 'getLatestClientRelease']; +let requireRunArray = ['extractor']; for (let i = 0; i < requireRunArray.length; i++) { try { require(`./scripts/${requireRunArray[i]}`).run(); diff --git a/grunt/nodemon.js b/grunt/nodemon.js index a6f772b7..1f1c55ef 100644 --- a/grunt/nodemon.js +++ b/grunt/nodemon.js @@ -7,7 +7,6 @@ module.exports = { 'node_modules/**', 'grunt/**', 'Gruntfile.js', - 'link.json', 'public/js/app/members/**' ] } diff --git a/package.json b/package.json index 4bed14cd..a33206e8 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,6 @@ "express": "^4.18.1", "express-session": "^1.17.3", "express-validator": "7.0.1", - "github-api": "3.4.0", "moment": "^2.29.4", "node-fetch": "^2.6.7", "npm-check": "^6.0.1", diff --git a/public/js/app/play.js b/public/js/app/play.js new file mode 100644 index 00000000..9f3a90cb --- /dev/null +++ b/public/js/app/play.js @@ -0,0 +1,54 @@ +import {Octokit} from "https://esm.sh/@octokit/core"; + +const githubOrg = 'faforever' +const githubRepository = 'downlords-faf-client' +const githubFallbackUrl = 'https://github.com/FAForever/downlords-faf-client/releases/latest'; +const downloadButtonId = 'faf-client-download' + +const startDownloadFile = (url) => window.location.assign(url) + +const openFallbackDownloadPage = () => open(githubFallbackUrl, '_blank') + +const getWindowsDownloadLink = (response) => { + let [exeAsset] = response?.data?.assets?.filter?.(function (asset) { + return asset.name?.includes?.('.exe') + }) ?? [] + + if (exeAsset) { + try { + new URL(exeAsset.browser_download_url ?? false) + + return exeAsset.browser_download_url + } catch (e) {} + } + + return false +} + +const onGithubResponse = (response) => { + const windowsDownloadLink = getWindowsDownloadLink(response) + + if (windowsDownloadLink) { + startDownloadFile(windowsDownloadLink) + + return + } + + openFallbackDownloadPage() +} + +const onDownloadButtonClicked = (event) => { + event.preventDefault() + const octokit = new Octokit() + + octokit + .request(`GET /repos/${githubOrg}/${githubRepository}/releases/latest`) + .then(onGithubResponse) + .catch(() => openFallbackDownloadPage()) +} + +const downloadButton = document.getElementById(downloadButtonId) + +if (downloadButton) { + downloadButton.addEventListener("click", onDownloadButtonClicked) +} diff --git a/routes/middleware.js b/routes/middleware.js index b0eda288..d6fec6d3 100755 --- a/routes/middleware.js +++ b/routes/middleware.js @@ -5,38 +5,13 @@ the navigation in the header, you may wish to change this array or replace it with your own templates / logic. */ -const express = require('express'); const fs = require('fs'); -const {exec} = require("child_process"); -const app = express(); exports.initLocals = function(req, res, next) { let locals = res.locals; locals.navLinks = []; locals.cNavLinks = []; next(); }; -exports.getLatestClientRelease = function(req, res, next) { - try { - let locals = res.locals; - const fs = require('fs'); - let clientLink; - let exec = require('child_process').exec; - - fs.readFile('link.json', 'utf8', function (err, data) { - try { - clientLink = JSON.parse(data); - } catch (e) { - clientLink = {}; - clientLink.fafClientLink = 'https://github.com/FAForever/downlords-faf-client/releases'; - } - locals.fafClientDownloadLink = clientLink.fafClientLink; - next(); - }); - } catch (e) { - console.log(e); - } - -}; exports.clientChecks = function(req, res, next) { let locals = res.locals; diff --git a/scripts/getLatestClientRelease.js b/scripts/getLatestClientRelease.js deleted file mode 100755 index 2f85c373..00000000 --- a/scripts/getLatestClientRelease.js +++ /dev/null @@ -1,60 +0,0 @@ -let moment = require('moment'); -let GitHub = require('github-api'); -let fs = require('fs'); -let gh = new GitHub(); -let clientLink; - -module.exports.run = function run() { - console.log(moment().format('DD-MM-YYYY - HH:mm:ss') + ' - Updating the client links...'); - function verifyOutput() - { - //Try to read the file after creating it - fs.readFile('link.json', 'utf8', function (err, data) { - try { - //Must of parsed out properly - clientLink = JSON.parse(data); - } catch (e) { - //Must not have... - console.log(moment().format('DD-MM-YYYY - HH:mm:ss') + ' - Link file incorrectly made. Data was - ' + data); - //Write default values to file... - data = { - fafClientLink: 'https://github.com/FAForever/downlords-faf-client/releases' - }; - fs.writeFile('link.json', JSON.stringify(data), function(error) { - if (error) { - console.log(error); - } else { - console.log(moment().format('DD-MM-YYYY - HH:mm:ss') + ' - Link file verified.'); - } - }); - } - }); - } - - //Get main client - gh.getRepo('faforever', 'downlords-faf-client').getRelease('latest', function(err, release) { - if (!err) { - let exeAsset = release.assets.filter(function (asset) { - return asset.name.includes('.exe'); - })[0]; - - let data = { - fafClientLink: exeAsset.browser_download_url - }; - - //Write to file - fs.writeFile('link.json', JSON.stringify(data), function(err) { - if (err) { - console.log(err); - verifyOutput(); - } else { - console.log(moment().format('DD-MM-YYYY - HH:mm:ss') + ' - Link file created successfully for latest client links.'); - } - }); - } else { - console.log(err); - verifyOutput(); - } - }); -}; - diff --git a/templates/views/play.pug b/templates/views/play.pug index f13fc709..aa834fdb 100644 --- a/templates/views/play.pug +++ b/templates/views/play.pug @@ -60,7 +60,7 @@ block content .playContainer h1 1 - Download and install FAF p Download the FAF Client and install it. FAF is open source and safe to use.
If your Windows computer stops you from running FAF, click on "More info" and you'll be able to run it.
This happens because FAF doesn't pay Microsoft for security certifications. So it's an "unrecognized app" for Windows.
If you have any issues or troubleshooting, join our Discord or use our forum for help. - a(href=fafClientDownloadLink) + a(href="#" id="faf-client-download") button Download FAF br br @@ -96,3 +96,7 @@ block content br br iframe(style="width:75%; height:45vh;" src="https://www.youtube.com/embed/Nks9loE96ok" title="NEW TO FAF? || SUPREME COMMANDER TUTORIAL", allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture;" allowfullscreen ) + +block js + script(src="../../js/app/play.js" type="module" defer="true") + diff --git a/yarn.lock b/yarn.lock index 675794c6..9a1cedc6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -519,13 +519,6 @@ axios@1.6.1: form-data "^4.0.0" proxy-from-env "^1.1.0" -axios@^0.21.1: - version "0.21.4" - resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575" - integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg== - dependencies: - follow-redirects "^1.14.0" - babel-walk@3.0.0-canary-5: version "3.0.0-canary-5" resolved "https://registry.yarnpkg.com/babel-walk/-/babel-walk-3.0.0-canary-5.tgz#f66ecd7298357aee44955f235a6ef54219104b11" @@ -1961,7 +1954,7 @@ flagged-respawn@^1.0.1: resolved "https://registry.yarnpkg.com/flagged-respawn/-/flagged-respawn-1.0.1.tgz#e7de6f1279ddd9ca9aac8a5971d618606b3aab41" integrity sha512-lNaHNVymajmk0OJMBn8fVUAU1BtDeKIqKoVhk4xAALB57aALg6b4W0MfJ/cUE0g9YBXy5XhSlPIpYIJ7HaY/3Q== -follow-redirects@^1.14.0, follow-redirects@^1.15.0: +follow-redirects@^1.15.0: version "1.15.3" resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.3.tgz#fe2f3ef2690afce7e82ed0b44db08165b207123a" integrity sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q== @@ -2109,16 +2102,6 @@ getpass@^0.1.1: dependencies: assert-plus "^1.0.0" -github-api@3.4.0: - version "3.4.0" - resolved "https://registry.yarnpkg.com/github-api/-/github-api-3.4.0.tgz#5da2f56442d4839d324e9faf0ffb2cf30f7650b8" - integrity sha512-2yYqYS6Uy4br1nw0D3VrlYWxtGTkUhIZrumBrcBwKdBOzMT8roAe8IvI6kjIOkxqxapKR5GkEsHtz3Du/voOpA== - dependencies: - axios "^0.21.1" - debug "^2.2.0" - js-base64 "^2.1.9" - utf8 "^2.1.1" - giturl@^1.0.0: version "1.0.3" resolved "https://registry.yarnpkg.com/giturl/-/giturl-1.0.3.tgz#27f9d1f251d138eb2a5a56cc9dd8512b0fc0bbc6" @@ -3057,11 +3040,6 @@ jit-grunt@0.10.0: resolved "https://registry.yarnpkg.com/jit-grunt/-/jit-grunt-0.10.0.tgz#008c3a7fe1e96bd0d84e260ea1fa1783457f79c2" integrity sha512-eT/f4c9wgZ3buXB7X1JY1w6uNtAV0bhrbOGf/mFmBb0CDNLUETJ/VRoydayWOI54tOoam0cz9RooVCn3QY1WoA== -js-base64@^2.1.9: - version "2.6.4" - resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-2.6.4.tgz#f4e686c5de1ea1f867dbcad3d46d969428df98c4" - integrity sha512-pZe//GGmwJndub7ZghVHz7vjb2LgC1m8B07Au3eYqeqv9emhESByMXxaEgkUkEqJe87oBbSniGYoQNIBklc7IQ== - js-stringify@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/js-stringify/-/js-stringify-1.0.2.tgz#1736fddfd9724f28a3682adc6230ae7e4e9679db" @@ -5754,11 +5732,6 @@ use@^3.1.0: resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f" integrity sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ== -utf8@^2.1.1: - version "2.1.2" - resolved "https://registry.yarnpkg.com/utf8/-/utf8-2.1.2.tgz#1fa0d9270e9be850d9b05027f63519bf46457d96" - integrity sha512-QXo+O/QkLP/x1nyi54uQiG0XrODxdysuQvE5dtVqv7F5K2Qb6FsN+qbr6KhF5wQ20tfcV3VQp0/2x1e1MRSPWg== - util-deprecate@^1.0.1, util-deprecate@^1.0.2, util-deprecate@~1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"