UNPKG

@catladder/cli

Version:

Panter cli tool for cloud CI/CD and DevOps

58 lines 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setPreference = exports.getPreference = exports.hasPreference = exports.getPreferences = void 0; const promises_1 = require("fs/promises"); const fs_1 = require("fs"); const path_1 = require("path"); const os_1 = require("os"); const yaml_1 = require("yaml"); const legacyPreferencesPath = (0, path_1.join)((0, os_1.homedir)(), ".catladder/preferences.yml"); const preferencesPath = (0, path_1.join)((0, os_1.homedir)(), ".config/catladder/preferences.yml"); const ensurePreferencesFile = async () => { if ((0, fs_1.existsSync)(preferencesPath)) { return; } await (0, promises_1.mkdir)((0, path_1.dirname)(preferencesPath), { recursive: true, mode: 0o700 }); if (!(0, fs_1.existsSync)(legacyPreferencesPath)) { return (0, promises_1.writeFile)(preferencesPath, "---\n{}", { encoding: "utf-8", mode: 0o600, }); } await (0, promises_1.cp)(legacyPreferencesPath, preferencesPath); await (0, promises_1.chmod)(preferencesPath, 0o600); await (0, promises_1.cp)(preferencesPath, (0, path_1.join)((0, path_1.dirname)(preferencesPath), "old_preferences_backup.yml")); }; const loadPreferences = async () => { await ensurePreferencesFile(); return (0, promises_1.readFile)(preferencesPath, { encoding: "utf-8" }); }; const getPreferences = async () => { var _a; const yamlContent = await loadPreferences(); return ((_a = (0, yaml_1.parse)(yamlContent)) !== null && _a !== void 0 ? _a : {}); }; exports.getPreferences = getPreferences; const hasPreference = async (key) => { const preferences = await (0, exports.getPreferences)(); return key in preferences; }; exports.hasPreference = hasPreference; const getPreference = async (key) => { const preferences = await (0, exports.getPreferences)(); return preferences[key]; }; exports.getPreference = getPreference; const setPreference = async (key, value) => { const preferences = await (0, exports.getPreferences)(); const newPreferences = { ...preferences, [key]: value, }; await (0, promises_1.writeFile)(preferencesPath, (0, yaml_1.stringify)(newPreferences), { encoding: "utf-8", mode: 0o600, }); }; exports.setPreference = setPreference; //# sourceMappingURL=preferences.js.map