Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aymeric-giraudet committed Nov 22, 2020
1 parent 8dcd8f9 commit 04a0853
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
24 changes: 24 additions & 0 deletions getAppDataPath.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
};
26 changes: 22 additions & 4 deletions parseXlsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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!");
}
});
}
13 changes: 11 additions & 2 deletions preload.js
Original file line number Diff line number Diff line change
@@ -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,
});
2 changes: 0 additions & 2 deletions src/lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class TraversableArray extends Array {
}
}

window.api.getTranslations();

function mapTranslations(translations) {
const episodesSelect = document.getElementById("episode");
translations.Episodes.forEach((episode, index) => {
Expand Down

0 comments on commit 04a0853

Please sign in to comment.