-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetService.ts
112 lines (99 loc) · 2.97 KB
/
getService.ts
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
import { getExportProps } from './ast';
import { readFileSync } from 'fs';
import { join } from 'path';
import glob from 'glob';
import { IApi } from '@umijs/types';
import { winPath } from '@umijs/utils';
const firstCharToLowerCase = (str: string) =>
str.slice(0, 1).toLowerCase() + str.slice(1);
const firstCharToUpperCase = (str: string) =>
str.slice(0, 1).toUpperCase() + str.slice(1);
type getServiceArg = {
srcPath: string;
relativePath: string;
api: IApi;
superClassName: string;
ignore: RegExp[];
};
const getService = ({
srcPath,
relativePath,
api,
ignore,
superClassName,
}: getServiceArg) => {
const storeModuleTargetDir = join(srcPath, relativePath);
let files = glob.sync(join(storeModuleTargetDir, '**', '*.{j,t}s'));
files = files.filter(fileName => !/\.d\.ts$/.test(fileName));
const serviceList = files
.map(f => {
const fileCode = readFileSync(f, 'utf-8');
const res = getExportProps(fileCode);
const isNaturService =
(!!res && (res as any)?.superName === 'BaseService') ||
(res as any)?.superName === superClassName;
let isValidService = true;
if (isNaturService && !(res as any)?.name) {
api.logger.warn(
`文件:${f}中的类没有名字,不建议使用匿名类。此类会被忽略!`,
);
isValidService = false;
}
if (isNaturService && isValidService) {
return {
path: f,
serviceName: (res as { name: string }).name,
};
}
return null;
})
.filter(i => !!i)
.filter(i => {
const shouldIgnore = ignore.some(pattern => pattern.test(i!.serviceName));
return !shouldIgnore;
})
.map(f => ({
serviceName: firstCharToUpperCase(f!.serviceName),
path: winPath(f!.path),
}));
const serviceNames = serviceList.map(s => s.serviceName);
const importModulesCode = serviceNames
.reduce((res, serviceName, index) => {
res =
res +
`import ${serviceName} from '${serviceList[index].path.replace(
/\.(j|t)s$/,
'',
)}';\n`;
return res;
}, '')
.replace(/\n$/, '');
let exportServiceInstanceCode = serviceNames.reduce(
(res, serviceName, index) => {
res =
res +
`export const ${firstCharToLowerCase(
serviceName,
)} = new ${serviceName}();\n`;
return res;
},
'',
);
const exportClassModuleCode =
serviceNames.reduce((res, serviceName, index) => {
res = res + ` ${serviceName},\n`;
return res;
}, importModulesCode + '\n\nexport {\n') + '}';
const importFromExportClassCode =
serviceNames.reduce((res, serviceName, index) => {
res = res + ` ${serviceName},\n`;
return res;
}, 'import {\n') + '} from "./serviceObj";\n';
exportServiceInstanceCode =
importFromExportClassCode + '\n' + exportServiceInstanceCode;
return {
exportServiceInstanceCode,
exportClassModuleCode,
};
};
export default getService;