diff --git a/.changeset/perfect-cherries-vanish.md b/.changeset/perfect-cherries-vanish.md new file mode 100644 index 000000000..69654f79a --- /dev/null +++ b/.changeset/perfect-cherries-vanish.md @@ -0,0 +1,5 @@ +--- +'@roadiehq/scaffolder-backend-module-utils': minor +--- + +Add ability to load multi-yaml files using the yaml JSONata transformer. diff --git a/plugins/scaffolder-actions/scaffolder-backend-module-utils/src/actions/jsonata/yaml.test.ts b/plugins/scaffolder-actions/scaffolder-backend-module-utils/src/actions/jsonata/yaml.test.ts index fd36b72cf..ae0c2fba1 100644 --- a/plugins/scaffolder-actions/scaffolder-backend-module-utils/src/actions/jsonata/yaml.test.ts +++ b/plugins/scaffolder-actions/scaffolder-backend-module-utils/src/actions/jsonata/yaml.test.ts @@ -21,12 +21,6 @@ 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(), @@ -34,6 +28,15 @@ describe('roadiehq:utils:jsonata:yaml:transform', () => { 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 () => { @@ -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'], + }); + }); }); diff --git a/plugins/scaffolder-actions/scaffolder-backend-module-utils/src/actions/jsonata/yaml.ts b/plugins/scaffolder-actions/scaffolder-backend-module-utils/src/actions/jsonata/yaml.ts index de01e9bbe..25971d189 100644 --- a/plugins/scaffolder-actions/scaffolder-backend-module-utils/src/actions/jsonata/yaml.ts +++ b/plugins/scaffolder-actions/scaffolder-backend-module-utils/src/actions/jsonata/yaml.ts @@ -25,6 +25,7 @@ export function createYamlJSONataTransformAction() { path: string; expression: string; options?: supportedDumpOptions; + loadAll?: boolean; as?: 'string' | 'object'; }>({ id: 'roadiehq:utils:jsonata:yaml:transform', @@ -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: @@ -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);