swagger-typescript-api 10.0.2 → 10.0.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/README.md +45 -41
- package/index.d.ts +4 -0
- package/index.js +115 -115
- package/package.json +4 -1
- package/src/apiConfig.js +30 -30
- package/src/common.js +28 -28
- package/src/config.js +2 -0
- package/src/filePrefix.js +14 -14
- package/src/index.js +271 -271
- package/src/logger.js +68 -59
- package/src/modelNames.js +74 -78
- package/src/modelTypes.js +31 -31
- package/src/output.js +165 -165
- package/src/prettierOptions.js +23 -23
- package/src/render/utils/fmtToJSDocLine.js +10 -10
- package/src/render/utils/index.js +31 -31
- package/src/render/utils/templateRequire.js +17 -17
- package/src/routeNames.js +46 -46
- package/src/templates.js +155 -155
- package/src/translators/JavaScript.js +49 -60
- package/src/typeFormatters.js +121 -121
- package/src/utils/id.js +9 -9
- package/src/utils/random.js +14 -14
- package/src/utils/resolveName.js +94 -97
- package/templates/base/route-docs.ejs +1 -2
- package/templates/default/procedure-call.ejs +2 -2
- package/templates/modular/procedure-call.ejs +2 -2
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
const { classNameCase, formatDescription, internalCase } = require("../../common");
|
|
2
|
-
const { getComponentByRef } = require("../../components");
|
|
3
|
-
const { formatModelName } = require("../../modelNames");
|
|
4
|
-
const {
|
|
5
|
-
getInlineParseContent,
|
|
6
|
-
getParseContent,
|
|
7
|
-
parseSchema,
|
|
8
|
-
checkAndAddNull,
|
|
9
|
-
isNeedToAddNull,
|
|
10
|
-
} = require("../../schema");
|
|
11
|
-
const { formatters, inlineExtraFormatters } = require("../../typeFormatters");
|
|
12
|
-
const { NameResolver } = require("../../utils/resolveName");
|
|
13
|
-
|
|
14
|
-
module.exports = {
|
|
15
|
-
formatDescription,
|
|
16
|
-
internalCase,
|
|
17
|
-
classNameCase,
|
|
18
|
-
getInlineParseContent,
|
|
19
|
-
getParseContent,
|
|
20
|
-
getComponentByRef,
|
|
21
|
-
parseSchema,
|
|
22
|
-
formatters,
|
|
23
|
-
checkAndAddNull,
|
|
24
|
-
isNeedToAddNull,
|
|
25
|
-
inlineExtraFormatters,
|
|
26
|
-
formatModelName,
|
|
27
|
-
fmtToJSDocLine: require("./fmtToJSDocLine"),
|
|
28
|
-
NameResolver: NameResolver,
|
|
29
|
-
_: require("lodash"),
|
|
30
|
-
require: require("./templateRequire").templateRequire,
|
|
31
|
-
};
|
|
1
|
+
const { classNameCase, formatDescription, internalCase } = require("../../common");
|
|
2
|
+
const { getComponentByRef } = require("../../components");
|
|
3
|
+
const { formatModelName } = require("../../modelNames");
|
|
4
|
+
const {
|
|
5
|
+
getInlineParseContent,
|
|
6
|
+
getParseContent,
|
|
7
|
+
parseSchema,
|
|
8
|
+
checkAndAddNull,
|
|
9
|
+
isNeedToAddNull,
|
|
10
|
+
} = require("../../schema");
|
|
11
|
+
const { formatters, inlineExtraFormatters } = require("../../typeFormatters");
|
|
12
|
+
const { NameResolver } = require("../../utils/resolveName");
|
|
13
|
+
|
|
14
|
+
module.exports = {
|
|
15
|
+
formatDescription,
|
|
16
|
+
internalCase,
|
|
17
|
+
classNameCase,
|
|
18
|
+
getInlineParseContent,
|
|
19
|
+
getParseContent,
|
|
20
|
+
getComponentByRef,
|
|
21
|
+
parseSchema,
|
|
22
|
+
formatters,
|
|
23
|
+
checkAndAddNull,
|
|
24
|
+
isNeedToAddNull,
|
|
25
|
+
inlineExtraFormatters,
|
|
26
|
+
formatModelName,
|
|
27
|
+
fmtToJSDocLine: require("./fmtToJSDocLine"),
|
|
28
|
+
NameResolver: NameResolver,
|
|
29
|
+
_: require("lodash"),
|
|
30
|
+
require: require("./templateRequire").templateRequire,
|
|
31
|
+
};
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
const _ = require("lodash");
|
|
2
|
-
const path = require("path");
|
|
3
|
-
const { config } = require("../../config");
|
|
4
|
-
|
|
5
|
-
const templateRequire = (packageOrPath) => {
|
|
6
|
-
const isPath = _.startsWith(packageOrPath, "./") || _.startsWith(packageOrPath, "../");
|
|
7
|
-
|
|
8
|
-
if (isPath) {
|
|
9
|
-
return require(path.resolve(config.templates, packageOrPath));
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return require(packageOrPath);
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
module.exports = {
|
|
16
|
-
templateRequire,
|
|
17
|
-
};
|
|
1
|
+
const _ = require("lodash");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const { config } = require("../../config");
|
|
4
|
+
|
|
5
|
+
const templateRequire = (packageOrPath) => {
|
|
6
|
+
const isPath = _.startsWith(packageOrPath, "./") || _.startsWith(packageOrPath, "../");
|
|
7
|
+
|
|
8
|
+
if (isPath) {
|
|
9
|
+
return require(path.resolve(config.templates, packageOrPath));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return require(packageOrPath);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
module.exports = {
|
|
16
|
+
templateRequire,
|
|
17
|
+
};
|
package/src/routeNames.js
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
const { config } = require("./config");
|
|
2
|
-
const { logger } = require("./logger");
|
|
3
|
-
const { renderTemplate } = require("./templates");
|
|
4
|
-
|
|
5
|
-
const getRouteName = (routeInfo) => {
|
|
6
|
-
const { moduleName } = routeInfo;
|
|
7
|
-
const { routeNameDuplicatesMap, templatesToRender } = config;
|
|
8
|
-
const routeNameTemplate = templatesToRender.routeName;
|
|
9
|
-
|
|
10
|
-
const routeNameFromTemplate = renderTemplate(routeNameTemplate, {
|
|
11
|
-
routeInfo: routeInfo,
|
|
12
|
-
utils: require("./render/utils"),
|
|
13
|
-
config,
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
const routeName = config.hooks.onFormatRouteName(routeInfo, routeNameFromTemplate) || routeNameFromTemplate;
|
|
17
|
-
|
|
18
|
-
const duplicateIdentifier = `${moduleName}|${routeName}`;
|
|
19
|
-
|
|
20
|
-
if (routeNameDuplicatesMap.has(duplicateIdentifier)) {
|
|
21
|
-
routeNameDuplicatesMap.set(duplicateIdentifier, routeNameDuplicatesMap.get(duplicateIdentifier) + 1);
|
|
22
|
-
|
|
23
|
-
logger.warn(
|
|
24
|
-
`Module "${moduleName}" already has method "${routeName}()"`,
|
|
25
|
-
`\nThis method has been renamed to "${
|
|
26
|
-
routeName + routeNameDuplicatesMap.get(duplicateIdentifier)
|
|
27
|
-
}()" to solve conflict names.`,
|
|
28
|
-
);
|
|
29
|
-
} else {
|
|
30
|
-
routeNameDuplicatesMap.set(duplicateIdentifier, 1);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const duplicates = routeNameDuplicatesMap.get(duplicateIdentifier);
|
|
34
|
-
|
|
35
|
-
const routeNameInfo = {
|
|
36
|
-
usage: routeName + (duplicates > 1 ? duplicates : ""),
|
|
37
|
-
original: routeName,
|
|
38
|
-
duplicate: duplicates > 1,
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
return config.hooks.onCreateRouteName(routeNameInfo, routeInfo) || routeNameInfo;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
module.exports = {
|
|
45
|
-
getRouteName,
|
|
46
|
-
};
|
|
1
|
+
const { config } = require("./config");
|
|
2
|
+
const { logger } = require("./logger");
|
|
3
|
+
const { renderTemplate } = require("./templates");
|
|
4
|
+
|
|
5
|
+
const getRouteName = (routeInfo) => {
|
|
6
|
+
const { moduleName } = routeInfo;
|
|
7
|
+
const { routeNameDuplicatesMap, templatesToRender } = config;
|
|
8
|
+
const routeNameTemplate = templatesToRender.routeName;
|
|
9
|
+
|
|
10
|
+
const routeNameFromTemplate = renderTemplate(routeNameTemplate, {
|
|
11
|
+
routeInfo: routeInfo,
|
|
12
|
+
utils: require("./render/utils"),
|
|
13
|
+
config,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const routeName = config.hooks.onFormatRouteName(routeInfo, routeNameFromTemplate) || routeNameFromTemplate;
|
|
17
|
+
|
|
18
|
+
const duplicateIdentifier = `${moduleName}|${routeName}`;
|
|
19
|
+
|
|
20
|
+
if (routeNameDuplicatesMap.has(duplicateIdentifier)) {
|
|
21
|
+
routeNameDuplicatesMap.set(duplicateIdentifier, routeNameDuplicatesMap.get(duplicateIdentifier) + 1);
|
|
22
|
+
|
|
23
|
+
logger.warn(
|
|
24
|
+
`Module "${moduleName}" already has method "${routeName}()"`,
|
|
25
|
+
`\nThis method has been renamed to "${
|
|
26
|
+
routeName + routeNameDuplicatesMap.get(duplicateIdentifier)
|
|
27
|
+
}()" to solve conflict names.`,
|
|
28
|
+
);
|
|
29
|
+
} else {
|
|
30
|
+
routeNameDuplicatesMap.set(duplicateIdentifier, 1);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const duplicates = routeNameDuplicatesMap.get(duplicateIdentifier);
|
|
34
|
+
|
|
35
|
+
const routeNameInfo = {
|
|
36
|
+
usage: routeName + (duplicates > 1 ? duplicates : ""),
|
|
37
|
+
original: routeName,
|
|
38
|
+
duplicate: duplicates > 1,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
return config.hooks.onCreateRouteName(routeNameInfo, routeInfo) || routeNameInfo;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
module.exports = {
|
|
45
|
+
getRouteName,
|
|
46
|
+
};
|
package/src/templates.js
CHANGED
|
@@ -1,155 +1,155 @@
|
|
|
1
|
-
const _ = require("lodash");
|
|
2
|
-
const Eta = require("eta");
|
|
3
|
-
const { getFileContent, pathIsExist } = require("./files");
|
|
4
|
-
const { config } = require("./config");
|
|
5
|
-
const { resolve } = require("path");
|
|
6
|
-
const { logger } = require("./logger");
|
|
7
|
-
|
|
8
|
-
const TEMPLATE_EXTENSIONS = [".eta", ".ejs"];
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* name - project template name,
|
|
12
|
-
* fileName - template file name,
|
|
13
|
-
*/
|
|
14
|
-
const TEMPLATE_INFOS = [
|
|
15
|
-
{ name: "api", fileName: "api" },
|
|
16
|
-
{ name: "dataContracts", fileName: "data-contracts" },
|
|
17
|
-
{ name: "dataContractJsDoc", fileName: "data-contract-jsdoc" },
|
|
18
|
-
{ name: "interfaceDataContract", fileName: "interface-data-contract" },
|
|
19
|
-
{ name: "typeDataContract", fileName: "type-data-contract" },
|
|
20
|
-
{ name: "enumDataContract", fileName: "enum-data-contract" },
|
|
21
|
-
{ name: "objectFieldJsDoc", fileName: "object-field-jsdoc" },
|
|
22
|
-
{ name: "httpClient", fileName: "http-client" },
|
|
23
|
-
{ name: "routeTypes", fileName: "route-types" },
|
|
24
|
-
{ name: "routeName", fileName: "route-name" },
|
|
25
|
-
];
|
|
26
|
-
|
|
27
|
-
const getTemplatePaths = ({ templates, modular }) => {
|
|
28
|
-
const baseTemplatesPath = resolve(__dirname, "../templates/base");
|
|
29
|
-
const defaultTemplatesPath = resolve(__dirname, "../templates/default");
|
|
30
|
-
const modularTemplatesPath = resolve(__dirname, "../templates/modular");
|
|
31
|
-
const originalTemplatesPath = modular ? modularTemplatesPath : defaultTemplatesPath;
|
|
32
|
-
const customTemplatesPath = templates ? resolve(process.cwd(), templates) : originalTemplatesPath;
|
|
33
|
-
|
|
34
|
-
return {
|
|
35
|
-
/** `templates/base` */
|
|
36
|
-
base: baseTemplatesPath,
|
|
37
|
-
/** `templates/default` */
|
|
38
|
-
default: defaultTemplatesPath,
|
|
39
|
-
/** `templates/modular` */
|
|
40
|
-
modular: modularTemplatesPath,
|
|
41
|
-
/** usage path if `--templates` option is not set */
|
|
42
|
-
original: originalTemplatesPath,
|
|
43
|
-
/** custom path to templates (`--templates`) */
|
|
44
|
-
custom: customTemplatesPath,
|
|
45
|
-
};
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
const cropExtension = (path) =>
|
|
49
|
-
TEMPLATE_EXTENSIONS.reduce((path, ext) => (_.endsWith(path, ext) ? path.replace(ext, "") : path), path);
|
|
50
|
-
|
|
51
|
-
const getTemplateFullPath = (path, fileName) => {
|
|
52
|
-
const raw = resolve(path, "./", cropExtension(fileName));
|
|
53
|
-
const pathVariants = TEMPLATE_EXTENSIONS.map((extension) => `${raw}${extension}`);
|
|
54
|
-
|
|
55
|
-
return pathVariants.find((variant) => !!pathIsExist(variant));
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const getTemplate = ({ fileName, name, path }) => {
|
|
59
|
-
const { templatePaths } = config;
|
|
60
|
-
|
|
61
|
-
if (path) {
|
|
62
|
-
return getFileContent(path);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (!fileName) return "";
|
|
66
|
-
|
|
67
|
-
const customFullPath = getTemplateFullPath(templatePaths.custom, fileName);
|
|
68
|
-
let fileContent = customFullPath && getFileContent(customFullPath);
|
|
69
|
-
|
|
70
|
-
if (!fileContent) {
|
|
71
|
-
const baseFullPath = getTemplateFullPath(templatePaths.base, fileName);
|
|
72
|
-
|
|
73
|
-
if (baseFullPath) {
|
|
74
|
-
fileContent = getFileContent(baseFullPath);
|
|
75
|
-
} else {
|
|
76
|
-
logger.warn(
|
|
77
|
-
`${_.lowerCase(name)} template not found in ${customFullPath}`,
|
|
78
|
-
`\nCode generator will use the default template`,
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const originalFullPath = getTemplateFullPath(templatePaths.original, fileName);
|
|
83
|
-
|
|
84
|
-
if (originalFullPath) {
|
|
85
|
-
fileContent = getFileContent(originalFullPath);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return fileContent;
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
const getTemplates = ({ templatePaths }) => {
|
|
93
|
-
logger.log(`try to read templates from directory "${templatePaths.custom}"`);
|
|
94
|
-
|
|
95
|
-
const templatesMap = _.reduce(
|
|
96
|
-
TEMPLATE_INFOS,
|
|
97
|
-
(acc, { fileName, name }) => ({
|
|
98
|
-
...acc,
|
|
99
|
-
[name]: getTemplate({ fileName, name }),
|
|
100
|
-
}),
|
|
101
|
-
{},
|
|
102
|
-
);
|
|
103
|
-
|
|
104
|
-
return templatesMap;
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
const getTemplateContent = (path) => {
|
|
108
|
-
const foundTemplatePathKey = _.keys(config.templatePaths).find((key) => _.startsWith(path, `@${key}`));
|
|
109
|
-
|
|
110
|
-
const findPathWithExt = (path) => {
|
|
111
|
-
const raw = cropExtension(path);
|
|
112
|
-
const pathVariants = TEMPLATE_EXTENSIONS.map((extension) => `${raw}${extension}`);
|
|
113
|
-
return pathVariants.find((variant) => pathIsExist(variant));
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const rawPath = resolve(_.replace(path, `@${foundTemplatePathKey}`, config.templatePaths[foundTemplatePathKey]));
|
|
117
|
-
const fixedPath = findPathWithExt(rawPath);
|
|
118
|
-
|
|
119
|
-
if (fixedPath) {
|
|
120
|
-
return getFileContent(fixedPath);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const customPath = findPathWithExt(resolve(config.templatePaths.custom, path));
|
|
124
|
-
|
|
125
|
-
if (customPath) {
|
|
126
|
-
return getFileContent(customPath);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const originalPath = findPathWithExt(resolve(config.templatePaths.original, path));
|
|
130
|
-
|
|
131
|
-
if (originalPath) {
|
|
132
|
-
return getFileContent(originalPath);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return "";
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
const renderTemplate = (template, configuration, options) => {
|
|
139
|
-
if (!template) return "";
|
|
140
|
-
|
|
141
|
-
return Eta.render(template, configuration, {
|
|
142
|
-
async: false,
|
|
143
|
-
...(options || {}),
|
|
144
|
-
includeFile: (path, payload, options) => {
|
|
145
|
-
return renderTemplate(getTemplateContent(path), payload, options);
|
|
146
|
-
},
|
|
147
|
-
});
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
module.exports = {
|
|
151
|
-
getTemplate,
|
|
152
|
-
getTemplates,
|
|
153
|
-
getTemplatePaths,
|
|
154
|
-
renderTemplate,
|
|
155
|
-
};
|
|
1
|
+
const _ = require("lodash");
|
|
2
|
+
const Eta = require("eta");
|
|
3
|
+
const { getFileContent, pathIsExist } = require("./files");
|
|
4
|
+
const { config } = require("./config");
|
|
5
|
+
const { resolve } = require("path");
|
|
6
|
+
const { logger } = require("./logger");
|
|
7
|
+
|
|
8
|
+
const TEMPLATE_EXTENSIONS = [".eta", ".ejs"];
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* name - project template name,
|
|
12
|
+
* fileName - template file name,
|
|
13
|
+
*/
|
|
14
|
+
const TEMPLATE_INFOS = [
|
|
15
|
+
{ name: "api", fileName: "api" },
|
|
16
|
+
{ name: "dataContracts", fileName: "data-contracts" },
|
|
17
|
+
{ name: "dataContractJsDoc", fileName: "data-contract-jsdoc" },
|
|
18
|
+
{ name: "interfaceDataContract", fileName: "interface-data-contract" },
|
|
19
|
+
{ name: "typeDataContract", fileName: "type-data-contract" },
|
|
20
|
+
{ name: "enumDataContract", fileName: "enum-data-contract" },
|
|
21
|
+
{ name: "objectFieldJsDoc", fileName: "object-field-jsdoc" },
|
|
22
|
+
{ name: "httpClient", fileName: "http-client" },
|
|
23
|
+
{ name: "routeTypes", fileName: "route-types" },
|
|
24
|
+
{ name: "routeName", fileName: "route-name" },
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
const getTemplatePaths = ({ templates, modular }) => {
|
|
28
|
+
const baseTemplatesPath = resolve(__dirname, "../templates/base");
|
|
29
|
+
const defaultTemplatesPath = resolve(__dirname, "../templates/default");
|
|
30
|
+
const modularTemplatesPath = resolve(__dirname, "../templates/modular");
|
|
31
|
+
const originalTemplatesPath = modular ? modularTemplatesPath : defaultTemplatesPath;
|
|
32
|
+
const customTemplatesPath = templates ? resolve(process.cwd(), templates) : originalTemplatesPath;
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
/** `templates/base` */
|
|
36
|
+
base: baseTemplatesPath,
|
|
37
|
+
/** `templates/default` */
|
|
38
|
+
default: defaultTemplatesPath,
|
|
39
|
+
/** `templates/modular` */
|
|
40
|
+
modular: modularTemplatesPath,
|
|
41
|
+
/** usage path if `--templates` option is not set */
|
|
42
|
+
original: originalTemplatesPath,
|
|
43
|
+
/** custom path to templates (`--templates`) */
|
|
44
|
+
custom: customTemplatesPath,
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const cropExtension = (path) =>
|
|
49
|
+
TEMPLATE_EXTENSIONS.reduce((path, ext) => (_.endsWith(path, ext) ? path.replace(ext, "") : path), path);
|
|
50
|
+
|
|
51
|
+
const getTemplateFullPath = (path, fileName) => {
|
|
52
|
+
const raw = resolve(path, "./", cropExtension(fileName));
|
|
53
|
+
const pathVariants = TEMPLATE_EXTENSIONS.map((extension) => `${raw}${extension}`);
|
|
54
|
+
|
|
55
|
+
return pathVariants.find((variant) => !!pathIsExist(variant));
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const getTemplate = ({ fileName, name, path }) => {
|
|
59
|
+
const { templatePaths } = config;
|
|
60
|
+
|
|
61
|
+
if (path) {
|
|
62
|
+
return getFileContent(path);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!fileName) return "";
|
|
66
|
+
|
|
67
|
+
const customFullPath = getTemplateFullPath(templatePaths.custom, fileName);
|
|
68
|
+
let fileContent = customFullPath && getFileContent(customFullPath);
|
|
69
|
+
|
|
70
|
+
if (!fileContent) {
|
|
71
|
+
const baseFullPath = getTemplateFullPath(templatePaths.base, fileName);
|
|
72
|
+
|
|
73
|
+
if (baseFullPath) {
|
|
74
|
+
fileContent = getFileContent(baseFullPath);
|
|
75
|
+
} else {
|
|
76
|
+
logger.warn(
|
|
77
|
+
`${_.lowerCase(name)} template not found in ${customFullPath}`,
|
|
78
|
+
`\nCode generator will use the default template`,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const originalFullPath = getTemplateFullPath(templatePaths.original, fileName);
|
|
83
|
+
|
|
84
|
+
if (originalFullPath) {
|
|
85
|
+
fileContent = getFileContent(originalFullPath);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return fileContent;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const getTemplates = ({ templatePaths }) => {
|
|
93
|
+
logger.log(`try to read templates from directory "${templatePaths.custom}"`);
|
|
94
|
+
|
|
95
|
+
const templatesMap = _.reduce(
|
|
96
|
+
TEMPLATE_INFOS,
|
|
97
|
+
(acc, { fileName, name }) => ({
|
|
98
|
+
...acc,
|
|
99
|
+
[name]: getTemplate({ fileName, name }),
|
|
100
|
+
}),
|
|
101
|
+
{},
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
return templatesMap;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const getTemplateContent = (path) => {
|
|
108
|
+
const foundTemplatePathKey = _.keys(config.templatePaths).find((key) => _.startsWith(path, `@${key}`));
|
|
109
|
+
|
|
110
|
+
const findPathWithExt = (path) => {
|
|
111
|
+
const raw = cropExtension(path);
|
|
112
|
+
const pathVariants = TEMPLATE_EXTENSIONS.map((extension) => `${raw}${extension}`);
|
|
113
|
+
return pathVariants.find((variant) => pathIsExist(variant));
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const rawPath = resolve(_.replace(path, `@${foundTemplatePathKey}`, config.templatePaths[foundTemplatePathKey]));
|
|
117
|
+
const fixedPath = findPathWithExt(rawPath);
|
|
118
|
+
|
|
119
|
+
if (fixedPath) {
|
|
120
|
+
return getFileContent(fixedPath);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const customPath = findPathWithExt(resolve(config.templatePaths.custom, path));
|
|
124
|
+
|
|
125
|
+
if (customPath) {
|
|
126
|
+
return getFileContent(customPath);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const originalPath = findPathWithExt(resolve(config.templatePaths.original, path));
|
|
130
|
+
|
|
131
|
+
if (originalPath) {
|
|
132
|
+
return getFileContent(originalPath);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return "";
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const renderTemplate = (template, configuration, options) => {
|
|
139
|
+
if (!template) return "";
|
|
140
|
+
|
|
141
|
+
return Eta.render(template, configuration, {
|
|
142
|
+
async: false,
|
|
143
|
+
...(options || {}),
|
|
144
|
+
includeFile: (path, payload, options) => {
|
|
145
|
+
return renderTemplate(getTemplateContent(path), payload, options);
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
module.exports = {
|
|
151
|
+
getTemplate,
|
|
152
|
+
getTemplates,
|
|
153
|
+
getTemplatePaths,
|
|
154
|
+
renderTemplate,
|
|
155
|
+
};
|
|
@@ -1,60 +1,49 @@
|
|
|
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(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
const sourceFileName = fileName.replace(ts.Extension.Ts, ts.Extension.Js);
|
|
53
|
-
const declarationFileName = fileName.replace(ts.Extension.Ts, ts.Extension.Dts);
|
|
54
|
-
|
|
55
|
-
return {
|
|
56
|
-
sourceContent: translated[sourceFileName],
|
|
57
|
-
declarationContent: translated[declarationFileName],
|
|
58
|
-
};
|
|
59
|
-
},
|
|
60
|
-
};
|
|
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
|
+
|
|
44
|
+
return {
|
|
45
|
+
sourceContent: translated[sourceFileName],
|
|
46
|
+
declarationContent: translated[declarationFileName],
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
};
|