UNPKG

gcal-commander

Version:

A command-line interface for Google Calendar operations

174 lines (173 loc) 5.81 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var ConfigService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigService = void 0; const tsyringe_1 = require("tsyringe"); const zod_1 = require("zod"); const languages_1 = require("../constants/languages"); const tokens_1 = require("../di/tokens"); const configValueSchema = zod_1.z.object({ defaultCalendar: zod_1.z.string(), language: zod_1.z.enum(languages_1.SUPPORTED_LANGUAGES), 'events.days': zod_1.z.number().min(1).max(365), 'events.format': zod_1.z.enum(['table', 'json', 'pretty-json']), 'events.maxResults': zod_1.z.number().min(1).max(100), }); let ConfigService = class ConfigService { static { ConfigService_1 = this; } configStorage; static VALID_KEYS = [ 'defaultCalendar', 'language', 'events.maxResults', 'events.format', 'events.days', ]; config = {}; loaded = false; constructor(configStorage) { this.configStorage = configStorage; } async get(key) { await this.load(); return this.getNestedValue(this.config, key); } getConfigPath() { return this.configStorage.getConfigPath(); } getValidKeys() { return ConfigService_1.VALID_KEYS; } async list() { await this.load(); return { ...this.config }; } async load() { if (this.loaded) return; try { const exists = await this.configStorage.exists(); if (exists) { const content = await this.configStorage.read(); this.config = JSON.parse(content); } else { this.config = {}; } } catch { // If file doesn't exist or is invalid, use empty config this.config = {}; } this.loaded = true; } async reset() { this.config = {}; await this.save(); } async save() { const content = JSON.stringify(this.config, null, 2); await this.configStorage.write(content); } async set(key, value) { await this.load(); this.setNestedValue(this.config, key, value); await this.save(); } async unset(key) { await this.load(); this.deleteNestedValue(this.config, key); await this.save(); } validateKey(key) { return ConfigService_1.VALID_KEYS.includes(key); } validateValue(key, value) { if (!this.validateKey(key)) { return { errorKey: 'config.validation.unknownKey', errorOptions: { key }, valid: false, }; } try { const schema = configValueSchema.shape[key]; schema.parse(value); return { valid: true }; } catch (error) { if (error instanceof zod_1.z.ZodError) { const message = error.errors.map((e) => e.message).join(', '); return { errorKey: 'config.validation.zodError', errorOptions: { key, message }, valid: false, }; } return { errorKey: 'config.validation.invalidValue', errorOptions: { key }, valid: false, }; } } deleteNestedValue(obj, path) { const keys = path.split('.'); const lastKey = keys.pop(); let target = obj; for (const key of keys) { if (target && typeof target === 'object' && key in target) { target = target[key]; } else { target = null; break; } } if (target && typeof target === 'object') { delete target[lastKey]; } } getNestedValue(obj, path) { let current = obj; for (const key of path.split('.')) { if (current && typeof current === 'object' && key in current) { current = current[key]; } else { return undefined; } } return current; } setNestedValue(obj, path, value) { const keys = path.split('.'); const lastKey = keys.pop(); let target = obj; for (const key of keys) { if (!(key in target) || typeof target[key] !== 'object' || target[key] === null) { target[key] = {}; } target = target[key]; } target[lastKey] = value; } }; exports.ConfigService = ConfigService; exports.ConfigService = ConfigService = ConfigService_1 = __decorate([ (0, tsyringe_1.injectable)(), __param(0, (0, tsyringe_1.inject)(tokens_1.TOKENS.ConfigStorage)), __metadata("design:paramtypes", [Object]) ], ConfigService);