refactor: change function of put es templates

This commit is contained in:
Wieland Schöbl
2021-08-05 14:47:13 +02:00
parent bf862cfb78
commit ccb4cedc5f
3 changed files with 35 additions and 22 deletions

View File

@@ -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 <srcPath> <esAddress> [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;