UNPKG

@sprucelabs/spruce-skill-utils

Version:

Loosely coupled classes and functions to make skill development faster! 🏎

92 lines (91 loc) 2.95 kB
import get from 'lodash/get.js'; import set from 'lodash/set.js'; import unset from 'lodash/unset.js'; import { HASH_SPRUCE_DIR } from '../constants.js'; import diskUtil from '../utilities/disk.utility.js'; export default class SettingsService { constructor(cwd) { this.fileName = 'settings.json'; this.cwd = cwd; } isMarkedAsInstalled(code) { var _a; const settings = this.loadSettings(); return !!((_a = settings.installed) === null || _a === void 0 ? void 0 : _a.find((c) => c === code)); } markAsInstalled(code) { if (!this.isMarkedAsInstalled(code)) { const settings = this.loadSettings(); if (!settings.installed) { settings.installed = []; } if (settings.installed.indexOf(code) === -1) { settings.installed.push(code); this.saveSettings(settings); } } } markAsPermanentlySkipped(code) { const settings = this.loadSettings(); if (!settings.skipped) { settings.skipped = []; } if (settings.installed) { settings.installed = settings.installed.filter((c) => c !== code); } if (settings.skipped.indexOf(code) === -1) { settings.skipped.push(code); this.saveSettings(settings); } } isMarkedAsPermanentlySkipped(code) { var _a; const settings = this.loadSettings(); return !!((_a = settings.skipped) === null || _a === void 0 ? void 0 : _a.find((c) => c === code)); } get(key) { return get(this.loadSettings(), key); } set(key, value) { const settings = this.loadSettings(); set(settings, key, value); this.saveSettings(settings); } unset(key) { const doesFileExist = diskUtil.doesFileExist(this.getSettingsPath()); if (!doesFileExist) { return; } const settings = this.loadSettings(); unset(settings, key); this.saveSettings(settings); this.settings = undefined; } loadSettings() { const path = this.getSettingsPath(); if (!this.settings) { try { const contents = diskUtil.readFile(path); this.settings = JSON.parse(contents); } catch (_a) { this.settings = {}; } } return this.settings; } getSettingsPath() { return diskUtil.resolvePath(this.cwd, HASH_SPRUCE_DIR, this.fileName); } saveSettings(settings) { const path = diskUtil.resolvePath(this.cwd, HASH_SPRUCE_DIR, this.fileName); const contents = JSON.stringify(settings, null, 2); this.write(path, contents); } write(path, contents) { diskUtil.writeFile(path, contents); } setFile(name) { this.fileName = name; } }