feat: add the uml generator

This commit is contained in:
Michel Jonathan Schmitz
2019-05-27 13:10:41 +02:00
parent a9c0fddb23
commit 0f21da4a92
24 changed files with 2310 additions and 96 deletions

View File

@@ -21,6 +21,7 @@ import {join, sep} from 'path';
import {Definition} from 'ts-json-schema-generator';
import {Application, ProjectReflection} from 'typedoc';
import {promisify} from 'util';
import {LightweightType} from './uml/model/LightweightType';
export const globPromisified = promisify(glob);
export const mkdirPromisified = promisify(mkdir);
@@ -142,14 +143,14 @@ export interface ExpectableValidationErrors {
*
* @param srcPath Path to get reflection from
*/
export function getProjectReflection(srcPath: PathLike): ProjectReflection {
export function getProjectReflection(srcPath: PathLike, excludeExternals: boolean = true): ProjectReflection {
Logger.info(`Generating project reflection for ${srcPath.toString()}.`);
const tsconfigPath = getTsconfigPath(srcPath.toString());
// initialize new Typedoc application
const app = new Application({
excludeExternals: true,
excludeExternals: excludeExternals,
includeDeclarations: true,
module: 'commonjs',
tsconfig: join(tsconfigPath, 'tsconfig.json'),
@@ -178,7 +179,9 @@ export function getProjectReflection(srcPath: PathLike): ProjectReflection {
*
* @param schema Schema to check
*/
export function isSchemaWithDefinitions(schema: JSONSchema): schema is SchemaWithDefinitions {
export function isSchemaWithDefinitions(
schema: JSONSchema,
): schema is SchemaWithDefinitions {
return typeof schema.definitions !== 'undefined';
}
@@ -212,7 +215,9 @@ export function getTsconfigPath(startPath: string): string {
// repeat until a tsconfig.json is found
while (!existsSync(join(tsconfigPath, 'tsconfig.json'))) {
if (tsconfigPath === root) {
throw new Error(`Reached file system root ${root} while searching for 'tsconfig.json' in ${startPath}!`);
throw new Error(
`Reached file system root ${root} while searching for 'tsconfig.json' in ${startPath}!`,
);
}
// pop last directory
@@ -225,3 +230,51 @@ export function getTsconfigPath(startPath: string): string {
return tsconfigPath;
}
/**
* Converts a comma seperated string into a string array
*
* @param val Comma seperated string
*/
export function toArray(val: string): string[] {
return val.split(',');
}
/**
* Creates the full name of a lightweight type recursivly
*
* @param type Type to get the full name of
*/
export function getFullTypeName(type: LightweightType): string {
// init name
let fullName: string = type.name;
if (type.isTypeParameter) {
// type parameters are a sink
return fullName;
}
if (type.isLiteral) {
// literals are a sink
return "'" + fullName + "'";
}
if (type.isUnion && type.specificationTypes.length > 0) {
const tempNames: string[] = [];
for (const easyType of type.specificationTypes) {
tempNames.push(getFullTypeName(easyType));
}
// since unions can't be applied to other types, it is a sink.
return tempNames.join(' | ');
}
// check if type is generic and has a type attached
if (type.isTyped && type.genericsTypes.length > 0) {
const tempNames: string[] = [];
for (const easyType of type.genericsTypes) {
tempNames.push(getFullTypeName(easyType));
}
fullName += '<' + tempNames.join(', ') + '>';
}
// check if type is array
if (type.isArray) {
fullName += '[]';
}
return fullName;
}