Skip to content

Commit

Permalink
[7.17] Migrate from listr to listr2 (#182683) (#183141)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `7.17`:
- [Migrate from listr to listr2
(#182683)](#182683)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Larry
Gregory","email":"larry.gregory@elastic.co"},"sourceCommit":{"committedDate":"2024-05-07T11:30:42Z","message":"Migrate
from listr to listr2 (#182683)\n\n## Summary\r\n\r\nMigrates CLI tasks
from `listr` to `listr2`. The former hasn't been\r\nupdated in a long
time, and the community has moved on to
`listr2`.\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"40a16413572140b857e52dc45b206558ee0f1c9c","branchLabelMapping":{"^v8.15.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["chore","Team:Security","release_note:skip","backport
missing","backport:all-open","ci:project-deploy-observability","Team:obs-ux-infra_services","apm:review","v8.15.0"],"number":182683,"url":"https://github.com/elastic/kibana/pull/182683","mergeCommit":{"message":"Migrate
from listr to listr2 (#182683)\n\n## Summary\r\n\r\nMigrates CLI tasks
from `listr` to `listr2`. The former hasn't been\r\nupdated in a long
time, and the community has moved on to
`listr2`.\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"40a16413572140b857e52dc45b206558ee0f1c9c"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.15.0","labelRegex":"^v8.15.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/182683","number":182683,"mergeCommit":{"message":"Migrate
from listr to listr2 (#182683)\n\n## Summary\r\n\r\nMigrates CLI tasks
from `listr` to `listr2`. The former hasn't been\r\nupdated in a long
time, and the community has moved on to
`listr2`.\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"40a16413572140b857e52dc45b206558ee0f1c9c"}}]}]
BACKPORT-->

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
legrego and kibanamachine authored May 14, 2024
1 parent 2721968 commit 521be9f
Show file tree
Hide file tree
Showing 15 changed files with 258 additions and 289 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@
"@types/json-stable-stringify": "^1.0.32",
"@types/json5": "^2.2.0",
"@types/license-checker": "15.0.0",
"@types/listr": "^0.14.0",
"@types/loader-utils": "^2.0.3",
"@types/lodash": "^4.14.159",
"@types/lru-cache": "^5.1.0",
Expand Down Expand Up @@ -753,7 +752,7 @@
"json5": "^2.2.3",
"jsondiffpatch": "0.4.1",
"license-checker": "^25.0.1",
"listr": "^0.14.1",
"listr2": "^8.2.1",
"lmdb": "^2.9.2",
"marge": "^1.0.1",
"micromatch": "3.1.10",
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-telemetry-tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ RUNTIME_DEPS = [
"//packages/kbn-dev-utils",
"//packages/kbn-utility-types",
"@npm//glob",
"@npm//listr",
"@npm//listr2",
"@npm//normalize-path",
]

TYPES_DEPS = [
"//packages/kbn-dev-utils",
"//packages/kbn-utility-types",
"@npm//listr2",
"@npm//@types/glob",
"@npm//@types/jest",
"@npm//@types/listr",
"@npm//@types/lodash",
"@npm//@types/node",
"@npm//@types/normalize-path",
Expand Down
36 changes: 19 additions & 17 deletions packages/kbn-telemetry-tools/src/cli/run_telemetry_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import Listr from 'listr';
import { Listr } from 'listr2';
import chalk from 'chalk';
import { createFailError, run } from '@kbn/dev-utils';

Expand Down Expand Up @@ -47,22 +47,22 @@ export function runTelemetryCheck() {
);
}

const list = new Listr(
const list = new Listr<TaskContext>(
[
{
title: 'Checking .telemetryrc.json files',
task: () => new Listr(parseConfigsTask(), { exitOnError: true }),
task: (context, task) => task.newListr(parseConfigsTask(), { exitOnError: true }),
},
{
title: 'Extracting Collectors',
task: (context) =>
new Listr(extractCollectorsTask(context, path), { exitOnError: true }),
task: (context, task) =>
task.newListr(extractCollectorsTask(context, path), { exitOnError: true }),
},
{
enabled: () => typeof path !== 'undefined',
title: 'Checking collectors in --path are not excluded',
task: ({ roots }: TaskContext) => {
const totalCollections = roots.reduce((acc, root) => {
task: (context) => {
const totalCollections = context.roots.reduce((acc, root) => {
return acc + (root.parsedCollections?.length || 0);
}, 0);
const collectorsInPath = Array.isArray(path) ? path.length : 1;
Expand All @@ -76,39 +76,41 @@ export function runTelemetryCheck() {
},
{
title: 'Checking Compatible collector.schema with collector.fetch type',
task: (context) => new Listr(checkCompatibleTypesTask(context), { exitOnError: true }),
task: (context, task) =>
task.newListr(checkCompatibleTypesTask(context), { exitOnError: true }),
},
{
enabled: (_) => fix || !ignoreStoredJson,
title: 'Checking Matching collector.schema against stored json files',
task: (context) =>
new Listr(checkMatchingSchemasTask(context, !fix), { exitOnError: true }),
task: (context, task) =>
task.newListr(checkMatchingSchemasTask(context, !fix), { exitOnError: true }),
},
{
enabled: (_) => fix,
skip: ({ roots }: TaskContext) => {
const noDiffs = roots.every(
skip: (context) => {
const noDiffs = context.roots.every(
({ esMappingDiffs }) => !esMappingDiffs || !esMappingDiffs.length
);
return noDiffs && 'No changes needed.';
},
title: 'Generating new telemetry mappings',
task: (context) => new Listr(generateSchemasTask(context), { exitOnError: true }),
task: (context, task) =>
task.newListr(generateSchemasTask(context), { exitOnError: true }),
},
{
enabled: (_) => fix,
skip: ({ roots }: TaskContext) => {
const noDiffs = roots.every(
skip: (context) => {
const noDiffs = context.roots.every(
({ esMappingDiffs }) => !esMappingDiffs || !esMappingDiffs.length
);
return noDiffs && 'No changes needed.';
},
title: 'Updating telemetry mapping files',
task: (context) => new Listr(writeToFileTask(context), { exitOnError: true }),
task: (context, task) => task.newListr(writeToFileTask(context), { exitOnError: true }),
},
],
{
renderer: process.env.CI ? 'verbose' : 'default',
renderer: process.env.CI ? 'verbose' : ('default' as any),
}
);

Expand Down
12 changes: 7 additions & 5 deletions packages/kbn-telemetry-tools/src/cli/run_telemetry_extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import Listr from 'listr';
import { Listr } from 'listr2';
import { run } from '@kbn/dev-utils';

import {
Expand All @@ -25,19 +25,21 @@ export function runTelemetryExtract() {
[
{
title: 'Parsing .telemetryrc.json files',
task: () => new Listr(parseConfigsTask(), { exitOnError: true }),
task: (context, task) => task.newListr(parseConfigsTask(), { exitOnError: true }),
},
{
title: 'Extracting Telemetry Collectors',
task: (context) => new Listr(extractCollectorsTask(context), { exitOnError: true }),
task: (context, task) =>
task.newListr(extractCollectorsTask(context), { exitOnError: true }),
},
{
title: 'Generating Schema files',
task: (context) => new Listr(generateSchemasTask(context), { exitOnError: true }),
task: (context, task) =>
task.newListr(generateSchemasTask(context), { exitOnError: true }),
},
{
title: 'Writing to file',
task: (context) => new Listr(writeToFileTask(context), { exitOnError: true }),
task: (context, task) => task.newListr(writeToFileTask(context), { exitOnError: true }),
},
],
{
Expand Down
3 changes: 2 additions & 1 deletion src/dev/i18n/tasks/check_compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { ToolingLog } from '@kbn/dev-utils';
import { integrateLocaleFiles, I18nConfig } from '..';
import { I18nCheckTaskContext } from '../types';

export interface I18nFlags {
fix: boolean;
Expand All @@ -20,7 +21,7 @@ export interface I18nFlags {
export function checkCompatibility(config: I18nConfig, flags: I18nFlags, log: ToolingLog) {
const { fix, ignoreIncompatible, ignoreUnused, ignoreMalformed, ignoreMissing } = flags;
return config.translations.map((translationsPath) => ({
task: async ({ messages }: { messages: Map<string, { message: string }> }) => {
task: async ({ messages }: I18nCheckTaskContext) => {
// If `fix` is set we should try apply all possible fixes and override translations file.
await integrateLocaleFiles(messages, {
dryRun: !fix,
Expand Down
5 changes: 3 additions & 2 deletions src/dev/i18n/tasks/check_configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import { resolve, join } from 'path';
import { I18N_RC } from '../constants';
import { ErrorReporter, checkConfigNamespacePrefix, arrayify } from '..';
import { checkConfigNamespacePrefix, arrayify } from '..';
import { I18nCheckTaskContext } from '../types';

export function checkConfigs(additionalConfigPaths: string | string[] = []) {
const root = join(__dirname, '../../../../');
Expand All @@ -18,7 +19,7 @@ export function checkConfigs(additionalConfigPaths: string | string[] = []) {
const configPaths = [kibanaRC, xpackRC, ...arrayify(additionalConfigPaths)];

return configPaths.map((configPath) => ({
task: async (context: { reporter: ErrorReporter }) => {
task: async (context: I18nCheckTaskContext) => {
try {
await checkConfigNamespacePrefix(configPath);
} catch (err) {
Expand Down
8 changes: 3 additions & 5 deletions src/dev/i18n/tasks/extract_default_translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import chalk from 'chalk';
import { createFailError } from '@kbn/dev-utils';
import { ErrorReporter, extractMessagesFromPathToMap, filterConfigPaths, I18nConfig } from '..';
import { extractMessagesFromPathToMap, filterConfigPaths, I18nConfig } from '..';
import { I18nCheckTaskContext } from '../types';

export function extractDefaultMessages(config: I18nConfig, inputPaths: string[]) {
const filteredPaths = filterConfigPaths(inputPaths, config) as string[];
Expand All @@ -20,10 +21,7 @@ export function extractDefaultMessages(config: I18nConfig, inputPaths: string[])
);
}
return filteredPaths.map((filteredPath) => ({
task: async (context: {
messages: Map<string, { message: string }>;
reporter: ErrorReporter;
}) => {
task: async (context: I18nCheckTaskContext) => {
const { messages, reporter } = context;
const initialErrorsNumber = reporter.errors.length;

Expand Down
11 changes: 8 additions & 3 deletions src/dev/i18n/tasks/extract_untracked_translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
readFileAsync,
ErrorReporter,
} from '..';
import { I18nCheckTaskContext } from '../types';

function filterEntries(entries: string[], exclude: string[]) {
return entries.filter((entry: string) =>
Expand All @@ -28,7 +29,7 @@ export async function extractUntrackedMessagesTask({
}: {
path?: string | string[];
config: I18nConfig;
reporter: any;
reporter: ErrorReporter;
}) {
const inputPaths = Array.isArray(path) ? path : [path || './'];
const availablePaths = Object.values(config.paths).flat();
Expand Down Expand Up @@ -82,10 +83,14 @@ export async function extractUntrackedMessagesTask({
export function extractUntrackedMessages(inputPaths: string[]) {
return inputPaths.map((inputPath) => ({
title: `Checking untracked messages in ${inputPath}`,
task: async (context: { reporter: ErrorReporter; config: I18nConfig }) => {
task: async (context: I18nCheckTaskContext) => {
const { reporter, config } = context;
const initialErrorsNumber = reporter.errors.length;
const result = await extractUntrackedMessagesTask({ path: inputPath, config, reporter });
const result = await extractUntrackedMessagesTask({
path: inputPath,
config: config as I18nConfig,
reporter,
});
if (reporter.errors.length === initialErrorsNumber) {
return result;
}
Expand Down
5 changes: 3 additions & 2 deletions src/dev/i18n/tasks/merge_configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*/

import { resolve, join } from 'path';
import { ErrorReporter, I18nConfig, assignConfigFromPath, arrayify } from '..';
import { assignConfigFromPath, arrayify } from '..';
import { I18nCheckTaskContext } from '../types';

export function mergeConfigs(additionalConfigPaths: string | string[] = []) {
const root = join(__dirname, '../../../../');
Expand All @@ -17,7 +18,7 @@ export function mergeConfigs(additionalConfigPaths: string | string[] = []) {
const configPaths = [kibanaRC, xpackRC, ...arrayify(additionalConfigPaths)];

return configPaths.map((configPath) => ({
task: async (context: { reporter: ErrorReporter; config?: I18nConfig }) => {
task: async (context: I18nCheckTaskContext) => {
try {
context.config = await assignConfigFromPath(context.config, configPath);
} catch (err) {
Expand Down
16 changes: 16 additions & 0 deletions src/dev/i18n/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { I18nConfig } from './config';
import { ErrorReporter } from './utils';

export interface I18nCheckTaskContext {
config?: I18nConfig;
reporter: ErrorReporter;
messages: Map<string, { message: string }>;
}
33 changes: 18 additions & 15 deletions src/dev/run_i18n_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
*/

import chalk from 'chalk';
import Listr from 'listr';
import { Listr } from 'listr2';

import { createFailError, run, ToolingLog, getTimeReporter } from '@kbn/dev-utils';
import { ErrorReporter, I18nConfig } from './i18n';
import { ErrorReporter } from './i18n';
import {
extractDefaultMessages,
extractUntrackedMessages,
checkCompatibility,
checkConfigs,
mergeConfigs,
} from './i18n/tasks';
import { I18nCheckTaskContext } from './i18n/types';

const toolingLog = new ToolingLog({
level: 'info',
Expand All @@ -27,8 +28,8 @@ const toolingLog = new ToolingLog({
const runStartTime = Date.now();
const reportTime = getTimeReporter(toolingLog, 'scripts/i18n_check');

const skipOnNoTranslations = ({ config }: { config: I18nConfig }) =>
!config.translations.length && 'No translations found.';
const skipOnNoTranslations = ({ config }: I18nCheckTaskContext) =>
!config?.translations.length && 'No translations found.';

run(
async ({
Expand Down Expand Up @@ -71,36 +72,38 @@ run(

const srcPaths = Array().concat(path || ['./src', './packages', './x-pack']);

const list = new Listr(
const list = new Listr<I18nCheckTaskContext>(
[
{
title: 'Checking .i18nrc.json files',
task: () => new Listr(checkConfigs(includeConfig), { exitOnError: true }),
task: (context, task) =>
task.newListr(checkConfigs(includeConfig), { exitOnError: true }),
},
{
title: 'Merging .i18nrc.json files',
task: () => new Listr(mergeConfigs(includeConfig), { exitOnError: true }),
task: (context, task) =>
task.newListr(mergeConfigs(includeConfig), { exitOnError: true }),
},
{
title: 'Checking For Untracked Messages based on .i18nrc.json',
enabled: (_) => !ignoreUntracked,
skip: skipOnNoTranslations,
task: ({ config }) =>
new Listr(extractUntrackedMessages(srcPaths), { exitOnError: true }),
task: (context, task) =>
task.newListr(extractUntrackedMessages(srcPaths), { exitOnError: true }),
},
{
title: 'Validating Default Messages',
skip: skipOnNoTranslations,
task: ({ config }) =>
new Listr(extractDefaultMessages(config, srcPaths), { exitOnError: true }),
task: (context, task) =>
task.newListr(extractDefaultMessages(context.config!, srcPaths), { exitOnError: true }),
},
{
title: 'Compatibility Checks',
skip: skipOnNoTranslations,
task: ({ config }) =>
new Listr(
task: (context, task) =>
task.newListr(
checkCompatibility(
config,
context.config!,
{
ignoreMalformed: !!ignoreMalformed,
ignoreIncompatible: !!ignoreIncompatible,
Expand All @@ -117,7 +120,7 @@ run(
{
concurrent: false,
exitOnError: true,
renderer: process.env.CI ? 'verbose' : 'default',
renderer: process.env.CI ? 'verbose' : ('default' as any),
}
);

Expand Down
Loading

0 comments on commit 521be9f

Please sign in to comment.