UNPKG

alwaysai

Version:

The alwaysAI command-line interface (CLI)

56 lines 2.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeOrValidateDeviceCfgFile = exports.writeDeviceCfgFile = exports.DeviceCfgErrors = void 0; const coded_error_1 = require("@carnesen/coded-error"); const infrastructure_1 = require("../../infrastructure"); const device_paths_1 = require("../../infrastructure/device-paths"); const util_1 = require("../../util"); const device_json_file_1 = require("./device-json-file"); exports.DeviceCfgErrors = (0, util_1.keyMirror)({ TARGET_DEVICE_CONFIGURED: null, TARGET_DEVICE_MISMATCH: null }); async function writeDeviceCfgFile(args) { const { spawner, deviceUuid, accessToken, idToken, refreshToken } = args; if (await spawner.exists(device_paths_1.DEVICE_CONFIG_FILE_NAME)) { throw new coded_error_1.CodedError('Device is already configured!', exports.DeviceCfgErrors.TARGET_DEVICE_CONFIGURED); } const contents = { systemId: (0, infrastructure_1.getSystemId)(), deviceUuid, accessToken, idToken, refreshToken }; await spawner.mkdirp(); await spawner.writeFile(device_paths_1.DEVICE_CONFIG_FILE_NAME, JSON.stringify(contents, null, 2)); } exports.writeDeviceCfgFile = writeDeviceCfgFile; async function writeOrValidateDeviceCfgFile(args) { const { spawner, deviceUuid } = args; if (!(await spawner.exists(device_paths_1.DEVICE_CONFIG_FILE_NAME))) { await writeDeviceCfgFile(args); return; } const origContents = await spawner.readFile(device_paths_1.DEVICE_CONFIG_FILE_NAME); let origParsed; try { origParsed = JSON.parse(origContents); } catch (_a) { await spawner.rimraf(device_paths_1.DEVICE_CONFIG_FILE_NAME); await writeDeviceCfgFile(args); return; } if (!(0, device_json_file_1.validateDeviceConfig)(origParsed)) { util_1.logger.warn(`Invalid device config contents: ${JSON.stringify(device_json_file_1.validateDeviceConfig.errors, null, 2)}`); await spawner.rimraf(device_paths_1.DEVICE_CONFIG_FILE_NAME); await writeDeviceCfgFile(args); return; } if (origParsed.deviceUuid !== deviceUuid) { throw new coded_error_1.CodedError("Target device doesn't match selected device!", exports.DeviceCfgErrors.TARGET_DEVICE_MISMATCH); } } exports.writeOrValidateDeviceCfgFile = writeOrValidateDeviceCfgFile; //# sourceMappingURL=write-or-validate-device-cfg-file.js.map