UNPKG

@ephemeras/profile

Version:

Profile manager for ephemeras.

136 lines (124 loc) 4.63 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class;require('./chunk-3HT76LNN.cjs'); // src/index.ts var _fs = require('fs'); var _os = require('os'); var _path = require('path'); var Profile = (_class = class { __init() {this.transformer = JSON.parse} __init2() {this.serializer = JSON.stringify} constructor(options) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this); const { base, path, data, transformer, serializer } = options; if (base && !_fs.existsSync.call(void 0, base)) { throw Error(`base directory: "${base}" is not exists`); } if (path && !_path.extname.call(void 0, path)) { throw Error(`path: "${path}" must be a file`); } if (_path.extname.call(void 0, path) !== ".json" && !transformer) { throw Error( `detect path: ${path} not json file, please offer the transformer & serializer for your file` ); } const baseDir = base || _os.homedir.call(void 0, ); const url = _path.resolve.call(void 0, baseDir, path); this.url = url; if (serializer) { this.serializer = serializer; } if (transformer) { this.transformer = transformer; } if (!_fs.existsSync.call(void 0, this.url)) { _fs.mkdirSync.call(void 0, _path.dirname.call(void 0, this.url), { recursive: true }); _fs.writeFileSync.call(void 0, this.url, _path.extname.call(void 0, path) === ".json" ? "{}" : "", { encoding: "utf-8" }); } const fileContent = _fs.readFileSync.call(void 0, this.url, "utf-8") || (_path.extname.call(void 0, 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(_fs.readFileSync.call(void 0, this.url, "utf-8")); this.data = data; } else { _fs.writeFileSync.call(void 0, 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 (_optionalChain([props, 'optionalAccess', _ => _[i + 1]]) && /^\d+$/.test(_optionalChain([props, 'optionalAccess', _2 => _2[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 _optionalChain([temp, 'optionalAccess', _3 => _3[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() { _fs.unlinkSync.call(void 0, this.url); } }, _class); exports.Profile = Profile;