Skip to content

Commit 59b22ae

Browse files
author
Chris Wiechmann
committed
Added the option for a dynamich filename for fileRead method
1 parent ded04f0 commit 59b22ae

5 files changed

Lines changed: 66 additions & 2 deletions

File tree

api-builder-plugin-fn-file/Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
88

9+
## [0.0.6] 2021-01-09
10+
### Added
11+
- Added option for a dynamic path for readFile
12+
913
## [0.0.5] 2020-11-16
1014
### Added
1115
- New Exit: NotFound if the file cannot be found

api-builder-plugin-fn-file/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@axway-api-builder-ext/api-builder-plugin-fn-file",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "File Flow-Node to read/write files as part of an API-Builder flow.",
55
"author": "Chris Wiechmann <cwiechmann@axway.com> (http://www.axway.com)",
66
"license": "Apache-2.0",

api-builder-plugin-fn-file/src/flow-nodes.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ flow-nodes:
232232
schema:
233233
default: false
234234
type: boolean
235+
data:
236+
name: Data
237+
description: 'If you are using a dynamic filename that contains variables, you need to provide this data object, which is used to replace your variables. Example: ${conf.myFolder}/some/static/${filename}'
238+
required: false
239+
schema:
240+
type: object
235241
outputs:
236242
next:
237243
name: Next

api-builder-plugin-fn-file/src/standardFileActions.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async function writeFile(params, options) {
6060
}
6161

6262
async function readFile(params, options) {
63-
var { filename, encoding, parseJson } = params;
63+
var { filename, encoding, parseJson, data } = params;
6464
const { logger } = options;
6565
if (!filename) {
6666
throw new Error('Missing required parameter: filename');
@@ -72,6 +72,10 @@ async function readFile(params, options) {
7272
if(params.notFoundFails) {
7373
notFoundFails = params.notFoundFails;
7474
}
75+
debugger;
76+
if(data) {
77+
filename = await interpolate(filename, data, logger);
78+
}
7579
try {
7680
var content = await fs.readFile(filename, {encoding: encoding});
7781
} catch(ex) {
@@ -88,6 +92,38 @@ async function readFile(params, options) {
8892
return content;
8993
}
9094

95+
async function interpolate(string, data, logger) {
96+
if(string) {
97+
string = JSON.stringify(string);
98+
} else {
99+
return;
100+
}
101+
logger.debug(`Got string: ${string}`);
102+
var result = string.replace(/\${([^}]+)}/g, (_, target) => {
103+
let keys = target.split(".");
104+
return keys.reduce((prev, curr) => {
105+
if (curr.search(/\[/g) > -1) {
106+
//if element/key in target array is array, get the value and return
107+
let m_curr = curr.replace(/\]/g, "");
108+
let arr = m_curr.split("[");
109+
return arr.reduce((pr, cu) => {
110+
if(pr[cu] == undefined) {
111+
throw new Error(`Missing data for selector: \$\{${curr}\}`);
112+
}
113+
return pr && pr[cu];
114+
}, prev);
115+
} else {
116+
//else it is a object, get the value and return
117+
if(prev[curr] == undefined) {
118+
throw new Error(`Missing data for selector: \$\{${curr}\}`);
119+
}
120+
return prev && prev[curr];
121+
}
122+
}, data);
123+
});
124+
return JSON.parse(result);
125+
};
126+
91127
module.exports = {
92128
writeFile,
93129
readFile

api-builder-plugin-fn-file/test/testStandardFileHandling.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,24 @@ describe('Standard file handling test', () => {
216216
expect(output).to.equal('next');
217217
expect(value).to.deep.equal(testData);
218218
});
219+
220+
it('should read a standard file as normal text using a dynamic path', async () => {
221+
debugger;
222+
const testFilename = getTestFilename('testFile.txt');
223+
fs.writeFileSync(testFilename, 'Some data');
224+
const testFile = path.parse(testFilename);
225+
const data = {
226+
fileDetails: {
227+
basename: testFile.base
228+
}
229+
}
230+
const givenFilename = testFile.dir + "/${fileDetails.basename}"
231+
232+
const { value, output } = await flowNode.readFile({ filename: givenFilename, data: data });
233+
234+
expect(output).to.equal('next');
235+
expect(value).to.equal('Some data');
236+
});
219237
});
220238
});
221239

0 commit comments

Comments
 (0)