From a3dfe1bb75a9a1227559ab1c2267755c301b7db6 Mon Sep 17 00:00:00 2001 From: Gerard Soldevila Date: Mon, 23 Dec 2024 13:57:25 +0100 Subject: [PATCH] Fix getAppName + test --- .../helpers/get_app_name.test.ts | 8 ++-- .../helpers/get_app_name.ts | 47 +++++-------------- 2 files changed, 16 insertions(+), 39 deletions(-) diff --git a/packages/kbn-eslint-plugin-telemetry/helpers/get_app_name.test.ts b/packages/kbn-eslint-plugin-telemetry/helpers/get_app_name.test.ts index 4e5e8e9d10582..01cea6c4aa354 100644 --- a/packages/kbn-eslint-plugin-telemetry/helpers/get_app_name.test.ts +++ b/packages/kbn-eslint-plugin-telemetry/helpers/get_app_name.test.ts @@ -14,11 +14,11 @@ const SYSTEMPATH = 'systemPath'; const testMap = [ ['x-pack/solutions/observability/plugins/observability/foo/bar/baz/header_actions.tsx', 'o11y'], ['x-pack/plugins/observability_solution/apm/baz/header_actions.tsx', 'apm'], - ['x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx', 'apm'], - ['x-pack/plugins/observability/foo/bar/baz/header_actions.tsx', 'o11y'], - ['x-pack/plugins/observability_solution/apm/baz/header_actions.tsx', 'apm'], ['x-pack/platform/plugins/shared/cases/public/components/foo.tsx', 'cases'], - ['src/platform/packages/shared/kbn-alerts-ui-shared/src/alert_lifecycle_status_badge/index.tsx', 'kbnAlertsUiShared'], + [ + 'src/platform/packages/shared/kbn-alerts-ui-shared/src/alert_lifecycle_status_badge/index.tsx', + 'kbnAlertsUiShared', + ], ]; describe('Get App Name', () => { diff --git a/packages/kbn-eslint-plugin-telemetry/helpers/get_app_name.ts b/packages/kbn-eslint-plugin-telemetry/helpers/get_app_name.ts index 66cb162fdd211..5f3d7b96e33b9 100644 --- a/packages/kbn-eslint-plugin-telemetry/helpers/get_app_name.ts +++ b/packages/kbn-eslint-plugin-telemetry/helpers/get_app_name.ts @@ -8,46 +8,23 @@ */ import { camelCase } from 'lodash'; -import path from 'path'; +import { basename, parse } from 'path'; import { getPkgDirMap } from '@kbn/repo-packages'; import { REPO_ROOT } from '@kbn/repo-info'; +const APP_ALIASES: Record = { + observability: 'o11y', +}; + export function getAppName(fileName: string, cwd: string) { - const { dir } = path.parse(fileName); + const { dir } = parse(fileName); const relativePathToFile = dir.replace(cwd, ''); - const packageDirs = Array.from( - Array.from(getPkgDirMap(REPO_ROOT).values()).reduce((acc, currentDir) => { - const topDirectory = currentDir.normalizedRepoRelativeDir.split('/')[0]; - - if (topDirectory) { - acc.add(topDirectory); - } - - return acc; - }, new Set()) - ); - - const relativePathArray = relativePathToFile.split('/'); - - const appName = camelCase( - packageDirs.reduce((acc, repoPath) => { - if (!relativePathArray[1]) return ''; - - if (relativePathArray[1] === 'x-pack') { - if (relativePathArray[3] === 'observability_solution') { - return relativePathArray[4]; - } - return relativePathArray[3]; - } - - if (relativePathArray[1].includes(repoPath)) { - return relativePathArray[2]; - } - - return acc; - }, '') - ); + const allPaths = Array.from(getPkgDirMap(REPO_ROOT).values()) + .map((module) => module.directory.replace(REPO_ROOT, '')) + .sort((a, b) => b.length - a.length); - return appName === 'observability' ? 'o11y' : appName; + const moduleDir = allPaths.find((path) => relativePathToFile.startsWith(path)) ?? ''; + const moduleBasename = camelCase(basename(moduleDir)); + return APP_ALIASES[moduleBasename] ?? moduleBasename; }