From 21480c436dd109b0e97d094e5e3f2bfe63bd880b Mon Sep 17 00:00:00 2001 From: Revadike Date: Sun, 26 Apr 2020 17:13:40 +0200 Subject: [PATCH] V1.3.0 --- .eslintrc.json | 1 + README.md | 1 + gimme_free_epic_shit.js | 17 +++++++++-------- logger.js | 42 +++++++++++++++++++++++++++++++++++++++++ package.json | 4 +++- 5 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 logger.js diff --git a/.eslintrc.json b/.eslintrc.json index ee30741..f6e7296 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -62,6 +62,7 @@ "no-caller": "error", "no-case-declarations": "error", "no-confusing-arrow": "error", + "no-console": "error", "no-constructor-return": "error", "no-dupe-else-if": "error", "no-duplicate-imports": "error", diff --git a/README.md b/README.md index 2e5d657..4e1c4b3 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ No, these are only usable by using the config. ## Changelog ### V1.3.0 * Changed method of obtaining free games list (Closes #13) + * Added better logger (Closes #14) ### V1.2.3 * Small bugfix diff --git a/gimme_free_epic_shit.js b/gimme_free_epic_shit.js index cb8e9c2..133e716 100644 --- a/gimme_free_epic_shit.js +++ b/gimme_free_epic_shit.js @@ -1,5 +1,6 @@ "use strict"; const Config = require(`${__dirname}/config.json`); +const Logger = require("tracer").console(`${__dirname}/logger.js`); const ClientLoginAdapter = require("epicgames-client-login-adapter"); const { "Launcher": EpicGames } = require("epicgames-client"); const PROMO_QUERY = `query searchStoreQuery($category: String, $locale: String, $start: Int) { @@ -47,7 +48,7 @@ const PROMO_QUERY = `query searchStoreQuery($category: String, $locale: String, } if (!await client.login().catch(() => false)) { - console.log(`Failed to login as ${client.config.email}, please attempt manually.`); + Logger.warn(`Failed to login as ${client.config.email}, please attempt manually.`); let auth = await ClientLoginAdapter.init(account); let exchangeCode = await auth.getExchangeCode(); await auth.close(); @@ -57,7 +58,7 @@ const PROMO_QUERY = `query searchStoreQuery($category: String, $locale: String, } } - console.log(`Logged in as ${client.account.name} (${client.account.id})`); + Logger.info(`Logged in as ${client.account.name} (${client.account.id})`); let { data } = await client.http.sendGraphQL(null, PROMO_QUERY, { "category": "freegames", "locale": "en-US" }); let { elements } = JSON.parse(data).data.Catalog.searchStore; @@ -69,27 +70,27 @@ const PROMO_QUERY = `query searchStoreQuery($category: String, $locale: String, try { let purchased = await client.purchase(offer, 1); if (purchased) { - console.log(`Successfully claimed ${offer.title} (${purchased})`); + Logger.info(`Successfully claimed ${offer.title} (${purchased})`); } else { - console.log(`${offer.title} was already claimed for this account`); + Logger.info(`${offer.title} was already claimed for this account`); } } catch (err) { - console.log(`Failed to claim ${offer.title} (${err})`); + Logger.warn(`Failed to claim ${offer.title} (${err})`); } } await client.logout(); - console.log(`Logged ${client.account.name} out of Epic Games`); + Logger.info(`Logged ${client.account.name} out of Epic Games`); } if (loop) { - console.log(`Waiting ${delay} minutes`); + Logger.info(`Waiting ${delay} minutes`); await sleep(delay); } else { process.exit(0); } } while (loop); })().catch(err => { - console.error(err); + Logger.error(err); process.exit(1); }); diff --git a/logger.js b/logger.js new file mode 100644 index 0000000..47b99fd --- /dev/null +++ b/logger.js @@ -0,0 +1,42 @@ +"use strict"; +const colors = require("colors"); +const fs = require("fs"); +const path = require("path"); + +module.exports = { + "format": " {{timestamp}} | {{title}} | {{message}}", + "dateformat": "yyyy-mm-dd | HH:MM:ss.l", + "filters": { + "log": colors.white, + "trace": colors.magenta, + "debug": colors.blue, + "info": colors.green, + "warn": colors.magenta, + "error": [colors.red, colors.bold] + }, + "preprocess": data => { + data.title = data.title.toUpperCase(); + while (data.title.length < 5) { data.title += " "; } + data.args = [...data.args]; + if (data.args[0] && data.args[0].logpath) { + data.logpath = data.args[0].logpath; + data.args.shift(); + } + }, + "transport": data => { + // eslint-disable-next-line no-console + console.log(data.output); + if (data.level === 4) { + return; + } + + const streamoptions = { + "flags": "a", + "encoding": "utf8" + }; + fs.createWriteStream(path.join(__dirname, "log.txt"), streamoptions).write(`\r\n${data.rawoutput}`); + if (data.logpath) { + fs.createWriteStream(data.logpath, streamoptions).write(`\r\n${data.rawoutput}`); + } + } +}; diff --git a/package.json b/package.json index 1f24d05..de6bc05 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,9 @@ "eslint": "^6.8.0" }, "dependencies": { + "colors": "^1.4.0", "epicgames-client": "^2.0.23", - "epicgames-client-login-adapter": "^0.1.0" + "epicgames-client-login-adapter": "^0.1.0", + "tracer": "^1.0.3" } }