forked from opensearch-project/opensearch-protobufs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.ts
More file actions
137 lines (116 loc) · 3.92 KB
/
Copy pathhelper.ts
File metadata and controls
137 lines (116 loc) · 3.92 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {mkdirSync, writeFileSync, readFileSync} from 'fs'
import {parse, visit, Document} from 'yaml'
import {dirname} from "path";
import {OpenAPIV3} from "openapi-types";
import _ from 'lodash';
export function read_yaml<T = Record<string, any>> (file_path: string, exclude_schema: boolean = false): T {
const doc = parse(readFileSync(file_path, 'utf8'))
if (typeof doc === 'object' && exclude_schema) delete doc.$schema
return doc
}
export function write_yaml (file_path: string, content: any): void {
const doc = new Document(content, null, { aliasDuplicateObjects: false })
visit(doc, {
// eslint-disable-next-line @typescript-eslint/naming-convention
Scalar(_, node) {
if (typeof node.value === 'string') {
const value = node.value.toLowerCase();
// Ensure "human" boolean string values are quoted as old YAML parsers might coerce them to boolean true/false
if (value === 'no' || value === 'yes' || value === 'n' || value === 'y' || value === 'off' || value === 'on') {
node.type = 'QUOTE_SINGLE'
}
}
}
})
write_text(file_path, doc.toString({
lineWidth: 0,
singleQuote: true
}))
}
export function ensure_parent_dir (file_path: string): void {
mkdirSync(dirname(file_path), { recursive: true })
}
export function write_text (file_path: string, text: string): void {
ensure_parent_dir(file_path)
writeFileSync(file_path, text)
}
export function compressMultipleUnderscores(str: string): string {
return str.replace(/_+/g, '_');
}
export function resolveObj(
obj: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined,
root: OpenAPIV3.Document
): OpenAPIV3.SchemaObject | undefined {
if (!obj) return undefined;
if ("$ref" in obj) {
return resolveRef(obj.$ref, root);
}
return obj as OpenAPIV3.SchemaObject;
}
export function resolveRef(
ref: string,
root: OpenAPIV3.Document
): OpenAPIV3.SchemaObject | undefined {
if (!ref.startsWith("#/")) {
return undefined;
}
const pathParts = ref.replace(/^#\//, "").split("/");
let current: any = root;
for (const part of pathParts) {
if (current == null || typeof current !== "object") {
return undefined;
}
current = current[part];
}
if (!current) {
return undefined;
}
if ("$ref" in current) {
return resolveObj(current, root);
}
return current as OpenAPIV3.SchemaObject;
}
export function isPrimitiveType(schema: OpenAPIV3.SchemaObject): boolean {
if (schema.type === undefined || schema.type === "array") {
return false;
}
const primitiveTypes: Array<OpenAPIV3.NonArraySchemaObjectType> = [
'string',
'number',
'integer',
'boolean'
];
return primitiveTypes.includes(schema.type);
}
export function isEmptyObjectSchema(schema: OpenAPIV3.SchemaObject): boolean {
return (
schema.type === 'object' &&
!schema.properties &&
!schema.allOf &&
!schema.anyOf &&
!schema.oneOf &&
!('$ref' in schema)
);
}
export function isReferenceObject(schema: any): schema is OpenAPIV3.ReferenceObject {
return schema !== null && typeof schema === 'object' && '$ref' in schema;
}
/**
* Recursively delete all items matching the given condition
* This includes removing them from their parent collections and cleaning up empty arrays
*/
export function deleteMatchingKeys(obj: any, condition: (item: any) => boolean): void {
for (const key in obj) {
const item = obj[key];
if (_.isObject(item)) {
if (condition(item)) {
delete obj[key];
} else {
deleteMatchingKeys(item, condition);
if (_.isArray(item)) {
obj[key] = _.compact(item);
}
}
}
}
}