pass emit to resolvers

Signed-off-by: Kiss Miklos <miklos@roadie.io>
This commit is contained in:
Kiss Miklos
2022-07-06 16:14:14 +02:00
parent a35aae43b0
commit 3e3d8282ef
2 changed files with 19 additions and 19 deletions
@@ -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(() => {
@@ -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<JsonValue> {
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<JsonValue> {
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<JsonValue> {
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<string> {
): 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}`,