style: apply strict style rules for this feature

This commit is contained in:
Michel Jonathan Schmitz
2019-06-05 16:29:08 +02:00
parent 843e59811a
commit 23cbc53fef
10 changed files with 90 additions and 67 deletions

View File

@@ -29,9 +29,9 @@ import {
getNodeMetaInformationMap,
} from './routes';
import {Converter, getValidatableTypesFromReflection} from './schema';
import {createDiagram, createDiagramFromString} from './uml/createDiagram';
import {readDefinitions} from './uml/readDefinitions';
import {UMLConfig} from './uml/umlConfig';
import {createDiagram, createDiagramFromString} from './uml/create-diagram';
import {readDefinitions} from './uml/read-definitions';
import {UMLConfig} from './uml/uml-config';
import {validateFiles, writeReport} from './validate';
// handle unhandled promise rejections
@@ -169,9 +169,11 @@ commander
}
});
commander.command('pack').action(async () => {
await pack();
});
commander
.command('pack')
.action(async () => {
await pack();
});
commander
.command('plantuml <srcPath> <plantumlserver>')
@@ -250,7 +252,8 @@ commander
commander
.command('plantuml-file <inputFile> <plantumlserver> [outputFile]')
.action(async (file: string, plantumlserver: string, outputFile: string) => {
const fileContent = readFileSync(resolve(file)).toString();
const fileContent = readFileSync(resolve(file))
.toString();
await createDiagramFromString(fileContent, plantumlserver, outputFile);
});

View File

