UNPKG

tauri-settings

Version:

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

75 lines (74 loc) 2.82 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 { appConfigDir, join } from '@tauri-apps/api/path'; import { createDir, readDir, readTextFile, writeFile } from '@tauri-apps/api/fs'; import { parseOptions } from '../config/config'; /** * @internal */ export var STATUS; (function (STATUS) { STATUS["FILE_EXISTS"] = "file_exists"; STATUS["FILE_CREATED"] = "file_created"; })(STATUS || (STATUS = {})); /** * @internal */ export function ensureSettingsFile() { return __awaiter(this, arguments, void 0, function* (options = {}) { var _a; try { const finalConfig = parseOptions(options); const finalDir = (_a = finalConfig.dir) !== null && _a !== void 0 ? _a : yield appConfigDir(); const settingsFilePath = yield join(finalDir, finalConfig.fileName); // create appConfigDir() try { yield readDir(finalDir); } catch (e) { // doesn't exist try { yield createDir(finalDir, { recursive: true }); } catch (e) { throw e; } } try { const content = yield readTextFile(settingsFilePath); return { status: STATUS.FILE_EXISTS, path: settingsFilePath, content }; } catch (e) { // doesn't exist try { yield writeFile({ contents: JSON.stringify({}, null, finalConfig.prettify ? finalConfig.numSpaces : 0), path: settingsFilePath }); return { status: STATUS.FILE_CREATED, path: settingsFilePath, content: JSON.stringify({}, null, finalConfig.prettify ? finalConfig.numSpaces : 0) }; } catch (e) { throw e; } } } catch (e) { throw e; } }); }