forked from opensearch-project/opensearch-protobufs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreProcessing.ts
More file actions
62 lines (54 loc) · 2.5 KB
/
Copy pathPreProcessing.ts
File metadata and controls
62 lines (54 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { Command, Option } from '@commander-js/extra-typings';
import { read_yaml, write_yaml } from './utils/helper';
import Filter from './Filter';
import { Sanitizer } from './Sanitizer';
import Logger from './utils/logger';
import * as path from 'path';
import {SchemaModifier} from "./SchemaModifier";
import {VendorExtensionProcessor} from "./VendorExtensionProcessor";
import {GlobalParameterConsolidator} from "./GlobalParamWrapper";
import type {OpenAPIV3} from "openapi-types";
let config_filtered_path: string[] | undefined;
try {
const config = read_yaml(path.join(__dirname, 'config','target_api.yaml'));
config_filtered_path = config.paths;
} catch (e) {
console.error(e);
config_filtered_path = undefined;
}
const default_api_to_proto = config_filtered_path ?? ['/_search'];
const default_api_to_proto_str = default_api_to_proto.join(',');
const command = new Command()
.description('Preprocess an OpenAPI spec by filtering for specific paths and then sanitizing it.')
.addOption(new Option('-i, --input <path>', 'input YAML file').default((path.resolve(__dirname, '../../../opensearch-openapi.yaml'))))
.addOption(new Option('-o, --output <path>', 'output YAML file').default((path.resolve(__dirname, '../../../build/processed-opensearch-openapi.yaml'))))
.addOption(
new Option('-p, --filtered_path <paths>', 'the paths to keep (comma-separated, e.g., /_search,)')
.argParser((val: string) => val.split(',').map(s => s.trim()))
.default(default_api_to_proto)
)
.addOption(new Option('--verbose', 'show merge details').default(false))
.allowExcessArguments(false)
.parse();
type PreprocessingOpts = {
input: string;
output: string;
filtered_path: string[];
verbose: boolean;
};
const opts = command.opts() as PreprocessingOpts;
const logger = new Logger();
try {
logger.info(`PreProcessing ${opts.filtered_path.join(', ')} into ${opts.output} ...`)
const original_spec = read_yaml(opts.input)
const filtered_spec = new Filter().filter_spec(original_spec, opts.filtered_path);
const sanitized_spec = new Sanitizer().sanitize(filtered_spec);
const consolidated_spec = new GlobalParameterConsolidator(sanitized_spec).consolidate();
const vendor_processed_spec = new VendorExtensionProcessor(consolidated_spec, logger).process();
const schema_modified_spec = new SchemaModifier(vendor_processed_spec, logger).modify();
write_yaml(opts.output, schema_modified_spec);
} catch (err) {
logger.error(`Error in preprocessing: ${err}`);
process.exit(1);
}
logger.info('Done.')