@nestia/sdk 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -58
- package/assets/config/nestia.config.ts +70 -70
- package/lib/executable/internal/NestiaSdkConfig.js.map +1 -1
- package/lib/executable/sdk.js +16 -16
- package/lib/generates/FunctionGenerator.js.map +1 -1
- package/lib/generates/SwaggerGenerator.js +3 -2
- package/lib/generates/SwaggerGenerator.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +4 -0
- package/lib/index.js.map +1 -1
- package/package.json +2 -1
- package/src/INestiaConfig.ts +147 -147
- package/src/NestiaSdkApplication.ts +183 -183
- package/src/analyses/ControllerAnalyzer.ts +223 -223
- package/src/analyses/GenericAnalyzer.ts +53 -53
- package/src/analyses/ImportAnalyzer.ts +143 -143
- package/src/analyses/PathAnalyzer.ts +58 -58
- package/src/analyses/ReflectAnalyzer.ts +287 -287
- package/src/analyses/SourceFinder.ts +59 -59
- package/src/executable/internal/CommandParser.ts +15 -15
- package/src/executable/internal/NestiaConfigCompilerOptions.ts +18 -18
- package/src/executable/internal/NestiaSdkCommand.ts +174 -174
- package/src/executable/internal/NestiaSdkConfig.ts +1 -0
- package/src/executable/internal/nestia.config.getter.ts +12 -12
- package/src/executable/sdk.ts +74 -74
- package/src/generates/FileGenerator.ts +156 -156
- package/src/generates/FunctionGenerator.ts +1 -0
- package/src/generates/SdkGenerator.ts +50 -50
- package/src/generates/SwaggerGenerator.ts +4 -1
- package/src/index.ts +1 -0
- package/src/module.ts +2 -2
- package/src/structures/IController.ts +27 -27
- package/src/structures/IRoute.ts +33 -33
- package/src/structures/ISwaggerDocument.ts +119 -119
- package/src/structures/ITypeTuple.ts +6 -6
- package/src/structures/MethodType.ts +11 -11
- package/src/structures/ParamCategory.ts +1 -1
- package/src/structures/TypeEntry.ts +22 -22
- package/src/utils/ArrayUtil.ts +26 -26
- package/src/utils/ImportDictionary.ts +56 -56
- package/src/utils/MapUtil.ts +14 -14
- package/src/utils/StripEnums.ts +10 -10
package/src/INestiaConfig.ts
CHANGED
|
@@ -1,147 +1,147 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { ISwaggerDocument } from "./structures/ISwaggerDocument";
|
|
4
|
-
import type { StripEnums } from "./utils/StripEnums";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Definition for the `nestia.config.ts` file.
|
|
8
|
-
*
|
|
9
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
10
|
-
*/
|
|
11
|
-
export interface INestiaConfig {
|
|
12
|
-
/**
|
|
13
|
-
* List of files or directories containing the NestJS controller classes.
|
|
14
|
-
*/
|
|
15
|
-
input: string | string[] | INestiaConfig.IInput;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Output directory that SDK would be placed in.
|
|
19
|
-
*
|
|
20
|
-
* If not configured, you can't build the SDK library.
|
|
21
|
-
*/
|
|
22
|
-
output?: string;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Compiler options for the TypeScript.
|
|
26
|
-
*
|
|
27
|
-
* If you've omitted this property or the assigned property cannot fully cover the
|
|
28
|
-
* `tsconfig.json`, the properties from the `tsconfig.json` would be assigned to here.
|
|
29
|
-
* Otherwise, this property has been configured and it's detailed values are different
|
|
30
|
-
* with the `tsconfig.json`, this property values would be used instead.
|
|
31
|
-
*
|
|
32
|
-
* ```typescript
|
|
33
|
-
* import ts from "typescript";
|
|
34
|
-
*
|
|
35
|
-
* const tsconfig: ts.TsConfig;
|
|
36
|
-
* const nestiaConfig: IConfiguration;
|
|
37
|
-
*
|
|
38
|
-
* const compilerOptions: ts.CompilerOptions = {
|
|
39
|
-
* ...tsconfig.compilerOptions,
|
|
40
|
-
* ...(nestiaConfig.compilerOptions || {})
|
|
41
|
-
* }
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
|
-
compilerOptions?: StripEnums<ts.CompilerOptions>;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Whether to assert parameter types or not.
|
|
48
|
-
*
|
|
49
|
-
* If you configure this property to be `true`, all of the function parameters
|
|
50
|
-
* would be checked through [typia](https://github.com/samchon/typia#runtime-validators).
|
|
51
|
-
* This option would make your SDK library slower, but would enahcne the type safety
|
|
52
|
-
* even in the runtime level.
|
|
53
|
-
*
|
|
54
|
-
* @default false
|
|
55
|
-
*/
|
|
56
|
-
assert?: boolean;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Whether to optimize JSON string conversion 10x faster or not.
|
|
60
|
-
*
|
|
61
|
-
* If you configure this property to be `true`, the SDK library would utilize the
|
|
62
|
-
* [typia](https://github.com/samchon/typia#enhanced-json)
|
|
63
|
-
* and the JSON string conversion speed really be 10x faster.
|
|
64
|
-
*
|
|
65
|
-
* @default false
|
|
66
|
-
*/
|
|
67
|
-
json?: boolean;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Whether to wrap DTO by primitive type.
|
|
71
|
-
*
|
|
72
|
-
* If you don't configure this property as `false`, all of DTOs in the
|
|
73
|
-
* SDK library would be automatically wrapped by {@link Primitive} type.
|
|
74
|
-
*
|
|
75
|
-
* For refenrece, if a DTO type be capsuled by the {@link Primitive} type,
|
|
76
|
-
* all of methods in the DTO type would be automatically erased. Also, if
|
|
77
|
-
* the DTO has a `toJSON()` method, the DTO type would be automatically
|
|
78
|
-
* converted to return type of the `toJSON()` method.
|
|
79
|
-
*
|
|
80
|
-
* @default true
|
|
81
|
-
*/
|
|
82
|
-
primitive?: boolean;
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Building `swagger.json` is also possible.
|
|
86
|
-
*
|
|
87
|
-
* If not specified, you can't build the `swagger.json`.
|
|
88
|
-
*/
|
|
89
|
-
swagger?: INestiaConfig.ISwaggerConfig;
|
|
90
|
-
}
|
|
91
|
-
export namespace INestiaConfig {
|
|
92
|
-
/**
|
|
93
|
-
* List of files or directories to include or exclude to specifying the NestJS
|
|
94
|
-
* controllers.
|
|
95
|
-
*/
|
|
96
|
-
export interface IInput {
|
|
97
|
-
/**
|
|
98
|
-
* List of files or directories containing the NestJS controller classes.
|
|
99
|
-
*/
|
|
100
|
-
include: string[];
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* List of files or directories to be excluded.
|
|
104
|
-
*/
|
|
105
|
-
exclude?: string[];
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Building `swagger.json` is also possible.
|
|
110
|
-
*/
|
|
111
|
-
export interface ISwaggerConfig {
|
|
112
|
-
/**
|
|
113
|
-
* Output path of the `swagger.json`.
|
|
114
|
-
*
|
|
115
|
-
* If you've configured only directory, the file name would be the `swagger.json`.
|
|
116
|
-
* Otherwise you've configured the full path with file name and extension, the
|
|
117
|
-
* `swagger.json` file would be renamed to it.
|
|
118
|
-
*/
|
|
119
|
-
output: string;
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Security schemes.
|
|
123
|
-
*/
|
|
124
|
-
security?: Record<string, ISwaggerConfig.ISecurityScheme>;
|
|
125
|
-
}
|
|
126
|
-
export namespace ISwaggerConfig {
|
|
127
|
-
export type ISecurityScheme =
|
|
128
|
-
| IApiKey
|
|
129
|
-
| Exclude<
|
|
130
|
-
ISwaggerDocument.ISecurityScheme,
|
|
131
|
-
ISwaggerDocument.ISecurityScheme.IApiKey
|
|
132
|
-
>;
|
|
133
|
-
export interface IApiKey {
|
|
134
|
-
type: "apiKey";
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* @default header
|
|
138
|
-
*/
|
|
139
|
-
in?: "header" | "query" | "cookie";
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* @default Authorization
|
|
143
|
-
*/
|
|
144
|
-
name?: string;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { ISwaggerDocument } from "./structures/ISwaggerDocument";
|
|
4
|
+
import type { StripEnums } from "./utils/StripEnums";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Definition for the `nestia.config.ts` file.
|
|
8
|
+
*
|
|
9
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
10
|
+
*/
|
|
11
|
+
export interface INestiaConfig {
|
|
12
|
+
/**
|
|
13
|
+
* List of files or directories containing the NestJS controller classes.
|
|
14
|
+
*/
|
|
15
|
+
input: string | string[] | INestiaConfig.IInput;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Output directory that SDK would be placed in.
|
|
19
|
+
*
|
|
20
|
+
* If not configured, you can't build the SDK library.
|
|
21
|
+
*/
|
|
22
|
+
output?: string;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Compiler options for the TypeScript.
|
|
26
|
+
*
|
|
27
|
+
* If you've omitted this property or the assigned property cannot fully cover the
|
|
28
|
+
* `tsconfig.json`, the properties from the `tsconfig.json` would be assigned to here.
|
|
29
|
+
* Otherwise, this property has been configured and it's detailed values are different
|
|
30
|
+
* with the `tsconfig.json`, this property values would be used instead.
|
|
31
|
+
*
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import ts from "typescript";
|
|
34
|
+
*
|
|
35
|
+
* const tsconfig: ts.TsConfig;
|
|
36
|
+
* const nestiaConfig: IConfiguration;
|
|
37
|
+
*
|
|
38
|
+
* const compilerOptions: ts.CompilerOptions = {
|
|
39
|
+
* ...tsconfig.compilerOptions,
|
|
40
|
+
* ...(nestiaConfig.compilerOptions || {})
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
compilerOptions?: StripEnums<ts.CompilerOptions>;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Whether to assert parameter types or not.
|
|
48
|
+
*
|
|
49
|
+
* If you configure this property to be `true`, all of the function parameters
|
|
50
|
+
* would be checked through [typia](https://github.com/samchon/typia#runtime-validators).
|
|
51
|
+
* This option would make your SDK library slower, but would enahcne the type safety
|
|
52
|
+
* even in the runtime level.
|
|
53
|
+
*
|
|
54
|
+
* @default false
|
|
55
|
+
*/
|
|
56
|
+
assert?: boolean;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Whether to optimize JSON string conversion 10x faster or not.
|
|
60
|
+
*
|
|
61
|
+
* If you configure this property to be `true`, the SDK library would utilize the
|
|
62
|
+
* [typia](https://github.com/samchon/typia#enhanced-json)
|
|
63
|
+
* and the JSON string conversion speed really be 10x faster.
|
|
64
|
+
*
|
|
65
|
+
* @default false
|
|
66
|
+
*/
|
|
67
|
+
json?: boolean;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Whether to wrap DTO by primitive type.
|
|
71
|
+
*
|
|
72
|
+
* If you don't configure this property as `false`, all of DTOs in the
|
|
73
|
+
* SDK library would be automatically wrapped by {@link Primitive} type.
|
|
74
|
+
*
|
|
75
|
+
* For refenrece, if a DTO type be capsuled by the {@link Primitive} type,
|
|
76
|
+
* all of methods in the DTO type would be automatically erased. Also, if
|
|
77
|
+
* the DTO has a `toJSON()` method, the DTO type would be automatically
|
|
78
|
+
* converted to return type of the `toJSON()` method.
|
|
79
|
+
*
|
|
80
|
+
* @default true
|
|
81
|
+
*/
|
|
82
|
+
primitive?: boolean;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Building `swagger.json` is also possible.
|
|
86
|
+
*
|
|
87
|
+
* If not specified, you can't build the `swagger.json`.
|
|
88
|
+
*/
|
|
89
|
+
swagger?: INestiaConfig.ISwaggerConfig;
|
|
90
|
+
}
|
|
91
|
+
export namespace INestiaConfig {
|
|
92
|
+
/**
|
|
93
|
+
* List of files or directories to include or exclude to specifying the NestJS
|
|
94
|
+
* controllers.
|
|
95
|
+
*/
|
|
96
|
+
export interface IInput {
|
|
97
|
+
/**
|
|
98
|
+
* List of files or directories containing the NestJS controller classes.
|
|
99
|
+
*/
|
|
100
|
+
include: string[];
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* List of files or directories to be excluded.
|
|
104
|
+
*/
|
|
105
|
+
exclude?: string[];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Building `swagger.json` is also possible.
|
|
110
|
+
*/
|
|
111
|
+
export interface ISwaggerConfig {
|
|
112
|
+
/**
|
|
113
|
+
* Output path of the `swagger.json`.
|
|
114
|
+
*
|
|
115
|
+
* If you've configured only directory, the file name would be the `swagger.json`.
|
|
116
|
+
* Otherwise you've configured the full path with file name and extension, the
|
|
117
|
+
* `swagger.json` file would be renamed to it.
|
|
118
|
+
*/
|
|
119
|
+
output: string;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Security schemes.
|
|
123
|
+
*/
|
|
124
|
+
security?: Record<string, ISwaggerConfig.ISecurityScheme>;
|
|
125
|
+
}
|
|
126
|
+
export namespace ISwaggerConfig {
|
|
127
|
+
export type ISecurityScheme =
|
|
128
|
+
| IApiKey
|
|
129
|
+
| Exclude<
|
|
130
|
+
ISwaggerDocument.ISecurityScheme,
|
|
131
|
+
ISwaggerDocument.ISecurityScheme.IApiKey
|
|
132
|
+
>;
|
|
133
|
+
export interface IApiKey {
|
|
134
|
+
type: "apiKey";
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* @default header
|
|
138
|
+
*/
|
|
139
|
+
in?: "header" | "query" | "cookie";
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @default Authorization
|
|
143
|
+
*/
|
|
144
|
+
name?: string;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -1,183 +1,183 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import * as runner from "ts-node";
|
|
4
|
-
import { Singleton } from "tstl/thread/Singleton";
|
|
5
|
-
import { Pair } from "tstl/utility/Pair";
|
|
6
|
-
import ts from "typescript";
|
|
7
|
-
|
|
8
|
-
import { INestiaConfig } from "./INestiaConfig";
|
|
9
|
-
import { ControllerAnalyzer } from "./analyses/ControllerAnalyzer";
|
|
10
|
-
import { ReflectAnalyzer } from "./analyses/ReflectAnalyzer";
|
|
11
|
-
import { SourceFinder } from "./analyses/SourceFinder";
|
|
12
|
-
import { NestiaConfigCompilerOptions } from "./executable/internal/NestiaConfigCompilerOptions";
|
|
13
|
-
import { SdkGenerator } from "./generates/SdkGenerator";
|
|
14
|
-
import { SwaggerGenerator } from "./generates/SwaggerGenerator";
|
|
15
|
-
import { IController } from "./structures/IController";
|
|
16
|
-
import { IRoute } from "./structures/IRoute";
|
|
17
|
-
import { ArrayUtil } from "./utils/ArrayUtil";
|
|
18
|
-
|
|
19
|
-
export class NestiaSdkApplication {
|
|
20
|
-
private readonly config_: INestiaConfig;
|
|
21
|
-
private readonly bundle_checker_: Singleton<
|
|
22
|
-
Promise<(str: string) => boolean>
|
|
23
|
-
>;
|
|
24
|
-
|
|
25
|
-
public constructor(config: INestiaConfig) {
|
|
26
|
-
this.config_ = config;
|
|
27
|
-
this.bundle_checker_ = new Singleton(async () => {
|
|
28
|
-
if (!this.config_.output) return () => false;
|
|
29
|
-
|
|
30
|
-
const bundles: string[] = await fs.promises.readdir(
|
|
31
|
-
SdkGenerator.BUNDLE_PATH,
|
|
32
|
-
);
|
|
33
|
-
const tuples: Pair<string, boolean>[] = await ArrayUtil.asyncMap(
|
|
34
|
-
bundles,
|
|
35
|
-
async (file) => {
|
|
36
|
-
const relative: string = path.join(
|
|
37
|
-
this.config_.output!,
|
|
38
|
-
file,
|
|
39
|
-
);
|
|
40
|
-
const location: string = path.join(
|
|
41
|
-
SdkGenerator.BUNDLE_PATH,
|
|
42
|
-
file,
|
|
43
|
-
);
|
|
44
|
-
const stats: fs.Stats = await fs.promises.stat(location);
|
|
45
|
-
|
|
46
|
-
return new Pair(relative, stats.isDirectory());
|
|
47
|
-
},
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
return (file: string): boolean => {
|
|
51
|
-
for (const it of tuples)
|
|
52
|
-
if (it.second === false && file === it.first) return true;
|
|
53
|
-
else if (it.second === true && file.indexOf(it.first) === 0)
|
|
54
|
-
return true;
|
|
55
|
-
return false;
|
|
56
|
-
};
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public async sdk(): Promise<void> {
|
|
61
|
-
if (!this.config_.output)
|
|
62
|
-
throw new Error(
|
|
63
|
-
"Error on NestiaApplication.sdk(): output path is not specified.",
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
const parent: string = path.resolve(this.config_.output + "/..");
|
|
67
|
-
const stats: fs.Stats = await fs.promises.lstat(parent);
|
|
68
|
-
if (stats.isDirectory() === false)
|
|
69
|
-
throw new Error(
|
|
70
|
-
"Error on NestiaApplication.sdk(): output directory does not exists.",
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
await this.generate((config) => config, SdkGenerator.generate);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
public async swagger(): Promise<void> {
|
|
77
|
-
if (!this.config_.swagger || !this.config_.swagger.output)
|
|
78
|
-
throw new Error(
|
|
79
|
-
`Error on NestiaApplication.swagger(): output path of the "swagger.json" is not specified.`,
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
const parsed: path.ParsedPath = path.parse(this.config_.swagger.output);
|
|
83
|
-
const directory: string = !!parsed.ext
|
|
84
|
-
? path.resolve(parsed.dir)
|
|
85
|
-
: this.config_.swagger.output;
|
|
86
|
-
const stats: fs.Stats = await fs.promises.lstat(directory);
|
|
87
|
-
if (stats.isDirectory() === false)
|
|
88
|
-
throw new Error(
|
|
89
|
-
"Error on NestiaApplication.swagger(): output directory does not exists.",
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
await this.generate(
|
|
93
|
-
(config) => config.swagger!,
|
|
94
|
-
SwaggerGenerator.generate,
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
private async generate<Config>(
|
|
99
|
-
config: (entire: INestiaConfig) => Config,
|
|
100
|
-
archiver: (
|
|
101
|
-
checker: ts.TypeChecker,
|
|
102
|
-
config: Config,
|
|
103
|
-
routes: IRoute[],
|
|
104
|
-
) => Promise<void>,
|
|
105
|
-
): Promise<void> {
|
|
106
|
-
// MOUNT TS-NODE
|
|
107
|
-
this.prepare();
|
|
108
|
-
|
|
109
|
-
// LOAD CONTROLLER FILES
|
|
110
|
-
const input: INestiaConfig.IInput =
|
|
111
|
-
this.config_.input instanceof Array
|
|
112
|
-
? { include: this.config_.input }
|
|
113
|
-
: typeof this.config_.input === "string"
|
|
114
|
-
? { include: [this.config_.input] }
|
|
115
|
-
: this.config_.input;
|
|
116
|
-
const fileList: string[] = await ArrayUtil.asyncFilter(
|
|
117
|
-
await SourceFinder.find(input),
|
|
118
|
-
(file) => this.is_not_excluded(file),
|
|
119
|
-
);
|
|
120
|
-
|
|
121
|
-
// ANALYZE REFLECTS
|
|
122
|
-
const unique: WeakSet<any> = new WeakSet();
|
|
123
|
-
const controllerList: IController[] = [];
|
|
124
|
-
|
|
125
|
-
for (const file of fileList)
|
|
126
|
-
controllerList.push(
|
|
127
|
-
...(await ReflectAnalyzer.analyze(unique, file)),
|
|
128
|
-
);
|
|
129
|
-
|
|
130
|
-
// ANALYZE TYPESCRIPT CODE
|
|
131
|
-
const program: ts.Program = ts.createProgram(
|
|
132
|
-
controllerList.map((c) => c.file),
|
|
133
|
-
this.config_.compilerOptions || { noEmit: true },
|
|
134
|
-
);
|
|
135
|
-
const checker: ts.TypeChecker = program.getTypeChecker();
|
|
136
|
-
|
|
137
|
-
const routeList: IRoute[] = [];
|
|
138
|
-
for (const controller of controllerList) {
|
|
139
|
-
const sourceFile: ts.SourceFile | undefined = program.getSourceFile(
|
|
140
|
-
controller.file,
|
|
141
|
-
);
|
|
142
|
-
if (sourceFile === undefined) continue;
|
|
143
|
-
|
|
144
|
-
routeList.push(
|
|
145
|
-
...ControllerAnalyzer.analyze(checker, sourceFile, controller),
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// DO GENERATE
|
|
150
|
-
await archiver(checker, config(this.config_), routeList);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
private prepare(): void {
|
|
154
|
-
// CONSTRUCT OPTIONS
|
|
155
|
-
if (!this.config_.compilerOptions)
|
|
156
|
-
this.config_.compilerOptions =
|
|
157
|
-
NestiaConfigCompilerOptions.DEFAULT_OPTIONS as any;
|
|
158
|
-
const absoluted: boolean = !!this.config_.compilerOptions?.baseUrl;
|
|
159
|
-
|
|
160
|
-
// MOUNT TS-NODE
|
|
161
|
-
runner.register({
|
|
162
|
-
emit: false,
|
|
163
|
-
compiler: "ttypescript",
|
|
164
|
-
compilerOptions: this.config_.compilerOptions,
|
|
165
|
-
require: absoluted ? ["tsconfig-paths/register"] : undefined,
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
private async is_not_excluded(file: string): Promise<boolean> {
|
|
170
|
-
if (this.config_.output)
|
|
171
|
-
return (
|
|
172
|
-
file.indexOf(path.join(this.config_.output, "functional")) ===
|
|
173
|
-
-1 && (await this.bundle_checker_.get())(file) === false
|
|
174
|
-
);
|
|
175
|
-
|
|
176
|
-
const content: string = await fs.promises.readFile(file, "utf8");
|
|
177
|
-
return (
|
|
178
|
-
content.indexOf(
|
|
179
|
-
" * @nestia Generated by Nestia - https://github.com/samchon/nestia",
|
|
180
|
-
) === -1
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import * as runner from "ts-node";
|
|
4
|
+
import { Singleton } from "tstl/thread/Singleton";
|
|
5
|
+
import { Pair } from "tstl/utility/Pair";
|
|
6
|
+
import ts from "typescript";
|
|
7
|
+
|
|
8
|
+
import { INestiaConfig } from "./INestiaConfig";
|
|
9
|
+
import { ControllerAnalyzer } from "./analyses/ControllerAnalyzer";
|
|
10
|
+
import { ReflectAnalyzer } from "./analyses/ReflectAnalyzer";
|
|
11
|
+
import { SourceFinder } from "./analyses/SourceFinder";
|
|
12
|
+
import { NestiaConfigCompilerOptions } from "./executable/internal/NestiaConfigCompilerOptions";
|
|
13
|
+
import { SdkGenerator } from "./generates/SdkGenerator";
|
|
14
|
+
import { SwaggerGenerator } from "./generates/SwaggerGenerator";
|
|
15
|
+
import { IController } from "./structures/IController";
|
|
16
|
+
import { IRoute } from "./structures/IRoute";
|
|
17
|
+
import { ArrayUtil } from "./utils/ArrayUtil";
|
|
18
|
+
|
|
19
|
+
export class NestiaSdkApplication {
|
|
20
|
+
private readonly config_: INestiaConfig;
|
|
21
|
+
private readonly bundle_checker_: Singleton<
|
|
22
|
+
Promise<(str: string) => boolean>
|
|
23
|
+
>;
|
|
24
|
+
|
|
25
|
+
public constructor(config: INestiaConfig) {
|
|
26
|
+
this.config_ = config;
|
|
27
|
+
this.bundle_checker_ = new Singleton(async () => {
|
|
28
|
+
if (!this.config_.output) return () => false;
|
|
29
|
+
|
|
30
|
+
const bundles: string[] = await fs.promises.readdir(
|
|
31
|
+
SdkGenerator.BUNDLE_PATH,
|
|
32
|
+
);
|
|
33
|
+
const tuples: Pair<string, boolean>[] = await ArrayUtil.asyncMap(
|
|
34
|
+
bundles,
|
|
35
|
+
async (file) => {
|
|
36
|
+
const relative: string = path.join(
|
|
37
|
+
this.config_.output!,
|
|
38
|
+
file,
|
|
39
|
+
);
|
|
40
|
+
const location: string = path.join(
|
|
41
|
+
SdkGenerator.BUNDLE_PATH,
|
|
42
|
+
file,
|
|
43
|
+
);
|
|
44
|
+
const stats: fs.Stats = await fs.promises.stat(location);
|
|
45
|
+
|
|
46
|
+
return new Pair(relative, stats.isDirectory());
|
|
47
|
+
},
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
return (file: string): boolean => {
|
|
51
|
+
for (const it of tuples)
|
|
52
|
+
if (it.second === false && file === it.first) return true;
|
|
53
|
+
else if (it.second === true && file.indexOf(it.first) === 0)
|
|
54
|
+
return true;
|
|
55
|
+
return false;
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public async sdk(): Promise<void> {
|
|
61
|
+
if (!this.config_.output)
|
|
62
|
+
throw new Error(
|
|
63
|
+
"Error on NestiaApplication.sdk(): output path is not specified.",
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const parent: string = path.resolve(this.config_.output + "/..");
|
|
67
|
+
const stats: fs.Stats = await fs.promises.lstat(parent);
|
|
68
|
+
if (stats.isDirectory() === false)
|
|
69
|
+
throw new Error(
|
|
70
|
+
"Error on NestiaApplication.sdk(): output directory does not exists.",
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
await this.generate((config) => config, SdkGenerator.generate);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public async swagger(): Promise<void> {
|
|
77
|
+
if (!this.config_.swagger || !this.config_.swagger.output)
|
|
78
|
+
throw new Error(
|
|
79
|
+
`Error on NestiaApplication.swagger(): output path of the "swagger.json" is not specified.`,
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const parsed: path.ParsedPath = path.parse(this.config_.swagger.output);
|
|
83
|
+
const directory: string = !!parsed.ext
|
|
84
|
+
? path.resolve(parsed.dir)
|
|
85
|
+
: this.config_.swagger.output;
|
|
86
|
+
const stats: fs.Stats = await fs.promises.lstat(directory);
|
|
87
|
+
if (stats.isDirectory() === false)
|
|
88
|
+
throw new Error(
|
|
89
|
+
"Error on NestiaApplication.swagger(): output directory does not exists.",
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
await this.generate(
|
|
93
|
+
(config) => config.swagger!,
|
|
94
|
+
SwaggerGenerator.generate,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private async generate<Config>(
|
|
99
|
+
config: (entire: INestiaConfig) => Config,
|
|
100
|
+
archiver: (
|
|
101
|
+
checker: ts.TypeChecker,
|
|
102
|
+
config: Config,
|
|
103
|
+
routes: IRoute[],
|
|
104
|
+
) => Promise<void>,
|
|
105
|
+
): Promise<void> {
|
|
106
|
+
// MOUNT TS-NODE
|
|
107
|
+
this.prepare();
|
|
108
|
+
|
|
109
|
+
// LOAD CONTROLLER FILES
|
|
110
|
+
const input: INestiaConfig.IInput =
|
|
111
|
+
this.config_.input instanceof Array
|
|
112
|
+
? { include: this.config_.input }
|
|
113
|
+
: typeof this.config_.input === "string"
|
|
114
|
+
? { include: [this.config_.input] }
|
|
115
|
+
: this.config_.input;
|
|
116
|
+
const fileList: string[] = await ArrayUtil.asyncFilter(
|
|
117
|
+
await SourceFinder.find(input),
|
|
118
|
+
(file) => this.is_not_excluded(file),
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
// ANALYZE REFLECTS
|
|
122
|
+
const unique: WeakSet<any> = new WeakSet();
|
|
123
|
+
const controllerList: IController[] = [];
|
|
124
|
+
|
|
125
|
+
for (const file of fileList)
|
|
126
|
+
controllerList.push(
|
|
127
|
+
...(await ReflectAnalyzer.analyze(unique, file)),
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
// ANALYZE TYPESCRIPT CODE
|
|
131
|
+
const program: ts.Program = ts.createProgram(
|
|
132
|
+
controllerList.map((c) => c.file),
|
|
133
|
+
this.config_.compilerOptions || { noEmit: true },
|
|
134
|
+
);
|
|
135
|
+
const checker: ts.TypeChecker = program.getTypeChecker();
|
|
136
|
+
|
|
137
|
+
const routeList: IRoute[] = [];
|
|
138
|
+
for (const controller of controllerList) {
|
|
139
|
+
const sourceFile: ts.SourceFile | undefined = program.getSourceFile(
|
|
140
|
+
controller.file,
|
|
141
|
+
);
|
|
142
|
+
if (sourceFile === undefined) continue;
|
|
143
|
+
|
|
144
|
+
routeList.push(
|
|
145
|
+
...ControllerAnalyzer.analyze(checker, sourceFile, controller),
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// DO GENERATE
|
|
150
|
+
await archiver(checker, config(this.config_), routeList);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private prepare(): void {
|
|
154
|
+
// CONSTRUCT OPTIONS
|
|
155
|
+
if (!this.config_.compilerOptions)
|
|
156
|
+
this.config_.compilerOptions =
|
|
157
|
+
NestiaConfigCompilerOptions.DEFAULT_OPTIONS as any;
|
|
158
|
+
const absoluted: boolean = !!this.config_.compilerOptions?.baseUrl;
|
|
159
|
+
|
|
160
|
+
// MOUNT TS-NODE
|
|
161
|
+
runner.register({
|
|
162
|
+
emit: false,
|
|
163
|
+
compiler: "ttypescript",
|
|
164
|
+
compilerOptions: this.config_.compilerOptions,
|
|
165
|
+
require: absoluted ? ["tsconfig-paths/register"] : undefined,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private async is_not_excluded(file: string): Promise<boolean> {
|
|
170
|
+
if (this.config_.output)
|
|
171
|
+
return (
|
|
172
|
+
file.indexOf(path.join(this.config_.output, "functional")) ===
|
|
173
|
+
-1 && (await this.bundle_checker_.get())(file) === false
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
const content: string = await fs.promises.readFile(file, "utf8");
|
|
177
|
+
return (
|
|
178
|
+
content.indexOf(
|
|
179
|
+
" * @nestia Generated by Nestia - https://github.com/samchon/nestia",
|
|
180
|
+
) === -1
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
}
|