Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2dfe88a

Browse files
committed
feat(app): Nest @entity support
fix #1131
1 parent 122afa7 commit 2dfe88a

32 files changed

+379
-38
lines changed

src/app/application.ts

+63-6
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,9 @@ export class Application {
506506
if (diffCrawledData.controllers.length > 0) {
507507
actions.push(() => this.prepareControllers());
508508
}
509+
if (diffCrawledData.entities.length > 0) {
510+
actions.push(() => this.prepareEntities());
511+
}
509512
if (diffCrawledData.modules.length > 0) {
510513
actions.push(() => this.prepareModules());
511514
}
@@ -610,6 +613,9 @@ export class Application {
610613
if (DependenciesEngine.controllers.length > 0) {
611614
logger.info(`- controller : ${DependenciesEngine.controllers.length}`);
612615
}
616+
if (DependenciesEngine.entities.length > 0) {
617+
logger.info(`- entity : ${DependenciesEngine.entities.length}`);
618+
}
613619
if (DependenciesEngine.directives.length > 0) {
614620
logger.info(`- directive : ${DependenciesEngine.directives.length}`);
615621
}
@@ -659,6 +665,12 @@ export class Application {
659665
});
660666
}
661667

668+
if (DependenciesEngine.entities.length > 0) {
669+
actions.push(() => {
670+
return this.prepareEntities();
671+
});
672+
}
673+
662674
if (DependenciesEngine.injectables.length > 0) {
663675
actions.push(() => {
664676
return this.prepareInjectables();
@@ -1439,6 +1451,42 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
14391451
});
14401452
}
14411453

