diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b8c8d732ae5..d96af633e11c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -163,7 +163,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [MD] Refactor data source error handling ([#2661](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2661)) - Refactor and improve Discover field summaries ([#2391](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2391)) - [Vis Builder] Removed Hard Coded Strings and Used i18n to transalte([#2867](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2867)) -- [Console] Replace jQuery.ajax with core.http when calling OSD APIs in console ([#3080]https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3080)) +- [Console] Replace jQuery.ajax with core.http when calling OSD APIs in console ([#3080](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3080)) +- [I18n] Fix Listr type errors and error handlers ([#3629](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3629)) ### 🔩 Tests diff --git a/src/dev/i18n/integrate_locale_files.ts b/src/dev/i18n/integrate_locale_files.ts index 2a362214d6d8..0f3a74a82faa 100644 --- a/src/dev/i18n/integrate_locale_files.ts +++ b/src/dev/i18n/integrate_locale_files.ts @@ -106,7 +106,7 @@ export function verifyMessages( typeof message === 'string' ? message : message.text, messageId ); - } catch (err) { + } catch (err: any) { if (options.ignoreIncompatible) { localizedMessagesMap.delete(messageId); options.log.warning(`Incompatible translation ignored: ${err.message}`); diff --git a/src/dev/i18n/tasks/check_compatibility.ts b/src/dev/i18n/tasks/check_compatibility.ts index fed6e00a11de..7af9ef5604a9 100644 --- a/src/dev/i18n/tasks/check_compatibility.ts +++ b/src/dev/i18n/tasks/check_compatibility.ts @@ -39,7 +39,14 @@ export interface I18nFlags { ignoreMissing: boolean; } -export function checkCompatibility(config: I18nConfig, flags: I18nFlags, log: ToolingLog) { +export function checkCompatibility( + config: I18nConfig | undefined, + flags: I18nFlags, + log: ToolingLog +) { + if (!config) { + throw new Error('Config is missing'); + } const { fix, ignoreIncompatible, ignoreUnused, ignoreMalformed, ignoreMissing } = flags; return config.translations.map((translationsPath) => ({ task: async ({ messages }: { messages: Map }) => { diff --git a/src/dev/i18n/tasks/check_configs.ts b/src/dev/i18n/tasks/check_configs.ts index df57c874f258..2e37a5e00cf0 100644 --- a/src/dev/i18n/tasks/check_configs.ts +++ b/src/dev/i18n/tasks/check_configs.ts @@ -27,10 +27,10 @@ * specific language governing permissions and limitations * under the License. */ - import { resolve, join } from 'path'; +import { ListrContext } from '.'; import { I18N_RC } from '../constants'; -import { ErrorReporter, checkConfigNamespacePrefix, arrayify } from '..'; +import { checkConfigNamespacePrefix, arrayify } from '..'; export function checkConfigs(additionalConfigPaths: string | string[] = []) { const root = join(__dirname, '../../../../'); @@ -39,7 +39,7 @@ export function checkConfigs(additionalConfigPaths: string | string[] = []) { const configPaths = [opensearchDashboardsRC, ...arrayify(additionalConfigPaths)]; return configPaths.map((configPath) => ({ - task: async (context: { reporter: ErrorReporter }) => { + task: async (context: ListrContext) => { try { await checkConfigNamespacePrefix(configPath); } catch (err) { diff --git a/src/dev/i18n/tasks/extract_default_translations.ts b/src/dev/i18n/tasks/extract_default_translations.ts index 42eda5726eef..0955ce884ed9 100644 --- a/src/dev/i18n/tasks/extract_default_translations.ts +++ b/src/dev/i18n/tasks/extract_default_translations.ts @@ -32,7 +32,10 @@ import chalk from 'chalk'; import { createFailError } from '@osd/dev-utils'; import { ErrorReporter, extractMessagesFromPathToMap, filterConfigPaths, I18nConfig } from '..'; -export function extractDefaultMessages(config: I18nConfig, inputPaths: string[]) { +export function extractDefaultMessages(config: I18nConfig | undefined, inputPaths: string[]) { + if (!config) { + throw new Error('Config is missing'); + } const filteredPaths = filterConfigPaths(inputPaths, config) as string[]; if (filteredPaths.length === 0) { throw createFailError( diff --git a/src/dev/i18n/tasks/extract_untracked_translations.ts b/src/dev/i18n/tasks/extract_untracked_translations.ts index a30e28fa41d3..9c98d22db3f2 100644 --- a/src/dev/i18n/tasks/extract_untracked_translations.ts +++ b/src/dev/i18n/tasks/extract_untracked_translations.ts @@ -29,13 +29,8 @@ */ import { createFailError } from '@osd/dev-utils'; -import { - I18nConfig, - matchEntriesWithExctractors, - normalizePath, - readFileAsync, - ErrorReporter, -} from '..'; +import { ListrContext } from '.'; +import { I18nConfig, matchEntriesWithExctractors, normalizePath, readFileAsync } from '..'; function filterEntries(entries: string[], exclude: string[]) { return entries.filter((entry: string) => @@ -104,8 +99,11 @@ 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: ListrContext) => { const { reporter, config } = context; + if (!config) { + throw new Error('Config is not defined'); + } const initialErrorsNumber = reporter.errors.length; const result = await extractUntrackedMessagesTask({ path: inputPath, config, reporter }); if (reporter.errors.length === initialErrorsNumber) { diff --git a/src/dev/i18n/tasks/index.ts b/src/dev/i18n/tasks/index.ts index 53038e7da605..6b9c899b5649 100644 --- a/src/dev/i18n/tasks/index.ts +++ b/src/dev/i18n/tasks/index.ts @@ -28,8 +28,16 @@ * under the License. */ +import { ErrorReporter, I18nConfig } from '..'; + export { extractDefaultMessages } from './extract_default_translations'; export { extractUntrackedMessages } from './extract_untracked_translations'; export { checkCompatibility } from './check_compatibility'; export { mergeConfigs } from './merge_configs'; export { checkConfigs } from './check_configs'; + +export interface ListrContext { + config?: I18nConfig; + reporter: ErrorReporter; + messages: Map; +} diff --git a/src/dev/i18n/tasks/merge_configs.ts b/src/dev/i18n/tasks/merge_configs.ts index 83e94d4da6f6..79c1fde91e95 100644 --- a/src/dev/i18n/tasks/merge_configs.ts +++ b/src/dev/i18n/tasks/merge_configs.ts @@ -27,9 +27,9 @@ * specific language governing permissions and limitations * under the License. */ - import { resolve, join } from 'path'; -import { ErrorReporter, I18nConfig, assignConfigFromPath, arrayify } from '..'; +import { ListrContext } from '.'; +import { assignConfigFromPath, arrayify } from '..'; export function mergeConfigs(additionalConfigPaths: string | string[] = []) { const root = join(__dirname, '../../../../'); @@ -38,7 +38,7 @@ export function mergeConfigs(additionalConfigPaths: string | string[] = []) { const configPaths = [opensearchDashboardsRC, ...arrayify(additionalConfigPaths)]; return configPaths.map((configPath) => ({ - task: async (context: { reporter: ErrorReporter; config?: I18nConfig }) => { + task: async (context: ListrContext) => { try { context.config = await assignConfigFromPath(context.config, configPath); } catch (err) { diff --git a/src/dev/i18n/utils/verify_icu_message.ts b/src/dev/i18n/utils/verify_icu_message.ts index 0470cac4ea1f..db9180039208 100644 --- a/src/dev/i18n/utils/verify_icu_message.ts +++ b/src/dev/i18n/utils/verify_icu_message.ts @@ -70,8 +70,8 @@ export function verifyICUMessage(message: string) { verifySelectFormatNode(node.format); } } - } catch (error) { - if (error.name === 'SyntaxError') { + } catch (error: unknown) { + if (error instanceof parser.SyntaxError && error.name === 'SyntaxError') { const errorWithContext = createParserErrorMessage(message, { loc: { line: error.location.start.line, diff --git a/src/dev/run_i18n_check.ts b/src/dev/run_i18n_check.ts index fb0d9d3a3f01..17fc397a7bf1 100644 --- a/src/dev/run_i18n_check.ts +++ b/src/dev/run_i18n_check.ts @@ -32,18 +32,18 @@ import chalk from 'chalk'; import Listr from 'listr'; import { createFailError, run } from '@osd/dev-utils'; -import { ErrorReporter, I18nConfig } from './i18n'; +import { ErrorReporter } from './i18n'; import { extractDefaultMessages, extractUntrackedMessages, checkCompatibility, checkConfigs, mergeConfigs, + ListrContext, } from './i18n/tasks'; -const skipOnNoTranslations = ({ config }: { config: I18nConfig }) => - !config.translations.length && 'No translations found.'; - +const skipOnNoTranslations = (context: ListrContext) => + !context.config?.translations?.length && 'No translations found.'; run( async ({ flags: { @@ -85,7 +85,7 @@ run( const srcPaths = Array().concat(path || ['./src', './packages']); - const list = new Listr( + const list = new Listr( [ { title: 'Checking .i18nrc.json files', @@ -105,14 +105,15 @@ run( { title: 'Validating Default Messages', skip: skipOnNoTranslations, - task: ({ config }) => - new Listr(extractDefaultMessages(config, srcPaths), { exitOnError: true }), + task: ({ config }) => { + return new Listr(extractDefaultMessages(config, srcPaths), { exitOnError: true }); + }, }, { title: 'Compatibility Checks', skip: skipOnNoTranslations, - task: ({ config }) => - new Listr( + task: ({ config }) => { + return new Listr( checkCompatibility( config, { @@ -125,7 +126,8 @@ run( log ), { exitOnError: true } - ), + ); + }, }, ], { @@ -138,7 +140,7 @@ run( const reporter = new ErrorReporter(); const messages: Map = new Map(); await list.run({ messages, reporter }); - } catch (error) { + } catch (error: ErrorReporter | Error) { process.exitCode = 1; if (error instanceof ErrorReporter) { error.errors.forEach((e: string | Error) => log.error(e)); diff --git a/src/dev/run_i18n_extract.ts b/src/dev/run_i18n_extract.ts index 28c49c3ab868..6de6b0904abb 100644 --- a/src/dev/run_i18n_extract.ts +++ b/src/dev/run_i18n_extract.ts @@ -34,7 +34,7 @@ import { resolve } from 'path'; import { createFailError, run } from '@osd/dev-utils'; import { ErrorReporter, serializeToJson, serializeToJson5, writeFileAsync } from './i18n'; -import { extractDefaultMessages, mergeConfigs } from './i18n/tasks'; +import { extractDefaultMessages, mergeConfigs, ListrContext } from './i18n/tasks'; run( async ({ @@ -59,7 +59,7 @@ run( } const srcPaths = Array().concat(path || ['./src', './packages']); - const list = new Listr([ + const list = new Listr([ { title: 'Merging .i18nrc.json files', task: () => new Listr(mergeConfigs(includeConfig), { exitOnError: true }), @@ -71,7 +71,7 @@ run( }, { title: 'Writing to file', - enabled: (ctx) => outputDir && ctx.messages.size, + enabled: (ctx) => Boolean(outputDir && ctx.messages.size), task: async (ctx) => { const sortedMessages = [...ctx.messages].sort(([key1], [key2]) => key1.localeCompare(key2) @@ -90,7 +90,7 @@ run( const reporter = new ErrorReporter(); const messages: Map = new Map(); await list.run({ messages, reporter }); - } catch (error) { + } catch (error: ErrorReporter | Error) { process.exitCode = 1; if (error instanceof ErrorReporter) { error.errors.forEach((e: string | Error) => log.error(e)); diff --git a/src/dev/run_i18n_integrate.ts b/src/dev/run_i18n_integrate.ts index 70c5d82422d2..6f0314e757e2 100644 --- a/src/dev/run_i18n_integrate.ts +++ b/src/dev/run_i18n_integrate.ts @@ -33,7 +33,7 @@ import Listr from 'listr'; import { createFailError, run } from '@osd/dev-utils'; import { ErrorReporter, integrateLocaleFiles } from './i18n'; -import { extractDefaultMessages, mergeConfigs } from './i18n/tasks'; +import { extractDefaultMessages, mergeConfigs, ListrContext } from './i18n/tasks'; run( async ({ @@ -90,7 +90,7 @@ run( const srcPaths = Array().concat(path || ['./src', './packages']); - const list = new Listr([ + const list = new Listr([ { title: 'Merging .i18nrc.json files', task: () => new Listr(mergeConfigs(includeConfig), { exitOnError: true }), @@ -103,17 +103,21 @@ run( { title: 'Integrating Locale File', task: async ({ messages, config }) => { - await integrateLocaleFiles(messages, { - sourceFileName: source, - targetFileName: target, - dryRun, - ignoreIncompatible, - ignoreUnused, - ignoreMissing, - ignoreMalformed, - config, - log, - }); + if (!config) { + throw new Error('Config is missing'); + } else { + await integrateLocaleFiles(messages, { + sourceFileName: source, + targetFileName: target, + dryRun, + ignoreIncompatible, + ignoreUnused, + ignoreMissing, + ignoreMalformed, + config, + log, + }); + } }, }, ]); @@ -123,7 +127,7 @@ run( const messages: Map = new Map(); await list.run({ messages, reporter }); process.exitCode = 0; - } catch (error) { + } catch (error: ErrorReporter | Error) { process.exitCode = 1; if (error instanceof ErrorReporter) { error.errors.forEach((e: string | Error) => log.error(e));