-
-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathmerge-schemes.ts
More file actions
44 lines (38 loc) · 1.51 KB
/
merge-schemes.ts
File metadata and controls
44 lines (38 loc) · 1.51 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
import { writeFileSync } from 'fs';
import { mergeWith } from 'lodash';
import { resolvePackagePath } from './packages/common/src';
interface CustomSchema {
originalSchemaPackage?: string;
originalSchemaPath: string;
schemaExtensionPaths: string[];
newSchemaPath: string;
}
const wd = process.cwd();
const schemesToMerge: CustomSchema[] = require(`${wd}/src/schemes`);
for (const {
originalSchemaPackage,
originalSchemaPath,
schemaExtensionPaths,
newSchemaPath,
} of schemesToMerge) {
const resolvedOriginalSchemaPath = originalSchemaPackage
? // Need it to bypass the Node resolution mechanism which respects only exported paths, currently only for esbuild
resolvePackagePath(originalSchemaPackage, originalSchemaPath)
: originalSchemaPath;
const originalSchema = require(resolvedOriginalSchemaPath);
const schemaExtensions = schemaExtensionPaths.map((path: string) => require(path));
const newSchema = schemaExtensions.reduce(
(extendedSchema: any, currentExtension: any) =>
mergeWith(extendedSchema, currentExtension, schemaMerger),
originalSchema
);
writeFileSync(newSchemaPath, JSON.stringify(newSchema, schemaValueReplacer, 2), 'utf-8');
}
function schemaMerger(resultSchemaValue: unknown, extensionSchemaValue: unknown) {
if (Array.isArray(extensionSchemaValue) && extensionSchemaValue[0] === '__REPLACE__') {
return extensionSchemaValue.slice(1);
}
}
function schemaValueReplacer(key: unknown, value: unknown) {
return value === '__DELETE__' ? undefined : value;
}