Skip to content

Commit

Permalink
Report errors during SDK generation.
Browse files Browse the repository at this point in the history
When an error of NestJS project being detected in the SDK generation process, `@nestia/sdk` had thrown an exception. Therefore, if there're many errors in the NestJS project, user may lost many times due to only one error being reported.

To fix the problem, I've updated `@nestia/sdk` to gather every errors of target NestJS project, and print all of them at once.
  • Loading branch information
samchon committed Oct 19, 2023
1 parent d6aa14d commit df0b006
Show file tree
Hide file tree
Showing 49 changed files with 1,622 additions and 573 deletions.
2 changes: 1 addition & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"homepage": "https://nestia.io",
"dependencies": {
"@nestia/core": "^2.2.1-dev.20231012",
"typia": "^5.2.2"
"typia": "^5.2.3"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
Expand Down
10 changes: 5 additions & 5 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/core",
"version": "2.3.0-dev.20231019",
"version": "2.3.0-dev.20231020",
"description": "Super-fast validation decorators of NestJS",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -34,7 +34,7 @@
},
"homepage": "https://nestia.io",
"dependencies": {
"@nestia/fetcher": "^2.3.0-dev.20231019",
"@nestia/fetcher": "^2.3.0-dev.20231020",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"@nestjs/platform-express": ">=7.0.1",
Expand All @@ -44,10 +44,10 @@
"raw-body": ">=2.0.0",
"reflect-metadata": ">=0.1.12",
"rxjs": ">=6.0.0",
"typia": "^5.2.2"
"typia": "^5.2.3"
},
"peerDependencies": {
"@nestia/fetcher": ">=2.3.0-dev.20231019",
"@nestia/fetcher": ">=2.3.0-dev.20231020",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"@nestjs/platform-express": ">=7.0.1",
Expand All @@ -56,7 +56,7 @@
"reflect-metadata": ">=0.1.12",
"rxjs": ">=6.0.0",
"typescript": ">=4.8.0",
"typia": ">=5.2.2 <6.0.0"
"typia": ">=5.2.3 <6.0.0"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"ts-node": "^10.9.1",
"ts-patch": "^3.0.2",
"typescript": "^5.2.2",
"typia": "^5.2.2"
"typia": "^5.2.3"
},
"dependencies": {
"chalk": "^4.1.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/fetcher/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/fetcher",
"version": "2.3.0-dev.20231019",
"version": "2.3.0-dev.20231020",
"description": "Fetcher library of Nestia SDK",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/migrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"typescript-transform-paths": "^3.4.6"
},
"dependencies": {
"typia": "^5.2.2"
"typia": "^5.2.3"
},
"files": [
"lib",
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/assets/config/nestia.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ export const NESTIA_CONFIG: INestiaConfig = {
/**
* Accessor of controller classes.
*
* You can specify it within two ways
* You can specify it within two ways.
*
* - Asynchronous function returning `INestApplication` instance
* - Specify the path or directory of controller class files
*/
// input: "src/controllers",
// input: "src/**/*.controller.ts",
input: async () => {
// change this to your own module
@Module({
Expand All @@ -25,8 +27,6 @@ export const NESTIA_CONFIG: INestiaConfig = {
// })
return app;
},
// input: "src/controllers",
// input: "src/**/*.controller.ts",

/**
* Building `swagger.json` is also possible.
Expand Down
10 changes: 5 additions & 5 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/sdk",
"version": "2.3.0-dev.20231019",
"version": "2.3.0-dev.20231020",
"description": "Nestia SDK and Swagger generator",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -35,7 +35,7 @@
},
"homepage": "https://nestia.io",
"dependencies": {
"@nestia/fetcher": "^2.3.0-dev.20231019",
"@nestia/fetcher": "^2.3.0-dev.20231020",
"cli": "^1.0.1",
"get-function-location": "^2.0.0",
"glob": "^7.2.0",
Expand All @@ -45,16 +45,16 @@
"tsconfck": "^2.0.1",
"tsconfig-paths": "^4.1.1",
"tstl": "^2.5.13",
"typia": "^5.2.2"
"typia": "^5.2.3"
},
"peerDependencies": {
"@nestia/fetcher": ">=2.3.0-dev.20231019",
"@nestia/fetcher": ">=2.3.0-dev.20231020",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"reflect-metadata": ">=0.1.12",
"ts-node": ">=10.6.0",
"typescript": ">=4.8.0",
"typia": ">=5.2.2 <6.0.0"
"typia": ">=5.2.3 <6.0.0"
},
"devDependencies": {
"@nestjs/common": ">= 7.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/INestiaConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface INestiaConfig {
/**
* Accessor of controller classes.
*
* You can specify target controller classes within two ways
* You can specify target controller classes within two ways.
*
* - Asynchronous function returning `INestApplication` instance
* - Specify the path or directory of controller class files
Expand Down
54 changes: 45 additions & 9 deletions packages/sdk/src/NestiaSdkApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { E2eGenerator } from "./generates/E2eGenerator";
import { SdkGenerator } from "./generates/SdkGenerator";
import { SwaggerGenerator } from "./generates/SwaggerGenerator";
import { IController } from "./structures/IController";
import { IErrorReport } from "./structures/IErrorReport";
import { INestiaProject } from "./structures/INestiaProject";
import { IRoute } from "./structures/IRoute";
import { MapUtil } from "./utils/MapUtil";

export class NestiaSdkApplication {
public constructor(
Expand Down Expand Up @@ -104,11 +107,17 @@ export class NestiaSdkApplication {
// ANALYZE REFLECTS
const unique: WeakSet<any> = new WeakSet();
const controllers: IController[] = [];
const project: INestiaProject = {
config: this.config,
input: await ConfigAnalyzer.input(this.config),
checker: null!,
errors: [],
};

console.log("Analyzing reflections");
for (const include of (await ConfigAnalyzer.input(this.config)).include)
controllers.push(
...(await ReflectAnalyzer.analyze(
...(await ReflectAnalyzer.analyze(project)(
unique,
include.file,
include.paths,
Expand Down Expand Up @@ -146,7 +155,7 @@ export class NestiaSdkApplication {
controllers.map((c) => c.file),
this.compilerOptions,
);
const checker: ts.TypeChecker = program.getTypeChecker();
project.checker = program.getTypeChecker();

const routeList: IRoute[] = [];
for (const c of controllers) {
Expand All @@ -155,15 +164,16 @@ export class NestiaSdkApplication {
);
if (file === undefined) continue;
routeList.push(
...(await ControllerAnalyzer.analyze(
this.config,
checker,
file,
c,
)),
...(await ControllerAnalyzer.analyze(project)(file, c)),
);
}

// REPORT ERRORS
if (project.errors.length) {
report_errors(project.errors);
process.exit(-1);
}

// FIND IMPLICIT TYPES
const implicit: IRoute[] = routeList.filter(is_implicit_return_typed);
if (implicit.length > 0)
Expand All @@ -181,7 +191,7 @@ export class NestiaSdkApplication {

// DO GENERATE
AccessorAnalyzer.analyze(routeList);
await archiver(checker)(config(this.config))(routeList);
await archiver(project.checker)(config(this.config))(routeList);
}
}

Expand All @@ -207,4 +217,30 @@ const is_implicit_return_typed = (route: IRoute): boolean => {
return true;
};

const report_errors = (errors: IErrorReport[]): void => {
// key: file
// key: controller
// key: function
// value: message
const map: Map<string, Map<string, Map<string, Set<string>>>> = new Map();
for (const e of errors) {
const file = MapUtil.take(map, e.file, () => new Map());
const controller = MapUtil.take(file, e.controller, () => new Map());
const func = MapUtil.take(controller, e.function, () => new Set());
func.add(e.message);
}

console.log("");
title("Nestia Error Report");
for (const [file, cMap] of map) {
for (const [controller, fMap] of cMap)
for (const [func, messages] of fMap) {
const location: string = path.relative(process.cwd(), file);
console.log(`${location} - error on ${controller}.${func}()`);
for (const msg of messages) console.log(` - ${msg}`);
console.log("");
}
}
};

const VARIABLE = /[a-zA-Z_$0-9]/;
Loading

0 comments on commit df0b006

Please sign in to comment.