* feat: Make saving the configuration more robust * parse backup user config before return * fix some bug * Add write-userconfig.js and invalid-config.bin * throw exception if failed user config parsing * Merge branch 'master' into feat-467-make-save-more-robust * hide keymaps and macros if agent in restore mode * fix Device name settings
33 lines
1.2 KiB
TypeScript
33 lines
1.2 KiB
TypeScript
import { app } from 'electron';
|
|
import { LogService, UserConfiguration, SaveUserConfigurationData } from 'uhk-common';
|
|
import * as path from 'path';
|
|
import * as fs from 'fs-extra';
|
|
|
|
export const getBackupUserConfigurationPath = (uniqueId: number): string => {
|
|
const appDataDir = app.getPath('userData');
|
|
|
|
return path.join(appDataDir, `${uniqueId}.json`);
|
|
};
|
|
|
|
export const backupUserConfiguration = (data: SaveUserConfigurationData): Promise<void> => {
|
|
const backupFilePath = getBackupUserConfigurationPath(data.uniqueId);
|
|
return fs.writeJSON(backupFilePath, data.configuration, {spaces: 2});
|
|
};
|
|
|
|
export const getBackupUserConfigurationContent = async (logService: LogService, uniqueId: number): Promise<UserConfiguration> => {
|
|
try {
|
|
const backupFilePath = getBackupUserConfigurationPath(uniqueId);
|
|
|
|
if (await fs.pathExists(backupFilePath)) {
|
|
const json = await fs.readJSON(backupFilePath);
|
|
new UserConfiguration().fromJsonObject(json);
|
|
|
|
return json;
|
|
}
|
|
|
|
return null;
|
|
} catch (error) {
|
|
logService.error('Can not load backup user configuration for device', {uniqueId, error});
|
|
}
|
|
};
|