cumulocity-cypress
Version:
Cypress commands for Cumulocity IoT
150 lines (149 loc) • 5.09 kB
JavaScript
import * as fs from "fs";
import * as path from "path";
import * as glob from "glob";
import debug from "debug";
import { pactId } from "../c8ypact";
import { safeStringify } from "../../util";
import lodash1 from "lodash";
import * as lodash2 from "lodash";
const _ = lodash1 || lodash2;
const log = debug("c8y:fileadapter");
/**
* Default implementation of C8yPactFileAdapter which loads and saves pact objects from/to
* json files using C8yPact objects.
*/
export class C8yPactDefaultFileAdapter {
constructor(folder) {
this.folder = path.isAbsolute(folder)
? folder
: this.toAbsolutePath(folder);
}
description() {
return `C8yPactDefaultFileAdapter: ${this.folder}`;
}
getFolder() {
return this.folder;
}
loadPacts() {
const jsonFiles = this.loadPactObjects();
log(`loadPacts() - ${jsonFiles.length} pact files from ${this.folder}`);
return jsonFiles.reduce((acc, obj) => {
if (!obj?.info?.id)
return acc;
acc[obj.info.id] = obj;
return acc;
}, {});
}
loadPact(id) {
log(`loadPact() - ${id}`);
const pId = pactId(id);
if (pId == null) {
log(`loadPact() - invalid pact id ${id} -> ${pId}`);
return null;
}
if (!this.folder || !fs.existsSync(this.folder)) {
log(`loadPact() - folder ${this.folder} does not exist`);
return null;
}
const file = path.join(this.folder, `${pId}.json`);
if (fs.existsSync(file)) {
const pact = fs.readFileSync(file, "utf-8");
log(`loadPact() - ${file} loaded`);
const json = JSON.parse(pact);
log(`loadPact() - parsed as json`);
return json || null;
}
else {
log(`loadPact() - ${file} does not exist`);
}
return null;
}
pactExists(id) {
return fs.existsSync(path.join(this.folder, `${pactId(id)}.json`));
}
savePact(pact) {
this.createFolderRecursive(this.folder);
const pId = pactId(pact.id);
if (pId == null) {
log(`savePact() - invalid pact id ${pact.id} -> ${pId}`);
return;
}
const file = path.join(this.folder, `${pId}.json`);
log(`savePact() - write ${file} (${pact.records?.length || 0} records)`);
try {
fs.writeFileSync(file, safeStringify({
id: pact.id,
info: pact.info,
records: pact.records,
}, 2), "utf-8");
}
catch (error) {
console.error(`Failed to save pact.`, error);
}
}
deletePact(id) {
const pId = pactId(id);
if (pId == null) {
log(`deletePact() - invalid pact id ${id} -> ${pId}`);
return;
}
const filePath = path.join(this.folder, `${pId}.json`);
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
log(`deletePact() - deleted ${filePath}`);
}
else {
log(`deletePact() - ${filePath} does not exist`);
}
}
readJsonFiles() {
log(`readJsonFiles() - ${this.folder}`);
if (!this.folder || !fs.existsSync(this.folder)) {
log(`readJsonFiles() - ${this.folder} does not exist`);
return [];
}
const jsonFiles = glob.sync(path.join(this.folder, "*.json"));
log(`readJsonFiles() - reading ${jsonFiles.length} json files from ${this.folder}`);
const pacts = jsonFiles.map((file) => {
return fs.readFileSync(file, "utf-8");
});
return pacts;
}
deleteJsonFiles() {
if (!this.folder || !fs.existsSync(this.folder)) {
log(`deleteJsonFiles() - ${this.folder} does not exist`);
return;
}
const jsonFiles = glob.sync(path.join(this.folder, "*.json"));
log(`deleteJsonFiles() - deleting ${jsonFiles.length} json files from ${this.folder}`);
jsonFiles.forEach((file) => {
fs.unlinkSync(file);
});
}
loadPactObjects() {
const pacts = this.readJsonFiles();
return pacts.map((pact) => JSON.parse(pact));
}
createFolderRecursive(f) {
log(`createFolderRecursive() - ${f}`);
if (!f || !_.isString(f))
return undefined;
const absolutePath = !path.isAbsolute(f) ? this.toAbsolutePath(f) : f;
if (f !== absolutePath) {
log(`createFolderRecursive() - resolved ${f} to ${absolutePath}`);
}
if (fs.existsSync(f))
return undefined;
const result = fs.mkdirSync(absolutePath, { recursive: true });
if (result) {
log(`createFolderRecursive() - created ${absolutePath}`);
}
return result;
}
toAbsolutePath(f) {
return path.isAbsolute(f) ? f : path.resolve(process.cwd(), f);
}
isNodeError(error, type) {
return error instanceof type;
}
}