config.js
5.42 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkConfigReturnType = void 0;
const tslib_1 = require("tslib");
const fs_1 = tslib_1.__importDefault(require("fs"));
const path_1 = tslib_1.__importDefault(require("path"));
const typescript_1 = tslib_1.__importDefault(require("typescript"));
const config_1 = require("../config");
const utils = tslib_1.__importStar(require("../utils"));
const base_1 = require("./base");
const EXPORT_DEFAULT_FUNCTION = 1;
const EXPORT_DEFAULT = 2;
const EXPORT = 3;
const globalCache = {};
class ConfigGenerator extends base_1.BaseGenerator {
buildParams(config) {
const { baseConfig } = this;
const fileList = config.fileList;
const cache = globalCache[baseConfig.id] = globalCache[baseConfig.id] || {};
if (!fileList.length)
return;
const importList = [];
const declarationList = [];
const moduleList = [];
fileList.forEach(f => {
const abUrl = path_1.default.resolve(config.dir, f);
// read from cache
if (!cache[abUrl] || config.file === abUrl) {
const skipLibCheck = !!baseConfig.tsConfig.skipLibCheck;
const { type, usePowerPartial } = checkConfigReturnType(abUrl);
// skip when not usePowerPartial and skipLibCheck in ts file
// because it maybe cause types error.
if (path_1.default.extname(f) !== '.js' && !usePowerPartial && !skipLibCheck)
return;
const { moduleName: sModuleName } = utils.getModuleObjByPath(f);
const moduleName = `Export${sModuleName}`;
const importContext = utils.getImportStr(config.dtsDir, abUrl, moduleName, type === EXPORT);
let tds = `type ${sModuleName} = `;
if (type === EXPORT_DEFAULT_FUNCTION) {
tds += `ReturnType<typeof ${moduleName}>;`;
}
else if (type === EXPORT_DEFAULT || type === EXPORT) {
tds += `typeof ${moduleName};`;
}
else {
return;
}
// cache the file
cache[abUrl] = {
import: importContext,
declaration: tds,
moduleName: sModuleName,
};
}
const cacheItem = cache[abUrl];
importList.push(cacheItem.import);
declarationList.push(cacheItem.declaration);
moduleList.push(cacheItem.moduleName);
});
return {
importList,
declarationList,
moduleList,
};
}
renderWithParams(config, params) {
const dist = path_1.default.resolve(config.dtsDir, 'index.d.ts');
if (!params)
return { dist };
if (!params.importList.length)
return { dist };
const { baseConfig } = this;
const { importList, declarationList, moduleList } = params;
const newConfigType = `New${config.interface}`;
return {
dist,
content: `import { ${config.interface} } from '${baseConfig.framework}';\n` +
`${importList.join('\n')}\n` +
`${declarationList.join('\n')}\n` +
`type ${newConfigType} = ${moduleList.join(' & ')};\n` +
`declare module '${baseConfig.framework}' {\n` +
` interface ${config.interface} extends ${newConfigType} { }\n` +
'}',
};
}
}
exports.default = ConfigGenerator;
ConfigGenerator.defaultConfig = {
// only need to parse config.default.ts or config.ts
pattern: 'config(.default|).(ts|js)',
interface: config_1.declMapping.config,
};
// check config return type.
function checkConfigReturnType(f) {
const result = utils.findExportNode(fs_1.default.readFileSync(f, 'utf-8'));
const resp = {
type: undefined,
usePowerPartial: false,
};
if (result.exportDefaultNode) {
const exportDefaultNode = result.exportDefaultNode;
if (typescript_1.default.isFunctionLike(exportDefaultNode)) {
if ((typescript_1.default.isFunctionDeclaration(exportDefaultNode) || typescript_1.default.isArrowFunction(exportDefaultNode)) && exportDefaultNode.body) {
exportDefaultNode.body.forEachChild(tNode => {
if (!resp.usePowerPartial && typescript_1.default.isVariableStatement(tNode)) {
// check wether use PowerPartial<EggAppInfo>
resp.usePowerPartial = !!tNode.declarationList.declarations.find(decl => {
let typeText = decl.type ? decl.type.getText() : undefined;
if (decl.initializer && typescript_1.default.isAsExpression(decl.initializer) && decl.initializer.type) {
typeText = decl.initializer.type.getText();
}
return !!(typeText && typeText.includes('PowerPartial') && typeText.includes('EggAppConfig'));
});
}
});
}
resp.type = EXPORT_DEFAULT_FUNCTION;
}
else {
resp.type = EXPORT_DEFAULT;
}
}
else if (result.exportNodeList.length) {
resp.type = EXPORT;
}
return resp;
}
exports.checkConfigReturnType = checkConfigReturnType;