TechDocs: Dir preparer should also use URL Reader

This commit is contained in:
Himanshu Mishra
2021-01-28 14:54:48 +01:00
parent 8793dd1f07
commit 4bc89c555a
3 changed files with 21 additions and 12 deletions
+1 -1
View File
@@ -208,7 +208,7 @@ catalog:
# Backstage example components
- type: file
target: ../catalog-model/examples/all-components.yaml
# Example component for github-actions
# Example component for github-actions and TechDocs
- type: file
target: ../../plugins/github-actions/examples/sample.yaml
# Example component for TechDocs
@@ -18,17 +18,19 @@ import { Entity } from '@backstage/catalog-model';
import { Config } from '@backstage/config';
import path from 'path';
import { parseReferenceAnnotation, checkoutGitRepository } from '../../helpers';
import { InputError } from '@backstage/backend-common';
import { UrlReader, InputError } from '@backstage/backend-common';
import parseGitUrl from 'git-url-parse';
import { Logger } from 'winston';
export class DirectoryPreparer implements PreparerBase {
private readonly config: Config;
private readonly logger: Logger;
constructor(config: Config, logger: Logger) {
constructor(
private readonly config: Config,
private readonly logger: Logger,
private readonly reader: UrlReader,
) {
this.config = config;
this.logger = logger;
this.reader = reader;
}
private async resolveManagedByLocationToDir(entity: Entity) {
@@ -41,9 +43,12 @@ export class DirectoryPreparer implements PreparerBase {
`Building docs for entity with type 'dir' and managed-by-location '${type}'`,
);
switch (type) {
case 'url': {
const response = await this.reader.readTree(target);
return await response.dir();
}
case 'github':
case 'gitlab':
case 'url':
case 'azure/api': {
const parsedGitLocation = parseGitUrl(target);
const repoLocation = await checkoutGitRepository(
@@ -56,7 +61,6 @@ export class DirectoryPreparer implements PreparerBase {
path.join(repoLocation, parsedGitLocation.filepath),
);
}
case 'file':
return path.dirname(target);
default:
@@ -35,17 +35,22 @@ export class Preparers implements PreparerBuilder {
): Promise<PreparerBuilder> {
const preparers = new Preparers();
const directoryPreparer = new DirectoryPreparer(config, logger);
const urlPreparer = new UrlPreparer(reader, logger);
preparers.register('url', urlPreparer);
/**
* Dir preparer is a syntactic sugar for users to define techdocs-ref annotation.
* When using dir preparer, the docs will be fetched using URL Reader.
*/
const directoryPreparer = new DirectoryPreparer(config, logger, reader);
preparers.register('dir', directoryPreparer);
// Common git preparers will be deprecated soon.
const commonGitPreparer = new CommonGitPreparer(config, logger);
preparers.register('github', commonGitPreparer);
preparers.register('gitlab', commonGitPreparer);
preparers.register('azure/api', commonGitPreparer);
const urlPreparer = new UrlPreparer(reader, logger);
preparers.register('url', urlPreparer);
return preparers;
}