-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateOntology.ts
79 lines (64 loc) · 2.52 KB
/
generateOntology.ts
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
import { generateBaseObject } from './generateBaseObject.js';
import { generateClasses } from './generateClasses.js';
import { store } from './store.js';
import { camelCaseify } from './utils.js';
// TODO: Replace with actual project config file.
import { generatePropTypeMapping } from './generatePropTypeMapping.js';
import { generateSubjectToNameMapping } from './generateSubjectToNameMapping.js';
import { generateClassExports } from './generateClassExports.js';
import { atomicConfig } from './config.js';
import { PropertyRecord } from './PropertyRecord.js';
import { Core } from '@tomic/lib';
enum Inserts {
MODULE_ALIAS = '{{1}}',
BASE_OBJECT = '{{2}}',
CLASS_EXPORTS = '{{3}}',
CLASSES = '{{4}}',
PROP_TYPE_MAPPING = '{{7}}',
PROP_SUBJECT_TO_NAME_MAPPING = '{{8}}',
}
const TEMPLATE = `
/* -----------------------------------
* GENERATED WITH @tomic/cli
* For more info on how to use ontologies: https://github.com/atomicdata-dev/atomic-server/blob/develop/browser/cli/readme.md
* -------------------------------- */
import { BaseProps } from '${Inserts.MODULE_ALIAS}'
${Inserts.BASE_OBJECT}
${Inserts.CLASS_EXPORTS}
declare module '${Inserts.MODULE_ALIAS}' {
${Inserts.CLASSES}
${Inserts.PROP_TYPE_MAPPING}
${Inserts.PROP_SUBJECT_TO_NAME_MAPPING}
}
`;
export const generateOntology = async (
subject: string,
propertyRecord: PropertyRecord,
): Promise<{
filename: string;
content: string;
}> => {
const ontology = await store.getResourceAsync<Core.Ontology>(subject);
const properties = ontology.props.properties ?? [];
for (const prop of properties) {
propertyRecord.repordPropertyDefined(prop);
}
const [baseObjStr, reverseMapping] = await generateBaseObject(ontology);
const classesStr = generateClasses(ontology, reverseMapping, propertyRecord);
const propertiesStr = generatePropTypeMapping(ontology, reverseMapping);
const subToNameStr = generateSubjectToNameMapping(ontology, reverseMapping);
const classExportsStr = generateClassExports(ontology, reverseMapping);
const content = TEMPLATE.replaceAll(
Inserts.MODULE_ALIAS,
atomicConfig.moduleAlias ?? '@tomic/lib',
)
.replace(Inserts.BASE_OBJECT, baseObjStr)
.replace(Inserts.CLASS_EXPORTS, classExportsStr)
.replace(Inserts.CLASSES, classesStr)
.replace(Inserts.PROP_TYPE_MAPPING, propertiesStr)
.replace(Inserts.PROP_SUBJECT_TO_NAME_MAPPING, subToNameStr);
return {
filename: `${camelCaseify(ontology.title)}.ts`,
content,
};
};