-
Notifications
You must be signed in to change notification settings - Fork 568
/
fileDetector.ts
204 lines (185 loc) · 5.3 KB
/
fileDetector.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {
IFileDetector,
IMidwayContainer,
IObjectDefinition,
} from '../interface';
import { Types } from '../util/types';
import { run } from '@midwayjs/glob';
import { MidwayDuplicateClassNameError } from '../error';
import { DEFAULT_PATTERN, IGNORE_PATTERN } from '../constants';
import { getProviderName } from '../decorator';
import { loadModule } from '../util';
export abstract class AbstractFileDetector<T> implements IFileDetector {
options: T;
extraDetectorOptions: T;
constructor(options?: T) {
this.options = options;
this.extraDetectorOptions = {} as T;
}
abstract run(container: IMidwayContainer): void | Promise<void>;
setExtraDetectorOptions(detectorOptions: T) {
this.extraDetectorOptions = detectorOptions;
}
}
const DEFAULT_GLOB_PATTERN = ['**/**.tsx'].concat(DEFAULT_PATTERN);
const DEFAULT_IGNORE_PATTERN = [
'**/logs/**',
'**/run/**',
'**/public/**',
'**/app/view/**',
'**/app/views/**',
'**/app/extend/**',
'**/node_modules/**',
'**/**.test.ts',
'**/**.test.js',
'**/__test__/**',
].concat(IGNORE_PATTERN);
/**
* CommonJS module loader
*/
export class CommonJSFileDetector extends AbstractFileDetector<{
loadDir?: string | string[];
pattern?: string | string[];
ignore?: string | string[];
namespace?: string;
conflictCheck?: boolean;
}> {
private duplicateModuleCheckSet = new Map();
run(container) {
if (this.getType() === 'commonjs') {
return this.loadSync(container);
} else {
return this.loadAsync(container);
}
}
loadSync(container) {
this.options = this.options || {};
const loadDirs = [].concat(
this.options.loadDir ?? container.get('baseDir')
);
for (const dir of loadDirs) {
const fileResults = run(
DEFAULT_GLOB_PATTERN.concat(this.options.pattern || []).concat(
this.extraDetectorOptions.pattern || []
),
{
cwd: dir,
ignore: DEFAULT_IGNORE_PATTERN.concat(
this.options.ignore || []
).concat(this.extraDetectorOptions.ignore || []),
}
);
// 检查重复模块
const checkDuplicatedHandler = (module, options?: IObjectDefinition) => {
if (
(this.options.conflictCheck ||
this.extraDetectorOptions.conflictCheck) &&
Types.isClass(module)
) {
const name = getProviderName(module);
if (name) {
if (this.duplicateModuleCheckSet.has(name)) {
throw new MidwayDuplicateClassNameError(
name,
options.srcPath,
this.duplicateModuleCheckSet.get(name)
);
} else {
this.duplicateModuleCheckSet.set(name, options.srcPath);
}
}
}
};
for (const file of fileResults) {
const exports = require(file);
// add module to set
container.bindClass(exports, {
namespace: this.options.namespace,
srcPath: file,
createFrom: 'file',
bindHook: checkDuplicatedHandler,
});
}
}
// check end
this.duplicateModuleCheckSet.clear();
}
async loadAsync(container) {
this.options = this.options || {};
const loadDirs = [].concat(
this.options.loadDir ?? container.get('baseDir')
);
for (const dir of loadDirs) {
const fileResults = run(
DEFAULT_GLOB_PATTERN.concat(this.options.pattern || []).concat(
this.extraDetectorOptions.pattern || []
),
{
cwd: dir,
ignore: DEFAULT_IGNORE_PATTERN.concat(
this.options.ignore || []
).concat(this.extraDetectorOptions.ignore || []),
}
);
// 检查重复模块
const checkDuplicatedHandler = (module, options?: IObjectDefinition) => {
if (
(this.options.conflictCheck ||
this.extraDetectorOptions.conflictCheck) &&
Types.isClass(module)
) {
const name = getProviderName(module);
if (name) {
if (this.duplicateModuleCheckSet.has(name)) {
throw new MidwayDuplicateClassNameError(
name,
options.srcPath,
this.duplicateModuleCheckSet.get(name)
);
} else {
this.duplicateModuleCheckSet.set(name, options.srcPath);
}
}
}
};
for (const file of fileResults) {
const exports = await loadModule(file, {
loadMode: 'esm',
});
// add module to set
container.bindClass(exports, {
namespace: this.options.namespace,
srcPath: file,
createFrom: 'file',
bindHook: checkDuplicatedHandler,
});
}
}
// check end
this.duplicateModuleCheckSet.clear();
}
getType(): 'commonjs' | 'module' {
return 'commonjs';
}
}
/**
* ES module loader
*/
export class ESModuleFileDetector extends CommonJSFileDetector {
getType(): 'commonjs' | 'module' {
return 'module';
}
}
export class CustomModuleDetector extends AbstractFileDetector<{
modules?: any[];
namespace?: string;
}> {
async run(container) {
for (const module of this.options.modules) {
container.bindClass(module, {
namespace: this.options.namespace,
createFrom: 'module',
});
}
}
}