1454+
public prepareEntities(someEntities?) {
1455+
logger.info('Prepare entities');
1456+
Configuration.mainData.entities = someEntities
1457+
? someEntities
1458+
: DependenciesEngine.getEntities();
1459+
1460+
return new Promise((resolve, reject) => {
1461+
let i = 0;
1462+
const len = Configuration.mainData.entities.length;
1463+
const loop = () => {
1464+
if (i < len) {
1465+
let entity = Configuration.mainData.entities[i];
1466+
let page = {
1467+
path: 'entities',
1468+
name: entity.name,
1469+
id: entity.id,
1470+
navTabs: this.getNavTabs(entity),
1471+
context: 'entity',
1472+
entity: entity,
1473+
depth: 1,
1474+
pageType: COMPODOC_DEFAULTS.PAGE_TYPES.INTERNAL
1475+
};
1476+
if (entity.isDuplicate) {
1477+
page.name += '-' + entity.duplicateId;
1478+
}
1479+
Configuration.addPage(page);
1480+
i++;
1481+
loop();
1482+
} else {
1483+
resolve(true);
1484+
}
1485+
};
1486+
loop();
1487+
});
1488+
}
1489+
14421490
public prepareComponents(someComponents?) {
14431491
logger.info('Prepare components');
14441492
Configuration.mainData.components = someComponents
@@ -1747,7 +1795,7 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
17471795

17481796
return new Promise((resolve, reject) => {
17491797
/*
1750-
* loop with components, directives, controllers, classes, injectables, interfaces, pipes, guards, misc functions variables
1798+
* loop with components, directives, controllers, entities, classes, injectables, interfaces, pipes, guards, misc functions variables
17511799
*/
17521800
let files = [];
17531801
let totalProjectStatementDocumented = 0;
@@ -1764,9 +1812,9 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
17641812
}
17651813
return status;
17661814
};
1767-
let processComponentsAndDirectivesAndControllers = list => {
1815+
const processComponentsAndDirectivesAndControllersAndEntities = list => {
17681816
_.forEach(list, (el: any) => {
1769-
let element = (Object as any).assign({}, el);
1817+
const element = (Object as any).assign({}, el);
17701818
if (!element.propertiesClass) {
17711819
element.propertiesClass = [];
17721820
}
@@ -2046,9 +2094,18 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
20462094
});
20472095
};
20482096

2049-
processComponentsAndDirectivesAndControllers(Configuration.mainData.components);
2050-
processComponentsAndDirectivesAndControllers(Configuration.mainData.directives);
2051-
processComponentsAndDirectivesAndControllers(Configuration.mainData.controllers);
2097+
processComponentsAndDirectivesAndControllersAndEntities(
2098+
Configuration.mainData.components
2099+
);
2100+
processComponentsAndDirectivesAndControllersAndEntities(
2101+
Configuration.mainData.directives
2102+
);
2103+
processComponentsAndDirectivesAndControllersAndEntities(
2104+
Configuration.mainData.controllers
2105+
);
2106+
processComponentsAndDirectivesAndControllersAndEntities(
2107+
Configuration.mainData.entities
2108+
);
20522109

20532110
processClasses(Configuration.mainData.classes, 'class', 'classe');
20542111
processClasses(Configuration.mainData.injectables, 'injectable', 'injectable');

src/app/compiler/angular-dependencies.ts

+22
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { JsDocHelper } from './angular/deps/helpers/js-doc-helper';
3333
import { ModuleHelper } from './angular/deps/helpers/module-helper';
3434
import { SymbolHelper } from './angular/deps/helpers/symbol-helper';
3535
import { ModuleDepFactory } from './angular/deps/module-dep.factory';
36+
import { EntityDepFactory } from './angular/deps/entity-dep.factory';
3637

3738
import Configuration from '../configuration';
3839

@@ -72,6 +73,7 @@ export class AngularDependencies extends FrameworkDependencies {
7273
modulesForGraph: [],
7374
components: [],
7475
controllers: [],
76+
entities: [],
7577
injectables: [],
7678
interceptors: [],
7779
guards: [],
@@ -397,6 +399,22 @@ export class AngularDependencies extends FrameworkDependencies {
397399
if (typeof IO.ignore === 'undefined') {
398400
outputSymbols.controllers.push(controllerDep);
399401
}
402+
} else if (this.isEntity(visitedDecorator)) {
403+
const entityDep = new EntityDepFactory().create(
404+
file,
405+
srcFile,
406+
name,
407+
props,
408+
IO
409+
);
410+
deps = entityDep;
411+
if (deps.name === 'Comment') {
412+
console.log(deps.properties[0]);
413+
}
414+
415+
if (typeof IO.ignore === 'undefined') {
416+
outputSymbols.entities.push(entityDep);
417+
}
400418
} else if (this.isInjectable(visitedDecorator)) {
401419
const injectableDeps: IInjectableDep = {
402420
name,
@@ -1062,6 +1080,10 @@ export class AngularDependencies extends FrameworkDependencies {
10621080
return this.parseDecorator(metadata, 'Controller');
10631081
}
10641082

1083+
private isEntity(metadata) {
1084+
return this.parseDecorator(metadata, 'Entity');
1085+
}
1086+
10651087
private isComponent(metadata) {
10661088
return this.parseDecorator(metadata, 'Component');
10671089
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { IDep } from '../dependencies.interfaces';
2+
import { ts } from 'ts-morph';
3+
4+
const crypto = require('crypto');
5+
6+
export class EntityDepFactory {
7+
constructor() {}
8+
9+
public create(
10+
file: any,
11+
srcFile: ts.SourceFile,
12+
name: string,
13+
properties: ReadonlyArray<ts.ObjectLiteralElementLike>,
14+
IO: any
15+
): IEntityDep {
16+
const sourceCode = srcFile.getText();
17+
const hash = crypto.createHash('sha512').update(sourceCode).digest('hex');
18+
const infos: IEntityDep = {
19+
name,
20+
id: 'controller-' + name + '-' + hash,
21+
file: file,
22+
type: 'entity',
23+
description: IO.description,
24+
rawdescription: IO.rawdescription,
25+
sourceCode: srcFile.text,
26+
deprecated: IO.deprecated,
27+
deprecationMessage: IO.deprecationMessage,
28+
properties: IO.properties
29+
};
30+
return infos;
31+
}
32+
}
33+
34+
export interface IEntityDep extends IDep {
35+
file: any;
36+
sourceCode: string;
37+
description: string;
38+
rawdescription: string;
39+
deprecated: boolean;
40+
deprecationMessage: string;
41+
properties: Array<any>;
42+
}

src/app/compiler/angular/deps/helpers/class-helper.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { StringifyObjectLiteralExpression } from '../../../../../utils/object-li
1212

1313
import DependenciesEngine from '../../../../engines/dependencies.engine';
1414
import Configuration from '../../../../configuration';
15+
import { StringifyArrowFunction } from '../../../../../utils/arrow-function.util';
1516

1617
const crypto = require('crypto');
1718
const { marked } = require('marked');
@@ -152,7 +153,7 @@ export class ClassHelper {
152153

153154
stringifyArgs = args
154155
.map(arg => {
155-
let _result = DependenciesEngine.find(arg.type);
156+
const _result = DependenciesEngine.find(arg.type);
156157
if (_result) {
157158
if (_result.source === 'internal') {
158159
let path = _result.data.type;
@@ -198,10 +199,17 @@ export class ClassHelper {
198199
}
199200
result += ']';
200201
return result;
202+
} else if (
203+
arg.kind &&
204+
arg.kind === SyntaxKind.ArrowFunction &&
205+
arg.parameters &&
206+
arg.parameters.length > 0
207+
) {
208+
return StringifyArrowFunction(arg);
201209
} else if (arg.kind && arg.kind === SyntaxKind.ObjectLiteralExpression) {
202210
return StringifyObjectLiteralExpression(arg);
203211
} else if (BasicTypeUtil.isKnownType(arg.type)) {
204-
let path = BasicTypeUtil.getTypeUrl(arg.type);
212+
const path = BasicTypeUtil.getTypeUrl(arg.type);
205213
return `${arg.name}${this.getOptionalString(
206214
arg
207215
)}: <a href="${path}" target="_blank">${arg.type}</a>`;
@@ -543,11 +551,13 @@ export class ClassHelper {
543551

544552
// RETURN TOO EARLY FOR MANY DECORATORS !!!!
545553
// iterating through the decorators array we have to keep the flags `true` values from the previous loop iteration
546-
isDirective = isDirective || this.isDirectiveDecorator(classDeclaration.decorators[a]);
554+
isDirective =
555+
isDirective || this.isDirectiveDecorator(classDeclaration.decorators[a]);
547556
isService = isService || this.isServiceDecorator(classDeclaration.decorators[a]);
548557
isPipe = isPipe || this.isPipeDecorator(classDeclaration.decorators[a]);
549558
isModule = isModule || this.isModuleDecorator(classDeclaration.decorators[a]);
550-
isController = isController || this.isControllerDecorator(classDeclaration.decorators[a]);
559+
isController =
560+
isController || this.isControllerDecorator(classDeclaration.decorators[a]);
551561
}
552562
if (isDirective) {
553563
return {

src/app/configuration.ts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export class Configuration implements ConfigurationInterface {
3838
interfaces: [],
3939
components: [],
4040
controllers: [],
41+
entities: [],
4142
directives: [],
4243
injectables: [],
4344
interceptors: [],

0 commit comments

Comments
 (0)