UNPKG

@ephemeras/profile

Version:

Profile manager for ephemeras.

136 lines (134 loc) 3.62 kB
import "./chunk-QGM4M3NI.js"; // src/index.ts import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs"; import { homedir } from "node:os"; import { dirname, extname, resolve } from "node:path"; var Profile = class { url; data; transformer = JSON.parse; serializer = JSON.stringify; constructor(options) { const { base, path, data, transformer, serializer } = options; if (base && !existsSync(base)) { throw Error(`base directory: "${base}" is not exists`); } if (path && !extname(path)) { throw Error(`path: "${path}" must be a file`); } if (extname(path) !== ".json" && !transformer) { throw Error( `detect path: ${path} not json file, please offer the transformer & serializer for your file` ); } const baseDir = base || homedir(); const url = resolve(baseDir, path); this.url = url; if (serializer) { this.serializer = serializer; } if (transformer) { this.transformer = transformer; } if (!existsSync(this.url)) { mkdirSync(dirname(this.url), { recursive: true }); writeFileSync(this.url, extname(path) === ".json" ? "{}" : "", { encoding: "utf-8" }); } const fileContent = readFileSync(this.url, "utf-8") || (extname(path) === ".json" ? "{}" : ""); const fileData = this.transformer(fileContent); this.data = data || fileData || {}; this.set(this.data); } sync(syncType = "get") { if (syncType === "get") { const data = this.transformer(readFileSync(this.url, "utf-8")); this.data = data; } else { writeFileSync(this.url, this.serializer(this.data)); } } getData() { return this.data; } getUrl() { return this.url; } getParent(prop) { const props = prop.split("."); let temp = this.data; for (let i = 0; i < props.length - 1; i++) { if (!temp[props[i]]) { if (props?.[i + 1] && /^\d+$/.test(props?.[i + 1])) { temp[props[i]] = []; } else { temp[props[i]] = {}; } } temp = temp[props[i]]; } return temp; } get(prop) { this.sync("get"); const props = prop.split("."); let temp = this.data; for (let i = 0; i < props.length - 1; i++) { temp = temp[props[i]]; } return temp?.[props[props.length - 1]]; } set(propOrData, propValue) { if (typeof propOrData === "string") { const props = propOrData.split("."); const data = this.getParent(propOrData); if (Object.prototype.toString.call(data) === "[object Array]") { let resolveIndex = props[props.length - 1]; if (resolveIndex > data.length) { resolveIndex = data.length; } data[resolveIndex] = propValue; } else { data[props[props.length - 1]] = propValue; } } else if (Object.prototype.toString.call(propOrData) === "[object Object]") { this.data = { ...this.data, ...propOrData }; } else { throw Error("invalid params for function set()"); } this.sync("set"); return this.data; } delete(prop) { const props = prop.split("."); const data = this.getParent(prop); if (Object.prototype.toString.call(data) === "[object Array]") { data.splice(props[props.length - 1], 1); } else { delete data[props[props.length - 1]]; } this.sync("set"); return this.data; } clear() { this.data = {}; this.sync("set"); return this.data; } remove() { unlinkSync(this.url); } }; export { Profile };