Use parser to read files

Fix https://github.com/backstage/backstage/issues/7706

Signed-off-by: Julio Zynger <julio.zynger@soundcloud.com>
This commit is contained in:
Julio Zynger
2021-10-20 18:19:32 +02:00
parent 8238c8998f
commit 3adaf88db2
3 changed files with 21 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend': patch
---
Take CatalogParser in account when processing file locations.
@@ -21,6 +21,7 @@ import {
CatalogProcessorResult,
} from './types';
import path from 'path';
import { defaultEntityDataParser } from './util/parse';
describe('FileReaderProcessor', () => {
const fixturesRoot = path.join(__dirname, '__fixtures__/fileReaderProcessor');
@@ -33,7 +34,7 @@ describe('FileReaderProcessor', () => {
};
const generated = (await new Promise<CatalogProcessorResult>(emit =>
processor.readLocation(spec, false, emit),
processor.readLocation(spec, false, emit, defaultEntityDataParser),
)) as CatalogProcessorEntityResult;
expect(generated.type).toBe('entity');
@@ -49,7 +50,7 @@ describe('FileReaderProcessor', () => {
};
const generated = (await new Promise<CatalogProcessorResult>(emit =>
processor.readLocation(spec, false, emit),
processor.readLocation(spec, false, emit, defaultEntityDataParser),
)) as CatalogProcessorErrorResult;
expect(generated.type).toBe('error');
@@ -69,6 +70,7 @@ describe('FileReaderProcessor', () => {
{ type: 'file', target: `${path.join(fixturesRoot, '**', '*.yaml')}` },
false,
emit,
defaultEntityDataParser,
);
expect(emit).toBeCalledTimes(2);
@@ -20,8 +20,11 @@ import g from 'glob';
import path from 'path';
import { promisify } from 'util';
import * as result from './results';
import { CatalogProcessor, CatalogProcessorEmit } from './types';
import { parseEntityYaml } from './util/parse';
import {
CatalogProcessor,
CatalogProcessorEmit,
CatalogProcessorParser,
} from './types';
const glob = promisify(g);
@@ -30,6 +33,7 @@ export class FileReaderProcessor implements CatalogProcessor {
location: LocationSpec,
optional: boolean,
emit: CatalogProcessorEmit,
parser: CatalogProcessorParser,
): Promise<boolean> {
if (location.type !== 'file') {
return false;
@@ -44,9 +48,12 @@ export class FileReaderProcessor implements CatalogProcessor {
// The normalize converts to native slashes; the glob library returns
// forward slashes even on windows
for (const parseResult of parseEntityYaml(data, {
type: 'file',
target: path.normalize(fileMatch),
for await (const parseResult of parser({
data: data,
location: {
type: 'file',
target: path.normalize(fileMatch),
},
})) {
emit(parseResult);
}