UNPKG

koneko-cli

Version:

Your CLI for reading manga from the terminal

73 lines (72 loc) 1.96 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const os_1 = __importDefault(require("os")); const lowdb_1 = require("lowdb"); const node_1 = require("lowdb/node"); const DefaultSettings = { Settings: { ApiKey: null, Lang: 'English', Server: [], DownloadFolder: './Images', DeletePrevious: false }, }; const dataFolder = path_1.default.join(os_1.default.homedir(), '.config', 'koneko'); fs_extra_1.default.ensureDirSync(dataFolder); const settingsFile = path_1.default.join(dataFolder, 'settings.json'); const adapter = new node_1.JSONFile(settingsFile); const db = new lowdb_1.Low(adapter, DefaultSettings); async function init() { await db.read(); if (!db.data) { db.data = DefaultSettings; await db.write(); } } function getNested(obj, path) { return path.split('.').reduce((o, key) => (o ? o[key] : undefined), obj); } function setNested(obj, path, value) { const keys = path.split('.'); let current = obj; keys.forEach((key, i) => { if (i === keys.length - 1) { current[key] = value; } else { if (!(key in current)) current[key] = {}; current = current[key]; } }); } async function get(path) { await db.read(); return getNested(db.data, path); } async function set(path, value) { await db.read(); setNested(db.data, path, value); await db.write(); } async function getAll() { await db.read(); return db.data; } exports.default = { get data() { return db.data; }, db, read: () => db.read(), write: () => db.write(), get, set, getAll };