swagger-typescript-api 12.0.4 → 13.0.0
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/README.md +1 -1
- package/cli/constants.js +3 -3
- package/cli/execute.js +52 -31
- package/cli/index.d.ts +1 -2
- package/cli/index.js +18 -17
- package/cli/operations/display-help.js +51 -29
- package/cli/parse-args.js +3 -3
- package/cli/process-option.js +28 -20
- package/index.d.ts +99 -7
- package/index.js +158 -136
- package/package.json +35 -30
- package/src/code-formatter.js +28 -13
- package/src/code-gen-process.js +367 -264
- package/src/commands/generate-templates/configuration.js +2 -2
- package/src/commands/generate-templates/index.js +1 -2
- package/src/commands/generate-templates/templates-gen-process.js +62 -35
- package/src/component-type-name-resolver.js +44 -0
- package/src/configuration.js +166 -98
- package/src/constants.js +28 -22
- package/src/index.js +3 -4
- package/src/schema-components-map.js +39 -23
- package/src/schema-parser/base-schema-parsers/array.js +43 -0
- package/src/schema-parser/base-schema-parsers/complex.js +51 -0
- package/src/schema-parser/base-schema-parsers/discriminator.js +301 -0
- package/src/schema-parser/base-schema-parsers/enum.js +158 -0
- package/src/schema-parser/base-schema-parsers/object.js +105 -0
- package/src/schema-parser/base-schema-parsers/primitive.js +63 -0
- package/src/schema-parser/complex-schema-parsers/all-of.js +26 -0
- package/src/schema-parser/complex-schema-parsers/any-of.js +34 -0
- package/src/schema-parser/complex-schema-parsers/not.js +9 -0
- package/src/schema-parser/complex-schema-parsers/one-of.js +27 -0
- package/src/schema-parser/mono-schema-parser.js +48 -0
- package/src/schema-parser/schema-formatters.js +58 -44
- package/src/schema-parser/schema-parser-fabric.js +131 -0
- package/src/schema-parser/schema-parser.js +212 -361
- package/src/schema-parser/schema-utils.js +158 -33
- package/src/schema-parser/util/enum-key-resolver.js +26 -0
- package/src/{schema-parser → schema-routes}/schema-routes.js +472 -203
- package/src/schema-routes/util/specific-arg-name-resolver.js +26 -0
- package/src/schema-walker.js +93 -0
- package/src/swagger-schema-resolver.js +61 -28
- package/src/templates-worker.js +240 -0
- package/src/translators/javascript.js +83 -0
- package/src/translators/translator.js +35 -0
- package/src/{type-name.js → type-name-formatter.js} +35 -20
- package/src/util/file-system.js +30 -14
- package/src/util/id.js +2 -2
- package/src/util/internal-case.js +1 -1
- package/src/util/logger.js +46 -20
- package/src/util/name-resolver.js +50 -58
- package/src/util/object-assign.js +7 -3
- package/src/util/pascal-case.js +1 -1
- package/src/util/request.js +5 -5
- package/src/util/sort-by-property.js +17 -0
- package/templates/base/data-contract-jsdoc.ejs +37 -37
- package/templates/base/data-contracts.ejs +40 -28
- package/templates/base/enum-data-contract.ejs +12 -12
- package/templates/base/http-client.ejs +2 -2
- package/templates/base/http-clients/axios-http-client.ejs +139 -138
- package/templates/base/http-clients/fetch-http-client.ejs +224 -224
- package/templates/base/interface-data-contract.ejs +10 -10
- package/templates/base/object-field-jsdoc.ejs +28 -28
- package/templates/base/route-docs.ejs +30 -30
- package/templates/base/route-name.ejs +42 -42
- package/templates/base/route-type.ejs +22 -21
- package/templates/base/type-data-contract.ejs +15 -15
- package/templates/default/api.ejs +69 -65
- package/templates/default/procedure-call.ejs +100 -100
- package/templates/default/route-types.ejs +32 -28
- package/templates/modular/api.ejs +28 -28
- package/templates/modular/procedure-call.ejs +100 -100
- package/templates/modular/route-types.ejs +18 -18
- package/src/templates.js +0 -177
- package/src/translators/JavaScript.js +0 -60
|
@@ -1,100 +1,100 @@
|
|
|
1
|
-
<%
|
|
2
|
-
const { utils, route, config } = it;
|
|
3
|
-
const { requestBodyInfo, responseBodyInfo, specificArgNameResolver } = route;
|
|
4
|
-
const { _, getInlineParseContent, getParseContent, parseSchema, getComponentByRef, require } = utils;
|
|
5
|
-
const { parameters, path, method, payload, query, formData, security, requestParams } = route.request;
|
|
6
|
-
const { type, errorType, contentTypes } = route.response;
|
|
7
|
-
const { HTTP_CLIENT, RESERVED_REQ_PARAMS_ARG_NAMES } = config.constants;
|
|
8
|
-
const routeDocs = includeFile("@base/route-docs", { config, route, utils });
|
|
9
|
-
const queryName = (query && query.name) || "query";
|
|
10
|
-
const pathParams = _.values(parameters);
|
|
11
|
-
const pathParamsNames = _.map(pathParams, "name");
|
|
12
|
-
|
|
13
|
-
const isFetchTemplate = config.httpClientType === HTTP_CLIENT.FETCH;
|
|
14
|
-
|
|
15
|
-
const requestConfigParam = {
|
|
16
|
-
name: specificArgNameResolver.resolve(RESERVED_REQ_PARAMS_ARG_NAMES),
|
|
17
|
-
optional: true,
|
|
18
|
-
type: "RequestParams",
|
|
19
|
-
defaultValue: "{}",
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const argToTmpl = ({ name, optional, type, defaultValue }) => `${name}${!defaultValue && optional ? '?' : ''}: ${type}${defaultValue ? ` = ${defaultValue}` : ''}`;
|
|
23
|
-
|
|
24
|
-
const rawWrapperArgs = config.extractRequestParams ?
|
|
25
|
-
_.compact([
|
|
26
|
-
requestParams && {
|
|
27
|
-
name: pathParams.length ? `{ ${_.join(pathParamsNames, ", ")}, ...${queryName} }` : queryName,
|
|
28
|
-
optional: false,
|
|
29
|
-
type: getInlineParseContent(requestParams),
|
|
30
|
-
},
|
|
31
|
-
...(!requestParams ? pathParams : []),
|
|
32
|
-
payload,
|
|
33
|
-
requestConfigParam,
|
|
34
|
-
]) :
|
|
35
|
-
_.compact([
|
|
36
|
-
...pathParams,
|
|
37
|
-
query,
|
|
38
|
-
payload,
|
|
39
|
-
requestConfigParam,
|
|
40
|
-
])
|
|
41
|
-
|
|
42
|
-
const wrapperArgs = _
|
|
43
|
-
// Sort by optionality
|
|
44
|
-
.sortBy(rawWrapperArgs, [o => o.optional])
|
|
45
|
-
.map(argToTmpl)
|
|
46
|
-
.join(', ')
|
|
47
|
-
|
|
48
|
-
// RequestParams["type"]
|
|
49
|
-
const requestContentKind = {
|
|
50
|
-
"JSON": "ContentType.Json",
|
|
51
|
-
"URL_ENCODED": "ContentType.UrlEncoded",
|
|
52
|
-
"FORM_DATA": "ContentType.FormData",
|
|
53
|
-
"TEXT": "ContentType.Text",
|
|
54
|
-
}
|
|
55
|
-
// RequestParams["format"]
|
|
56
|
-
const responseContentKind = {
|
|
57
|
-
"JSON": '"json"',
|
|
58
|
-
"IMAGE": '"blob"',
|
|
59
|
-
"FORM_DATA": isFetchTemplate ? '"formData"' : '"document"'
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const bodyTmpl = _.get(payload, "name") || null;
|
|
63
|
-
const queryTmpl = (query != null && queryName) || null;
|
|
64
|
-
const bodyContentKindTmpl = requestContentKind[requestBodyInfo.contentKind] || null;
|
|
65
|
-
const responseFormatTmpl = responseContentKind[responseBodyInfo.success && responseBodyInfo.success.schema && responseBodyInfo.success.schema.contentKind] || null;
|
|
66
|
-
const securityTmpl = security ? 'true' : null;
|
|
67
|
-
|
|
68
|
-
const describeReturnType = () => {
|
|
69
|
-
if (!config.toJS) return "";
|
|
70
|
-
|
|
71
|
-
switch(config.httpClientType) {
|
|
72
|
-
case HTTP_CLIENT.AXIOS: {
|
|
73
|
-
return `Promise<AxiosResponse<${type}>>`
|
|
74
|
-
}
|
|
75
|
-
default: {
|
|
76
|
-
return `Promise<HttpResponse<${type}, ${errorType}>`
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
%>
|
|
82
|
-
/**
|
|
83
|
-
<%~ routeDocs.description %>
|
|
84
|
-
|
|
85
|
-
*<% /* Here you can add some other JSDoc tags */ %>
|
|
86
|
-
|
|
87
|
-
<%~ routeDocs.lines %>
|
|
88
|
-
|
|
89
|
-
*/
|
|
90
|
-
<%~ route.routeName.usage %> = (<%~ wrapperArgs %>)<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
|
|
91
|
-
<%~ config.singleHttpClient ? 'this.http.request' : 'this.request' %><<%~ type %>, <%~ errorType %>>({
|
|
92
|
-
path: `<%~ path %>`,
|
|
93
|
-
method: '<%~ _.upperCase(method) %>',
|
|
94
|
-
<%~ queryTmpl ? `query: ${queryTmpl},` : '' %>
|
|
95
|
-
<%~ bodyTmpl ? `body: ${bodyTmpl},` : '' %>
|
|
96
|
-
<%~ securityTmpl ? `secure: ${securityTmpl},` : '' %>
|
|
97
|
-
<%~ bodyContentKindTmpl ? `type: ${bodyContentKindTmpl},` : '' %>
|
|
98
|
-
<%~ responseFormatTmpl ? `format: ${responseFormatTmpl},` : '' %>
|
|
99
|
-
...<%~ _.get(requestConfigParam, "name") %>,
|
|
100
|
-
})
|
|
1
|
+
<%
|
|
2
|
+
const { utils, route, config } = it;
|
|
3
|
+
const { requestBodyInfo, responseBodyInfo, specificArgNameResolver } = route;
|
|
4
|
+
const { _, getInlineParseContent, getParseContent, parseSchema, getComponentByRef, require } = utils;
|
|
5
|
+
const { parameters, path, method, payload, query, formData, security, requestParams } = route.request;
|
|
6
|
+
const { type, errorType, contentTypes } = route.response;
|
|
7
|
+
const { HTTP_CLIENT, RESERVED_REQ_PARAMS_ARG_NAMES } = config.constants;
|
|
8
|
+
const routeDocs = includeFile("@base/route-docs", { config, route, utils });
|
|
9
|
+
const queryName = (query && query.name) || "query";
|
|
10
|
+
const pathParams = _.values(parameters);
|
|
11
|
+
const pathParamsNames = _.map(pathParams, "name");
|
|
12
|
+
|
|
13
|
+
const isFetchTemplate = config.httpClientType === HTTP_CLIENT.FETCH;
|
|
14
|
+
|
|
15
|
+
const requestConfigParam = {
|
|
16
|
+
name: specificArgNameResolver.resolve(RESERVED_REQ_PARAMS_ARG_NAMES),
|
|
17
|
+
optional: true,
|
|
18
|
+
type: "RequestParams",
|
|
19
|
+
defaultValue: "{}",
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const argToTmpl = ({ name, optional, type, defaultValue }) => `${name}${!defaultValue && optional ? '?' : ''}: ${type}${defaultValue ? ` = ${defaultValue}` : ''}`;
|
|
23
|
+
|
|
24
|
+
const rawWrapperArgs = config.extractRequestParams ?
|
|
25
|
+
_.compact([
|
|
26
|
+
requestParams && {
|
|
27
|
+
name: pathParams.length ? `{ ${_.join(pathParamsNames, ", ")}, ...${queryName} }` : queryName,
|
|
28
|
+
optional: false,
|
|
29
|
+
type: getInlineParseContent(requestParams),
|
|
30
|
+
},
|
|
31
|
+
...(!requestParams ? pathParams : []),
|
|
32
|
+
payload,
|
|
33
|
+
requestConfigParam,
|
|
34
|
+
]) :
|
|
35
|
+
_.compact([
|
|
36
|
+
...pathParams,
|
|
37
|
+
query,
|
|
38
|
+
payload,
|
|
39
|
+
requestConfigParam,
|
|
40
|
+
])
|
|
41
|
+
|
|
42
|
+
const wrapperArgs = _
|
|
43
|
+
// Sort by optionality
|
|
44
|
+
.sortBy(rawWrapperArgs, [o => o.optional])
|
|
45
|
+
.map(argToTmpl)
|
|
46
|
+
.join(', ')
|
|
47
|
+
|
|
48
|
+
// RequestParams["type"]
|
|
49
|
+
const requestContentKind = {
|
|
50
|
+
"JSON": "ContentType.Json",
|
|
51
|
+
"URL_ENCODED": "ContentType.UrlEncoded",
|
|
52
|
+
"FORM_DATA": "ContentType.FormData",
|
|
53
|
+
"TEXT": "ContentType.Text",
|
|
54
|
+
}
|
|
55
|
+
// RequestParams["format"]
|
|
56
|
+
const responseContentKind = {
|
|
57
|
+
"JSON": '"json"',
|
|
58
|
+
"IMAGE": '"blob"',
|
|
59
|
+
"FORM_DATA": isFetchTemplate ? '"formData"' : '"document"'
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const bodyTmpl = _.get(payload, "name") || null;
|
|
63
|
+
const queryTmpl = (query != null && queryName) || null;
|
|
64
|
+
const bodyContentKindTmpl = requestContentKind[requestBodyInfo.contentKind] || null;
|
|
65
|
+
const responseFormatTmpl = responseContentKind[responseBodyInfo.success && responseBodyInfo.success.schema && responseBodyInfo.success.schema.contentKind] || null;
|
|
66
|
+
const securityTmpl = security ? 'true' : null;
|
|
67
|
+
|
|
68
|
+
const describeReturnType = () => {
|
|
69
|
+
if (!config.toJS) return "";
|
|
70
|
+
|
|
71
|
+
switch(config.httpClientType) {
|
|
72
|
+
case HTTP_CLIENT.AXIOS: {
|
|
73
|
+
return `Promise<AxiosResponse<${type}>>`
|
|
74
|
+
}
|
|
75
|
+
default: {
|
|
76
|
+
return `Promise<HttpResponse<${type}, ${errorType}>`
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
%>
|
|
82
|
+
/**
|
|
83
|
+
<%~ routeDocs.description %>
|
|
84
|
+
|
|
85
|
+
*<% /* Here you can add some other JSDoc tags */ %>
|
|
86
|
+
|
|
87
|
+
<%~ routeDocs.lines %>
|
|
88
|
+
|
|
89
|
+
*/
|
|
90
|
+
<%~ route.routeName.usage %> = (<%~ wrapperArgs %>)<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
|
|
91
|
+
<%~ config.singleHttpClient ? 'this.http.request' : 'this.request' %><<%~ type %>, <%~ errorType %>>({
|
|
92
|
+
path: `<%~ path %>`,
|
|
93
|
+
method: '<%~ _.upperCase(method) %>',
|
|
94
|
+
<%~ queryTmpl ? `query: ${queryTmpl},` : '' %>
|
|
95
|
+
<%~ bodyTmpl ? `body: ${bodyTmpl},` : '' %>
|
|
96
|
+
<%~ securityTmpl ? `secure: ${securityTmpl},` : '' %>
|
|
97
|
+
<%~ bodyContentKindTmpl ? `type: ${bodyContentKindTmpl},` : '' %>
|
|
98
|
+
<%~ responseFormatTmpl ? `format: ${responseFormatTmpl},` : '' %>
|
|
99
|
+
...<%~ _.get(requestConfigParam, "name") %>,
|
|
100
|
+
})
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
<%
|
|
2
|
-
const { utils, config, route, modelTypes } = it;
|
|
3
|
-
const { _, pascalCase } = utils;
|
|
4
|
-
const { routes, moduleName } = route;
|
|
5
|
-
const dataContracts = config.modular ? _.map(modelTypes, "name") : [];
|
|
6
|
-
|
|
7
|
-
%>
|
|
8
|
-
<% if (dataContracts.length) { %>
|
|
9
|
-
import { <%~ dataContracts.join(", ") %> } from "./<%~ config.fileNames.dataContracts %>"
|
|
10
|
-
<% } %>
|
|
11
|
-
|
|
12
|
-
export namespace <%~ pascalCase(moduleName) %> {
|
|
13
|
-
<%
|
|
14
|
-
|
|
15
|
-
<%~ includeFile('@base/route-type.ejs', { ...it, route }) %>
|
|
16
|
-
|
|
17
|
-
<% }
|
|
18
|
-
}
|
|
1
|
+
<%
|
|
2
|
+
const { utils, config, route, modelTypes } = it;
|
|
3
|
+
const { _, pascalCase } = utils;
|
|
4
|
+
const { routes, moduleName } = route;
|
|
5
|
+
const dataContracts = config.modular ? _.map(modelTypes, "name") : [];
|
|
6
|
+
|
|
7
|
+
%>
|
|
8
|
+
<% if (dataContracts.length) { %>
|
|
9
|
+
import { <%~ dataContracts.join(", ") %> } from "./<%~ config.fileNames.dataContracts %>"
|
|
10
|
+
<% } %>
|
|
11
|
+
|
|
12
|
+
export namespace <%~ pascalCase(moduleName) %> {
|
|
13
|
+
<% for (const route of routes) { %>
|
|
14
|
+
|
|
15
|
+
<%~ includeFile('@base/route-type.ejs', { ...it, route }) %>
|
|
16
|
+
|
|
17
|
+
<% } %>
|
|
18
|
+
}
|
package/src/templates.js
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
const { resolve } = require("path");
|
|
2
|
-
const _ = require("lodash");
|
|
3
|
-
const Eta = require("eta");
|
|
4
|
-
const path = require("path");
|
|
5
|
-
|
|
6
|
-
class Templates {
|
|
7
|
-
/**
|
|
8
|
-
* @type {CodeGenConfig}
|
|
9
|
-
*/
|
|
10
|
-
config;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @type {Logger}
|
|
14
|
-
*/
|
|
15
|
-
logger;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @type {FileSystem}
|
|
19
|
-
*/
|
|
20
|
-
fileSystem;
|
|
21
|
-
|
|
22
|
-
getRenderTemplateData;
|
|
23
|
-
|
|
24
|
-
constructor(config, logger, fileSystem, getRenderTemplateData) {
|
|
25
|
-
this.config = config;
|
|
26
|
-
this.logger = logger;
|
|
27
|
-
this.fileSystem = fileSystem;
|
|
28
|
-
this.getRenderTemplateData = getRenderTemplateData;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
getTemplatePaths = ({ templates, modular }) => {
|
|
32
|
-
const baseTemplatesPath = resolve(__dirname, "../templates/base");
|
|
33
|
-
const defaultTemplatesPath = resolve(__dirname, "../templates/default");
|
|
34
|
-
const modularTemplatesPath = resolve(__dirname, "../templates/modular");
|
|
35
|
-
const originalTemplatesPath = modular ? modularTemplatesPath : defaultTemplatesPath;
|
|
36
|
-
const customTemplatesPath = templates ? resolve(process.cwd(), templates) : originalTemplatesPath;
|
|
37
|
-
|
|
38
|
-
return {
|
|
39
|
-
/** `templates/base` */
|
|
40
|
-
base: baseTemplatesPath,
|
|
41
|
-
/** `templates/default` */
|
|
42
|
-
default: defaultTemplatesPath,
|
|
43
|
-
/** `templates/modular` */
|
|
44
|
-
modular: modularTemplatesPath,
|
|
45
|
-
/** usage path if `--templates` option is not set */
|
|
46
|
-
original: originalTemplatesPath,
|
|
47
|
-
/** custom path to templates (`--templates`) */
|
|
48
|
-
custom: customTemplatesPath,
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
cropExtension = (path) =>
|
|
53
|
-
this.config.templateExtensions.reduce((path, ext) => (_.endsWith(path, ext) ? path.replace(ext, "") : path), path);
|
|
54
|
-
|
|
55
|
-
getTemplateFullPath = (path, fileName) => {
|
|
56
|
-
const raw = resolve(path, "./", this.cropExtension(fileName));
|
|
57
|
-
const pathVariants = this.config.templateExtensions.map((extension) => `${raw}${extension}`);
|
|
58
|
-
|
|
59
|
-
return pathVariants.find((variant) => !!this.fileSystem.pathIsExist(variant));
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
requireFnFromTemplate = (packageOrPath) => {
|
|
63
|
-
const isPath = _.startsWith(packageOrPath, "./") || _.startsWith(packageOrPath, "../");
|
|
64
|
-
|
|
65
|
-
if (isPath) {
|
|
66
|
-
return require(path.resolve(this.config.templates, packageOrPath));
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return require(packageOrPath);
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
getTemplate = ({ fileName, name, path }) => {
|
|
73
|
-
const { templatePaths } = this.config;
|
|
74
|
-
|
|
75
|
-
if (path) {
|
|
76
|
-
return this.fileSystem.getFileContent(path);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (!fileName) return "";
|
|
80
|
-
|
|
81
|
-
const customFullPath = this.getTemplateFullPath(templatePaths.custom, fileName);
|
|
82
|
-
let fileContent = customFullPath && this.fileSystem.getFileContent(customFullPath);
|
|
83
|
-
|
|
84
|
-
if (fileContent) {
|
|
85
|
-
this.logger.log(`"${_.lowerCase(name)}" template found in "${templatePaths.custom}"`);
|
|
86
|
-
return fileContent;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const baseFullPath = this.getTemplateFullPath(templatePaths.base, fileName);
|
|
90
|
-
|
|
91
|
-
if (baseFullPath) {
|
|
92
|
-
fileContent = this.fileSystem.getFileContent(baseFullPath);
|
|
93
|
-
} else {
|
|
94
|
-
this.logger.warn(
|
|
95
|
-
`"${_.lowerCase(name)}" template not found in "${templatePaths.custom}"`,
|
|
96
|
-
`\nCode generator will use the default template`,
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const originalFullPath = this.getTemplateFullPath(templatePaths.original, fileName);
|
|
101
|
-
|
|
102
|
-
if (originalFullPath) {
|
|
103
|
-
fileContent = this.fileSystem.getFileContent(originalFullPath);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return fileContent;
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
getTemplates = ({ templatePaths }) => {
|
|
110
|
-
this.logger.log(`try to read templates from directory "${templatePaths.custom}"`);
|
|
111
|
-
|
|
112
|
-
return _.reduce(
|
|
113
|
-
this.config.templateInfos,
|
|
114
|
-
(acc, { fileName, name }) => ({
|
|
115
|
-
...acc,
|
|
116
|
-
[name]: this.getTemplate({ fileName, name }),
|
|
117
|
-
}),
|
|
118
|
-
{},
|
|
119
|
-
);
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
findTemplateWithExt = (path) => {
|
|
123
|
-
const raw = this.cropExtension(path);
|
|
124
|
-
const pathVariants = this.config.templateExtensions.map((extension) => `${raw}${extension}`);
|
|
125
|
-
return pathVariants.find((variant) => this.fileSystem.pathIsExist(variant));
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
getTemplateContent = (path) => {
|
|
129
|
-
const foundTemplatePathKey = _.keys(this.config.templatePaths).find((key) => _.startsWith(path, `@${key}`));
|
|
130
|
-
|
|
131
|
-
const rawPath = resolve(
|
|
132
|
-
_.replace(path, `@${foundTemplatePathKey}`, this.config.templatePaths[foundTemplatePathKey]),
|
|
133
|
-
);
|
|
134
|
-
const fixedPath = this.findTemplateWithExt(rawPath);
|
|
135
|
-
|
|
136
|
-
if (fixedPath) {
|
|
137
|
-
return this.fileSystem.getFileContent(fixedPath);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const customPath = this.findTemplateWithExt(resolve(this.config.templatePaths.custom, path));
|
|
141
|
-
|
|
142
|
-
if (customPath) {
|
|
143
|
-
return this.fileSystem.getFileContent(customPath);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const originalPath = this.findTemplateWithExt(resolve(this.config.templatePaths.original, path));
|
|
147
|
-
|
|
148
|
-
if (originalPath) {
|
|
149
|
-
return this.fileSystem.getFileContent(originalPath);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return "";
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
renderTemplate = (template, configuration, options) => {
|
|
156
|
-
if (!template) return "";
|
|
157
|
-
|
|
158
|
-
return Eta.render(
|
|
159
|
-
template,
|
|
160
|
-
{
|
|
161
|
-
...this.getRenderTemplateData(),
|
|
162
|
-
...configuration,
|
|
163
|
-
},
|
|
164
|
-
{
|
|
165
|
-
async: false,
|
|
166
|
-
...(options || {}),
|
|
167
|
-
includeFile: (path, configuration, options) => {
|
|
168
|
-
return this.renderTemplate(this.getTemplateContent(path), configuration, options);
|
|
169
|
-
},
|
|
170
|
-
},
|
|
171
|
-
);
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
module.exports = {
|
|
176
|
-
Templates,
|
|
177
|
-
};
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
const ts = require("typescript");
|
|
2
|
-
|
|
3
|
-
function translate(fileName, content, options) {
|
|
4
|
-
const output = {};
|
|
5
|
-
const host = ts.createCompilerHost(options, true);
|
|
6
|
-
const fileNames = [fileName];
|
|
7
|
-
const originalSourceFileGet = host.getSourceFile.bind(host);
|
|
8
|
-
host.getSourceFile = (sourceFileName, languageVersion, onError, shouldCreateNewSourceFile) => {
|
|
9
|
-
if (sourceFileName !== fileName)
|
|
10
|
-
return originalSourceFileGet(sourceFileName, languageVersion, onError, shouldCreateNewSourceFile);
|
|
11
|
-
|
|
12
|
-
return ts.createSourceFile(sourceFileName, content, languageVersion, true, ts.ScriptKind.External);
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
host.writeFile = (fileName, contents) => {
|
|
16
|
-
output[fileName] = contents;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
ts.createProgram(fileNames, options, host).emit();
|
|
20
|
-
|
|
21
|
-
return output;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
module.exports = {
|
|
25
|
-
translate: (fileName, sourceTypeScript) => {
|
|
26
|
-
const translated = translate(fileName, sourceTypeScript, {
|
|
27
|
-
module: "ESNext",
|
|
28
|
-
noImplicitReturns: true,
|
|
29
|
-
alwaysStrict: true,
|
|
30
|
-
target: ts.ScriptTarget.ESNext,
|
|
31
|
-
declaration: true,
|
|
32
|
-
noImplicitAny: false,
|
|
33
|
-
sourceMap: false,
|
|
34
|
-
removeComments: false,
|
|
35
|
-
disableSizeLimit: true,
|
|
36
|
-
esModuleInterop: true,
|
|
37
|
-
emitDecoratorMetadata: true,
|
|
38
|
-
skipLibCheck: true,
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
const sourceFileName = fileName.replace(ts.Extension.Ts, ts.Extension.Js);
|
|
42
|
-
const declarationFileName = fileName.replace(ts.Extension.Ts, ts.Extension.Dts);
|
|
43
|
-
const sourceContent = translated[sourceFileName];
|
|
44
|
-
const tsImportRows = sourceTypeScript.split("\n").filter((line) => line.startsWith("import "));
|
|
45
|
-
const declarationContent = translated[declarationFileName]
|
|
46
|
-
.split("\n")
|
|
47
|
-
.map((line) => {
|
|
48
|
-
if (line.startsWith("import ")) {
|
|
49
|
-
return tsImportRows.shift();
|
|
50
|
-
}
|
|
51
|
-
return line;
|
|
52
|
-
})
|
|
53
|
-
.join("\n");
|
|
54
|
-
|
|
55
|
-
return {
|
|
56
|
-
sourceContent: sourceContent,
|
|
57
|
-
declarationContent: declarationContent,
|
|
58
|
-
};
|
|
59
|
-
},
|
|
60
|
-
};
|