UNPKG

lgtv2mqtt2

Version:

connect WebOS LG TV with MQTT

61 lines (45 loc) 1.39 kB
import { homedir } from "os"; import path from "path"; import fs from "fs"; import paths from "./paths.js"; function migrateIfNeeded(fileName, oldFileName) { const oldPath = path.join(homedir(), oldFileName); const newPath = path.join(paths.config, fileName); if (fs.existsSync(oldPath) && !fs.existsSync(newPath)) { fs.mkdirSync(paths.config, { recursive: true }); fs.renameSync(oldPath, newPath); console.log(`Migrated ${oldPath} -> ${newPath}`); } } export default function getConfig(fileName, exampleConfig, oldFileName) { if (oldFileName) { migrateIfNeeded(fileName, oldFileName); } const configPath = path.join(paths.config, fileName); if (!fs.existsSync(configPath)) { console.log(`No "${configPath}" found, create one with this content: ${JSON.stringify(exampleConfig, null, 2)}`); process.exit(1); } let config; try { config = JSON.parse(fs.readFileSync(configPath, "utf-8")); } catch (e) { console.log(`Couldn't parse "${configPath}" ${e}`); process.exit(1); } let anyKeyMissing = false; Object.keys(exampleConfig).forEach((requiredKey) => { if (!config.hasOwnProperty(requiredKey)) { console.log( `Missing key "${requiredKey}" in config file "${configPath}"` ); anyKeyMissing = true; } }); if (anyKeyMissing) { process.exit(1); } return config; }