-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
144 lines (120 loc) · 4.34 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
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
const globule = require('globule');
const path = require('path');
class AutomountPlugin {
constructor(
{ syncDataMatch, asyncDataMatch, methodGetter, tplUrlMap = {} } = {}
) {
const defaultUrlGetter = filePath => {
return '/' + filePath + '.html';
};
const defaultMethodGetter = () => {
return '';
};
this.syncDataMatch = syncDataMatch || defaultUrlGetter;
this.asyncDataMatch = asyncDataMatch || defaultUrlGetter;
this.methodGetter = methodGetter || defaultMethodGetter;
this.tplUrlMap = tplUrlMap;
}
init({ service, getter }) {
const createWatcher = service('watcher.create');
const registerRouterNamespace = service(
'server.registerRouterNamespace'
);
const { extension, viewRoot, asyncData } = getter('server');
this.extension = {
tpl: extension,
data: 'json'
};
this.initUrlTplMap({ tplUrlMap: this.tplUrlMap });
this.initRouterUpdater({
viewRoot,
asyncData,
registerRouterNamespace,
createWatcher
});
}
initRouterUpdater({
viewRoot,
asyncData,
registerRouterNamespace,
createWatcher
}) {
const syncRouterWatcher = createWatcher(
this.formatPattern(viewRoot, true)
);
const asyncRouterWatcher = createWatcher(this.formatPattern(asyncData));
const SYNCROUTER_NAMESPACE = 'automountSyncRouters';
const ASYNCROUTER_NAMESPACE = 'automountAsyncRouters';
const getSyncRouters = () => this.initRouters(viewRoot, true);
const getAsyncRouters = () => this.initRouters(asyncData, false);
const updateSyncRouters = () =>
registerRouterNamespace(SYNCROUTER_NAMESPACE, getSyncRouters());
const updateAsyncRouters = () =>
registerRouterNamespace(ASYNCROUTER_NAMESPACE, getAsyncRouters());
updateSyncRouters();
updateAsyncRouters();
syncRouterWatcher.on('add', updateSyncRouters);
syncRouterWatcher.on('unlink', updateSyncRouters);
asyncRouterWatcher.on('add', updateAsyncRouters);
asyncRouterWatcher.on('unlink', updateAsyncRouters);
}
initUrlTplMap({ tplUrlMap }) {
this.tplUrlMap = Object.keys(tplUrlMap).reduce((prev, url) => {
const tpl = tplUrlMap[url] || '';
const tplName = this.removeExtense(tpl, true);
return Object.assign(prev, {
[tplName]: (prev[tplName] || []).concat([url])
});
}, {});
}
removeExtense(template = '', sync = false) {
const reg = new RegExp('.' + this.getExtense(sync) + '$', 'ig');
return template.replace(reg, '');
}
transformSep(filePath) {
return filePath.replace(/\\/g, '/');
}
transformUnderline(filePath) {
return filePath.replace(/_\//g, ':');
}
getExtense(sync) {
const { tpl, data } = this.extension;
const extense = sync ? tpl : data;
return extense;
}
formatPattern(base, sync = false) {
const pattern = path.join(base, '**/**.' + this.getExtense(sync));
return pattern;
}
initRouters(base, sync) {
const formatPattern = this.formatPattern.bind(this);
const files = globule.find(formatPattern(base, sync));
const methodGetter = this.methodGetter;
const urlMatch = sync
? filePath => {
let url = this.tplUrlMap[filePath];
if (url) {
return url;
}
return this.syncDataMatch(filePath);
}
: this.asyncDataMatch;
const routers = files.reduce((prev, file) => {
const filePath = this.transformSep(
this.removeExtense(path.relative(base, file), sync)
);
let urls = urlMatch(filePath);
(Array.isArray(urls) ? urls : [urls]).forEach(url => {
prev.push({
method: methodGetter(filePath),
url: this.transformUnderline(url),
sync: sync,
filePath: filePath
});
});
return prev;
}, []);
return routers;
}
}
exports = module.exports = AutomountPlugin;