Skip to content

Commit f6f6fa7

Browse files
committed
template: Support base64-encoding data file contents
This is done with a new `base64` parameter property: ```yaml definitions: var: dataFile: example base64: true template: | {{var}} ```
1 parent 822c6a6 commit f6f6fa7

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,16 @@ Parameters with a `dataFile` property:
320320
* have their `default` set to the contents of the file
321321
* given a default `format` of `hidden`
322322
323+
Additionally, the contents of the data file can be base64 encoded before being used as for `default` by setting the `base64` property to `true`:
324+
325+
```yaml
326+
definitions:
327+
var:
328+
dataFile: example
329+
base64: true
330+
template: |
331+
{{var}}
332+
```
323333
324334
## Development
325335

lib/template.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,13 @@ class Template {
410410
if (!propDef.format) {
411411
propDef.format = 'hidden';
412412
}
413-
propDef.default = dataFiles[propDef.dataFile];
413+
414+
const dataFile = dataFiles[propDef.dataFile];
415+
if (propDef.base64) {
416+
propDef.default = Buffer.from(dataFile, 'utf8').toString('base64');
417+
} else {
418+
propDef.default = dataFile;
419+
}
414420
delete propDef.dataFile;
415421
}
416422
break;

test/template.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1257,10 +1257,14 @@ describe('Template class tests', function () {
12571257
definitions:
12581258
fromFile:
12591259
dataFile: textData.txt
1260+
base64:
1261+
dataFile: textData.txt
1262+
base64: true
12601263
template: |-
12611264
{{fromFile}}
1265+
{{base64}}
12621266
`;
1263-
const reference = 'Lorem ipsum\n';
1267+
const reference = 'Lorem ipsum\n\nTG9yZW0gaXBzdW0K';
12641268
return Template.loadYaml(yamldata, { dataProvider })
12651269
.then((tmpl) => {
12661270
const schema = tmpl.getParametersSchema();
@@ -1272,6 +1276,8 @@ describe('Template class tests', function () {
12721276
assert.strictEqual(fileProp.format, 'hidden');
12731277
assert.strictEqual(fileProp.default, 'Lorem ipsum\n');
12741278

1279+
assert.strictEqual(schema.properties.base64.default, 'TG9yZW0gaXBzdW0K');
1280+
12751281
const rendered = tmpl.render();
12761282
console.log(rendered);
12771283
assert.strictEqual(rendered, reference);

0 commit comments

Comments
 (0)