-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (44 loc) · 1.66 KB
/
Copy pathindex.js
File metadata and controls
57 lines (44 loc) · 1.66 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
'use strict';
const BB = require('bluebird'),
glob = BB.promisify(require('glob')),
path = require('path'),
objectPath = require('object-path');
// starting path should have forward slashes - https://www.npmjs.com/package/glob#windows
module.exports = namespaceGenerator;
module.exports.sync = namespaceGeneratorSync;
function namespaceGenerator(startingPath, pattern, suffixToRemove){
pattern = pattern || '/**/*.js';
suffixToRemove = suffixToRemove || '.js';
return glob(startingPath + pattern)
.then(curryHandlePaths(startingPath, suffixToRemove))
.then(namespace => {
return namespace.ns
})
.catch(function(err) {
console.log(err);
// don't swallow errors
throw err;
});
}
function namespaceGeneratorSync(startingPath, pattern, suffixToRemove){
pattern = pattern || '/**/*.js';
suffixToRemove = suffixToRemove || '.js';
let paths = glob.sync(startingPath + pattern);
let namespace = curryHandlePaths(startingPath, suffixToRemove)(paths);
return namespace.ns;
}
function curryHandlePaths(startingPath, suffixToRemove) {
return paths => {
return paths.reduce((namespace, currentPath) => {
// Store the final object at namespace.ns
const dirname = path.dirname(currentPath.replace(startingPath, 'ns'));
const basename = path.basename(currentPath, suffixToRemove).replace(/[.]/g,'_');
objectPath.set(
namespace,
(dirname + '/' + basename).split('/').join('.'),
require(currentPath)
);
return namespace;
}, {})
};
}