Merge pull request #16667 from JosiahCraw/feature/add-offline-linguist

feat: add offline support for linguist
This commit is contained in:
Johan Haals
2023-03-06 09:57:08 +01:00
committed by GitHub
5 changed files with 33 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-linguist-backend': patch
---
Added support for linguist-js options using the linguistJSOptions in the plugin, the available config can be found [here](https://www.npmjs.com/package/linguist-js#API).
+11
View File
@@ -85,6 +85,17 @@ return createRouter({ schedule: schedule, age: { days: 30 } }, { ...env });
With the `age` setup like this if the language breakdown is older than 15 days it will get regenerated. It's recommended that if you choose to use this configuration to set it to a large value - 30, 90, or 180 - as this data generally does not change drastically.
## Linguist JS options
The default setup will use the default [linguist-js](https://www.npmjs.com/package/linguist-js) options, a full list of the available options can be found [here](https://www.npmjs.com/package/linguist-js#API).
```ts
return createRouter(
{ schedule: schedule, linguistJsOptions: { offline: true } },
{ ...env },
);
```
## Use Source Location
You may wish to use the `backstage.io/source-location` annotation over using the `backstage.io/linguist` as you may not be able to quickly add that annotation to your Entities. To do this you'll just need to set the `useSourceLocation` boolean to `true` in your `packages/backend/src/plugins/linguist.ts` when you call `createRouter`:
+3
View File
@@ -35,6 +35,7 @@ export class LinguistBackendApi {
batchSize?: number,
useSourceLocation?: boolean,
kind?: string[],
linguistJsOptions?: Record<string, unknown>,
);
// (undocumented)
getEntityLanguages(entityRef: string): Promise<Languages>;
@@ -82,6 +83,8 @@ export interface PluginOptions {
// (undocumented)
kind?: string[];
// (undocumented)
linguistJsOptions?: Record<string, unknown>;
// (undocumented)
schedule?: TaskScheduleDefinition;
// (undocumented)
useSourceLocation?: boolean;
@@ -57,6 +57,7 @@ export class LinguistBackendApi {
private readonly batchSize?: number;
private readonly useSourceLocation?: boolean;
private readonly kind: string[];
private readonly linguistJsOptions?: Record<string, unknown>;
public constructor(
logger: Logger,
store: LinguistBackendStore,
@@ -67,6 +68,7 @@ export class LinguistBackendApi {
batchSize?: number,
useSourceLocation?: boolean,
kind?: string[],
linguistJsOptions?: Record<string, unknown>,
) {
this.logger = logger;
this.store = store;
@@ -78,6 +80,7 @@ export class LinguistBackendApi {
this.age = age;
this.useSourceLocation = useSourceLocation;
this.kind = kindOrDefault(kind);
this.linguistJsOptions = linguistJsOptions;
}
public async getEntityLanguages(entityRef: string): Promise<Languages> {
@@ -190,7 +193,7 @@ export class LinguistBackendApi {
const readTreeResponse = await this.urlReader.readTree(url);
const dir = await readTreeResponse.dir();
const results = await linguist(dir);
const results = await linguist(dir, this.linguistJsOptions);
try {
const totalBytes = results.languages.bytes;
+10 -1
View File
@@ -38,6 +38,7 @@ export interface PluginOptions {
age?: HumanDuration;
batchSize?: number;
useSourceLocation?: boolean;
linguistJsOptions?: Record<string, unknown>;
kind?: string[];
}
@@ -57,7 +58,14 @@ export async function createRouter(
pluginOptions: PluginOptions,
routerOptions: RouterOptions,
): Promise<express.Router> {
const { schedule, age, batchSize, useSourceLocation, kind } = pluginOptions;
const {
schedule,
age,
batchSize,
useSourceLocation,
kind,
linguistJsOptions,
} = pluginOptions;
const { logger, reader, database, discovery, scheduler, tokenManager } =
routerOptions;
@@ -78,6 +86,7 @@ export async function createRouter(
batchSize,
useSourceLocation,
kind,
linguistJsOptions,
);
if (scheduler && schedule) {