UNPKG

homebridge-hatch-baby-rest

Version:
40 lines (39 loc) 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.migrateRestBluetooth = migrateRestBluetooth; const fs_1 = require("fs"); const hatchRestBluetoothPlatformName = 'HatchRestBluetooth', hatchBabyRestPlatformName = 'HatchBabyRest'; function updateHomebridgeConfig(homebridge, update) { const configPath = homebridge.user.configPath(), config = (0, fs_1.readFileSync)(configPath).toString(), updatedConfig = update(config); if (config !== updatedConfig) { (0, fs_1.writeFileSync)(configPath, updatedConfig); } } function migrateRestBluetooth(homebridge) { updateHomebridgeConfig(homebridge, (originalConfig) => { try { const config = JSON.parse(originalConfig), hbrPlatform = config.platforms?.find((p) => p.platform === hatchBabyRestPlatformName), restLights = hbrPlatform?.restLights; if (!restLights?.length) { // no rest accessories to migrate return originalConfig; } // find or create the hatch rest bluetooth platform const hrbPlatform = config.platforms.find((platform) => platform.platform === hatchRestBluetoothPlatformName) || { platform: hatchRestBluetoothPlatformName }; if (!config.platforms.includes(hrbPlatform)) { config.platforms.push(hrbPlatform); } // add all the rest lights to the platform if (!hrbPlatform.restLights) { hrbPlatform.restLights = restLights; } // remove the rest lights from the hatch baby rest platform delete hbrPlatform.restLights; // save the migrated config return JSON.stringify(config, null, 4); } catch (_) { // return config with no changes if anything goes wrong return originalConfig; } }); }