-
Notifications
You must be signed in to change notification settings - Fork 5
/
tshelper.js
75 lines (71 loc) · 2.16 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
// 把query挂在model上
const sequelizeModelContent = `
interface Model {
query(sql: string, options?: any): function;
}
`
function selfGenerator(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}));
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: {
test: 'testModel', // 每增加一个model目录,就在这里新增一个对应的映射关系,key为目录名,value为前面config里设置的对应的delegate
test2: 'test2Model'
},
generator: selfGenerator
}
}
}