-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenComponent.sh
executable file
·118 lines (97 loc) · 4.09 KB
/
genComponent.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
if [ $# -lt 1 ]
then
echo "Missing parameter"
exit
fi
echo "Creating component $1"
cName=$1
cNameLC=$(tr '[A-Z]' '[a-z]' <<< $cName)
cd src
mkdir $cName
cd $cName
mkdir -p adapters application application/exceptions domain dto exposition/controller exposition/filters persistence
touch "adapters/$cNameLC.adapter.ts" "application/$cNameLC.service.ts"
touch "domain/$cNameLC.props.ts" "domain/$cNameLC.repository.ts" "domain/$cNameLC.response.ts" "domain/$cNameLC.ts"
touch "dto/add-$cNameLC.dto.ts"
touch "exposition/controller/$cNameLC.controller.ts" "exposition/filters/$cNameLC.exception.filter.ts"
touch "persistence/$cNameLC.repository.in-memory.ts"
touch "$cNameLC.module.ts"
echo "export interface ${cName}Props {}" >> "domain/$cNameLC.props.ts"
echo "import { ${cName}Props } from './${cNameLC}.props';
export class ${cName} implements ${cName}Props {}" >> "domain/$cNameLC.ts"
echo "import { ${cName}Props } from './${cNameLC}.props';
export class ${cName}Response implements ${cName}Props {}" >> "domain/$cNameLC.response.ts"
echo "import { ${cName} } from './${cNameLC}';
export interface ${cName}Repository {}" >> "domain/$cNameLC.repository.ts"
echo "import { ${cName} } from '../domain/${cNameLC}';
import { ${cName}Response } from '../domain/${cNameLC}.response';
export class ${cName}Adapter {
public static to${cName}Response(dto: ${cName}): ${cName}Response {
throw new Error('Not implemented');
}
}" >> "adapters/$cNameLC.adapter.ts"
echo "import { Injectable } from '@nestjs/common';
import { ${cName}RepositoryInMemory } from '../persistence/${cNameLC}.repository.in-memory';
import { ${cName} } from '../domain/${cNameLC}';
@Injectable()
export class ${cName}Service {
constructor(private ${cNameLC}Repository: ${cName}RepositoryInMemory) {}
}" >> "application/$cNameLC.service.ts"
echo "import { ${cName}Service } from '../../application/${cNameLC}.service';
import { Request } from 'express';
import { ${cName}Response } from '../../domain/${cNameLC}.response';
import { ${cName}Adapter } from '../../adapters/${cNameLC}.adapter';
import { HttpUtils } from '../../../shared/http/http.utils';
import { ${cName}ExceptionFilter } from '../filters/${cNameLC}.exception.filter';
import { Controller, Get, Param, Query, Req, UseFilters } from '@nestjs/common';
@Controller('${cNameLC}')
@UseFilters(new ${cName}ExceptionFilter())
export class ${cName}Controller {
constructor(private ${cNameLC}Service: ${cName}Service) {}
}" >> "exposition/controller/$cNameLC.controller.ts"
echo "import {
ArgumentsHost,
Catch,
ExceptionFilter,
HttpException,
HttpStatus,
} from '@nestjs/common';
import { Request, Response } from 'express';
@Catch()
export class ${cName}ExceptionFilter implements ExceptionFilter {
catch(exception: Error, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const body = {
statusCode:
exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR,
timestamp: new Date().toISOString(),
message: exception.message,
path: request.url,
};
switch (exception.name) {
default:
body.statusCode = 500;
break;
}
response.status(body.statusCode).json(body);
}
}" >> "exposition/filters/$cNameLC.exception.filter.ts"
echo "import { ${cName} } from '../domain/${cNameLC}';
import { ${cName}Repository } from '../domain/${cNameLC}.repository';
export class ${cName}RepositoryInMemory implements ${cName}Repository {
private ${cNameLC}s: ${cName}[] = [];
}" >> "persistence/$cNameLC.repository.in-memory.ts"
echo "import { Module } from '@nestjs/common';
import { ${cName}RepositoryInMemory } from './persistence/${cNameLC}.repository.in-memory';
import { ${cName}Service } from './application/${cNameLC}.service';
import { ${cName}Controller } from './exposition/controller/${cNameLC}.controller';
@Module({
controllers: [${cName}Controller],
providers: [${cName}Service, ${cName}RepositoryInMemory]
})
export class ${cName}Module {}" >> "$cNameLC.module.ts"
echo "Component created"