Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion browser/cli/src/DatatypeToTSTypeMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export const DatatypeToTSTypeMap = {
[Datatype.SLUG]: 'string',
[Datatype.MARKDOWN]: 'string',
[Datatype.URI]: 'string',
[Datatype.JSON]: 'unknown',
[Datatype.JSON]: 'JSONValue',
[Datatype.UNKNOWN]: 'JSONValue',
};
12 changes: 10 additions & 2 deletions browser/cli/src/generateOntology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum Inserts {
CLASSES = '{{4}}',
PROP_TYPE_MAPPING = '{{7}}',
PROP_SUBJECT_TO_NAME_MAPPING = '{{8}}',
TYPE_IMPORTS = '{{9}}',
}

const TEMPLATE = `
Expand All @@ -25,7 +26,7 @@ const TEMPLATE = `
* For more info on how to use ontologies: https://github.com/atomicdata-dev/atomic-server/blob/develop/browser/cli/readme.md
* -------------------------------- */

import type { OntologyBaseObject, BaseProps } from '${Inserts.MODULE_ALIAS}'
import type { ${Inserts.TYPE_IMPORTS} } from '${Inserts.MODULE_ALIAS}'

${Inserts.BASE_OBJECT}

Expand Down Expand Up @@ -57,14 +58,21 @@ export const generateOntology = async (

const [baseObjStr, reverseMapping] = await generateBaseObject(ontology);
const classesStr = generateClasses(ontology, reverseMapping, propertyRecord);
const propertiesStr = generatePropTypeMapping(ontology, reverseMapping);
const [propertiesStr, propertiesImports] = 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.TYPE_IMPORTS,
['OntologyBaseObject', 'BaseProps', ...propertiesImports].join(', '),
)
.replace(Inserts.BASE_OBJECT, baseObjStr)
.replace(Inserts.CLASS_EXPORTS, classExportsStr)
.replace(Inserts.CLASSES, classesStr)
Expand Down
8 changes: 6 additions & 2 deletions browser/cli/src/generatePropTypeMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ import { dedupe } from './utils.js';
export const generatePropTypeMapping = (
ontology: Resource<Core.Ontology>,
reverseMapping: ReverseMapping,
): string => {
): [mappingString: string, usedImports: string[]] => {
const properties = dedupe(ontology.props.properties ?? []);

const lines = properties
.map(subject => generateLine(subject, reverseMapping))
.join('\n');

return `interface PropTypeMapping {
const mappingString = `interface PropTypeMapping {
${lines}
}`;

const imports = mappingString.includes('JSONValue') ? ['JSONValue'] : [];

return [mappingString, imports];
};

const generateLine = (subject: string, reverseMapping: ReverseMapping) => {
Expand Down
Loading