feat: migrate to esm

This commit is contained in:
2023-03-16 01:58:13 +01:00
parent fd740b3091
commit 4df19e8c20
512 changed files with 3016 additions and 2222 deletions

View File

@@ -15,14 +15,13 @@
import {Logger} from '@openstapps/logger';
import {createWriteStream} from 'fs';
import * as request from 'got';
import {forEach, map, isEmpty} from 'lodash';
import {expandTypeValue, isLightweightClass, isUnionOrIntersectionType} from '../easy-ast/ast-util';
import {LightweightAliasDefinition} from '../easy-ast/types/lightweight-alias-definition';
import {LightweightClassDefinition} from '../easy-ast/types/lightweight-class-definition';
import {LightweightDefinition} from '../easy-ast/types/lightweight-definition';
import {LightweightProperty} from '../easy-ast/types/lightweight-property';
import {LightweightType} from '../easy-ast/types/lightweight-type';
import {UMLConfig} from './uml-config';
import {expandTypeValue, isLightweightClass} from '../easy-ast/ast-util.js';
import {LightweightAliasDefinition} from '../easy-ast/types/lightweight-alias-definition.js';
import {LightweightClassDefinition} from '../easy-ast/types/lightweight-class-definition.js';
import {LightweightDefinition} from '../easy-ast/types/lightweight-definition.js';
import {LightweightProperty} from '../easy-ast/types/lightweight-property.js';
import {LightweightType} from '../easy-ast/types/lightweight-type.js';
import {UMLConfig} from './uml-config.js';
/**
* Converts the lightweight class/enum definitions according to the configuration,
@@ -39,7 +38,7 @@ export async function createDiagram(
plantUmlBaseURL: string,
): Promise<string> {
// when non definitions were specified use all
config.definitions = map(definitions, 'name');
config.definitions = definitions.map(it => it.name);
// when providing definitions and either showing associations or inheritance the
// inherited definitions will be added automatically
@@ -54,9 +53,9 @@ export async function createDiagram(
// creates a UML definition for every specified definition name
// however if no definitions were provided all definitions will be transformed
const modelPlantUMLCode = map(
definitions.filter(it => !config.definitions.includes(it.name)),
definition =>
const modelPlantUMLCode = definitions
.filter(it => !config.definitions.includes(it.name))
.map(definition =>
isLightweightClass(definition)
? createPlantUMLCodeForClass(config, definition)
: createPlantUMLCodeForEnum(config, definition),
@@ -152,18 +151,20 @@ function getReferenceTypes(type: LightweightType): string[] {
types.push(type.referenceName);
}
forEach(type.genericsTypes, specificType => {
for (const value of getReferenceTypes(specificType)) {
types.push(value);
}
});
if ((isUnionOrIntersectionType(type) && isEmpty(type.specificationTypes)) || type.isArray) {
forEach(type.specificationTypes, specificType => {
if (type.genericsTypes) {
for (const specificType of type.genericsTypes) {
for (const value of getReferenceTypes(specificType)) {
types.push(value);
}
});
}
}
if (Array.isArray(type.specificationTypes)) {
for (const specificType of type.specificationTypes) {
for (const value of getReferenceTypes(specificType)) {
types.push(value);
}
}
}
return types;
@@ -194,35 +195,39 @@ function createPlantUMLCodeForClass(config: UMLConfig, readerClass: LightweightC
model += '{';
// add the properties to the definition body
if (config.showProperties) {
forEach(readerClass.properties, property => {
if (config.showProperties && readerClass.properties) {
for (const key in readerClass.properties) {
const property = readerClass.properties[key];
if (property.optional && !config.showOptionalProperties) {
// don't show optional attributes
return;
continue;
}
/*if (property.inherited && !config.showInheritedProperties) {
// don't show inherited properties
continue;
}*/
model += `\n\t${createPropertyLine(property)}`;
});
}
}
// close the definition body
model += '\n}\n';
// add associations from properties with references
forEach(readerClass.properties, property => {
const types: string[] = getReferenceTypes(property.type);
for (const type of types) {
if (config.showAssociations) {
/*if (property.inherited && !config.showInheritedProperties) {
continue;
}*/
model += `${readerClass.name} -up-> ${type} : ${property.name} >\n`;
if (readerClass.properties) {
for (const key in readerClass.properties) {
const property = readerClass.properties[key];
const types: string[] = getReferenceTypes(property.type);
for (const type of types) {
if (config.showAssociations) {
/*if (property.inherited && !config.showInheritedProperties) {
continue;
}*/
model += `${readerClass.name} -up-> ${type} : ${property.name} >\n`;
}
}
}
});
}
return model;
}
@@ -237,10 +242,10 @@ function createPlantUMLCodeForEnum(config: UMLConfig, readerEnum: LightweightAli
// create enum header
let model = `enum ${readerEnum.name} {`;
// add values
if (config.showEnumValues) {
forEach(readerEnum.type?.specificationTypes, value => {
if (config.showEnumValues && readerEnum.type?.specificationTypes) {
for (const value of readerEnum.type?.specificationTypes) {
model += `\n\t${value.toString()}`;
});
}
}
model += '\n}\n';