forked from acacode/swagger-typescript-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.js
132 lines (107 loc) · 3.65 KB
/
templates.js
1
2
3
4
5
6
7
8
9
10
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const _ = require("lodash");
const Eta = require("eta");
const { getFileContent, pathIsExist } = require("./files");
const { config } = require("./config");
const { resolve } = require("path");
const { logger } = require("./logger");
/**
* name - project template name,
* fileName - template file name,
*/
const TEMPLATE_INFOS = [
{ name: "api", fileName: "api.eta" },
{ name: "dataContracts", fileName: "data-contracts.eta" },
{ name: "httpClient", fileName: "http-client.eta" },
{ name: "routeTypes", fileName: "route-types.eta" },
{ name: "routeName", fileName: "route-name.eta" },
];
const getTemplatePaths = ({ templates, modular }) => {
const baseTemplatesPath = resolve(__dirname, "../templates/base");
const defaultTemplatesPath = resolve(__dirname, "../templates/default");
const modularTemplatesPath = resolve(__dirname, "../templates/modular");
const originalTemplatesPath = modular ? modularTemplatesPath : defaultTemplatesPath;
const customTemplatesPath = templates ? resolve(process.cwd(), templates) : originalTemplatesPath;
return {
/** `templates/base` */
base: baseTemplatesPath,
/** `templates/default` */
default: defaultTemplatesPath,
/** `templates/modular` */
modular: modularTemplatesPath,
/** usage path if `--templates` option is not set */
original: originalTemplatesPath,
/** custom path to templates (`--templates`) */
custom: customTemplatesPath,
};
};
const getTemplate = ({ fileName, name, path }) => {
const { templatePaths } = config;
if (path) {
return getFileContent(path);
}
if (!fileName) return "";
const customFullPath = resolve(templatePaths.custom, "./", fileName);
let fileContent = pathIsExist(customFullPath) && getFileContent(customFullPath);
if (!fileContent) {
const baseFullPath = resolve(templatePaths.base, "./", fileName);
const originalFullPath = resolve(templatePaths.original, "./", fileName);
if (pathIsExist(baseFullPath)) {
fileContent = getFileContent(baseFullPath);
} else {
logger.warn(
`${_.lowerCase(name)} template not found in ${customFullPath}`,
`\nCode generator will use the default template`,
);
}
if (pathIsExist(originalFullPath)) {
fileContent = getFileContent(originalFullPath);
}
}
return fileContent;
};
const getTemplates = ({ templatePaths }) => {
logger.log(`try to read templates from directory "${templatePaths.custom}"`);
const templatesMap = _.reduce(
TEMPLATE_INFOS,
(acc, { fileName, name }) => ({
...acc,
[name]: getTemplate({ fileName, name }),
}),
{},
);
return templatesMap;
};
const getTemplateContent = (path) => {
let fixedPath = _.endsWith(path, ".eta") ? path : `${path}.eta`;
_.keys(config.templatePaths).forEach((key) => {
if (_.startsWith(fixedPath, `@${key}`)) {
fixedPath = resolve(_.replace(fixedPath, `@${key}`, config.templatePaths[key]));
}
});
if (pathIsExist(fixedPath)) {
return getFileContent(fixedPath);
}
const customPath = resolve(config.templatePaths.custom, fixedPath);
if (pathIsExist(customPath)) {
return getFileContent(customPath);
}
const originalPath = resolve(config.templatePaths.original, fixedPath);
if (pathIsExist(originalPath)) {
return getFileContent(originalPath);
}
return "";
};
const renderTemplate = (template, configuration, options) => {
if (!template) return "";
return Eta.render(template, configuration, {
async: false,
...(options || {}),
includeFile: (path, payload) => renderTemplate(getTemplateContent(path), payload),
});
};
module.exports = {
getTemplate,
getTemplates,
getTemplatePaths,
renderTemplate,
};