Skip to content

Commit

Permalink
Merge pull request #1225 from RoadieHQ/load-all
Browse files Browse the repository at this point in the history
ability to load multi yaml files in transform action
  • Loading branch information
punkle authored Jan 16, 2024
2 parents d4235dd + 1f698aa commit b73ee5b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-cherries-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@roadiehq/scaffolder-backend-module-utils': minor
---

Add ability to load multi-yaml files using the yaml JSONata transformer.
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@ import mock from 'mock-fs';
import yaml from 'js-yaml';

describe('roadiehq:utils:jsonata:yaml:transform', () => {
beforeEach(() => {
mock({
'fake-tmp-dir': {},
});
});
afterEach(() => mock.restore());
const mockContext = {
workspacePath: 'lol',
logger: getVoidLogger(),
logStream: new PassThrough(),
output: jest.fn(),
createTemporaryDirectory: jest.fn(),
};

beforeEach(() => {
mockContext.output.mockReset();
mock({
'fake-tmp-dir': {},
});
});
afterEach(() => mock.restore());

const action = createYamlJSONataTransformAction();

it('should output default string result of having applied the given transformation', async () => {
Expand Down Expand Up @@ -135,4 +138,26 @@ describe('roadiehq:utils:jsonata:yaml:transform', () => {
hello: ['world', 'item2'],
});
});

it('should be able to handle multi yaml and apply a transformation', async () => {
mock({
'fake-tmp-dir': {
'fake-file.yaml': '---\nhello: [world]\n---\nfoo: [bar]',
},
});
await action.handler({
...mockContext,
workspacePath: 'fake-tmp-dir',
input: {
path: 'fake-file.yaml',
expression: '$[0]',
loadAll: true,
as: 'object',
},
});

expect(mockContext.output).toHaveBeenCalledWith('result', {
hello: ['world'],
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function createYamlJSONataTransformAction() {
path: string;
expression: string;
options?: supportedDumpOptions;
loadAll?: boolean;
as?: 'string' | 'object';
}>({
id: 'roadiehq:utils:jsonata:yaml:transform',
Expand All @@ -46,6 +47,14 @@ export function createYamlJSONataTransformAction() {
description: 'JSONata expression to perform on the input',
type: 'string',
},
loadAll: {
expression: {
title: 'Load All',
description:
'Use this if the yaml source file contains mutliple yaml objects',
type: 'boolean',
},
},
as: {
title: 'Desired Result Type',
description:
Expand Down Expand Up @@ -78,8 +87,12 @@ export function createYamlJSONataTransformAction() {
ctx.workspacePath,
ctx.input.path,
);

const data = yaml.load(fs.readFileSync(sourceFilepath).toString());
let data;
if (ctx.input.loadAll) {
data = yaml.loadAll(fs.readFileSync(sourceFilepath).toString());
} else {
data = yaml.load(fs.readFileSync(sourceFilepath).toString());
}
const expression = jsonata(ctx.input.expression);
const result = expression.evaluate(data);

Expand Down

0 comments on commit b73ee5b

Please sign in to comment.