@@ -21,7 +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';
import {LightweightType} from './uml/model/lightweight-type';
export const globPromisified = promisify(glob);
export const mkdirPromisified = promisify(mkdir);
@@ -143,7 +143,7 @@ export interface ExpectableValidationErrors {
*
* @param srcPath Path to get reflection from
*/
export function getProjectReflection(srcPath: PathLike, excludeExternals: boolean = true): ProjectReflection {
export function getProjectReflection(srcPath: PathLike, excludeExternals = true): ProjectReflection {
Logger.info(`Generating project reflection for ${srcPath.toString()}.`);
const tsconfigPath = getTsconfigPath(srcPath.toString());
@@ -254,13 +254,14 @@ export function getFullTypeName(type: LightweightType): string {
}
if (type.isLiteral) {
// literals are a sink
return "'" + fullName + "'";
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(' | ');
}
@@ -270,11 +271,12 @@ export function getFullTypeName(type: LightweightType): string {
for (const easyType of type.genericsTypes) {
tempNames.push(getFullTypeName(easyType));
}
fullName += '<' + tempNames.join(', ') + '>';
fullName = `${fullName}<${tempNames.join(', ')}>`;
}
// check if type is array
if (type.isArray) {
fullName += '[]';
}
return fullName;
}

View File

@@ -16,12 +16,12 @@ import {Logger} from '@openstapps/logger';
import {createWriteStream} from 'fs';
import * as request from 'got';
import {getFullTypeName} from '../common';
import {LightweightClassDefinition} from './model/LightweightClassDefinition';
import {LightweightDefinition} from './model/LightweightDefinition';
import {LightweightEnumDefinition} from './model/LightweightEnumDefinition';
import {LightweightProperty} from './model/LightweightProperty';
import {LightweightType} from './model/LightweightType';
import {UMLConfig} from './umlConfig';
import {LightweightClassDefinition} from './model/lightweight-class-definition';
import {LightweightDefinition} from './model/lightweight-definition';
import {LightweightEnumDefinition} from './model/lightweight-enum-definition';
import {LightweightProperty} from './model/lightweight-property';
import {LightweightType} from './model/lightweight-type';
import {UMLConfig} from './uml-config';
/**
* Converts the lightweight class/enum definitions according to the configuration,
@@ -57,7 +57,7 @@ export async function createDiagram(
config.definitions = config.definitions.concat(inheritedDefinitions);
}
let modelPlantUMLCode: string = '';
let modelPlantUMLCode = '';
// creates a UML definition for every specified definition name
// however if no definitions were provided all definitions will be transformed
for (const definition of definitions) {
@@ -70,7 +70,7 @@ export async function createDiagram(
}
// either the definitions are empty or the definition was specified, proceed
let definitionPlantUMLCode: string = '';
let definitionPlantUMLCode = '';
if (definition instanceof LightweightClassDefinition) {
definitionPlantUMLCode = createPlantUMLCodeForClass(config, definition);
} else if (definition instanceof LightweightEnumDefinition) {
@@ -81,14 +81,16 @@ export async function createDiagram(
modelPlantUMLCode += definitionPlantUMLCode;
}
return await createDiagramFromString(modelPlantUMLCode, plantUmlBaseURL, config.outputFileName);
return createDiagramFromString(modelPlantUMLCode, plantUmlBaseURL, config.outputFileName);
}
/**
* This will encode the plantuml code and post the code to the plantuml server
* The server will then parse the code and create a corresponding diagram
*
* @param modelPlantUMLCode
* @param modelPlantUMLCode raw PlantUML code
* @param plantUmlBaseURL PlantUML server address that shall be used
* @param outputFile filename of the output file without file extension
*/
export async function createDiagramFromString(
modelPlantUMLCode: string,
@@ -101,7 +103,8 @@ export async function createDiagramFromString(
let response;
try {
response = await request(url);
if (response.statusCode !== 200) {
const httpOK = 200;
if (response.statusCode !== httpOK) {
Logger.error(`Plantuml Server responded with an error.\n${response.statusMessage}`);
throw new Error('Response not okay');
}
@@ -112,11 +115,13 @@ export async function createDiagramFromString(
// attach file extension
const fileName = `${outputFile}.svg`;
try {
createWriteStream(fileName).write(response.body);
createWriteStream(fileName)
.write(response.body);
Logger.log(`Writen data to file: ${fileName}`);
} catch (e) {
throw new Error('Could not write file. Are you missing permissions?');
}
return fileName;
}
@@ -146,6 +151,7 @@ function gatherTypeAssociations(
);
}
}
return abstractions;
}
@@ -178,6 +184,7 @@ function getReferenceTypes(type: LightweightType): string[] {
}
}
}
return types;
}
@@ -192,7 +199,7 @@ function createPlantUMLCodeForClass(
readerClass: LightweightClassDefinition,
): string {
// create the definition header, what type the definition is, it's name and it's inheritance
let model: string = `${readerClass.type} ${readerClass.name}`;
let model = `${readerClass.type} ${readerClass.name}`;
if (readerClass.typeParameters.length > 0) {
model += `<${readerClass.typeParameters.join(', ')}>`;
@@ -253,7 +260,7 @@ function createPlantUMLCodeForEnum(
readerEnum: LightweightEnumDefinition,
): string {
// create enum header
let model: string = `enum ${readerEnum.name} {`;
let model = `enum ${readerEnum.name} {`;
// add values
if (config.showEnumValues) {
for (const value of readerEnum.values) {
@@ -269,11 +276,7 @@ function createPlantUMLCodeForEnum(
* Creates a property PlantUML Line
*/
function createPropertyLine(property: LightweightProperty): string {
return (
(property.inherited ? '/ ' : '') +
(property.optional ? '?' : '') +
property.name +
' : ' +
getFullTypeName(property.type)
);
const prefix = `${(property.inherited ? '/ ' : '')}${(property.optional ? '? ' : '')}`;
return `${prefix}${property.name} : ${getFullTypeName(property.type)}`;
}

View File

@@ -13,8 +13,8 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {LightweightDefinition} from './LightweightDefinition';
import {LightweightProperty} from './LightweightProperty';
import {LightweightDefinition} from './lightweight-definition';
import {LightweightProperty} from './lightweight-property';
/**
* Represents a class definition
*/

View File

@@ -13,7 +13,7 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {LightweightDefinition} from './LightweightDefinition';
import {LightweightDefinition} from './lightweight-definition';
/**
* Represents an enum definition
*/

View File

@@ -12,7 +12,7 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {LightweightType} from './LightweightType';
import {LightweightType} from './lightweight-type';
/**
* Represents a property definition
@@ -45,7 +45,7 @@ export class LightweightProperty {
* @param type Type of the property
* @param optional Is the property optional
*/
constructor(name: string, type: LightweightType, optional: boolean = true) {
constructor(name: string, type: LightweightType, optional = true) {
this.name = name;
this.optional = optional;
this.inherited = false;

View File

@@ -25,47 +25,47 @@ export class LightweightType {
/**
* Does the type have generic-parameters
*/
hasTypeInformation: boolean = false;
hasTypeInformation = false;
/**
* Does the type represent an array type
*/
isArray: boolean = false;
isArray = false;
/**
* Does the type represent a literal type
*/
isLiteral: boolean = false;
isLiteral = false;
/**
* Does the type represent a primitive type
*/
isPrimitive: boolean = false;
isPrimitive = false;
/**
* Does the type contain a reference to
*/
isReference: boolean = false;
isReference = false;
/**
* Is the type a reflection and not avaiblabe at compile time
*/
isReflection: boolean = false;
isReflection = false;
/**
* Does the type have type parameters
*/
isTyped: boolean = false;
isTyped = false;
/**
* Is the type a typed parameter
*/
isTypeParameter: boolean = false;
isTypeParameter = false;
/**
* Is the type a union type
*/
isUnion: boolean = false;
isUnion = false;
/**
* Name of the type

View File

@@ -28,11 +28,11 @@ import {
UnionType,
} from 'typedoc/dist/lib/models';
import {getFullTypeName} from '../common';
import {LightweightClassDefinition} from './model/LightweightClassDefinition';
import {LightweightDefinition} from './model/LightweightDefinition';
import {LightweightEnumDefinition} from './model/LightweightEnumDefinition';
import {LightweightProperty} from './model/LightweightProperty';
import {LightweightType} from './model/LightweightType';
import {LightweightClassDefinition} from './model/lightweight-class-definition';
import {LightweightDefinition} from './model/lightweight-definition';
import {LightweightEnumDefinition} from './model/lightweight-enum-definition';
import {LightweightProperty} from './model/lightweight-property';
import {LightweightType} from './model/lightweight-type';
/**
* Reads the reflection model from typedoc and converts it into a flatter, easier to handle model
@@ -142,6 +142,7 @@ function getTypeInformation(type: LightweightType): string[] {
} else {
values.push(type.name);
}
return values;
}
@@ -225,23 +226,30 @@ export function readAsClassDefinition(
function readTypeInformation(declarationType: Type): LightweightType {
if (declarationType instanceof ReflectionType) {
return readAsReflectionType(declarationType);
} else if (declarationType instanceof TypeOperatorType) {
return readAsTypeOperatorType(declarationType);
} else if (declarationType instanceof TypeParameterType) {
return readAsTypeParameterType(declarationType);
} else if (declarationType instanceof IntrinsicType) {
return readAsIntrinsicType(declarationType);
} else if (declarationType instanceof StringLiteralType) {
return readAsStringLiteralType(declarationType);
} else if (declarationType instanceof ReferenceType) {
return readAsReferenceType(declarationType);
} else if (declarationType instanceof ArrayType) {
return readAsArrayType(declarationType);
} else if (declarationType instanceof UnionType) {
return readAsUnionType(declarationType);
} else {
throw new Error(`Could not read type ${declarationType.type}`);
}
if (declarationType instanceof TypeOperatorType) {
return readAsTypeOperatorType(declarationType);
}
if (declarationType instanceof TypeParameterType) {
return readAsTypeParameterType(declarationType);
}
if (declarationType instanceof IntrinsicType) {
return readAsIntrinsicType(declarationType);
}
if (declarationType instanceof StringLiteralType) {
return readAsStringLiteralType(declarationType);
}
if (declarationType instanceof ReferenceType) {
return readAsReferenceType(declarationType);
}
if (declarationType instanceof ArrayType) {
return readAsArrayType(declarationType);
}
if (declarationType instanceof UnionType) {
return readAsUnionType(declarationType);
}
throw new Error(`Could not read type ${declarationType.type}`);
}
/**
@@ -256,6 +264,7 @@ function readAsIntrinsicType(type: IntrinsicType): LightweightType {
easyType.name = type.name;
easyType.isPrimitive = true;
easyType.hasTypeInformation = true;
return easyType;
}
@@ -271,6 +280,7 @@ function readAsStringLiteralType(type: StringLiteralType): LightweightType {
returnType.name = type.value;
returnType.isLiteral = true;
returnType.hasTypeInformation = true;
return returnType;
}
@@ -333,6 +343,7 @@ function readAsArrayType(type: ArrayType): LightweightType {
returnType.name = getFullTypeName(typeOfArray);
returnType.specificationTypes = [typeOfArray];
returnType.isArray = true;
return returnType;
}
@@ -355,6 +366,7 @@ function readAsUnionType(type: UnionType): LightweightType {
returnType.specificationTypes = typesOfUnion;
returnType.name = getFullTypeName(returnType);
returnType.isUnion = true;
return returnType;
}
@@ -379,6 +391,7 @@ function readAsReflectionType(type: ReflectionType): LightweightType {
}
returnType.name = 'object';
returnType.isReflection = true;
return returnType;
}
@@ -399,6 +412,7 @@ function readAsTypeOperatorType(type: TypeOperatorType): LightweightType {
// can't be traced deeper! so might as well be a primitive
returnType.isPrimitive = true;
returnType.hasTypeInformation = true;
return returnType;
}
@@ -419,5 +433,6 @@ function readAsTypeParameterType(type: TypeParameterType): LightweightType {
returnType.name = type.name;
returnType.isTypeParameter = true;
returnType.hasTypeInformation = true;
return returnType;
}