UNPKG

@constructorfleet/ultimate-govee

Version:

Library for interacting with Govee devices written in Typescript.

42 lines 2.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PersistResult = void 0; const fs_1 = require("fs"); const promises_1 = require("fs/promises"); const path_1 = require("path"); const json_stringify_safe_1 = __importDefault(require("json-stringify-safe")); const persist_module_1 = require("./persist.module"); function PersistResult(options) { return (_, __, descriptor) => { const original = descriptor.value; // eslint-disable-next-line func-names descriptor.value = async function (...args) { const result = await original.apply(this, args); const dirPath = options.path !== undefined ? (0, path_1.join)(persist_module_1.PersistModule.persistRootDirectory, options.path) : persist_module_1.PersistModule.persistRootDirectory; if (dirPath !== undefined && !(0, fs_1.existsSync)(dirPath)) { await (0, promises_1.mkdir)(dirPath, { recursive: true }); } const filePath = (0, path_1.join)(dirPath ?? '.', options.filename); const resolvedPath = args.reduce((fp, arg, index) => fp.replace(`{${index}}`, arg), filePath); const writeData = options.transform === undefined ? result : options.transform(result); if (options.append === true) { await (0, promises_1.appendFile)(resolvedPath, (0, json_stringify_safe_1.default)(writeData, null, 2), { ...options.writeOption, }); } else { await (0, promises_1.writeFile)(resolvedPath, (0, json_stringify_safe_1.default)(writeData, null, 2), { ...options.writeOption, }); } return result; }; }; } exports.PersistResult = PersistResult; //# sourceMappingURL=persist.decorators.js.map