-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
154 lines (119 loc) · 3.89 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
145
146
147
148
149
150
151
152
153
/**
* Module dependencies
*/
var helpBuildDictionary = require('./lib/help-build-dictionary');
var helpIncludeAllSync = require('./lib/help-include-all-sync');
/**
* includeAll
*
* An easy way to include all node.js modules within a directory.
*
* > Used by the module loader in Sails core.
*/
/**
* Build a dictionary of named modules
* (default usage-- see options in `README.md`)
*
* @param {Dictionary} options
*/
module.exports = function includeAllSync(options) {
// This is the original, pre-v1 `include-all` usage.
return helpIncludeAllSync(options);
};
//////////////////////////////////////////////////////////////////////////////
// The four methods below are all originally from sails-build-dictionary.
// They are asynchronous, and besides defaulting certain options, they do a
// handful of extra things. So it's more than just options getting defaulted!
//////////////////////////////////////////////////////////////////////////////
/**
* Build a dictionary of named modules
* (responds with an error if the container cannot be loaded)
*
* WARNING: THIS PARTICULAR CONVENIENCE METHOD WILL LIKELY BE DEPRECATED.
* (it's not actually being used anywhere in core)
*
* @async
* @param {Dictionary} options
* @param {Function} cb
*/
module.exports.required = function(options, cb) {
return helpBuildDictionary(options, cb);
};
/**
* Build a dictionary of named modules
* (fails silently-- returns {} if the container cannot be loaded)
*
* @async
* @param {Dictionary} options
* @param {Function} cb
*/
module.exports.optional = function(options, cb) {
options.optional = true;
return helpBuildDictionary(options, cb);
};
/**
* Build a dictionary indicating whether the matched modules exist
* (fails silently-- returns {} if the container cannot be loaded)
*
* @async
* @param {Dictionary} options
* @param {Function} cb
*/
module.exports.exists = function(options, cb) {
options.optional = true;
options.dontLoad = true;
return helpBuildDictionary(options, cb);
};
/**
* Build a single module dictionary by extending {} with the contents of each module
* (fail silently-- returns {} if the container cannot be loaded)
*
* @async
* @param {Dictionary} options
* @param {Function} cb
*/
module.exports.aggregate = function(options, cb) {
options.aggregate = true;
options.optional = true;
return helpBuildDictionary(options, cb);
};
//////////////////////////////////////////////////////////////////////////////
// Finally, this last method is sort of like a recursive `ls`.
// Similarly, it's more or less just a synchronous version of `.exists()`,
// but with a few more specific hard-coded overrides.
//////////////////////////////////////////////////////////////////////////////
/**
* Build a flat dictionary of the matched modules, where the keys are the
* paths, and the values are `true` (fails silently-- returns {} if the
* container cannot be loaded)
*
* @param {Dictionary} options
* @returns {Dictionary}
*/
module.exports.scanSync = function(options) {
// Higher level overrides.
options.flatten = true;
options.keepDirectoryPath = true;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Note: the combination of the following overrides make this
// more or less equivalent to `.exists()`-- but synchronous.
// Not all options work... instead, this is really designed
// for a slightly different kind of use case-- where you want
// to recursively, synchronously stat modules, rather than
// include them.
options.optional = true;
options.dontLoad = true;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Finally, some defaults:
if (!options.excludeDirs) {
options.excludeDirs = /^\.(git|svn)$/;
}
if (!options.depth) {
options.depth = 10;
}
if (!options.filter) {
options.filter = /(.+)$/;
}
// Now call the low-lvl helper.
return helpIncludeAllSync(options);
};