From 8a298b472401d04c12e178020c1f57d2e612bc65 Mon Sep 17 00:00:00 2001 From: Jos Craw Date: Thu, 2 Mar 2023 19:56:42 +1300 Subject: [PATCH 1/5] feat: add offline support for linguist Signed-off-by: Jos Craw --- .changeset/pink-dolls-unite.md | 5 +++++ plugins/linguist-backend/README.md | 8 ++++++++ plugins/linguist-backend/api-report.md | 3 +++ plugins/linguist-backend/src/api/LinguistBackendApi.ts | 5 ++++- plugins/linguist-backend/src/service/router.ts | 5 ++++- 5 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 .changeset/pink-dolls-unite.md diff --git a/.changeset/pink-dolls-unite.md b/.changeset/pink-dolls-unite.md new file mode 100644 index 0000000000..f7c8b8c40c --- /dev/null +++ b/.changeset/pink-dolls-unite.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-linguist-backend': minor +--- + +Added support for offline language data diff --git a/plugins/linguist-backend/README.md b/plugins/linguist-backend/README.md index a7c89d23b3..981cebc19e 100644 --- a/plugins/linguist-backend/README.md +++ b/plugins/linguist-backend/README.md @@ -85,6 +85,14 @@ 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. +## Offline + +The default setup will pull the language data from GitHub, by setting `offline` to `true` a packaged offline version of this data is used instead. + +```ts +return createRouter({ schedule: schedule, 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`: diff --git a/plugins/linguist-backend/api-report.md b/plugins/linguist-backend/api-report.md index d3a77bc2d7..c1486ab3ae 100644 --- a/plugins/linguist-backend/api-report.md +++ b/plugins/linguist-backend/api-report.md @@ -35,6 +35,7 @@ export class LinguistBackendApi { batchSize?: number, useSourceLocation?: boolean, kind?: string[], + offline?: boolean, ); // (undocumented) getEntityLanguages(entityRef: string): Promise; @@ -82,6 +83,8 @@ export interface PluginOptions { // (undocumented) kind?: string[]; // (undocumented) + offline?: boolean; + // (undocumented) schedule?: TaskScheduleDefinition; // (undocumented) useSourceLocation?: boolean; diff --git a/plugins/linguist-backend/src/api/LinguistBackendApi.ts b/plugins/linguist-backend/src/api/LinguistBackendApi.ts index 6a4cc0ea56..f58b913c2d 100644 --- a/plugins/linguist-backend/src/api/LinguistBackendApi.ts +++ b/plugins/linguist-backend/src/api/LinguistBackendApi.ts @@ -57,6 +57,7 @@ export class LinguistBackendApi { private readonly batchSize?: number; private readonly useSourceLocation?: boolean; private readonly kind: string[]; + private readonly offline?: boolean; public constructor( logger: Logger, store: LinguistBackendStore, @@ -67,6 +68,7 @@ export class LinguistBackendApi { batchSize?: number, useSourceLocation?: boolean, kind?: string[], + offline?: boolean, ) { this.logger = logger; this.store = store; @@ -78,6 +80,7 @@ export class LinguistBackendApi { this.age = age; this.useSourceLocation = useSourceLocation; this.kind = kindOrDefault(kind); + this.offline = offline; } public async getEntityLanguages(entityRef: string): Promise { @@ -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, { offline: this.offline }); try { const totalBytes = results.languages.bytes; diff --git a/plugins/linguist-backend/src/service/router.ts b/plugins/linguist-backend/src/service/router.ts index 66138f5ed2..4a279aba7d 100644 --- a/plugins/linguist-backend/src/service/router.ts +++ b/plugins/linguist-backend/src/service/router.ts @@ -38,6 +38,7 @@ export interface PluginOptions { age?: HumanDuration; batchSize?: number; useSourceLocation?: boolean; + offline?: boolean; kind?: string[]; } @@ -57,7 +58,8 @@ export async function createRouter( pluginOptions: PluginOptions, routerOptions: RouterOptions, ): Promise { - const { schedule, age, batchSize, useSourceLocation, kind } = pluginOptions; + const { schedule, age, batchSize, useSourceLocation, kind, offline } = + pluginOptions; const { logger, reader, database, discovery, scheduler, tokenManager } = routerOptions; @@ -78,6 +80,7 @@ export async function createRouter( batchSize, useSourceLocation, kind, + offline, ); if (scheduler && schedule) { From 9b3bc42f1c3c522ba69afbb7a7604067d433809c Mon Sep 17 00:00:00 2001 From: Jos Craw Date: Fri, 3 Mar 2023 07:39:28 +1300 Subject: [PATCH 2/5] Update .changeset/pink-dolls-unite.md Co-authored-by: Johan Haals Signed-off-by: Jos Craw --- .changeset/pink-dolls-unite.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/pink-dolls-unite.md b/.changeset/pink-dolls-unite.md index f7c8b8c40c..d3c489cd2b 100644 --- a/.changeset/pink-dolls-unite.md +++ b/.changeset/pink-dolls-unite.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-linguist-backend': minor +'@backstage/plugin-linguist-backend': patch --- Added support for offline language data From 4eae5083108c768b57df421bfdbde720afd87597 Mon Sep 17 00:00:00 2001 From: Jos Craw Date: Fri, 3 Mar 2023 08:15:37 +1300 Subject: [PATCH 3/5] feat: changed to provide entire linguist-js options object Signed-off-by: Jos Craw --- .changeset/pink-dolls-unite.md | 2 +- plugins/linguist-backend/README.md | 9 ++++++--- plugins/linguist-backend/api-report.md | 4 ++-- .../linguist-backend/src/api/LinguistBackendApi.ts | 8 ++++---- plugins/linguist-backend/src/service/router.ts | 14 ++++++++++---- 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/.changeset/pink-dolls-unite.md b/.changeset/pink-dolls-unite.md index d3c489cd2b..aca62166b4 100644 --- a/.changeset/pink-dolls-unite.md +++ b/.changeset/pink-dolls-unite.md @@ -2,4 +2,4 @@ '@backstage/plugin-linguist-backend': patch --- -Added support for offline language data +Added support for linguist-js options. diff --git a/plugins/linguist-backend/README.md b/plugins/linguist-backend/README.md index 981cebc19e..46c067fb5a 100644 --- a/plugins/linguist-backend/README.md +++ b/plugins/linguist-backend/README.md @@ -85,12 +85,15 @@ 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. -## Offline +## Linguist JS options -The default setup will pull the language data from GitHub, by setting `offline` to `true` a packaged offline version of this data is used instead. +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, offline: true }, { ...env }); +return createRouter( + { schedule: schedule, linguistJsOptions: { offline: true } }, + { ...env }, +); ``` ## Use Source Location diff --git a/plugins/linguist-backend/api-report.md b/plugins/linguist-backend/api-report.md index c1486ab3ae..8ac8b83dcd 100644 --- a/plugins/linguist-backend/api-report.md +++ b/plugins/linguist-backend/api-report.md @@ -35,7 +35,7 @@ export class LinguistBackendApi { batchSize?: number, useSourceLocation?: boolean, kind?: string[], - offline?: boolean, + linguistJsOptions?: Record, ); // (undocumented) getEntityLanguages(entityRef: string): Promise; @@ -83,7 +83,7 @@ export interface PluginOptions { // (undocumented) kind?: string[]; // (undocumented) - offline?: boolean; + linguistJsOptions?: Record; // (undocumented) schedule?: TaskScheduleDefinition; // (undocumented) diff --git a/plugins/linguist-backend/src/api/LinguistBackendApi.ts b/plugins/linguist-backend/src/api/LinguistBackendApi.ts index f58b913c2d..e0a3133255 100644 --- a/plugins/linguist-backend/src/api/LinguistBackendApi.ts +++ b/plugins/linguist-backend/src/api/LinguistBackendApi.ts @@ -57,7 +57,7 @@ export class LinguistBackendApi { private readonly batchSize?: number; private readonly useSourceLocation?: boolean; private readonly kind: string[]; - private readonly offline?: boolean; + private readonly linguistJsOptions?: Record; public constructor( logger: Logger, store: LinguistBackendStore, @@ -68,7 +68,7 @@ export class LinguistBackendApi { batchSize?: number, useSourceLocation?: boolean, kind?: string[], - offline?: boolean, + linguistJsOptions?: Record, ) { this.logger = logger; this.store = store; @@ -80,7 +80,7 @@ export class LinguistBackendApi { this.age = age; this.useSourceLocation = useSourceLocation; this.kind = kindOrDefault(kind); - this.offline = offline; + this.linguistJsOptions = linguistJsOptions; } public async getEntityLanguages(entityRef: string): Promise { @@ -193,7 +193,7 @@ export class LinguistBackendApi { const readTreeResponse = await this.urlReader.readTree(url); const dir = await readTreeResponse.dir(); - const results = await linguist(dir, { offline: this.offline }); + const results = await linguist(dir, this.linguistJsOptions); try { const totalBytes = results.languages.bytes; diff --git a/plugins/linguist-backend/src/service/router.ts b/plugins/linguist-backend/src/service/router.ts index 4a279aba7d..d10f185261 100644 --- a/plugins/linguist-backend/src/service/router.ts +++ b/plugins/linguist-backend/src/service/router.ts @@ -38,7 +38,7 @@ export interface PluginOptions { age?: HumanDuration; batchSize?: number; useSourceLocation?: boolean; - offline?: boolean; + linguistJsOptions?: Record; kind?: string[]; } @@ -58,8 +58,14 @@ export async function createRouter( pluginOptions: PluginOptions, routerOptions: RouterOptions, ): Promise { - const { schedule, age, batchSize, useSourceLocation, kind, offline } = - pluginOptions; + const { + schedule, + age, + batchSize, + useSourceLocation, + kind, + linguistJsOptions, + } = pluginOptions; const { logger, reader, database, discovery, scheduler, tokenManager } = routerOptions; @@ -80,7 +86,7 @@ export async function createRouter( batchSize, useSourceLocation, kind, - offline, + linguistJsOptions, ); if (scheduler && schedule) { From 4cd04a9bb35a8b180e0b3a6f4f2726fe156bb7b6 Mon Sep 17 00:00:00 2001 From: Jos Craw Date: Fri, 3 Mar 2023 16:43:06 +1300 Subject: [PATCH 4/5] fix: added more detail to the changeset Signed-off-by: Jos Craw --- .changeset/pink-dolls-unite.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/pink-dolls-unite.md b/.changeset/pink-dolls-unite.md index aca62166b4..418edfd92b 100644 --- a/.changeset/pink-dolls-unite.md +++ b/.changeset/pink-dolls-unite.md @@ -2,4 +2,4 @@ '@backstage/plugin-linguist-backend': patch --- -Added support for linguist-js options. +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). From 72c170fa682803a73729ac02593f7e21bf23a6b0 Mon Sep 17 00:00:00 2001 From: Jos Craw Date: Sat, 4 Mar 2023 08:13:38 +1300 Subject: [PATCH 5/5] fix: change Record value to unknown from any Signed-off-by: Jos Craw --- plugins/linguist-backend/api-report.md | 4 ++-- plugins/linguist-backend/src/api/LinguistBackendApi.ts | 4 ++-- plugins/linguist-backend/src/service/router.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/linguist-backend/api-report.md b/plugins/linguist-backend/api-report.md index 8ac8b83dcd..a47f123ac4 100644 --- a/plugins/linguist-backend/api-report.md +++ b/plugins/linguist-backend/api-report.md @@ -35,7 +35,7 @@ export class LinguistBackendApi { batchSize?: number, useSourceLocation?: boolean, kind?: string[], - linguistJsOptions?: Record, + linguistJsOptions?: Record, ); // (undocumented) getEntityLanguages(entityRef: string): Promise; @@ -83,7 +83,7 @@ export interface PluginOptions { // (undocumented) kind?: string[]; // (undocumented) - linguistJsOptions?: Record; + linguistJsOptions?: Record; // (undocumented) schedule?: TaskScheduleDefinition; // (undocumented) diff --git a/plugins/linguist-backend/src/api/LinguistBackendApi.ts b/plugins/linguist-backend/src/api/LinguistBackendApi.ts index e0a3133255..df7e30a7b0 100644 --- a/plugins/linguist-backend/src/api/LinguistBackendApi.ts +++ b/plugins/linguist-backend/src/api/LinguistBackendApi.ts @@ -57,7 +57,7 @@ export class LinguistBackendApi { private readonly batchSize?: number; private readonly useSourceLocation?: boolean; private readonly kind: string[]; - private readonly linguistJsOptions?: Record; + private readonly linguistJsOptions?: Record; public constructor( logger: Logger, store: LinguistBackendStore, @@ -68,7 +68,7 @@ export class LinguistBackendApi { batchSize?: number, useSourceLocation?: boolean, kind?: string[], - linguistJsOptions?: Record, + linguistJsOptions?: Record, ) { this.logger = logger; this.store = store; diff --git a/plugins/linguist-backend/src/service/router.ts b/plugins/linguist-backend/src/service/router.ts index d10f185261..8d52a2e01d 100644 --- a/plugins/linguist-backend/src/service/router.ts +++ b/plugins/linguist-backend/src/service/router.ts @@ -38,7 +38,7 @@ export interface PluginOptions { age?: HumanDuration; batchSize?: number; useSourceLocation?: boolean; - linguistJsOptions?: Record; + linguistJsOptions?: Record; kind?: string[]; }