UNPKG

@directctrl/fixturelibrary

Version:

Utility library making it easy to work with the open-fixture-library.

48 lines 1.76 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeJsonFile = exports.readJsonFile = exports.storageDirectory = void 0; const fs_extra_1 = require("fs-extra"); const node_path_1 = __importDefault(require("node:path")); const fixtureindex_1 = require("./fixtureindex"); exports.storageDirectory = node_path_1.default.resolve(__dirname, '../.fixturelibrary'); async function readJsonFile(filename) { // Preparing path let filePath = `${exports.storageDirectory}/${filename}`; if (!filename.endsWith('.json')) filePath += '.json'; // Checking if file exists if (!await (0, fs_extra_1.pathExists)(filePath)) { throw new fixtureindex_1.ItemExistanceError(`${filePath} doesn't exist!`); } try { return await (0, fs_extra_1.readJSON)(filePath); } catch (err) { console.error(err); } return undefined; } exports.readJsonFile = readJsonFile; async function writeJsonFile(name, data, override = false) { // Preparing filePath let filePath = `${exports.storageDirectory}/${name}`; if (!filePath.endsWith('.json')) filePath += '.json'; // Checking if file already exists if (!override && await (0, fs_extra_1.pathExists)(filePath)) { throw new fixtureindex_1.ItemExistanceError(`This file at ${node_path_1.default} already exist!`); } try { (0, fs_extra_1.outputJSON)(filePath, data); } catch (err) { console.error(err); return false; } return true; } exports.writeJsonFile = writeJsonFile; //# sourceMappingURL=filehandler.js.map