UNPKG

tauri-settings

Version:

A user settings manager for Tauri inspired by electron-settings.

67 lines (66 loc) 2.74 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { getSettings, saveSettings } from '../fs/load-save'; import { getDotNotation, setDotNotation } from '../utils/dot-notation'; /** * Checks whether a key exists in the settings. * @param key The key for the setting. Key supports dot notation. See https://github.com/harshkhandeparkar/tauri-settings#dot-notation. */ export function has(key_1) { return __awaiter(this, arguments, void 0, function* (key, options = {}) { try { const settings = (yield getSettings(options)).settings; const value = getDotNotation(settings, key); return value !== null; } catch (e) { throw e; } }); } /** * Get the value of a particular setting. * @param key The key for the setting. Key supports dot notation. See https://github.com/harshkhandeparkar/tauri-settings#dot-notation. * @returns The value of the setting */ export function get(key_1) { return __awaiter(this, arguments, void 0, function* (key, options = {}) { if (!(yield has(key))) throw 'Error: key does not exist'; try { const settings = (yield getSettings(options)).settings; return getDotNotation(settings, key); } catch (e) { throw e; } }); } /** * Sets the value of a particular setting * @param key The key for the setting. Key supports dot notation. See https://github.com/harshkhandeparkar/tauri-settings#dot-notation. * @param value The new value * @returns The entire settings object */ export function set(key_1, value_1) { return __awaiter(this, arguments, void 0, function* (key, value, options = {}) { if (!(yield has(key))) throw 'Error: key does not exist'; try { const settings = yield getSettings(options); setDotNotation(settings.settings, key, value); yield saveSettings(settings.settings, settings.path, options); return settings.settings; } catch (e) { throw e; } }); }