Skip to content

Commit

Permalink
[Console] Use constants from the Console plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
yuliacech committed Jul 13, 2023
1 parent cbfe17b commit d1b9596
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
19 changes: 2 additions & 17 deletions packages/kbn-generate-console-definitions/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,16 @@ export function runGenerateConsoleDefinitionsCli() {
(context) => {
const { log, flags } = context;
log.info('starting console definitions generation');
const { source, dest, emptyDest } = flags;
const { source, dest } = flags;
if (!source) {
throw createFlagError(`Missing --source argument`);
}
let generatedFilesFolder = Path.resolve(REPO_ROOT, `${dest}`);
if (!dest) {
generatedFilesFolder = Path.resolve(AUTOCOMPLETE_DEFINITIONS_FOLDER, 'generated');
generatedFilesFolder = AUTOCOMPLETE_DEFINITIONS_FOLDER;
}
log.info(`autocomplete definitions folder ${generatedFilesFolder}`);
createFolderIfDoesntExist(generatedFilesFolder, log);
const files = fs.readdirSync(generatedFilesFolder);
if (files.length > 0) {
if (!emptyDest) {
throw createFlagError(
`Definitions folder already contain files, use --emptyDest to clean the folder before generation`
);
}
log.warning(`folder ${generatedFilesFolder} already contains files, emptying the folder`);
for (const file of files) {
fs.rmSync(Path.resolve(generatedFilesFolder, file), { recursive: true });
}
log.warning(`folder ${generatedFilesFolder} has been emptied`);
}

const specsRepo = Path.resolve(`${source}`);
if (!fs.existsSync(specsRepo)) {
throw createFlagError(`ES specification folder ${specsRepo} doesn't exist`);
Expand All @@ -65,7 +51,6 @@ node scripts/generate_console_definitions.js --source <ES_SPECIFICATION_REPO> [-
help: `
--source Folder containing the root of the Elasticsearch specification repo
--dest Folder where console autocomplete definitions will be generated (relative to the Kibana repo root)
--emptyDest Flag to empty definitions folder if it already contains any files
`,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
import fs from 'fs';
import Path from 'path';
import type { ToolingLog } from '@kbn/tooling-log';
import { ENDPOINTS_SUBFOLDER, GLOBALS_SUBFOLDER } from '@kbn/console-plugin/common/constants';
import { generateQueryParams } from './generate_query_params';
import type {
AutocompleteBodyParams,
AutocompleteDefinition,
AutocompleteUrlParams,
SpecificationTypes,
} from './types';
import { createFolderIfDoesntExist, findTypeDefinition, saveJsonToFile } from './utils';
import {
createFolderIfDoesntExist,
emptyFolder,
findTypeDefinition,
saveJsonToFile,
} from './utils';
import { BodyParamsConverter } from './body_params_converter';

const generateMethods = (endpoint: SpecificationTypes.Endpoint): string[] => {
Expand Down Expand Up @@ -117,8 +123,9 @@ export function generateConsoleDefinitions({
const schema = JSON.parse(fs.readFileSync(pathToSchemaFile, 'utf8')) as SpecificationTypes.Model;

// convert endpoints
const definitionsFolder = Path.resolve(generatedFilesFolder, 'endpoints');
const definitionsFolder = Path.resolve(generatedFilesFolder, ENDPOINTS_SUBFOLDER);
createFolderIfDoesntExist(definitionsFolder, log);
emptyFolder(definitionsFolder, log);
const { endpoints } = schema;
log.info(`iterating over endpoints array: ${endpoints.length} endpoints`);
const bodyParamsConverter = new BodyParamsConverter(schema);
Expand All @@ -133,8 +140,9 @@ export function generateConsoleDefinitions({
});

// convert global types needed for endpoint definitions
const globalsFolder = Path.resolve(generatedFilesFolder, 'globals');
const globalsFolder = Path.resolve(generatedFilesFolder, GLOBALS_SUBFOLDER);
createFolderIfDoesntExist(globalsFolder, log);
emptyFolder(globalsFolder, log);
const globalTypes = bodyParamsConverter.getPublicTypes();
console.log({ globalTypes });
const globalDefinitions = bodyParamsConverter.convertGlobals();
Expand Down
13 changes: 12 additions & 1 deletion packages/kbn-generate-console-definitions/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import fs from 'fs';
import { ToolingLog } from '@kbn/tooling-log';
import { join } from 'path';
import Path, { join } from 'path';
import type { SpecificationTypes } from './types';
import { SpecificationTypes as S } from './types';

Expand All @@ -29,6 +29,17 @@ export const createFolderIfDoesntExist = (folder: string, log: ToolingLog) => {
}
};

export const emptyFolder = (folder: string, log: ToolingLog) => {
const files = fs.readdirSync(folder);
if (files.length > 0) {
log.warning(`folder ${folder} already contains files, emptying the folder`);
for (const file of files) {
fs.rmSync(Path.resolve(folder, file), { recursive: true });
}
log.warning(`folder ${folder} has been emptied`);
}
};

export const saveJsonToFile = ({
folder,
name,
Expand Down

0 comments on commit d1b9596

Please sign in to comment.