diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..111ec10 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +config.json +config.json.bak +instances.json \ No newline at end of file diff --git a/upnpMapper.js b/upnpMapper.js new file mode 100644 index 0000000..02d7090 --- /dev/null +++ b/upnpMapper.js @@ -0,0 +1,74 @@ +const fs = require('node:fs/promises'); +const path = require('path'); +const exec = require("child_process").execSync; + +var config = null; + +function clone(obj) { + return JSON.parse(JSON.stringify(obj)); +} + +function cleanPath(pathToClean) { + let regex = /[<>:"|\?\*]/g; + let cleanPath = path.normalize(pathToClean); + + let isAbsolute = path.isAbsolute(cleanPath); + cleanPath = cleanPath.replace(regex, ''); + if (/^win/i.test(process.platform) && isAbsolute) { + cleanPath = `${cleanPath.slice(0, 1)}:${cleanPath.slice(1)}`; + } + + return cleanPath; +} + +async function mkDir(dirPath) { + try { + await fs.access(cleanPath(dirPath)); + } + catch (err) { + await fs.mkdir(cleanPath(dirPath), { recursive: true }); + } + await fs.access(cleanPath(dirPath)); +} + +async function writeFile(dir, file, data) { + let cleanDir = cleanPath(dir); + let cleanFile = cleanPath(file); + + await mkDir(dir); + + return fs.writeFile(`${cleanDir}/${cleanFile}`, data); +} + +async function readFile(file, { format = 'utf8' } = {}) { + return fs.readFile(cleanPath(file), format); +} + +async function loadConfig({ configPath = __dirname, configName = 'config.json' } = {}) { + let configDefault = { + "instancesPath": "", + }; + + try { + config = JSON.parse(await readFile(cleanPath(`${configPath}/${configName}`))); + } + catch (err) { + config = clone(configDefault); + } + + await saveConfig(); + await saveConfig({ configName: `${configName}.bak` }); +} + + +async function saveConfig({ configPath = __dirname, configName = 'config.json' } = {}) { + await writeFile(cleanPath(configPath), cleanPath(configName), JSON.stringify(config, null, 4)); +} + +async function main() { + await loadConfig(); + console.log(config.instancesPath); + +} + +main(); \ No newline at end of file