/
di.js
69 lines (60 loc) · 2.13 KB
/
di.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
/*
* Copyright (c) 2015-2016 PointSource, LLC.
* MIT Licensed
*/
//Dependency injection utils
var fs = require('fs'),
path = require('path');
//Checks if last parameter is named callback
exports.hasCallback = function(fn) {
var funStr = fn.toString();
var params = funStr.slice(funStr.indexOf('(') + 1, funStr.indexOf(')')).match(/([^\s,]+)/g);
return params !== null && params.indexOf('callback') > -1;
};
/*
* Return an array of the argument names for a function
* If the function has no arguments, [] is returned.
*/
exports.getParamNames = function(fn) {
var funStr = fn.toString();
var toReturn = funStr.slice(funStr.indexOf('(') + 1, funStr.indexOf(')')).match(/([^\s,]+)/g);
//strip the callback
if (toReturn === null) {
return [];
}
if (toReturn.length > 0 && toReturn[toReturn.length - 1] === 'callback') {
toReturn = toReturn.slice(0, toReturn.length - 1);
}
return toReturn;
};
var MAX_LOAD_DEPTH = 1; //don't recurse below one subdir when looking for services/consumers
/*
* Use this to locate modules in the given directory.
* For each js file found, callback is called with two parameters callback(dir, file)
* where dir is the current directory and file is the file name.
* depth is used to keep track of how deep we've recursed.
* We won't recurse beyond MAX_LOAD_DEPTH
*/
function iterateOverJsFiles(dir, callback, depth) {
depth = depth || 0;
if (depth > MAX_LOAD_DEPTH) {
return;
}
//logger.debug('Looking for files in %s at depth %s', dir, depth);
if (fs.existsSync(dir)) {
var files = fs.readdirSync(dir);
files.forEach(function (file) {
if (path.extname(file) === '.js') {
return callback(dir, file);
} else {
var subDir = path.resolve(dir, file);
if (fs.lstatSync(subDir).isDirectory()) {
iterateOverJsFiles(subDir, callback, depth + 1);
}
}
});
} else { // jshint ignore:line
//logger.warn('%s is not a valid directory', dir);
}
}
exports.iterateOverJsFiles = iterateOverJsFiles;