From 3e3d8282efcca45022abde7b3c4db585fe361e9d Mon Sep 17 00:00:00 2001 From: Kiss Miklos Date: Wed, 6 Jul 2022 16:14:14 +0200 Subject: [PATCH] pass emit to resolvers Signed-off-by: Kiss Miklos --- .../modules/core/PlaceholderProcessor.test.ts | 2 ++ .../src/modules/core/PlaceholderProcessor.ts | 36 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/plugins/catalog-backend/src/modules/core/PlaceholderProcessor.test.ts b/plugins/catalog-backend/src/modules/core/PlaceholderProcessor.test.ts index c8a5a5355a..9bfa9027d9 100644 --- a/plugins/catalog-backend/src/modules/core/PlaceholderProcessor.test.ts +++ b/plugins/catalog-backend/src/modules/core/PlaceholderProcessor.test.ts @@ -397,6 +397,7 @@ describe('yamlPlaceholderResolver', () => { baseUrl: 'https://github.com/backstage/backstage/a/b/catalog-info.yaml', read, resolveUrl: (url, base) => integrations.resolveUrl({ url, base }), + emit: () => {}, }; beforeEach(() => { @@ -442,6 +443,7 @@ describe('jsonPlaceholderResolver', () => { baseUrl: 'https://github.com/backstage/backstage/a/b/catalog-info.yaml', read, resolveUrl: (url, base) => integrations.resolveUrl({ url, base }), + emit: () => {}, }; beforeEach(() => { diff --git a/plugins/catalog-backend/src/modules/core/PlaceholderProcessor.ts b/plugins/catalog-backend/src/modules/core/PlaceholderProcessor.ts index bcd474016b..be2e9f9f5b 100644 --- a/plugins/catalog-backend/src/modules/core/PlaceholderProcessor.ts +++ b/plugins/catalog-backend/src/modules/core/PlaceholderProcessor.ts @@ -42,6 +42,7 @@ export type PlaceholderResolverParams = { baseUrl: string; read: PlaceholderResolverRead; resolveUrl: PlaceholderResolverResolveUrl; + emit: CatalogProcessorEmit; }; /** @public */ @@ -134,18 +135,6 @@ export class PlaceholderProcessor implements CatalogProcessor { base, }); - emit( - processingResult.refresh( - `url:${relativeUrl({ - key: resolverKey, - value: resolverValue, - baseUrl: location.target, - read, - resolveUrl, - })}`, - ), - ); - return [ await resolver({ key: resolverKey, @@ -153,6 +142,7 @@ export class PlaceholderProcessor implements CatalogProcessor { baseUrl: location.target, read, resolveUrl, + emit, }), true, ]; @@ -170,11 +160,13 @@ export class PlaceholderProcessor implements CatalogProcessor { export async function yamlPlaceholderResolver( params: PlaceholderResolverParams, ): Promise { - const text = await readTextLocation(params); + const { content, url } = await readTextLocation(params); + + params.emit(processingResult.refresh(`url:${url}`)); let documents: yaml.Document.Parsed[]; try { - documents = yaml.parseAllDocuments(text).filter(d => d); + documents = yaml.parseAllDocuments(content).filter(d => d); } catch (e) { throw new Error( `Placeholder \$${params.key} failed to parse YAML data at ${params.value}, ${e}`, @@ -201,10 +193,12 @@ export async function yamlPlaceholderResolver( export async function jsonPlaceholderResolver( params: PlaceholderResolverParams, ): Promise { - const text = await readTextLocation(params); + const { content, url } = await readTextLocation(params); + + params.emit(processingResult.refresh(`url:${url}`)); try { - return JSON.parse(text); + return JSON.parse(content); } catch (e) { throw new Error( `Placeholder \$${params.key} failed to parse JSON data at ${params.value}, ${e}`, @@ -215,7 +209,11 @@ export async function jsonPlaceholderResolver( export async function textPlaceholderResolver( params: PlaceholderResolverParams, ): Promise { - return await readTextLocation(params); + const { content, url } = await readTextLocation(params); + + params.emit(processingResult.refresh(`url:${url}`)); + + return content; } /* @@ -224,12 +222,12 @@ export async function textPlaceholderResolver( async function readTextLocation( params: PlaceholderResolverParams, -): Promise { +): Promise<{ content: string; url: string }> { const newUrl = relativeUrl(params); try { const data = await params.read(newUrl); - return data.toString('utf-8'); + return { content: data.toString('utf-8'), url: newUrl }; } catch (e) { throw new Error( `Placeholder \$${params.key} could not read location ${params.value}, ${e}`,