From 572cdefa7343af248972db83fd99e50a27adab1e Mon Sep 17 00:00:00 2001 From: Anudeep Date: Wed, 1 Jan 2025 16:36:34 +0530 Subject: [PATCH] chore: read files recursively for data management --- src/exports/stash.js | 35 +++--------- src/helpers/file.utils.js | 53 +++++++++++++++++-- test/data/dm/maps/sub1/Hosue.map.json | 6 +++ .../dm/templates/sub2/House.template.json | 6 +++ test/unit/stash.spec.js | 22 ++++++-- 5 files changed, 86 insertions(+), 36 deletions(-) create mode 100644 test/data/dm/maps/sub1/Hosue.map.json create mode 100644 test/data/dm/templates/sub2/House.template.json diff --git a/src/exports/stash.js b/src/exports/stash.js index 590c137..62115b6 100644 --- a/src/exports/stash.js +++ b/src/exports/stash.js @@ -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'); @@ -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) { diff --git a/src/helpers/file.utils.js b/src/helpers/file.utils.js index 6fad142..6c799f4 100644 --- a/src/helpers/file.utils.js +++ b/src/helpers/file.utils.js @@ -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 { @@ -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); @@ -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); @@ -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 }; \ No newline at end of file diff --git a/test/data/dm/maps/sub1/Hosue.map.json b/test/data/dm/maps/sub1/Hosue.map.json new file mode 100644 index 0000000..56a4abc --- /dev/null +++ b/test/data/dm/maps/sub1/Hosue.map.json @@ -0,0 +1,6 @@ +{ + "House": { + "Name": "WinterFell", + "Owner": "Stark" + } +} \ No newline at end of file diff --git a/test/data/dm/templates/sub2/House.template.json b/test/data/dm/templates/sub2/House.template.json new file mode 100644 index 0000000..cd3fb41 --- /dev/null +++ b/test/data/dm/templates/sub2/House.template.json @@ -0,0 +1,6 @@ +{ + "House:New": { + "Name": "$M{House.Name}", + "Owner": "$M{House.Owner}" + } +} \ No newline at end of file diff --git a/test/unit/stash.spec.js b/test/unit/stash.spec.js index bbcd54d..45e2250 100644 --- a/test/unit/stash.spec.js +++ b/test/unit/stash.spec.js @@ -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}" + } + }); });