config util

This commit is contained in:
HorizonCode 2022-10-16 18:41:24 +02:00
parent 99d0d927e0
commit bd7f3ddefd
1 changed files with 51 additions and 0 deletions

51
config.js Normal file
View File

@ -0,0 +1,51 @@
const path = require('path');
const fs = require('fs');
const configFolder = path.join(process.env['LOCALAPPDATA'], 'EZPPLauncher');
if (!fs.existsSync(configFolder)) fs.mkdirSync(configFolder);
const configLocation = path.join(configFolder, `ezpplauncher.${path.basename(process.env['HOME'])}.cfg`);
if (!fs.existsSync(configLocation)) fs.writeFileSync(configLocation, "");
async function get(key, defaultValue) {
const fileStream = await fs.promises.readFile(configLocation, "utf-8");
const lines = fileStream.split(/\r?\n/)
for (const line of lines) {
if (line.includes('=')) {
const argsPair = line.split('=', 2);
const keyname = argsPair[0]
const value = argsPair[1];
if (keyname == key) {
return value;
}
}
}
return defaultValue;
}
async function set(key, value) {
console.log("setting " + key + " to " + value);
const configValues = new Map();
const fileStream = await fs.promises.readFile(configLocation, "utf-8");
const lines = fileStream.split(/\r?\n/)
for (const line of lines) {
if (line.includes('=')) {
const argsPair = line.split('=', 2);
const keyname = argsPair[0]
const value = argsPair[1];
configValues.set(keyname, value);
}
}
configValues.set(key, value);
const arr = [];
for (var [storkey, storvalue] of configValues.entries())
arr.push(`${storkey}=${storvalue}`);
await fs.promises.writeFile(configLocation, arr.join('\n'));
}
module.exports = { get, set }