This repository was archived by the owner on Jul 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (56 loc) · 1.57 KB
/
index.js
File metadata and controls
75 lines (56 loc) · 1.57 KB
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
'use strict';
const Namespace = require('./src/namespace');
const readdirRecursiveSync = require('./src/readdir');
const debug = require('debug')('express-handler-loader:');
const namespaceStore = {};
function excludePathname(pathnameList, exclude = /\s/) {
return pathnameList.filter(pathname => !exclude.test(pathname));
}
/**
*
*
* @param {string} options
* @param {string} options.pathname
* @param {RegExp} options.exclude
*/
function getRequireList({ pathname, exclude }) {
const pathnameList = readdirRecursiveSync(pathname);
return excludePathname(pathnameList, exclude);
};
/**
*
*
* @param {string} namespace
* @param {string} [options]
* @param {string} options.pathname
* @param {RegExp} options.exclude
*/
const $ = module.exports = function requireHanlder(namespace, options) {
if (typeof namespace !== 'string') {
throw new Error('A string excepted by argument 0.');
}
if (options) {
if (!options.pathname) {
throw new Error('A string property pathname required by argument 1.');
}
if (namespaceStore[namespace]) {
throw new Error('This namespace is exsited.');
}
const pathnameList = getRequireList(options);
debug('Construct new namespace %o', namespace);
return namespaceStore[namespace] = new Namespace(pathnameList);
}
if (!namespaceStore[namespace]) {
throw new Error('This namespace is not exsited.');
}
return namespaceStore[namespace];
};
/**
*
*
* @param {string} namespace
*/
$.remove = function destroyNamespace(namespace) {
return delete namespaceStore[namespace];
};
$.resolve = getRequireList;