diff --git a/getAppDataPath.js b/getAppDataPath.js new file mode 100644 index 0000000..5792c2b --- /dev/null +++ b/getAppDataPath.js @@ -0,0 +1,24 @@ +const path = require("path"); + +module.exports = function getAppDataPath() { + switch (process.platform) { + case "darwin": { + return path.join( + process.env.HOME, + "Library", + "Application Support", + "hajimari-no-overlay" + ); + } + case "win32": { + return path.join(process.env.APPDATA, "hajimari-no-overlay"); + } + case "linux": { + return path.join(process.env.HOME, ".hajimari-no-overlay"); + } + default: { + console.log("Unsupported platform!"); + process.exit(1); + } + } +}; diff --git a/parseXlsx.js b/parseXlsx.js index 1abd230..9456de1 100644 --- a/parseXlsx.js +++ b/parseXlsx.js @@ -2,6 +2,8 @@ const xlsx = require("xlsx"); const fs = require("fs"); const https = require("follow-redirects").https; const concat = require("concat-stream"); +const path = require("path"); +const getAppDataPath = require("./getAppDataPath"); function download(url, cb) { const concatStream = concat(cb); @@ -45,10 +47,26 @@ module.exports = function downloadAndParseTranslations(cb) { ), }; cb(translations); - fs.writeFileSync( - "translation.json", - JSON.stringify({ date: new Date(), ...translations }) - ); + saveAppData("translation.json", { date: new Date(), ...translations }); } ); }; + +function saveAppData(name, content) { + const appDataDirPath = getAppDataPath(); + + if (!fs.existsSync(appDataDirPath)) { + fs.mkdirSync(appDataDirPath); + } + + const appDataFilePath = path.join(appDataDirPath, name); + content = JSON.stringify(content); + + fs.writeFile(appDataFilePath, content, (err) => { + if (err) { + console.log("There was a problem saving data!"); + } else { + console.log("Data saved correctly!"); + } + }); +} diff --git a/preload.js b/preload.js index 4916e34..136715b 100644 --- a/preload.js +++ b/preload.js @@ -1,9 +1,18 @@ -const trads = require("./translation.json"); const { contextBridge } = require("electron"); const downloadAndParseTranslations = require("./parseXlsx"); +const getAppDataPath = require("./getAppDataPath"); +const fs = require("fs"); +const path = require("path"); contextBridge.exposeInMainWorld("api", { - getTranslations: () => trads, + getTranslations: () => { + const file = path.join(getAppDataPath(), "translation.json"); + if (!fs.existsSync(file)) { + return require("./translation.json"); + } + const translations = fs.readFileSync(file); + return JSON.parse(translations); + }, updateTranslations: (cb) => downloadAndParseTranslations(cb), getPlatform: () => process.platform, }); diff --git a/src/lines.js b/src/lines.js index f22fb02..50e89a8 100644 --- a/src/lines.js +++ b/src/lines.js @@ -10,8 +10,6 @@ class TraversableArray extends Array { } } -window.api.getTranslations(); - function mapTranslations(translations) { const episodesSelect = document.getElementById("episode"); translations.Episodes.forEach((episode, index) => {