-
Notifications
You must be signed in to change notification settings - Fork 7
/
findProvidesModule.js
52 lines (42 loc) · 1.09 KB
/
findProvidesModule.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
/**
* Created by tdzl2003 on 10/3/16.
*/
const fs = require('fs');
const path = require('path');
const blacklist = {
"__tests__": true,
"react-packager": true,
"androidTest": true,
}
function findProvidesModule(dirs){
const ret = {};
const walk = (dir) => {
const stat = fs.statSync(dir);
if (stat.isDirectory()){
fs.readdirSync(dir).forEach(file => {
if (!blacklist[file]) {
walk(path.resolve(dir, file));
}
});
return;
} else if (stat.isFile()) {
const mName = /^(.*)\.js$/.exec(dir);
if (!mName) {
return;
}
const mPlatform = /^(.*)\.\w+$/.exec(mName[1]);
const module = mPlatform ? mPlatform[1] : mName[1];
const content = fs.readFileSync(dir, 'utf-8');
const m = /@providesModule ([\w\.]+)/.exec(content);
if (m) {
if (ret[m[1]] && ret[m[1]] !== module) {
console.warn(`Duplicated module ${m[1]}: \n${ret[m[1]]}\n${module}`);
}
ret[m[1]] = module;
}
}
};
dirs.forEach(walk);
return ret;
}
module.exports = findProvidesModule;