@nestia/sdk 2.6.2 → 2.6.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/analyses/ControllerAnalyzer.js +3 -3
- package/lib/analyses/ControllerAnalyzer.js.map +1 -1
- package/lib/analyses/ImportAnalyzer.d.ts +1 -2
- package/lib/analyses/ImportAnalyzer.js +2 -2
- package/lib/analyses/ImportAnalyzer.js.map +1 -1
- package/lib/analyses/ReflectAnalyzer.js +2 -2
- package/lib/analyses/ReflectAnalyzer.js.map +1 -1
- package/lib/generates/SwaggerGenerator.js +4 -4
- package/lib/generates/SwaggerGenerator.js.map +1 -1
- package/lib/generates/internal/ImportDictionary.js +6 -8
- package/lib/generates/internal/ImportDictionary.js.map +1 -1
- package/lib/structures/TypeEntry.js +2 -2
- package/lib/structures/TypeEntry.js.map +1 -1
- package/package.json +4 -4
- package/src/INestiaConfig.ts +248 -248
- package/src/NestiaSdkApplication.ts +255 -255
- package/src/analyses/ControllerAnalyzer.ts +402 -402
- package/src/analyses/ExceptionAnalyzer.ts +148 -148
- package/src/analyses/ImportAnalyzer.ts +1 -2
- package/src/analyses/ReflectAnalyzer.ts +463 -463
- package/src/analyses/SecurityAnalyzer.ts +24 -24
- package/src/generates/CloneGenerator.ts +62 -62
- package/src/generates/E2eGenerator.ts +66 -66
- package/src/generates/SdkGenerator.ts +84 -84
- package/src/generates/SwaggerGenerator.ts +446 -446
- package/src/generates/internal/E2eFileProgrammer.ts +182 -182
- package/src/generates/internal/FilePrinter.ts +53 -53
- package/src/generates/internal/ImportDictionary.ts +147 -149
- package/src/generates/internal/SdkAliasCollection.ts +152 -152
- package/src/generates/internal/SdkCloneProgrammer.ts +155 -155
- package/src/generates/internal/SdkFileProgrammer.ts +115 -115
- package/src/generates/internal/SdkFunctionProgrammer.ts +298 -298
- package/src/generates/internal/SdkImportWizard.ts +55 -55
- package/src/generates/internal/SdkNamespaceProgrammer.ts +510 -510
- package/src/generates/internal/SdkRouteProgrammer.ts +83 -83
- package/src/generates/internal/SdkSimulationProgrammer.ts +365 -365
- package/src/generates/internal/SdkTypeProgrammer.ts +385 -385
- package/src/generates/internal/SwaggerSchemaGenerator.ts +438 -438
- package/src/structures/IController.ts +94 -94
- package/src/structures/IRoute.ts +53 -53
- package/src/structures/ISwagger.ts +91 -91
- package/src/structures/ISwaggerRoute.ts +54 -54
- package/src/structures/ISwaggerSecurityScheme.ts +65 -65
- package/src/structures/ParamCategory.ts +1 -1
- package/src/structures/TypeEntry.ts +1 -1
- package/src/utils/StringUtil.ts +6 -6
|
@@ -1,182 +1,182 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
|
|
3
|
-
|
|
4
|
-
import { INestiaConfig } from "../../INestiaConfig";
|
|
5
|
-
import { IRoute } from "../../structures/IRoute";
|
|
6
|
-
import { FilePrinter } from "./FilePrinter";
|
|
7
|
-
import { ImportDictionary } from "./ImportDictionary";
|
|
8
|
-
import { SdkAliasCollection } from "./SdkAliasCollection";
|
|
9
|
-
import { SdkImportWizard } from "./SdkImportWizard";
|
|
10
|
-
import { SdkTypeProgrammer } from "./SdkTypeProgrammer";
|
|
11
|
-
|
|
12
|
-
export namespace E2eFileProgrammer {
|
|
13
|
-
export const generate =
|
|
14
|
-
(checker: ts.TypeChecker) =>
|
|
15
|
-
(config: INestiaConfig) =>
|
|
16
|
-
(props: { api: string; current: string }) =>
|
|
17
|
-
async (route: IRoute): Promise<void> => {
|
|
18
|
-
const importer: ImportDictionary = new ImportDictionary(
|
|
19
|
-
`${props.current}/${getFunctionName(route)}.ts`,
|
|
20
|
-
);
|
|
21
|
-
if (config.clone !== true)
|
|
22
|
-
for (const tuple of route.imports)
|
|
23
|
-
for (const instance of tuple[1])
|
|
24
|
-
importer.internal({
|
|
25
|
-
file: tuple[0],
|
|
26
|
-
type: true,
|
|
27
|
-
instance,
|
|
28
|
-
});
|
|
29
|
-
importer.internal({
|
|
30
|
-
type: false,
|
|
31
|
-
file: props.api,
|
|
32
|
-
instance: null,
|
|
33
|
-
name: "api",
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
const functor = generate_function(checker)(config)(importer)(route);
|
|
37
|
-
await FilePrinter.write({
|
|
38
|
-
location: importer.file,
|
|
39
|
-
statements: [
|
|
40
|
-
...importer.toStatements(props.current),
|
|
41
|
-
FilePrinter.enter(),
|
|
42
|
-
functor,
|
|
43
|
-
],
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const generate_function =
|
|
48
|
-
(checker: ts.TypeChecker) =>
|
|
49
|
-
(config: INestiaConfig) =>
|
|
50
|
-
(importer: ImportDictionary) =>
|
|
51
|
-
(route: IRoute): ts.Statement =>
|
|
52
|
-
ts.factory.createVariableStatement(
|
|
53
|
-
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
|
|
54
|
-
ts.factory.createVariableDeclarationList(
|
|
55
|
-
[
|
|
56
|
-
ts.factory.createVariableDeclaration(
|
|
57
|
-
ts.factory.createIdentifier(getFunctionName(route)),
|
|
58
|
-
undefined,
|
|
59
|
-
undefined,
|
|
60
|
-
generate_arrow(checker)(config)(importer)(route),
|
|
61
|
-
),
|
|
62
|
-
],
|
|
63
|
-
ts.NodeFlags.Const,
|
|
64
|
-
),
|
|
65
|
-
);
|
|
66
|
-
|
|
67
|
-
const generate_arrow =
|
|
68
|
-
(checker: ts.TypeChecker) =>
|
|
69
|
-
(config: INestiaConfig) =>
|
|
70
|
-
(importer: ImportDictionary) =>
|
|
71
|
-
(route: IRoute) => {
|
|
72
|
-
const headers = route.parameters.find(
|
|
73
|
-
(p) => p.category === "headers" && p.field === undefined,
|
|
74
|
-
);
|
|
75
|
-
const connection = headers
|
|
76
|
-
? ts.factory.createObjectLiteralExpression(
|
|
77
|
-
[
|
|
78
|
-
ts.factory.createSpreadAssignment(
|
|
79
|
-
ts.factory.createIdentifier("connection"),
|
|
80
|
-
),
|
|
81
|
-
ts.factory.createPropertyAssignment(
|
|
82
|
-
"headers",
|
|
83
|
-
ts.factory.createObjectLiteralExpression(
|
|
84
|
-
[
|
|
85
|
-
ts.factory.createSpreadAssignment(
|
|
86
|
-
IdentifierFactory.access(
|
|
87
|
-
ts.factory.createIdentifier("connection"),
|
|
88
|
-
)("headers"),
|
|
89
|
-
),
|
|
90
|
-
ts.factory.createSpreadAssignment(
|
|
91
|
-
ts.factory.createCallExpression(
|
|
92
|
-
IdentifierFactory.access(
|
|
93
|
-
ts.factory.createIdentifier(
|
|
94
|
-
SdkImportWizard.typia(importer),
|
|
95
|
-
),
|
|
96
|
-
)("random"),
|
|
97
|
-
[getTypeName(config)(importer)(headers)],
|
|
98
|
-
undefined,
|
|
99
|
-
),
|
|
100
|
-
),
|
|
101
|
-
],
|
|
102
|
-
true,
|
|
103
|
-
),
|
|
104
|
-
),
|
|
105
|
-
],
|
|
106
|
-
true,
|
|
107
|
-
)
|
|
108
|
-
: ts.factory.createIdentifier("connection");
|
|
109
|
-
const caller = ts.factory.createCallExpression(
|
|
110
|
-
ts.factory.createIdentifier(
|
|
111
|
-
["api", "functional", ...route.accessors].join("."),
|
|
112
|
-
),
|
|
113
|
-
undefined,
|
|
114
|
-
[
|
|
115
|
-
connection,
|
|
116
|
-
...route.parameters
|
|
117
|
-
.filter((p) => p.category !== "headers")
|
|
118
|
-
.map((p) =>
|
|
119
|
-
ts.factory.createCallExpression(
|
|
120
|
-
IdentifierFactory.access(
|
|
121
|
-
ts.factory.createIdentifier(SdkImportWizard.typia(importer)),
|
|
122
|
-
)("random"),
|
|
123
|
-
[getTypeName(config)(importer)(p)],
|
|
124
|
-
undefined,
|
|
125
|
-
),
|
|
126
|
-
),
|
|
127
|
-
],
|
|
128
|
-
);
|
|
129
|
-
const assert = ts.factory.createCallExpression(
|
|
130
|
-
IdentifierFactory.access(
|
|
131
|
-
ts.factory.createIdentifier(SdkImportWizard.typia(importer)),
|
|
132
|
-
)("assert"),
|
|
133
|
-
undefined,
|
|
134
|
-
[ts.factory.createIdentifier("output")],
|
|
135
|
-
);
|
|
136
|
-
|
|
137
|
-
return ts.factory.createArrowFunction(
|
|
138
|
-
[ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword)],
|
|
139
|
-
undefined,
|
|
140
|
-
[
|
|
141
|
-
IdentifierFactory.parameter(
|
|
142
|
-
"connection",
|
|
143
|
-
ts.factory.createTypeReferenceNode("api.IConnection"),
|
|
144
|
-
),
|
|
145
|
-
],
|
|
146
|
-
undefined,
|
|
147
|
-
undefined,
|
|
148
|
-
ts.factory.createBlock([
|
|
149
|
-
ts.factory.createVariableStatement(
|
|
150
|
-
[],
|
|
151
|
-
ts.factory.createVariableDeclarationList(
|
|
152
|
-
[
|
|
153
|
-
ts.factory.createVariableDeclaration(
|
|
154
|
-
"output",
|
|
155
|
-
undefined,
|
|
156
|
-
config.propagate !== true && route.output.typeName === "void"
|
|
157
|
-
? undefined
|
|
158
|
-
: SdkAliasCollection.output(checker)(config)(importer)(
|
|
159
|
-
route,
|
|
160
|
-
),
|
|
161
|
-
ts.factory.createAwaitExpression(caller),
|
|
162
|
-
),
|
|
163
|
-
],
|
|
164
|
-
ts.NodeFlags.Const,
|
|
165
|
-
),
|
|
166
|
-
),
|
|
167
|
-
ts.factory.createExpressionStatement(assert),
|
|
168
|
-
]),
|
|
169
|
-
);
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const getFunctionName = (route: IRoute): string =>
|
|
174
|
-
["test", "api", ...route.accessors].join("_");
|
|
175
|
-
|
|
176
|
-
const getTypeName =
|
|
177
|
-
(config: INestiaConfig) =>
|
|
178
|
-
(importer: ImportDictionary) =>
|
|
179
|
-
(p: IRoute.IParameter | IRoute.IOutput) =>
|
|
180
|
-
p.metadata
|
|
181
|
-
? SdkTypeProgrammer.write(config)(importer)(p.metadata)
|
|
182
|
-
: ts.factory.createTypeReferenceNode(p.typeName);
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
|
|
3
|
+
|
|
4
|
+
import { INestiaConfig } from "../../INestiaConfig";
|
|
5
|
+
import { IRoute } from "../../structures/IRoute";
|
|
6
|
+
import { FilePrinter } from "./FilePrinter";
|
|
7
|
+
import { ImportDictionary } from "./ImportDictionary";
|
|
8
|
+
import { SdkAliasCollection } from "./SdkAliasCollection";
|
|
9
|
+
import { SdkImportWizard } from "./SdkImportWizard";
|
|
10
|
+
import { SdkTypeProgrammer } from "./SdkTypeProgrammer";
|
|
11
|
+
|
|
12
|
+
export namespace E2eFileProgrammer {
|
|
13
|
+
export const generate =
|
|
14
|
+
(checker: ts.TypeChecker) =>
|
|
15
|
+
(config: INestiaConfig) =>
|
|
16
|
+
(props: { api: string; current: string }) =>
|
|
17
|
+
async (route: IRoute): Promise<void> => {
|
|
18
|
+
const importer: ImportDictionary = new ImportDictionary(
|
|
19
|
+
`${props.current}/${getFunctionName(route)}.ts`,
|
|
20
|
+
);
|
|
21
|
+
if (config.clone !== true)
|
|
22
|
+
for (const tuple of route.imports)
|
|
23
|
+
for (const instance of tuple[1])
|
|
24
|
+
importer.internal({
|
|
25
|
+
file: tuple[0],
|
|
26
|
+
type: true,
|
|
27
|
+
instance,
|
|
28
|
+
});
|
|
29
|
+
importer.internal({
|
|
30
|
+
type: false,
|
|
31
|
+
file: props.api,
|
|
32
|
+
instance: null,
|
|
33
|
+
name: "api",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const functor = generate_function(checker)(config)(importer)(route);
|
|
37
|
+
await FilePrinter.write({
|
|
38
|
+
location: importer.file,
|
|
39
|
+
statements: [
|
|
40
|
+
...importer.toStatements(props.current),
|
|
41
|
+
FilePrinter.enter(),
|
|
42
|
+
functor,
|
|
43
|
+
],
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const generate_function =
|
|
48
|
+
(checker: ts.TypeChecker) =>
|
|
49
|
+
(config: INestiaConfig) =>
|
|
50
|
+
(importer: ImportDictionary) =>
|
|
51
|
+
(route: IRoute): ts.Statement =>
|
|
52
|
+
ts.factory.createVariableStatement(
|
|
53
|
+
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
|
|
54
|
+
ts.factory.createVariableDeclarationList(
|
|
55
|
+
[
|
|
56
|
+
ts.factory.createVariableDeclaration(
|
|
57
|
+
ts.factory.createIdentifier(getFunctionName(route)),
|
|
58
|
+
undefined,
|
|
59
|
+
undefined,
|
|
60
|
+
generate_arrow(checker)(config)(importer)(route),
|
|
61
|
+
),
|
|
62
|
+
],
|
|
63
|
+
ts.NodeFlags.Const,
|
|
64
|
+
),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
const generate_arrow =
|
|
68
|
+
(checker: ts.TypeChecker) =>
|
|
69
|
+
(config: INestiaConfig) =>
|
|
70
|
+
(importer: ImportDictionary) =>
|
|
71
|
+
(route: IRoute) => {
|
|
72
|
+
const headers = route.parameters.find(
|
|
73
|
+
(p) => p.category === "headers" && p.field === undefined,
|
|
74
|
+
);
|
|
75
|
+
const connection = headers
|
|
76
|
+
? ts.factory.createObjectLiteralExpression(
|
|
77
|
+
[
|
|
78
|
+
ts.factory.createSpreadAssignment(
|
|
79
|
+
ts.factory.createIdentifier("connection"),
|
|
80
|
+
),
|
|
81
|
+
ts.factory.createPropertyAssignment(
|
|
82
|
+
"headers",
|
|
83
|
+
ts.factory.createObjectLiteralExpression(
|
|
84
|
+
[
|
|
85
|
+
ts.factory.createSpreadAssignment(
|
|
86
|
+
IdentifierFactory.access(
|
|
87
|
+
ts.factory.createIdentifier("connection"),
|
|
88
|
+
)("headers"),
|
|
89
|
+
),
|
|
90
|
+
ts.factory.createSpreadAssignment(
|
|
91
|
+
ts.factory.createCallExpression(
|
|
92
|
+
IdentifierFactory.access(
|
|
93
|
+
ts.factory.createIdentifier(
|
|
94
|
+
SdkImportWizard.typia(importer),
|
|
95
|
+
),
|
|
96
|
+
)("random"),
|
|
97
|
+
[getTypeName(config)(importer)(headers)],
|
|
98
|
+
undefined,
|
|
99
|
+
),
|
|
100
|
+
),
|
|
101
|
+
],
|
|
102
|
+
true,
|
|
103
|
+
),
|
|
104
|
+
),
|
|
105
|
+
],
|
|
106
|
+
true,
|
|
107
|
+
)
|
|
108
|
+
: ts.factory.createIdentifier("connection");
|
|
109
|
+
const caller = ts.factory.createCallExpression(
|
|
110
|
+
ts.factory.createIdentifier(
|
|
111
|
+
["api", "functional", ...route.accessors].join("."),
|
|
112
|
+
),
|
|
113
|
+
undefined,
|
|
114
|
+
[
|
|
115
|
+
connection,
|
|
116
|
+
...route.parameters
|
|
117
|
+
.filter((p) => p.category !== "headers")
|
|
118
|
+
.map((p) =>
|
|
119
|
+
ts.factory.createCallExpression(
|
|
120
|
+
IdentifierFactory.access(
|
|
121
|
+
ts.factory.createIdentifier(SdkImportWizard.typia(importer)),
|
|
122
|
+
)("random"),
|
|
123
|
+
[getTypeName(config)(importer)(p)],
|
|
124
|
+
undefined,
|
|
125
|
+
),
|
|
126
|
+
),
|
|
127
|
+
],
|
|
128
|
+
);
|
|
129
|
+
const assert = ts.factory.createCallExpression(
|
|
130
|
+
IdentifierFactory.access(
|
|
131
|
+
ts.factory.createIdentifier(SdkImportWizard.typia(importer)),
|
|
132
|
+
)("assert"),
|
|
133
|
+
undefined,
|
|
134
|
+
[ts.factory.createIdentifier("output")],
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
return ts.factory.createArrowFunction(
|
|
138
|
+
[ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword)],
|
|
139
|
+
undefined,
|
|
140
|
+
[
|
|
141
|
+
IdentifierFactory.parameter(
|
|
142
|
+
"connection",
|
|
143
|
+
ts.factory.createTypeReferenceNode("api.IConnection"),
|
|
144
|
+
),
|
|
145
|
+
],
|
|
146
|
+
undefined,
|
|
147
|
+
undefined,
|
|
148
|
+
ts.factory.createBlock([
|
|
149
|
+
ts.factory.createVariableStatement(
|
|
150
|
+
[],
|
|
151
|
+
ts.factory.createVariableDeclarationList(
|
|
152
|
+
[
|
|
153
|
+
ts.factory.createVariableDeclaration(
|
|
154
|
+
"output",
|
|
155
|
+
undefined,
|
|
156
|
+
config.propagate !== true && route.output.typeName === "void"
|
|
157
|
+
? undefined
|
|
158
|
+
: SdkAliasCollection.output(checker)(config)(importer)(
|
|
159
|
+
route,
|
|
160
|
+
),
|
|
161
|
+
ts.factory.createAwaitExpression(caller),
|
|
162
|
+
),
|
|
163
|
+
],
|
|
164
|
+
ts.NodeFlags.Const,
|
|
165
|
+
),
|
|
166
|
+
),
|
|
167
|
+
ts.factory.createExpressionStatement(assert),
|
|
168
|
+
]),
|
|
169
|
+
);
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const getFunctionName = (route: IRoute): string =>
|
|
174
|
+
["test", "api", ...route.accessors].join("_");
|
|
175
|
+
|
|
176
|
+
const getTypeName =
|
|
177
|
+
(config: INestiaConfig) =>
|
|
178
|
+
(importer: ImportDictionary) =>
|
|
179
|
+
(p: IRoute.IParameter | IRoute.IOutput) =>
|
|
180
|
+
p.metadata
|
|
181
|
+
? SdkTypeProgrammer.write(config)(importer)(p.metadata)
|
|
182
|
+
: ts.factory.createTypeReferenceNode(p.typeName);
|
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import { format } from "prettier";
|
|
3
|
-
import ts from "typescript";
|
|
4
|
-
|
|
5
|
-
export namespace FilePrinter {
|
|
6
|
-
export const description = <Node extends ts.Node>(
|
|
7
|
-
node: Node,
|
|
8
|
-
comment: string,
|
|
9
|
-
): Node => {
|
|
10
|
-
if (comment.length === 0) return node;
|
|
11
|
-
ts.addSyntheticLeadingComment(
|
|
12
|
-
node,
|
|
13
|
-
ts.SyntaxKind.MultiLineCommentTrivia,
|
|
14
|
-
["*", ...comment.split("\n").map((str) => ` * ${str}`), ""].join("\n"),
|
|
15
|
-
true,
|
|
16
|
-
);
|
|
17
|
-
return node;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export const enter = () =>
|
|
21
|
-
ts.factory.createExpressionStatement(ts.factory.createIdentifier("\n"));
|
|
22
|
-
|
|
23
|
-
export const write = async (props: {
|
|
24
|
-
location: string;
|
|
25
|
-
statements: ts.Statement[];
|
|
26
|
-
top?: string;
|
|
27
|
-
}): Promise<void> => {
|
|
28
|
-
const script: string = ts
|
|
29
|
-
.createPrinter()
|
|
30
|
-
.printFile(
|
|
31
|
-
ts.factory.createSourceFile(
|
|
32
|
-
props.statements,
|
|
33
|
-
ts.factory.createToken(ts.SyntaxKind.EndOfFileToken),
|
|
34
|
-
ts.NodeFlags.None,
|
|
35
|
-
),
|
|
36
|
-
);
|
|
37
|
-
await fs.promises.writeFile(
|
|
38
|
-
props.location,
|
|
39
|
-
await beautify((props.top ?? "") + script),
|
|
40
|
-
"utf8",
|
|
41
|
-
);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const beautify = async (script: string): Promise<string> => {
|
|
45
|
-
try {
|
|
46
|
-
return await format(script, {
|
|
47
|
-
parser: "typescript",
|
|
48
|
-
});
|
|
49
|
-
} catch {
|
|
50
|
-
return script;
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
}
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { format } from "prettier";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
|
|
5
|
+
export namespace FilePrinter {
|
|
6
|
+
export const description = <Node extends ts.Node>(
|
|
7
|
+
node: Node,
|
|
8
|
+
comment: string,
|
|
9
|
+
): Node => {
|
|
10
|
+
if (comment.length === 0) return node;
|
|
11
|
+
ts.addSyntheticLeadingComment(
|
|
12
|
+
node,
|
|
13
|
+
ts.SyntaxKind.MultiLineCommentTrivia,
|
|
14
|
+
["*", ...comment.split("\n").map((str) => ` * ${str}`), ""].join("\n"),
|
|
15
|
+
true,
|
|
16
|
+
);
|
|
17
|
+
return node;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const enter = () =>
|
|
21
|
+
ts.factory.createExpressionStatement(ts.factory.createIdentifier("\n"));
|
|
22
|
+
|
|
23
|
+
export const write = async (props: {
|
|
24
|
+
location: string;
|
|
25
|
+
statements: ts.Statement[];
|
|
26
|
+
top?: string;
|
|
27
|
+
}): Promise<void> => {
|
|
28
|
+
const script: string = ts
|
|
29
|
+
.createPrinter()
|
|
30
|
+
.printFile(
|
|
31
|
+
ts.factory.createSourceFile(
|
|
32
|
+
props.statements,
|
|
33
|
+
ts.factory.createToken(ts.SyntaxKind.EndOfFileToken),
|
|
34
|
+
ts.NodeFlags.None,
|
|
35
|
+
),
|
|
36
|
+
);
|
|
37
|
+
await fs.promises.writeFile(
|
|
38
|
+
props.location,
|
|
39
|
+
await beautify((props.top ?? "") + script),
|
|
40
|
+
"utf8",
|
|
41
|
+
);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const beautify = async (script: string): Promise<string> => {
|
|
45
|
+
try {
|
|
46
|
+
return await format(script, {
|
|
47
|
+
parser: "typescript",
|
|
48
|
+
});
|
|
49
|
+
} catch {
|
|
50
|
+
return script;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|