Skip to content

Commit

Permalink
Fix getAppName + test
Browse files Browse the repository at this point in the history
  • Loading branch information
gsoldevila authored and pgayvallet committed Dec 26, 2024
1 parent 31fd406 commit a3dfe1b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
47 changes: 12 additions & 35 deletions packages/kbn-eslint-plugin-telemetry/helpers/get_app_name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {
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<string>())
);

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;
}

0 comments on commit a3dfe1b

Please sign in to comment.