-
Notifications
You must be signed in to change notification settings - Fork 0
/
tshelper.js
80 lines (77 loc) · 1.96 KB
/
tshelper.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
const sequelizeModelContent = `
interface Model {
query(sql: string, options?: object): Promise;
transaction(autoCallback: (t: any) => Promise<any>): Promise;
}
`
function selfGenerator2(config) {
if (!config.modelMap) {
throw 'modelMap must not be undefined'
}
const modelMap = {};
Object.entries(config.modelMap).forEach(item => {
modelMap[item[0]] = {
name: item[1],
pathList: []
}
})
const modelList = config.fileList.map(item => ({
name: item.split('/')[0],
path: item
}));
console.log(modelMap, modelList)
for (let i = 0; i < modelList.length; ++i) {
const map = modelMap[modelList[i].name];
if (!map) {
throw 'modelName must not be null';
}
map.pathList.push(modelList[i].path);
}
const importContent = [];
const modelInterface = [];
const modelContent = [];
Object.entries(modelMap).forEach(([k, v]) => {
const item = {
name: v.name,
contentList: []
};
modelContent.push(item);
v.pathList.forEach(path => {
const name = path[0].toUpperCase() + path.replace('/', '').slice(1, -3);
importContent.push(`import Export${name} from '../../../${config.directory}/${path.slice(0, -3)}'`);
item.contentList.push(` ${path.split('/')[1].slice(0, -3)}: ReturnType<typeof Export${name}>;`);
})
modelInterface.push(` ${v.name}: T_${v.name} & Model;`)
})
const content = `
${importContent.join('\n')}
${sequelizeModelContent}
declare module 'egg' {
interface Context {
model: {
${modelInterface.join('\n')}
}
}
${modelContent.map(item => {
return ` interface T_${item.name} {
${item.contentList.join('\n')}
}`
}).join('\n')}
}
`;
return {
dist: config.dtsDir + '/index.d.ts',
content
}
}
module.exports = {
watchDirs: {
model: {
directory: 'app/model',
modelMap: {
weixinThirdPlatform: "weixinThirdPlatformModel"
},
generator: selfGenerator2
}
}
}