@goast/typescript 0.6.1 → 0.6.3-beta.1
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/LICENSE +21 -21
- package/assets/client/k6/request-builder.js +4 -0
- package/assets/client/k6/request-builder.ts +4 -0
- package/assets/stubs/easy-network-stub/easy-network-stub.utils.ts +2 -6
- package/package.json +2 -2
- package/src/mod.ts +0 -7
- package/src/src/assets.ts +0 -9
- package/src/src/ast/_index.ts +0 -54
- package/src/src/ast/common.ts +0 -1
- package/src/src/ast/index.ts +0 -1
- package/src/src/ast/node.ts +0 -10
- package/src/src/ast/nodes/argument.ts +0 -40
- package/src/src/ast/nodes/array-type.ts +0 -49
- package/src/src/ast/nodes/arrow-function.ts +0 -91
- package/src/src/ast/nodes/call.ts +0 -55
- package/src/src/ast/nodes/class.ts +0 -133
- package/src/src/ast/nodes/constructor-parameter.ts +0 -70
- package/src/src/ast/nodes/constructor.ts +0 -77
- package/src/src/ast/nodes/decorator.ts +0 -76
- package/src/src/ast/nodes/doc-tag.ts +0 -291
- package/src/src/ast/nodes/doc.ts +0 -102
- package/src/src/ast/nodes/enum-value.ts +0 -72
- package/src/src/ast/nodes/enum.ts +0 -77
- package/src/src/ast/nodes/export.ts +0 -54
- package/src/src/ast/nodes/function-type.ts +0 -60
- package/src/src/ast/nodes/function.ts +0 -107
- package/src/src/ast/nodes/generic-parameter.ts +0 -71
- package/src/src/ast/nodes/indexer.ts +0 -60
- package/src/src/ast/nodes/interface.ts +0 -117
- package/src/src/ast/nodes/intersection-type.ts +0 -67
- package/src/src/ast/nodes/lookup-type.ts +0 -53
- package/src/src/ast/nodes/method.ts +0 -138
- package/src/src/ast/nodes/object-type.ts +0 -52
- package/src/src/ast/nodes/object.ts +0 -48
- package/src/src/ast/nodes/parameter.ts +0 -85
- package/src/src/ast/nodes/property-accessor.ts +0 -173
- package/src/src/ast/nodes/property.ts +0 -192
- package/src/src/ast/nodes/reference.ts +0 -194
- package/src/src/ast/nodes/string.ts +0 -49
- package/src/src/ast/nodes/tuple.ts +0 -53
- package/src/src/ast/nodes/type-alias.ts +0 -75
- package/src/src/ast/nodes/typeof.ts +0 -42
- package/src/src/ast/nodes/types.ts +0 -40
- package/src/src/ast/nodes/union-type.ts +0 -62
- package/src/src/ast/nodes/variable.ts +0 -73
- package/src/src/ast/references/angular.ts +0 -24
- package/src/src/ast/references/easy-network-stub.ts +0 -21
- package/src/src/ast/references/index.ts +0 -5
- package/src/src/ast/references/k6.ts +0 -8
- package/src/src/ast/references/rxjs.ts +0 -7
- package/src/src/ast/references/typescript.ts +0 -35
- package/src/src/ast/utils/get-type-script-builder-options.ts +0 -19
- package/src/src/ast/utils/to-ts-node.ts +0 -27
- package/src/src/ast/utils/write-ts-generic-parameters.ts +0 -25
- package/src/src/ast/utils/write-ts-members.ts +0 -28
- package/src/src/ast/utils/write-ts-nodes.ts +0 -35
- package/src/src/ast/utils/write-ts-parameters.ts +0 -25
- package/src/src/common-results.ts +0 -26
- package/src/src/config.ts +0 -89
- package/src/src/file-builder.ts +0 -137
- package/src/src/generators/file-generator.ts +0 -15
- package/src/src/generators/index.ts +0 -6
- package/src/src/generators/models/index.ts +0 -3
- package/src/src/generators/models/model-generator.ts +0 -481
- package/src/src/generators/models/models-generator.ts +0 -108
- package/src/src/generators/models/models.ts +0 -121
- package/src/src/generators/services/angular-services/angular-service-generator.ts +0 -414
- package/src/src/generators/services/angular-services/angular-services-generator.ts +0 -295
- package/src/src/generators/services/angular-services/index.ts +0 -3
- package/src/src/generators/services/angular-services/models.ts +0 -277
- package/src/src/generators/services/angular-services/refs.ts +0 -54
- package/src/src/generators/services/easy-network-stub/easy-network-stub-generator.ts +0 -250
- package/src/src/generators/services/easy-network-stub/easy-network-stubs-generator.ts +0 -211
- package/src/src/generators/services/easy-network-stub/index.ts +0 -3
- package/src/src/generators/services/easy-network-stub/models.ts +0 -124
- package/src/src/generators/services/easy-network-stub/refs.ts +0 -45
- package/src/src/generators/services/fetch-clients/fetch-client-generator.ts +0 -285
- package/src/src/generators/services/fetch-clients/fetch-clients-generator.ts +0 -163
- package/src/src/generators/services/fetch-clients/index.ts +0 -3
- package/src/src/generators/services/fetch-clients/models.ts +0 -130
- package/src/src/generators/services/fetch-clients/refs.ts +0 -21
- package/src/src/generators/services/k6-clients/index.ts +0 -3
- package/src/src/generators/services/k6-clients/k6-client-generator.ts +0 -474
- package/src/src/generators/services/k6-clients/k6-clients-generator.ts +0 -171
- package/src/src/generators/services/k6-clients/models.ts +0 -247
- package/src/src/generators/services/k6-clients/refs.ts +0 -16
- package/src/src/import-collection.ts +0 -236
- package/src/src/utils.ts +0 -72
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ApiSchema,
|
|
3
|
-
ApiService,
|
|
4
|
-
DefaultGenerationProviderConfig,
|
|
5
|
-
ExtendedStringCasing,
|
|
6
|
-
Nullable,
|
|
7
|
-
OpenApiServicesGenerationProviderContext,
|
|
8
|
-
} from '@goast/core';
|
|
9
|
-
|
|
10
|
-
import type { ts } from '../../../ast/index.js';
|
|
11
|
-
import type { TypeScriptExportOutput } from '../../../common-results.js';
|
|
12
|
-
import { defaultTypeScriptGeneratorConfig, type TypeScriptGeneratorConfig } from '../../../config.js';
|
|
13
|
-
import type { TypeScriptFileBuilder } from '../../../file-builder.js';
|
|
14
|
-
import type { TypeScriptModelsGeneratorOutput } from '../../models/index.js';
|
|
15
|
-
import type { getReferenceFactories } from './refs.js';
|
|
16
|
-
|
|
17
|
-
export type TypeScriptK6ClientsGeneratorConfig = TypeScriptGeneratorConfig & {
|
|
18
|
-
/**
|
|
19
|
-
* The language of the generated clients.
|
|
20
|
-
* @default 'javascript'
|
|
21
|
-
*/
|
|
22
|
-
language: 'javascript' | 'typescript';
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Whether to use strict response types for the services.
|
|
26
|
-
* If enabled only the status codes are included that are defined in the OpenAPI specification or the `defaultStatusCodeResponseTypes` option.
|
|
27
|
-
* @default true
|
|
28
|
-
*/
|
|
29
|
-
strictResponseTypes: boolean;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* The default response types for status codes that are not defined in the OpenAPI specification.
|
|
33
|
-
* @example
|
|
34
|
-
* {
|
|
35
|
-
* 400: {
|
|
36
|
-
* parser: 'json',
|
|
37
|
-
* type: schemas =>
|
|
38
|
-
* schemas.find(s => s.name === 'BadRequestResponse')
|
|
39
|
-
* ?? throwExp('BadRequestResponse not found'),
|
|
40
|
-
* },
|
|
41
|
-
* 404: null,
|
|
42
|
-
* 500: {
|
|
43
|
-
* parser: 'json',
|
|
44
|
-
* type: ts.refs.any(),
|
|
45
|
-
* },
|
|
46
|
-
* }
|
|
47
|
-
* @default
|
|
48
|
-
* {
|
|
49
|
-
* 401: null,
|
|
50
|
-
* 403: null,
|
|
51
|
-
* 500: null,
|
|
52
|
-
* }
|
|
53
|
-
*/
|
|
54
|
-
defaultStatusCodeResponseTypes: Record<
|
|
55
|
-
number,
|
|
56
|
-
{
|
|
57
|
-
parser: 'text' | 'json';
|
|
58
|
-
// deno-lint-ignore ban-types
|
|
59
|
-
type: Exclude<ts.Type<TypeScriptFileBuilder>, Function> | ((schemas: readonly ApiSchema[]) => ApiSchema);
|
|
60
|
-
} | null
|
|
61
|
-
>;
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* The possible status codes that any API endpoint can return.
|
|
65
|
-
* This is only used when `strictResponseTypes` is set to `false`.
|
|
66
|
-
* @default
|
|
67
|
-
* [100, 101, 102, 103, 200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 304, 305, 306, 307, 308, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 421, 422, 423, 424, 425, 426, 428, 429, 431, 451, 500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511]
|
|
68
|
-
*/
|
|
69
|
-
possibleStatusCodes: number[];
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* The casing of the client file names. If nullish, the `fileNameCasing` is used.
|
|
73
|
-
* @default { casing: 'kebab', suffix: '-client' }
|
|
74
|
-
*/
|
|
75
|
-
clientFileNameCasing: Nullable<ExtendedStringCasing>;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* The casing of the file names for the response models. If nullish, the `fileNameCasing` is used.
|
|
79
|
-
* @default { casing: 'kebab', suffix: '-responses' }
|
|
80
|
-
*/
|
|
81
|
-
responseModelFileNameCasing: Nullable<ExtendedStringCasing>;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* The casing of the client names. If nullish, the `typeNameCasing` is used.
|
|
85
|
-
* @default { casing: 'pascal', suffix: 'Client' }
|
|
86
|
-
*/
|
|
87
|
-
clientNameCasing: Nullable<ExtendedStringCasing>;
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* The directory where the client files should be saved. The path is relative to the output directory.
|
|
91
|
-
* @default 'clients'
|
|
92
|
-
*/
|
|
93
|
-
clientDir: string;
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* The directory where the response models should be saved. The path is relative to the output directory.
|
|
97
|
-
* @default 'models/responses'
|
|
98
|
-
*/
|
|
99
|
-
responseModelsDir: string;
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* The index file where all clients should be exported. The path is relative to the output directory. If nullish, no index file will be generated.
|
|
103
|
-
* @default 'clients.ts'
|
|
104
|
-
*/
|
|
105
|
-
clientsIndexFile: Nullable<string>;
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* The path to the index file where all response models should be exported. The path is relative to the output directory. If nullish, no index file will be generated.
|
|
109
|
-
* @default 'responses.ts'
|
|
110
|
-
*/
|
|
111
|
-
responseModelsIndexFile: Nullable<string>;
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* The directory where the utilies should be saved. The path is relative to the output directory.
|
|
115
|
-
* @default 'utils'
|
|
116
|
-
*/
|
|
117
|
-
utilsDir: string;
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Whether to generate clients calling the async API of k6.
|
|
121
|
-
* @default true
|
|
122
|
-
*/
|
|
123
|
-
async: boolean;
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
export const defaultTypeScriptK6ClientsGeneratorConfig: DefaultGenerationProviderConfig<
|
|
127
|
-
TypeScriptK6ClientsGeneratorConfig
|
|
128
|
-
> = {
|
|
129
|
-
...defaultTypeScriptGeneratorConfig,
|
|
130
|
-
|
|
131
|
-
language: 'javascript',
|
|
132
|
-
strictResponseTypes: true,
|
|
133
|
-
defaultStatusCodeResponseTypes: {
|
|
134
|
-
401: null,
|
|
135
|
-
403: null,
|
|
136
|
-
500: null,
|
|
137
|
-
},
|
|
138
|
-
possibleStatusCodes: [
|
|
139
|
-
100,
|
|
140
|
-
101,
|
|
141
|
-
102,
|
|
142
|
-
103,
|
|
143
|
-
200,
|
|
144
|
-
201,
|
|
145
|
-
202,
|
|
146
|
-
203,
|
|
147
|
-
204,
|
|
148
|
-
205,
|
|
149
|
-
206,
|
|
150
|
-
207,
|
|
151
|
-
208,
|
|
152
|
-
226,
|
|
153
|
-
300,
|
|
154
|
-
301,
|
|
155
|
-
302,
|
|
156
|
-
303,
|
|
157
|
-
304,
|
|
158
|
-
305,
|
|
159
|
-
306,
|
|
160
|
-
307,
|
|
161
|
-
308,
|
|
162
|
-
400,
|
|
163
|
-
401,
|
|
164
|
-
402,
|
|
165
|
-
403,
|
|
166
|
-
404,
|
|
167
|
-
405,
|
|
168
|
-
406,
|
|
169
|
-
407,
|
|
170
|
-
408,
|
|
171
|
-
409,
|
|
172
|
-
410,
|
|
173
|
-
411,
|
|
174
|
-
412,
|
|
175
|
-
413,
|
|
176
|
-
414,
|
|
177
|
-
415,
|
|
178
|
-
416,
|
|
179
|
-
417,
|
|
180
|
-
421,
|
|
181
|
-
422,
|
|
182
|
-
423,
|
|
183
|
-
424,
|
|
184
|
-
425,
|
|
185
|
-
426,
|
|
186
|
-
428,
|
|
187
|
-
429,
|
|
188
|
-
431,
|
|
189
|
-
451,
|
|
190
|
-
500,
|
|
191
|
-
501,
|
|
192
|
-
502,
|
|
193
|
-
503,
|
|
194
|
-
504,
|
|
195
|
-
505,
|
|
196
|
-
506,
|
|
197
|
-
507,
|
|
198
|
-
508,
|
|
199
|
-
510,
|
|
200
|
-
511,
|
|
201
|
-
],
|
|
202
|
-
|
|
203
|
-
clientFileNameCasing: { casing: 'kebab', suffix: '-client' },
|
|
204
|
-
responseModelFileNameCasing: { casing: 'kebab', suffix: '-responses' },
|
|
205
|
-
clientNameCasing: { casing: 'pascal', suffix: 'Client' },
|
|
206
|
-
clientDir: 'clients',
|
|
207
|
-
responseModelsDir: 'models/responses',
|
|
208
|
-
clientsIndexFile: 'clients.js',
|
|
209
|
-
responseModelsIndexFile: 'responses.ts',
|
|
210
|
-
utilsDir: 'utils',
|
|
211
|
-
async: true,
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
export type TypeScriptK6ClientsGeneratorInput = TypeScriptModelsGeneratorOutput;
|
|
215
|
-
|
|
216
|
-
export type TypeScriptK6ClientsGeneratorOutput = {
|
|
217
|
-
typescript: {
|
|
218
|
-
k6Clients: {
|
|
219
|
-
[serviceId: string]: TypeScriptK6ClientGeneratorOutput;
|
|
220
|
-
};
|
|
221
|
-
indexFiles: {
|
|
222
|
-
k6Clients: Nullable<string>;
|
|
223
|
-
k6ResponseModels: Nullable<string>;
|
|
224
|
-
};
|
|
225
|
-
};
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
export type TypeScriptK6ClientGeneratorOutput = TypeScriptExportOutput & {
|
|
229
|
-
responseModels: {
|
|
230
|
-
[operationId: string]: TypeScriptExportOutput;
|
|
231
|
-
};
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
export type TypeScriptK6ClientsGeneratorContext =
|
|
235
|
-
& OpenApiServicesGenerationProviderContext<
|
|
236
|
-
TypeScriptK6ClientsGeneratorInput,
|
|
237
|
-
TypeScriptK6ClientsGeneratorOutput,
|
|
238
|
-
TypeScriptK6ClientsGeneratorConfig,
|
|
239
|
-
TypeScriptK6ClientGeneratorOutput
|
|
240
|
-
>
|
|
241
|
-
& {
|
|
242
|
-
refs: ReturnType<typeof getReferenceFactories>;
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
export type TypeScriptK6ClientGeneratorContext = TypeScriptK6ClientsGeneratorContext & {
|
|
246
|
-
readonly service: ApiService;
|
|
247
|
-
};
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { join, resolve } from 'node:path';
|
|
2
|
-
|
|
3
|
-
import { ts } from '../../../ast/index.js';
|
|
4
|
-
import type { TypeScriptK6ClientsGeneratorConfig } from './models.js';
|
|
5
|
-
|
|
6
|
-
export function getReferenceFactories(options: TypeScriptK6ClientsGeneratorConfig): {
|
|
7
|
-
requestBuilder: ts.ModuleReferenceFactory;
|
|
8
|
-
httpStatusCode: ts.ModuleReferenceFactory;
|
|
9
|
-
} {
|
|
10
|
-
const utilsDirPath = resolve(options.outputDir, options.utilsDir);
|
|
11
|
-
const ext = options.language === 'javascript' ? '.js' : '.ts';
|
|
12
|
-
return {
|
|
13
|
-
requestBuilder: ts.reference.factory('RequestBuilder', join(utilsDirPath, `request-builder${ext}`)),
|
|
14
|
-
httpStatusCode: ts.reference.factory('HttpStatusCode', join(utilsDirPath, 'http-status-code.ts')),
|
|
15
|
-
};
|
|
16
|
-
}
|
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
import { extname } from 'node:path';
|
|
2
|
-
|
|
3
|
-
import { type SourceBuilderOptions, StringBuilder } from '@goast/core';
|
|
4
|
-
|
|
5
|
-
import type {
|
|
6
|
-
TypeScriptExportType,
|
|
7
|
-
TypeScriptImport,
|
|
8
|
-
TypeScriptImportKind,
|
|
9
|
-
TypeScriptImportType,
|
|
10
|
-
} from './common-results.js';
|
|
11
|
-
import { defaultTypeScriptGeneratorConfig } from './config.js';
|
|
12
|
-
import { getModulePathRelativeToFile, type ImportModuleTransformer } from './utils.js';
|
|
13
|
-
|
|
14
|
-
export type ImportExportCollectionOptions = {
|
|
15
|
-
filePath?: string;
|
|
16
|
-
importModuleTransformer: ImportModuleTransformer;
|
|
17
|
-
useSingleQuotes: boolean;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export const defaultImportExportCollectionOptions: ImportExportCollectionOptions = {
|
|
21
|
-
importModuleTransformer: defaultTypeScriptGeneratorConfig.importModuleTransformer,
|
|
22
|
-
useSingleQuotes: defaultTypeScriptGeneratorConfig.useSingleQuotes,
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export type TypeScriptImportOptions = { type?: TypeScriptImportType };
|
|
26
|
-
export type TypeScriptExportOptions = { type?: TypeScriptExportType };
|
|
27
|
-
|
|
28
|
-
export class ImportExportCollection {
|
|
29
|
-
private readonly _imports: Map<string, Set<string>> = new Map();
|
|
30
|
-
private readonly _typeImports: Map<string, Set<string>> = new Map();
|
|
31
|
-
private readonly _jsDocImports: Map<string, Set<string>> = new Map();
|
|
32
|
-
private readonly _exports: Map<string, Set<string>> = new Map();
|
|
33
|
-
private readonly _typeExports: Map<string, Set<string>> = new Map();
|
|
34
|
-
private readonly _options: ImportExportCollectionOptions;
|
|
35
|
-
|
|
36
|
-
constructor(options?: Partial<ImportExportCollectionOptions>) {
|
|
37
|
-
this._options = { ...defaultImportExportCollectionOptions, ...options };
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public get hasImports(): boolean {
|
|
41
|
-
return this._imports.size > 0 || this._typeImports.size > 0 || this._jsDocImports.size > 0;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
public get hasExports(): boolean {
|
|
45
|
-
return this._exports.size > 0 || this._typeExports.size > 0;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
public get imports(): TypeScriptImport[] {
|
|
49
|
-
return [
|
|
50
|
-
...this.toImport('import', this._imports),
|
|
51
|
-
...this.toImport('type-import', this._typeImports),
|
|
52
|
-
...this.toImport('js-doc', this._jsDocImports),
|
|
53
|
-
];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
private toImport(type: TypeScriptImportType, map: Map<string, Set<string>>): TypeScriptImport[] {
|
|
57
|
-
return Array.from(map.entries())
|
|
58
|
-
.map(([fromModule, importNames]) =>
|
|
59
|
-
Array.from(importNames).map(
|
|
60
|
-
(importName) => ({
|
|
61
|
-
kind: this.getImportKind(fromModule),
|
|
62
|
-
type,
|
|
63
|
-
modulePath: fromModule,
|
|
64
|
-
name: importName,
|
|
65
|
-
} satisfies TypeScriptImport),
|
|
66
|
-
)
|
|
67
|
-
)
|
|
68
|
-
.flat();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
public addImport(importName: string, fromModule: string, options?: TypeScriptImportOptions): void {
|
|
72
|
-
const map = this.getImportMap(options?.type ?? 'import');
|
|
73
|
-
const existingImport = map.get(fromModule);
|
|
74
|
-
if (existingImport) {
|
|
75
|
-
existingImport.add(importName);
|
|
76
|
-
} else {
|
|
77
|
-
map.set(fromModule, new Set([importName]));
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
public addExport(exportName: string, fromModule: string, options?: TypeScriptExportOptions): void {
|
|
82
|
-
const map = this.getExportMap(options?.type ?? 'export');
|
|
83
|
-
const existingExport = map.get(fromModule);
|
|
84
|
-
if (existingExport) {
|
|
85
|
-
existingExport.add(exportName);
|
|
86
|
-
} else {
|
|
87
|
-
map.set(fromModule, new Set([exportName]));
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
public clear(): void {
|
|
92
|
-
this._imports.clear();
|
|
93
|
-
this._typeImports.clear();
|
|
94
|
-
this._jsDocImports.clear();
|
|
95
|
-
this._exports.clear();
|
|
96
|
-
this._typeExports.clear();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
public toString(options: Partial<SourceBuilderOptions>): string {
|
|
100
|
-
const builder = new StringBuilder(options);
|
|
101
|
-
this.writeTo(builder);
|
|
102
|
-
return builder.toString();
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
public writeTo(builder: StringBuilder) {
|
|
106
|
-
const { filePath, importModuleTransformer, useSingleQuotes } = this._options;
|
|
107
|
-
let hasPrevious = false;
|
|
108
|
-
|
|
109
|
-
if (this._imports.size > 0) {
|
|
110
|
-
const sortedImports = this.sortAndResolve(this._imports, filePath, importModuleTransformer);
|
|
111
|
-
this.writeImportsExports(builder, 'import', sortedImports, { useSingleQuotes });
|
|
112
|
-
hasPrevious = true;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (this._typeImports.size > 0) {
|
|
116
|
-
if (hasPrevious) {
|
|
117
|
-
builder.appendLine();
|
|
118
|
-
}
|
|
119
|
-
const sortedImports = this.sortAndResolve(this._typeImports, filePath, importModuleTransformer);
|
|
120
|
-
this.writeImportsExports(builder, 'import type', sortedImports, { useSingleQuotes });
|
|
121
|
-
hasPrevious = true;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (this._jsDocImports.size > 0) {
|
|
125
|
-
if (hasPrevious) {
|
|
126
|
-
builder.appendLine();
|
|
127
|
-
}
|
|
128
|
-
const sortedImports = this.sortAndResolve(this._jsDocImports, filePath, importModuleTransformer);
|
|
129
|
-
builder.appendLine('/**');
|
|
130
|
-
this.writeImportsExports(builder, ' * @import', sortedImports, { useSingleQuotes });
|
|
131
|
-
builder.appendLine(' */');
|
|
132
|
-
hasPrevious = true;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (this._exports.size > 0) {
|
|
136
|
-
if (hasPrevious) {
|
|
137
|
-
builder.appendLine();
|
|
138
|
-
}
|
|
139
|
-
const sortedExports = this.sortAndResolve(this._exports, filePath, importModuleTransformer);
|
|
140
|
-
this.writeImportsExports(builder, 'export', sortedExports, { useSingleQuotes });
|
|
141
|
-
hasPrevious = true;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (this._typeExports.size > 0) {
|
|
145
|
-
if (hasPrevious) {
|
|
146
|
-
builder.appendLine();
|
|
147
|
-
}
|
|
148
|
-
const sortedExports = this.sortAndResolve(this._typeExports, filePath, importModuleTransformer);
|
|
149
|
-
this.writeImportsExports(builder, 'export type', sortedExports, { useSingleQuotes });
|
|
150
|
-
hasPrevious = true;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
protected writeImportsExports(
|
|
155
|
-
builder: StringBuilder,
|
|
156
|
-
keyword: 'import' | 'export' | 'import type' | 'export type' | ' * @import',
|
|
157
|
-
data: (readonly [string, TypeScriptImportKind, string[]])[],
|
|
158
|
-
options: { useSingleQuotes: boolean },
|
|
159
|
-
) {
|
|
160
|
-
const moduleData: (readonly [string, TypeScriptImportKind, string[]])[] = [];
|
|
161
|
-
const fileData: (readonly [string, TypeScriptImportKind, string[]])[] = [];
|
|
162
|
-
for (const x of data) {
|
|
163
|
-
if (x[1] === 'module') {
|
|
164
|
-
moduleData.push(x);
|
|
165
|
-
} else {
|
|
166
|
-
fileData.push(x);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const quote = options.useSingleQuotes ? "'" : '"';
|
|
171
|
-
|
|
172
|
-
for (const [fromModule, _, names] of moduleData) {
|
|
173
|
-
builder.appendLine(`${keyword} { ${names.join(', ')} } from ${quote}${fromModule}${quote};`);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (moduleData.length > 0 && fileData.length > 0) {
|
|
177
|
-
builder.appendLine();
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
for (const [fromModule, _, names] of fileData) {
|
|
181
|
-
builder.appendLine(`${keyword} { ${names.join(', ')} } from ${quote}${fromModule}${quote};`);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
protected sortAndResolve(
|
|
186
|
-
data: Map<string, Set<string>>,
|
|
187
|
-
filePath: string | undefined,
|
|
188
|
-
importModuleTransformer: ImportModuleTransformer,
|
|
189
|
-
): (readonly [string, TypeScriptImportKind, string[]])[] {
|
|
190
|
-
return Array.from(data.entries())
|
|
191
|
-
.sort(([fromModuleA], [fromModuleB]) => fromModuleA.localeCompare(fromModuleB))
|
|
192
|
-
.map(
|
|
193
|
-
([fromModule, names]) =>
|
|
194
|
-
[...this.resolveModulePath(fromModule, filePath, importModuleTransformer), Array.from(names).sort()] as const,
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
protected getImportKind(fromModule: string): TypeScriptImportKind {
|
|
199
|
-
const extName = extname(fromModule).toUpperCase();
|
|
200
|
-
return extName === '.TS' || extName === '.JS' || extName === '.JSON' ? 'file' : 'module';
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
protected resolveModulePath(
|
|
204
|
-
fromModule: string,
|
|
205
|
-
filePath: string | undefined,
|
|
206
|
-
importModuleTransformer: ImportModuleTransformer,
|
|
207
|
-
): [string, TypeScriptImportKind] {
|
|
208
|
-
const kind = this.getImportKind(fromModule);
|
|
209
|
-
return [
|
|
210
|
-
filePath && kind === 'file'
|
|
211
|
-
? getModulePathRelativeToFile(filePath, fromModule, importModuleTransformer)
|
|
212
|
-
: fromModule,
|
|
213
|
-
kind,
|
|
214
|
-
];
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
protected getImportMap(type: TypeScriptImportType): Map<string, Set<string>> {
|
|
218
|
-
switch (type) {
|
|
219
|
-
case 'import':
|
|
220
|
-
return this._imports;
|
|
221
|
-
case 'type-import':
|
|
222
|
-
return this._typeImports;
|
|
223
|
-
case 'js-doc':
|
|
224
|
-
return this._jsDocImports;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
protected getExportMap(type: TypeScriptExportType): Map<string, Set<string>> {
|
|
229
|
-
switch (type) {
|
|
230
|
-
case 'export':
|
|
231
|
-
return this._exports;
|
|
232
|
-
case 'type-export':
|
|
233
|
-
return this._typeExports;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
package/src/src/utils.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { dirname, relative } from 'node:path';
|
|
2
|
-
|
|
3
|
-
import type { Nullable } from '@goast/core';
|
|
4
|
-
|
|
5
|
-
export type ImportModuleTransformer =
|
|
6
|
-
| 'omit-extension'
|
|
7
|
-
| 'with-extension'
|
|
8
|
-
| 'js-extension'
|
|
9
|
-
| ((module: string) => string);
|
|
10
|
-
|
|
11
|
-
function doubleQuotedToSingleQuoted(value: string): string {
|
|
12
|
-
return `'${value.slice(1, -1).replace(/'/g, "\\'")}'`;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function toTypeScriptStringLiteral(value: string, useSingleQuotes: boolean): string {
|
|
16
|
-
return useSingleQuotes ? doubleQuotedToSingleQuoted(JSON.stringify(value)) : JSON.stringify(value);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function toTypeScriptPropertyName(value: string, useSingleQuotes: boolean): string {
|
|
20
|
-
if (value.match(/^[a-zA-Z_$][a-zA-Z_$0-9]*$/)) {
|
|
21
|
-
return value;
|
|
22
|
-
}
|
|
23
|
-
return useSingleQuotes ? doubleQuotedToSingleQuoted(JSON.stringify(value)) : JSON.stringify(value);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function getModulePathRelativeToFile(from: string, to: string, transformer: ImportModuleTransformer): string {
|
|
27
|
-
return transformModulePath(relative(dirname(from), to).replace(/\\/g, '/'), transformer);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function getModulePathRelativeToDirectory(
|
|
31
|
-
from: string,
|
|
32
|
-
to: string,
|
|
33
|
-
transformer: ImportModuleTransformer,
|
|
34
|
-
): string {
|
|
35
|
-
return transformModulePath(relative(from, to).replace(/\\/g, '/'), transformer);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function transformModulePath(modulePath: string, transformer: ImportModuleTransformer) {
|
|
39
|
-
if (!modulePath.startsWith('.')) {
|
|
40
|
-
modulePath = `./${modulePath}`;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (transformer === 'omit-extension') {
|
|
44
|
-
return modulePath.replace(/\.[^/.]+$/, '');
|
|
45
|
-
} else if (transformer === 'with-extension') {
|
|
46
|
-
return modulePath;
|
|
47
|
-
} else if (transformer === 'js-extension') {
|
|
48
|
-
return modulePath.replace(/\.[^/.]+$/, '.js');
|
|
49
|
-
} else if (typeof transformer === 'function') {
|
|
50
|
-
return transformer(modulePath);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return modulePath;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function modifyString<TArgs extends unknown[]>(
|
|
57
|
-
value: string,
|
|
58
|
-
modifier: Nullable<string | RegExp | ((value: string, ...args: TArgs) => string)>,
|
|
59
|
-
...args: TArgs
|
|
60
|
-
): string {
|
|
61
|
-
if (typeof modifier === 'string') {
|
|
62
|
-
return modifier;
|
|
63
|
-
}
|
|
64
|
-
if (modifier instanceof RegExp) {
|
|
65
|
-
const match = value.match(modifier);
|
|
66
|
-
return match?.[0] ?? value;
|
|
67
|
-
}
|
|
68
|
-
if (typeof modifier === 'function') {
|
|
69
|
-
return modifier(value, ...args);
|
|
70
|
-
}
|
|
71
|
-
return value;
|
|
72
|
-
}
|