Skip to content

Commit

Permalink
chore: read files recursively for data management (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
ASaiAnudeep authored Jan 1, 2025
1 parent c05f573 commit 6a92340
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 36 deletions.
35 changes: 6 additions & 29 deletions src/exports/stash.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const fs = require('fs');
const pt = require('path');
const log = require('../plugins/logger');
const { PactumConfigurationError } = require('../helpers/errors');
const { loadDataManagement } = require('../helpers/file.utils');
const config = require('../config');
const jq = require('json-query');

Expand All @@ -12,33 +9,13 @@ let dataStore = {};
const stash = {

loadData(path = './data') {
if (!fs.existsSync(path)) {
log.error(`path not found - ${path}`);
log.warn(`Current Working Dir: ${process.cwd()}`);
throw new PactumConfigurationError('`path` not found');
const { templates, maps } = loadDataManagement(path);
for (const template of templates) {
this.addDataTemplate(template);
}
const stats = fs.lstatSync(path);
if (!stats.isDirectory()) {
log.error(`path should be a directory - ${path}`);
throw new PactumConfigurationError('`path` should be a directory');
for (const map of maps) {
this.addDataMap(map);
}
const dir = fs.readdirSync(path);
dir.forEach(file => {
if (file === 'maps') {
const maps = fs.readdirSync(pt.resolve(path, file));
maps.forEach(map => this.addDataMap(JSON.parse(fs.readFileSync(pt.resolve(path, file, map)))));
}
if (file === 'templates') {
const templates = fs.readdirSync(pt.resolve(path, file));
templates.forEach(template => this.addDataTemplate(JSON.parse(fs.readFileSync(pt.resolve(path, file, template)))));
}
if (file.endsWith('.template.json')) {
this.addDataTemplate(JSON.parse(fs.readFileSync(pt.resolve(path, file))));
}
if (file.endsWith('.map.json')) {
this.addDataMap(JSON.parse(fs.readFileSync(pt.resolve(path, file))));
}
});
},

addDataMap(key, value) {
Expand Down
53 changes: 49 additions & 4 deletions src/helpers/file.utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const fs = require('fs');
const path = require('path');
const pt = require('path');
const config = require('../config');
const log = require('../plugins/logger');
const { PactumConfigurationError } = require('./errors');

function getSnapshotDirAndName(name) {
return {
Expand Down Expand Up @@ -28,7 +30,6 @@ function saveSnapshot(name, data) {
fs.writeFileSync(`${snapshotDir}/${snapshotFile}`, JSON.stringify(data, null, 2));
}


function findFileRecursively(name, dir = config.data.dir) {
if (fs.existsSync(name)) {
return fs.readFileSync(name);
Expand All @@ -41,7 +42,7 @@ function findFileRecursively(name, dir = config.data.dir) {
// get folders in dir
const files = fs.readdirSync(dir);
for (const file of files) {
const dirPath = path.resolve(dir, file);
const dirPath = pt.resolve(dir, file);
const stats = fs.statSync(dirPath);
if (stats.isDirectory()) {
const result = findFileRecursively(name, dirPath);
Expand All @@ -53,8 +54,52 @@ function findFileRecursively(name, dir = config.data.dir) {
}
}

function loadDataManagement(path = './data') {
const templates = [];
const maps = [];
if (!fs.existsSync(path)) {
log.error(`path not found - ${path}`);
log.warn(`Current Working Dir: ${process.cwd()}`);
throw new PactumConfigurationError(`path not found to load data - '${path}'`);
}
const stats = fs.lstatSync(path);
if (!stats.isDirectory()) {
log.error(`path should be a directory - ${path}`);
throw new PactumConfigurationError(`path should be a directory to load data - '${path}'`);
}
const dir = fs.readdirSync(path);
for (const file of dir) {
if (file.endsWith('.template.json')) {
templates.push(JSON.parse(fs.readFileSync(pt.resolve(path, file))));
}
if (file.endsWith('.map.json')) {
maps.push(JSON.parse(fs.readFileSync(pt.resolve(path, file))));
}
if (file === 'maps') {
loadAllFilesRecursively(pt.resolve(path, file), maps);
}
if (file === 'templates') {
loadAllFilesRecursively(pt.resolve(path, file), templates);
}
}
return { templates, maps };
}

function loadAllFilesRecursively(dir, data = []) {
const files = fs.readdirSync(dir);
for (const file of files) {
if (fs.lstatSync(pt.resolve(dir, file)).isDirectory()) {
loadAllFilesRecursively(pt.resolve(dir, file), data);
} else {
const json = JSON.parse(fs.readFileSync(pt.resolve(dir, file)));
data.push(json);
}
}
}

module.exports = {
getSnapshotFile,
saveSnapshot,
findFileRecursively
findFileRecursively,
loadDataManagement
};
6 changes: 6 additions & 0 deletions test/data/dm/maps/sub1/Hosue.map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"House": {
"Name": "WinterFell",
"Owner": "Stark"
}
}
6 changes: 6 additions & 0 deletions test/data/dm/templates/sub2/House.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"House:New": {
"Name": "$M{House.Name}",
"Owner": "$M{House.Owner}"
}
}
22 changes: 19 additions & 3 deletions test/unit/stash.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,33 @@ describe('Stash', () => {
} catch (error) {
err = error;
}
expect(err.message).equals('`path` not found');
expect(err.message).equals(`path not found to load data - './data'`);
});

it('default path not found', () => {
it('path is not a directory', () => {
let err;
try {
stash.loadData('./src/index.js');
} catch (error) {
err = error;
}
expect(err.message).equals('`path` should be a directory');
expect(err.message).equals(`path should be a directory to load data - './src/index.js'`);
});

it('load data from sub folders', () => {
stash.loadData('./test/data/dm');
expect(stash.getDataMap()).deep.equals({
"House": {
"Name": "WinterFell",
"Owner": "Stark"
}
});
expect(stash.getDataTemplate()).deep.equals({
"House:New": {
"Name": "$M{House.Name}",
"Owner": "$M{House.Owner}"
}
});
});


Expand Down

0 comments on commit 6a92340

Please sign in to comment.