From ccb4cedc5ff8ecb32d874e2401cb4c4667420299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wieland=20Sch=C3=B6bl?= Date: Thu, 5 Aug 2021 14:47:13 +0200 Subject: [PATCH] refactor: change function of put es templates --- src/cli.ts | 30 ++++++++++++------------------ src/mapping.ts | 6 ++---- src/types/mapping.d.ts | 21 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 51884e8b..d848e56d 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -20,6 +20,7 @@ import {resolve} from 'path'; import {exit} from 'process'; import {generateTemplate} from './mapping'; import {getProjectReflection} from './project-reflection'; +import {ElasticsearchTemplateCollection} from './types/mapping'; // handle unhandled promise rejections process.on('unhandledRejection', async (reason: unknown) => { @@ -81,38 +82,31 @@ commander // tslint:disable-next-line:no-magic-numbers writeFileSync(errPath, JSON.stringify(result.errors, null, 2)); Logger.ok(`Mapping errors written to ${errPath}.`); + } else { + for (const error of result.errors) { + await Logger.error(error); + } + + throw new Error('Mapping generation failed'); } }); commander .command('put-es-templates [ignoredTags]') - .action(async (relativeSrcPath, esAddress, ignoredTags) => { + .action(async (relativeSrcPath, esAddress) => { // get absolute paths const srcPath = resolve(relativeSrcPath); - let ignoredTagsList: string[] = []; - if (typeof ignoredTags === 'string') { - ignoredTagsList = ignoredTags.split(','); - } - // get project reflection - const projectReflection = getProjectReflection(srcPath); + const templates = JSON.parse(srcPath) as ElasticsearchTemplateCollection; - const result = generateTemplate(projectReflection, ignoredTagsList, true); - if (result.errors.length !== 0) { - await Logger.error(`Mapping generated with errors:\n${JSON.stringify(result.errors)}`); - exit(-1); - } else { - Logger.ok('Mapping generated without errors!'); - } - - for (const template in result.mappings) { - if (!result.mappings.hasOwnProperty(template)) { + for (const template in templates) { + if (!templates.hasOwnProperty(template)) { continue; } const response = await got.put(`${esAddress}_template/${template}`, { - json: result.mappings[template], + json: templates[template], }); const HTTP_STATUS_OK = 200; diff --git a/src/mapping.ts b/src/mapping.ts index ef2ca312..17fb0087 100644 --- a/src/mapping.ts +++ b/src/mapping.ts @@ -38,7 +38,7 @@ import { ElasticsearchObject, ElasticsearchTemplateCollection, ElasticsearchType, - ElasticsearchValue, + ElasticsearchValue, MappingGenTemplate, } from './types/mapping'; let dynamicTemplates: ElasticsearchDynamicTemplate[] = []; @@ -665,9 +665,7 @@ function reset(resetInheritTags = true) { export function generateTemplate(projectReflection: ProjectReflection, ignoredTags: string[], showErrorOutput = true, - interfaceFilter: string[] = []): -// tslint:disable-next-line:completed-docs - { aggregations: AggregationSchema; errors: string[]; mappings: ElasticsearchTemplateCollection; } { + interfaceFilter: string[] = []): MappingGenTemplate { reset(); showErrors = showErrorOutput; diff --git a/src/types/mapping.d.ts b/src/types/mapping.d.ts index fc3ffc3e..a7e17fb3 100644 --- a/src/types/mapping.d.ts +++ b/src/types/mapping.d.ts @@ -13,9 +13,30 @@ * this program. If not, see . */ import {ElasticsearchDataType} from '../config/typemap'; +import {AggregationSchema} from './aggregation'; // tslint:disable:no-any +/** + * Template output of the mapping generation + */ +export interface MappingGenTemplate { + /** + * All generated aggregations + */ + aggregations: AggregationSchema; + + /** + * All errors that occurred + */ + errors: string[]; + + /** + * All mappings that were generated + */ + mappings: ElasticsearchTemplateCollection; +} + /** * ElasticsearchValue can be either a type or an object. *