@quo0/stiletto
Version:
With stiletto library you will be able to mock requests and choose between preconfigured responses right on the fly via UI
154 lines • 7.15 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileStructureEntityModel = void 0;
const tslib_1 = require("tslib");
const cosmiconfig_1 = require("cosmiconfig");
const cosmiconfig_typescript_loader_1 = require("@endemolshinegroup/cosmiconfig-typescript-loader");
const fs_1 = require("../../services/fs");
const constants_1 = require("../../constants");
const path_1 = require("path");
class FileStructureEntityModel {
constructor({ privateName, name, defaultPath, defaultExtension }) {
this.privateName = privateName;
this.name = name;
this.defaultPath = defaultPath;
this.defaultExtension = defaultExtension;
}
clearCache() {
this.data = null;
this.path = null;
this.extension = null;
this.explorer = null;
}
find() {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
if (this.data && this.path && this.extension && this.explorer) {
return {
data: this.data,
path: this.path,
extension: this.extension,
explorer: this.explorer,
};
}
const pathToStart = this.defaultPath;
// TODO:IMPLEMENTATION Check travers path/to/target/file.ext
for (const extension of constants_1.FILE_EXTENSIONS) {
const fileNameWithExtension = this.addFileExtension(this.name, extension);
const relativePathToSearchIn = this.replaceExtensionPlaceholder(pathToStart, extension).split(process.cwd())[1];
const fileExplorer = (0, cosmiconfig_1.cosmiconfig)(fileNameWithExtension, {
searchPlaces: [relativePathToSearchIn],
loaders: { '.ts': cosmiconfig_typescript_loader_1.default },
});
const result = yield fileExplorer.search();
if (result && result.filepath && result.config) {
const foundExtension = (0, path_1.extname)(result.filepath).replace('.', '');
// TODO:TS can we use only FILE_EXTENSIONS somehow ?
if (foundExtension === 'ts' || foundExtension === 'js' || foundExtension === 'json') {
this.data = result.config;
this.path = result.filepath;
this.extension = foundExtension;
this.explorer = fileExplorer;
return {
data: this.data,
path: this.path,
extension: this.extension,
explorer: this.explorer,
};
}
}
}
return null;
});
}
create(fileData, fileExtension) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
const data = fileData || constants_1.DEFAULT_FILE_DATA[this.privateName]; // TODO:TS how to get rid of unknown to pass FileDataInterface to this.write()
const path = this.path || this.defaultPath;
const extension = fileExtension || this.extension || this.defaultExtension;
// TODO:IMPLEMENTATION do we need to check file alrady exists and throw?
const alreadyExistingFilePath = yield this.getFileIsExists(path, extension);
if (alreadyExistingFilePath) {
throw `File ${this.name} already exists at ${alreadyExistingFilePath}`;
}
return this.writeFile(data, path, extension);
});
}
read() {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
if (this.data) {
return this.data;
}
if (this.explorer && this.path) {
const result = yield this.explorer.load(this.path);
if (result) {
return result.config;
}
}
const result = yield this.find();
if (result && result.data) {
return result.data;
}
// TODO:IMPLEMENTATION do we need to throw? maybe return null?
// throw `File ${this.name} was not found exists at ${this.path}`;
return null;
});
}
update(newData) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
// TODO:IMPLEMENTATION maybe register update mapper function at structure and pass only data for patching?
if (this.path && this.extension) {
return this.writeFile(newData, this.path, this.extension);
}
console.error(`File ${this.name} was not updated. File was not found`);
});
}
delete() {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
// TODO:IMPLEMENTATION should we throw if file was not found?
if (this.path && this.extension) {
if (yield (0, fs_1.pathExists)(this.path)) {
return (0, fs_1.remove)(this.path);
}
}
console.error(`File ${this.name} was not deleted. File was not found`);
});
}
writeFile(data, path, extension) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
const filePath = this.replaceExtensionPlaceholder(path, extension);
const fileData = this.mapFileContent(data, extension);
yield (0, fs_1.writeFile)(filePath, fileData);
this.data = data;
this.path = path;
this.extension = extension;
});
}
addFileExtension(pathOrName, extension) {
if (pathOrName.endsWith(`.${extension}`)) {
return pathOrName;
}
return `${pathOrName}.${extension}`;
}
replaceExtensionPlaceholder(pathOrName, extension) {
return pathOrName.replace('<extension>', extension);
}
mapFileContent(data, extension) {
return null ||
extension === 'json' ? (0, fs_1.stringifyJSON)(data) :
extension === 'js' ? (0, fs_1.wrapInModuleExports)(data) : (0, fs_1.wrapInExportDefault)(data);
}
getFileIsExists(path, targetExtension) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
const targetExtensions = targetExtension ? [targetExtension] : constants_1.FILE_EXTENSIONS;
for (const extension of targetExtensions) {
const filePathWithExtension = this.replaceExtensionPlaceholder(path, extension);
if (yield (0, fs_1.pathExists)(filePathWithExtension)) {
return filePathWithExtension;
}
}
return false;
});
}
}
exports.FileStructureEntityModel = FileStructureEntityModel;
//# sourceMappingURL=file-structure-entity.model.js.map