@nestia/sdk 2.5.0-dev.20240130 → 2.5.0-dev.20240130-3
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/lib/INestiaConfig.d.ts +0 -6
- package/lib/executable/internal/NestiaConfigLoader.js +2 -6
- package/lib/executable/internal/NestiaConfigLoader.js.map +1 -1
- package/lib/generates/SwaggerGenerator.js +8 -11
- package/lib/generates/SwaggerGenerator.js.map +1 -1
- package/lib/generates/internal/SdkTypeProgrammer.js +3 -1
- package/lib/generates/internal/SdkTypeProgrammer.js.map +1 -1
- package/lib/structures/ISwagger.d.ts +1 -1
- package/package.json +3 -3
- package/src/INestiaConfig.ts +248 -255
- package/src/NestiaSdkApplication.ts +253 -253
- package/src/generates/E2eGenerator.ts +66 -66
- package/src/generates/SdkGenerator.ts +96 -96
- package/src/generates/SwaggerGenerator.ts +376 -378
- package/src/generates/internal/E2eFileProgrammer.ts +191 -191
- package/src/generates/internal/SdkAliasCollection.ts +145 -145
- package/src/generates/internal/SdkFileProgrammer.ts +135 -135
- package/src/generates/internal/SdkFunctionProgrammer.ts +219 -219
- package/src/generates/internal/SdkNamespaceProgrammer.ts +507 -507
- package/src/generates/internal/SdkRouteProgrammer.ts +86 -86
- package/src/generates/internal/SdkSimulationProgrammer.ts +357 -357
- package/src/generates/internal/SdkTypeProgrammer.ts +5 -1
- package/src/structures/ISwagger.ts +91 -91
- package/src/utils/FormatUtil.ts +31 -31
- package/src/utils/ImportDictionary.ts +197 -197
|
@@ -174,7 +174,11 @@ export namespace SdkTypeProgrammer {
|
|
|
174
174
|
decode(config)(importer)(elem.rest),
|
|
175
175
|
),
|
|
176
176
|
)
|
|
177
|
-
:
|
|
177
|
+
: elem.optional
|
|
178
|
+
? ts.factory.createOptionalTypeNode(
|
|
179
|
+
decode(config)(importer)(elem),
|
|
180
|
+
)
|
|
181
|
+
: decode(config)(importer)(elem),
|
|
178
182
|
),
|
|
179
183
|
);
|
|
180
184
|
|
|
@@ -1,91 +1,91 @@
|
|
|
1
|
-
import { ISwaggerComponents } from "./ISwaggerComponents";
|
|
2
|
-
import { ISwaggerInfo } from "./ISwaggerInfo";
|
|
3
|
-
import { ISwaggerRoute } from "./ISwaggerRoute";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Swagger Document.
|
|
7
|
-
*
|
|
8
|
-
* `ISwagger` is a data structure representing content of `swagger.json` file
|
|
9
|
-
* generated by Nestia. Note that, this is not an universal structure, but a dedicated
|
|
10
|
-
* structure only for Nestia.
|
|
11
|
-
*
|
|
12
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
13
|
-
*/
|
|
14
|
-
export interface ISwagger {
|
|
15
|
-
/**
|
|
16
|
-
* The version of the OpenAPI document.
|
|
17
|
-
*
|
|
18
|
-
* Nestia always generate OpenAPI 3.0.x document.
|
|
19
|
-
*/
|
|
20
|
-
openapi: `3
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* List of servers that provide the API.
|
|
24
|
-
*/
|
|
25
|
-
servers: ISwagger.IServer[];
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Information about the API.
|
|
29
|
-
*/
|
|
30
|
-
info: ISwaggerInfo;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* The available paths and operations for the API.
|
|
34
|
-
*
|
|
35
|
-
* The 1st key is the path, and the 2nd key is the HTTP method.
|
|
36
|
-
*/
|
|
37
|
-
paths: Record<string, Record<string, ISwaggerRoute>>;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* An object to hold reusable data structures.
|
|
41
|
-
*
|
|
42
|
-
* It stores both DTO schemas and security schemes.
|
|
43
|
-
*
|
|
44
|
-
* For reference, `nestia` defines every object and alias types as reusable DTO
|
|
45
|
-
* schemas. The alias type means that defined by `type` keyword in TypeScript.
|
|
46
|
-
*/
|
|
47
|
-
components: ISwaggerComponents;
|
|
48
|
-
|
|
49
|
-
// /**
|
|
50
|
-
// * A declaration of which security mechanisms can be used across the API.
|
|
51
|
-
// *
|
|
52
|
-
// * When this property be configured, it would be overwritten in every API routes.
|
|
53
|
-
// *
|
|
54
|
-
// * For reference, key means the name of security scheme and value means the `scopes`.
|
|
55
|
-
// * The `scopes` can be used only when target security scheme is `oauth2` type,
|
|
56
|
-
// * especially for {@link ISwaggerSecurityScheme.IOAuth2.IFlow.scopes} property.
|
|
57
|
-
// */
|
|
58
|
-
// security?: Record<string, string[]>[];
|
|
59
|
-
}
|
|
60
|
-
export namespace ISwagger {
|
|
61
|
-
/**
|
|
62
|
-
* Remote server definition.
|
|
63
|
-
*/
|
|
64
|
-
export interface IServer {
|
|
65
|
-
/**
|
|
66
|
-
* A URL to the target host.
|
|
67
|
-
*
|
|
68
|
-
* @format uri
|
|
69
|
-
*/
|
|
70
|
-
url: string;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* An optional string describing the target server.
|
|
74
|
-
*/
|
|
75
|
-
description?: string;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export interface IExternalDocs {
|
|
79
|
-
/**
|
|
80
|
-
* The URL for target documentation.
|
|
81
|
-
*
|
|
82
|
-
* @format uri
|
|
83
|
-
*/
|
|
84
|
-
url: string;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* A short description of the target documentation.
|
|
88
|
-
*/
|
|
89
|
-
description?: string;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
1
|
+
import { ISwaggerComponents } from "./ISwaggerComponents";
|
|
2
|
+
import { ISwaggerInfo } from "./ISwaggerInfo";
|
|
3
|
+
import { ISwaggerRoute } from "./ISwaggerRoute";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Swagger Document.
|
|
7
|
+
*
|
|
8
|
+
* `ISwagger` is a data structure representing content of `swagger.json` file
|
|
9
|
+
* generated by Nestia. Note that, this is not an universal structure, but a dedicated
|
|
10
|
+
* structure only for Nestia.
|
|
11
|
+
*
|
|
12
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
13
|
+
*/
|
|
14
|
+
export interface ISwagger {
|
|
15
|
+
/**
|
|
16
|
+
* The version of the OpenAPI document.
|
|
17
|
+
*
|
|
18
|
+
* Nestia always generate OpenAPI 3.0.x document.
|
|
19
|
+
*/
|
|
20
|
+
openapi: `3.0.${number}`;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* List of servers that provide the API.
|
|
24
|
+
*/
|
|
25
|
+
servers: ISwagger.IServer[];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Information about the API.
|
|
29
|
+
*/
|
|
30
|
+
info: ISwaggerInfo;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The available paths and operations for the API.
|
|
34
|
+
*
|
|
35
|
+
* The 1st key is the path, and the 2nd key is the HTTP method.
|
|
36
|
+
*/
|
|
37
|
+
paths: Record<string, Record<string, ISwaggerRoute>>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* An object to hold reusable data structures.
|
|
41
|
+
*
|
|
42
|
+
* It stores both DTO schemas and security schemes.
|
|
43
|
+
*
|
|
44
|
+
* For reference, `nestia` defines every object and alias types as reusable DTO
|
|
45
|
+
* schemas. The alias type means that defined by `type` keyword in TypeScript.
|
|
46
|
+
*/
|
|
47
|
+
components: ISwaggerComponents;
|
|
48
|
+
|
|
49
|
+
// /**
|
|
50
|
+
// * A declaration of which security mechanisms can be used across the API.
|
|
51
|
+
// *
|
|
52
|
+
// * When this property be configured, it would be overwritten in every API routes.
|
|
53
|
+
// *
|
|
54
|
+
// * For reference, key means the name of security scheme and value means the `scopes`.
|
|
55
|
+
// * The `scopes` can be used only when target security scheme is `oauth2` type,
|
|
56
|
+
// * especially for {@link ISwaggerSecurityScheme.IOAuth2.IFlow.scopes} property.
|
|
57
|
+
// */
|
|
58
|
+
// security?: Record<string, string[]>[];
|
|
59
|
+
}
|
|
60
|
+
export namespace ISwagger {
|
|
61
|
+
/**
|
|
62
|
+
* Remote server definition.
|
|
63
|
+
*/
|
|
64
|
+
export interface IServer {
|
|
65
|
+
/**
|
|
66
|
+
* A URL to the target host.
|
|
67
|
+
*
|
|
68
|
+
* @format uri
|
|
69
|
+
*/
|
|
70
|
+
url: string;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* An optional string describing the target server.
|
|
74
|
+
*/
|
|
75
|
+
description?: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface IExternalDocs {
|
|
79
|
+
/**
|
|
80
|
+
* The URL for target documentation.
|
|
81
|
+
*
|
|
82
|
+
* @format uri
|
|
83
|
+
*/
|
|
84
|
+
url: string;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A short description of the target documentation.
|
|
88
|
+
*/
|
|
89
|
+
description?: string;
|
|
90
|
+
}
|
|
91
|
+
}
|
package/src/utils/FormatUtil.ts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import { format } from "prettier";
|
|
2
|
-
import ts from "typescript";
|
|
3
|
-
|
|
4
|
-
export namespace FormatUtil {
|
|
5
|
-
export const description = <Node extends ts.Node>(
|
|
6
|
-
node: Node,
|
|
7
|
-
comment: string,
|
|
8
|
-
): Node => {
|
|
9
|
-
if (comment.length === 0) return node;
|
|
10
|
-
ts.addSyntheticLeadingComment(
|
|
11
|
-
node,
|
|
12
|
-
ts.SyntaxKind.MultiLineCommentTrivia,
|
|
13
|
-
["*", ...comment.split("\n").map((str) => ` * ${str}`), ""].join("\n"),
|
|
14
|
-
true,
|
|
15
|
-
);
|
|
16
|
-
return node;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export const enter = () =>
|
|
20
|
-
ts.factory.createExpressionStatement(ts.factory.createIdentifier("\n"));
|
|
21
|
-
|
|
22
|
-
export const beautify = async (script: string): Promise<string> => {
|
|
23
|
-
try {
|
|
24
|
-
return await format(script, {
|
|
25
|
-
parser: "typescript",
|
|
26
|
-
});
|
|
27
|
-
} catch {
|
|
28
|
-
return script;
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
}
|
|
1
|
+
import { format } from "prettier";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
|
|
4
|
+
export namespace FormatUtil {
|
|
5
|
+
export const description = <Node extends ts.Node>(
|
|
6
|
+
node: Node,
|
|
7
|
+
comment: string,
|
|
8
|
+
): Node => {
|
|
9
|
+
if (comment.length === 0) return node;
|
|
10
|
+
ts.addSyntheticLeadingComment(
|
|
11
|
+
node,
|
|
12
|
+
ts.SyntaxKind.MultiLineCommentTrivia,
|
|
13
|
+
["*", ...comment.split("\n").map((str) => ` * ${str}`), ""].join("\n"),
|
|
14
|
+
true,
|
|
15
|
+
);
|
|
16
|
+
return node;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const enter = () =>
|
|
20
|
+
ts.factory.createExpressionStatement(ts.factory.createIdentifier("\n"));
|
|
21
|
+
|
|
22
|
+
export const beautify = async (script: string): Promise<string> => {
|
|
23
|
+
try {
|
|
24
|
+
return await format(script, {
|
|
25
|
+
parser: "typescript",
|
|
26
|
+
});
|
|
27
|
+
} catch {
|
|
28
|
+
return script;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -1,197 +1,197 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { HashMap } from "tstl/container/HashMap";
|
|
3
|
-
import { HashSet } from "tstl/container/HashSet";
|
|
4
|
-
import { Pair } from "tstl/utility/Pair";
|
|
5
|
-
import ts from "typescript";
|
|
6
|
-
|
|
7
|
-
import { FormatUtil } from "./FormatUtil";
|
|
8
|
-
|
|
9
|
-
export class ImportDictionary {
|
|
10
|
-
private readonly components_: HashMap<Pair<string, boolean>, IComposition> =
|
|
11
|
-
new HashMap();
|
|
12
|
-
|
|
13
|
-
public constructor(public readonly file: string) {}
|
|
14
|
-
|
|
15
|
-
public empty(): boolean {
|
|
16
|
-
return this.components_.empty();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public external(props: ImportDictionary.IExternalProps): string {
|
|
20
|
-
const composition: IComposition = this.components_.take(
|
|
21
|
-
new Pair(props.library, props.type),
|
|
22
|
-
() => ({
|
|
23
|
-
location: `node_modules/${props.library}`,
|
|
24
|
-
elements: new HashSet(),
|
|
25
|
-
default: false,
|
|
26
|
-
type: props.type,
|
|
27
|
-
}),
|
|
28
|
-
);
|
|
29
|
-
if (props.instance === null) composition.default = true;
|
|
30
|
-
else composition.elements.insert(props.instance);
|
|
31
|
-
return props.instance ?? props.library;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public internal(props: ImportDictionary.IInternalProps): string {
|
|
35
|
-
const file: string = (() => {
|
|
36
|
-
if (props.file.substring(props.file.length - 5) === ".d.ts")
|
|
37
|
-
return props.file.substring(0, props.file.length - 5);
|
|
38
|
-
else if (props.file.substring(props.file.length - 3) === ".ts")
|
|
39
|
-
return props.file.substring(0, props.file.length - 3);
|
|
40
|
-
return props.file;
|
|
41
|
-
})();
|
|
42
|
-
const composition: IComposition = this.components_.take(
|
|
43
|
-
new Pair(file, props.type),
|
|
44
|
-
() => ({
|
|
45
|
-
location: file,
|
|
46
|
-
elements: new HashSet(),
|
|
47
|
-
default: false,
|
|
48
|
-
type: props.type,
|
|
49
|
-
}),
|
|
50
|
-
);
|
|
51
|
-
if (props.instance === null) {
|
|
52
|
-
composition.default = true;
|
|
53
|
-
if (props.name) composition.name = props.name;
|
|
54
|
-
} else composition.elements.insert(props.instance);
|
|
55
|
-
return props.instance ?? file;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public toStatements(outDir: string): ts.Statement[] {
|
|
59
|
-
const external: ts.ImportDeclaration[] = [];
|
|
60
|
-
const internal: ts.ImportDeclaration[] = [];
|
|
61
|
-
|
|
62
|
-
const locator = (str: string) => {
|
|
63
|
-
const location: string = path.relative(outDir, str).split("\\").join("/");
|
|
64
|
-
const index: number = location.lastIndexOf(NODE_MODULES);
|
|
65
|
-
return index === -1
|
|
66
|
-
? location.startsWith("..")
|
|
67
|
-
? location
|
|
68
|
-
: `./${location}`
|
|
69
|
-
: location.substring(index + NODE_MODULES.length);
|
|
70
|
-
};
|
|
71
|
-
const enroll =
|
|
72
|
-
(filter: (str: string) => boolean) =>
|
|
73
|
-
(container: ts.ImportDeclaration[]) => {
|
|
74
|
-
const compositions: IComposition[] = this.components_
|
|
75
|
-
.toJSON()
|
|
76
|
-
.filter((c) => filter(c.second.location))
|
|
77
|
-
.map((e) => ({
|
|
78
|
-
...e.second,
|
|
79
|
-
location: locator(e.second.location),
|
|
80
|
-
}))
|
|
81
|
-
.sort((a, b) => a.location.localeCompare(b.location));
|
|
82
|
-
for (const c of compositions) {
|
|
83
|
-
const brackets: string[] = [];
|
|
84
|
-
if (c.default) brackets.push(c.name ?? c.location);
|
|
85
|
-
if (c.elements.empty() === false)
|
|
86
|
-
brackets.push(
|
|
87
|
-
`{ ${c.elements
|
|
88
|
-
.toJSON()
|
|
89
|
-
.sort((a, b) => a.localeCompare(b))
|
|
90
|
-
.join(", ")} }`,
|
|
91
|
-
);
|
|
92
|
-
container.push(
|
|
93
|
-
ts.factory.createImportDeclaration(
|
|
94
|
-
undefined,
|
|
95
|
-
ts.factory.createImportClause(
|
|
96
|
-
c.type,
|
|
97
|
-
c.default
|
|
98
|
-
? ts.factory.createIdentifier(c.name ?? c.location)
|
|
99
|
-
: undefined,
|
|
100
|
-
c.elements.empty() === false
|
|
101
|
-
? ts.factory.createNamedImports(
|
|
102
|
-
[...c.elements].map((elem) =>
|
|
103
|
-
ts.factory.createImportSpecifier(
|
|
104
|
-
false,
|
|
105
|
-
undefined,
|
|
106
|
-
ts.factory.createIdentifier(elem),
|
|
107
|
-
),
|
|
108
|
-
),
|
|
109
|
-
)
|
|
110
|
-
: undefined,
|
|
111
|
-
),
|
|
112
|
-
ts.factory.createStringLiteral(c.location),
|
|
113
|
-
),
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
enroll((str) => str.indexOf(NODE_MODULES) !== -1)(external);
|
|
119
|
-
enroll((str) => str.indexOf(NODE_MODULES) === -1)(internal);
|
|
120
|
-
return [
|
|
121
|
-
...external,
|
|
122
|
-
...(external.length && internal.length ? [FormatUtil.enter()] : []),
|
|
123
|
-
...internal,
|
|
124
|
-
];
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
public toScript(outDir: string): string {
|
|
128
|
-
const external: string[] = [];
|
|
129
|
-
const internal: string[] = [];
|
|
130
|
-
|
|
131
|
-
const locator = (str: string) => {
|
|
132
|
-
const location: string = path.relative(outDir, str).split("\\").join("/");
|
|
133
|
-
const index: number = location.lastIndexOf(NODE_MODULES);
|
|
134
|
-
return index === -1
|
|
135
|
-
? location.startsWith("..")
|
|
136
|
-
? location
|
|
137
|
-
: `./${location}`
|
|
138
|
-
: location.substring(index + NODE_MODULES.length);
|
|
139
|
-
};
|
|
140
|
-
const enroll =
|
|
141
|
-
(filter: (str: string) => boolean) => (container: string[]) => {
|
|
142
|
-
const compositions: IComposition[] = this.components_
|
|
143
|
-
.toJSON()
|
|
144
|
-
.filter((c) => filter(c.second.location))
|
|
145
|
-
.map((e) => ({
|
|
146
|
-
...e.second,
|
|
147
|
-
location: locator(e.second.location),
|
|
148
|
-
}))
|
|
149
|
-
.sort((a, b) => a.location.localeCompare(b.location));
|
|
150
|
-
for (const c of compositions) {
|
|
151
|
-
const brackets: string[] = [];
|
|
152
|
-
if (c.default) brackets.push(c.name ?? c.location);
|
|
153
|
-
if (c.elements.empty() === false)
|
|
154
|
-
brackets.push(
|
|
155
|
-
`{ ${c.elements
|
|
156
|
-
.toJSON()
|
|
157
|
-
.sort((a, b) => a.localeCompare(b))
|
|
158
|
-
.join(", ")} }`,
|
|
159
|
-
);
|
|
160
|
-
container.push(
|
|
161
|
-
`import ${c.type ? "type " : ""}${brackets.join(", ")} from "${
|
|
162
|
-
c.location
|
|
163
|
-
}";`,
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
enroll((str) => str.indexOf(NODE_MODULES) !== -1)(external);
|
|
169
|
-
enroll((str) => str.indexOf(NODE_MODULES) === -1)(internal);
|
|
170
|
-
|
|
171
|
-
if (external.length && internal.length) external.push("");
|
|
172
|
-
return [...external, ...internal].join("\n");
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
export namespace ImportDictionary {
|
|
176
|
-
export interface IExternalProps {
|
|
177
|
-
type: boolean;
|
|
178
|
-
library: string;
|
|
179
|
-
instance: string | null;
|
|
180
|
-
}
|
|
181
|
-
export interface IInternalProps {
|
|
182
|
-
type: boolean;
|
|
183
|
-
file: string;
|
|
184
|
-
instance: string | null;
|
|
185
|
-
name?: string | null;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
interface IComposition {
|
|
190
|
-
location: string;
|
|
191
|
-
type: boolean;
|
|
192
|
-
default: boolean;
|
|
193
|
-
name?: string;
|
|
194
|
-
elements: HashSet<string>;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const NODE_MODULES = "node_modules/";
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { HashMap } from "tstl/container/HashMap";
|
|
3
|
+
import { HashSet } from "tstl/container/HashSet";
|
|
4
|
+
import { Pair } from "tstl/utility/Pair";
|
|
5
|
+
import ts from "typescript";
|
|
6
|
+
|
|
7
|
+
import { FormatUtil } from "./FormatUtil";
|
|
8
|
+
|
|
9
|
+
export class ImportDictionary {
|
|
10
|
+
private readonly components_: HashMap<Pair<string, boolean>, IComposition> =
|
|
11
|
+
new HashMap();
|
|
12
|
+
|
|
13
|
+
public constructor(public readonly file: string) {}
|
|
14
|
+
|
|
15
|
+
public empty(): boolean {
|
|
16
|
+
return this.components_.empty();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public external(props: ImportDictionary.IExternalProps): string {
|
|
20
|
+
const composition: IComposition = this.components_.take(
|
|
21
|
+
new Pair(props.library, props.type),
|
|
22
|
+
() => ({
|
|
23
|
+
location: `node_modules/${props.library}`,
|
|
24
|
+
elements: new HashSet(),
|
|
25
|
+
default: false,
|
|
26
|
+
type: props.type,
|
|
27
|
+
}),
|
|
28
|
+
);
|
|
29
|
+
if (props.instance === null) composition.default = true;
|
|
30
|
+
else composition.elements.insert(props.instance);
|
|
31
|
+
return props.instance ?? props.library;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public internal(props: ImportDictionary.IInternalProps): string {
|
|
35
|
+
const file: string = (() => {
|
|
36
|
+
if (props.file.substring(props.file.length - 5) === ".d.ts")
|
|
37
|
+
return props.file.substring(0, props.file.length - 5);
|
|
38
|
+
else if (props.file.substring(props.file.length - 3) === ".ts")
|
|
39
|
+
return props.file.substring(0, props.file.length - 3);
|
|
40
|
+
return props.file;
|
|
41
|
+
})();
|
|
42
|
+
const composition: IComposition = this.components_.take(
|
|
43
|
+
new Pair(file, props.type),
|
|
44
|
+
() => ({
|
|
45
|
+
location: file,
|
|
46
|
+
elements: new HashSet(),
|
|
47
|
+
default: false,
|
|
48
|
+
type: props.type,
|
|
49
|
+
}),
|
|
50
|
+
);
|
|
51
|
+
if (props.instance === null) {
|
|
52
|
+
composition.default = true;
|
|
53
|
+
if (props.name) composition.name = props.name;
|
|
54
|
+
} else composition.elements.insert(props.instance);
|
|
55
|
+
return props.instance ?? file;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public toStatements(outDir: string): ts.Statement[] {
|
|
59
|
+
const external: ts.ImportDeclaration[] = [];
|
|
60
|
+
const internal: ts.ImportDeclaration[] = [];
|
|
61
|
+
|
|
62
|
+
const locator = (str: string) => {
|
|
63
|
+
const location: string = path.relative(outDir, str).split("\\").join("/");
|
|
64
|
+
const index: number = location.lastIndexOf(NODE_MODULES);
|
|
65
|
+
return index === -1
|
|
66
|
+
? location.startsWith("..")
|
|
67
|
+
? location
|
|
68
|
+
: `./${location}`
|
|
69
|
+
: location.substring(index + NODE_MODULES.length);
|
|
70
|
+
};
|
|
71
|
+
const enroll =
|
|
72
|
+
(filter: (str: string) => boolean) =>
|
|
73
|
+
(container: ts.ImportDeclaration[]) => {
|
|
74
|
+
const compositions: IComposition[] = this.components_
|
|
75
|
+
.toJSON()
|
|
76
|
+
.filter((c) => filter(c.second.location))
|
|
77
|
+
.map((e) => ({
|
|
78
|
+
...e.second,
|
|
79
|
+
location: locator(e.second.location),
|
|
80
|
+
}))
|
|
81
|
+
.sort((a, b) => a.location.localeCompare(b.location));
|
|
82
|
+
for (const c of compositions) {
|
|
83
|
+
const brackets: string[] = [];
|
|
84
|
+
if (c.default) brackets.push(c.name ?? c.location);
|
|
85
|
+
if (c.elements.empty() === false)
|
|
86
|
+
brackets.push(
|
|
87
|
+
`{ ${c.elements
|
|
88
|
+
.toJSON()
|
|
89
|
+
.sort((a, b) => a.localeCompare(b))
|
|
90
|
+
.join(", ")} }`,
|
|
91
|
+
);
|
|
92
|
+
container.push(
|
|
93
|
+
ts.factory.createImportDeclaration(
|
|
94
|
+
undefined,
|
|
95
|
+
ts.factory.createImportClause(
|
|
96
|
+
c.type,
|
|
97
|
+
c.default
|
|
98
|
+
? ts.factory.createIdentifier(c.name ?? c.location)
|
|
99
|
+
: undefined,
|
|
100
|
+
c.elements.empty() === false
|
|
101
|
+
? ts.factory.createNamedImports(
|
|
102
|
+
[...c.elements].map((elem) =>
|
|
103
|
+
ts.factory.createImportSpecifier(
|
|
104
|
+
false,
|
|
105
|
+
undefined,
|
|
106
|
+
ts.factory.createIdentifier(elem),
|
|
107
|
+
),
|
|
108
|
+
),
|
|
109
|
+
)
|
|
110
|
+
: undefined,
|
|
111
|
+
),
|
|
112
|
+
ts.factory.createStringLiteral(c.location),
|
|
113
|
+
),
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
enroll((str) => str.indexOf(NODE_MODULES) !== -1)(external);
|
|
119
|
+
enroll((str) => str.indexOf(NODE_MODULES) === -1)(internal);
|
|
120
|
+
return [
|
|
121
|
+
...external,
|
|
122
|
+
...(external.length && internal.length ? [FormatUtil.enter()] : []),
|
|
123
|
+
...internal,
|
|
124
|
+
];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public toScript(outDir: string): string {
|
|
128
|
+
const external: string[] = [];
|
|
129
|
+
const internal: string[] = [];
|
|
130
|
+
|
|
131
|
+
const locator = (str: string) => {
|
|
132
|
+
const location: string = path.relative(outDir, str).split("\\").join("/");
|
|
133
|
+
const index: number = location.lastIndexOf(NODE_MODULES);
|
|
134
|
+
return index === -1
|
|
135
|
+
? location.startsWith("..")
|
|
136
|
+
? location
|
|
137
|
+
: `./${location}`
|
|
138
|
+
: location.substring(index + NODE_MODULES.length);
|
|
139
|
+
};
|
|
140
|
+
const enroll =
|
|
141
|
+
(filter: (str: string) => boolean) => (container: string[]) => {
|
|
142
|
+
const compositions: IComposition[] = this.components_
|
|
143
|
+
.toJSON()
|
|
144
|
+
.filter((c) => filter(c.second.location))
|
|
145
|
+
.map((e) => ({
|
|
146
|
+
...e.second,
|
|
147
|
+
location: locator(e.second.location),
|
|
148
|
+
}))
|
|
149
|
+
.sort((a, b) => a.location.localeCompare(b.location));
|
|
150
|
+
for (const c of compositions) {
|
|
151
|
+
const brackets: string[] = [];
|
|
152
|
+
if (c.default) brackets.push(c.name ?? c.location);
|
|
153
|
+
if (c.elements.empty() === false)
|
|
154
|
+
brackets.push(
|
|
155
|
+
`{ ${c.elements
|
|
156
|
+
.toJSON()
|
|
157
|
+
.sort((a, b) => a.localeCompare(b))
|
|
158
|
+
.join(", ")} }`,
|
|
159
|
+
);
|
|
160
|
+
container.push(
|
|
161
|
+
`import ${c.type ? "type " : ""}${brackets.join(", ")} from "${
|
|
162
|
+
c.location
|
|
163
|
+
}";`,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
enroll((str) => str.indexOf(NODE_MODULES) !== -1)(external);
|
|
169
|
+
enroll((str) => str.indexOf(NODE_MODULES) === -1)(internal);
|
|
170
|
+
|
|
171
|
+
if (external.length && internal.length) external.push("");
|
|
172
|
+
return [...external, ...internal].join("\n");
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
export namespace ImportDictionary {
|
|
176
|
+
export interface IExternalProps {
|
|
177
|
+
type: boolean;
|
|
178
|
+
library: string;
|
|
179
|
+
instance: string | null;
|
|
180
|
+
}
|
|
181
|
+
export interface IInternalProps {
|
|
182
|
+
type: boolean;
|
|
183
|
+
file: string;
|
|
184
|
+
instance: string | null;
|
|
185
|
+
name?: string | null;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
interface IComposition {
|
|
190
|
+
location: string;
|
|
191
|
+
type: boolean;
|
|
192
|
+
default: boolean;
|
|
193
|
+
name?: string;
|
|
194
|
+
elements: HashSet<string>;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const NODE_MODULES = "node_modules/";
|