-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
112 lines (89 loc) · 2.8 KB
/
index.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
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
var path = require('path');
var componentsInfo, componentsDir;
function onReleaseStart() {
// 读取组件信息
componentsInfo = {};
componentsDir = (fis.config.env().get('component.dir') || 'components/').replace(/\/$/, '');
if (componentsDir.substr(-1) !== '/') {
componentsDir += '/';
}
var files = fis.project.getSourceByPatterns(componentsDir + '*/component.json', {
ignore: ['node_modules/**']
});
Object.keys(files).forEach(function(subpath) {
var file = files[subpath];
var cName = path.basename(path.dirname(subpath));
var json;
try {
json = JSON.parse(file.getContent());
} catch (e) {
fis.log.warn('unable to load component.json of component `%s`.', cName);
}
if (json.hook && typeof json.hook === 'string') {
try {
var root = fis.project.getProjectPath();
var hook = require(path.join(root, componentsDir, cName, json.hook));
hook && hook(json, files);
} catch (e) {
// don't care
}
}
json.name = json.name || cName;
componentsInfo[json.name] = json;
});
fis.emit('components:info', componentsInfo);
}
function _findResource(name, path) {
var extList = ['.js', '.jsx', '.coffee', '.css', '.sass', '.scss', '.less', '.html', '.tpl', '.vm'];
var info = fis.uri(name, path);
for (var i = 0, len = extList.length; i < len && !info.file; i++) {
info = fis.uri(name + extList[i], path);
}
return info;
}
function findResource(name, dir) {
var list = [
name,
path.join(name, 'index'),
path.join(name, path.basename(name))
];
var info;
while (list.length) {
name = list.shift();
info = _findResource(name, dir);
if (info && info.file) {
break;
}
}
return info;
}
function onFileLookUp(info, file) {
// 如果已经找到了,没必要再找了。
if (info.file || file && file.useShortPath === false) {
return;
}
var m = /^([0-9a-zA-Z\.\-_]+)(?:\/(.+))?$/.exec(info.rest);
if (m) {
var cName = m[1];
var subpath = m[2];
var config = componentsInfo[cName] || {};
var resolved;
if (subpath) {
resolved = findResource('/' + componentsDir+ cName + '/' + subpath, file ? file.dirname : fis.project.getProjectPath());
} else {
resolved = findResource('/' + componentsDir + cName + '/' + (config.main || 'index'), file ? file.dirname : fis.project.getProjectPath());
if (!resolved.file) {
resolved = findResource('/' + componentsDir + cName + '/' + cName, file ? file.dirname : fis.project.getProjectPath());
}
}
// 根据规则找到了。
if (resolved.file) {
info.id = resolved.file.getId();
info.file = resolved.file;
}
}
}
module.exports = function(fis, opts) {
fis.on('release:start', onReleaseStart);
fis.on('lookup:file', onFileLookUp);
};