@show-runner/fixturelibrary
Version:
Utility library making it easy to work with the open-fixture-library.
54 lines • 2 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = require("fs-extra");
const node_path_1 = __importDefault(require("node:path"));
const fixtureindex_1 = require("./fixtureindex");
class FileHandler {
// eslint-disable-next-line class-methods-use-this
get storageDirectory() {
return process.env.OFL_INDEX ?? node_path_1.default.resolve(__dirname, '../.fixturelibrary');
}
get indexPath() {
return `${this.storageDirectory}/index.json`;
}
async readJson(filename) {
// Preparing path
let filePath = `${this.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;
}
async writeJson(name, data, override = false) {
// Preparing filePath
let filePath = `${this.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.default = new FileHandler();
//# sourceMappingURL=filehandler.js.map