Refactor getFactorioLocale

Move getFactorioLocale to the new lib/factorio namespace and refactor it
to use async functions instead of callbacks.  Split tests into
integration with factorio and testing of the function itself.

Also removes the cacheing from the function itself as this breaks the
isolated testing of it.
This commit is contained in:
Hornwitser committed 2019-10-14 18:56:46 +02:00
1 parent a01b971ba2
commit 93480c8b29
8 files changed
+125 -132

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
module.exports = {
...require('lib/factorio/locale'),
...require('lib/factorio/server'),
};
+28
View File
@@ -0,0 +1,28 @@
/**
* Function for parsing factorio locale from a local factorio install
*/
"use strict";
const fs = require("fs-extra");
const path = require("path");
const ini = require("ini");
/**
* Gets factorios locale as an object, does not respect mods.
*
* @param {string} factorioDirectory
* @param {string} languageCode
* @param {function} callback
*/
async function getLocale(dataDir, languageCode) {
if (typeof dataDir !== "string") throw new TypeError("dataDir must be a string");
if (typeof languageCode !== "string") throw new TypeError("languageCode must be a string");
let localeFilePath = path.join(dataDir, "base", "locale", languageCode, "base.cfg");
let content = await fs.readFile(localeFilePath, "utf8");
return ini.parse(content);
}
module.exports = {
getLocale,
};