-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage-builder.js
55 lines (45 loc) · 1.51 KB
/
package-builder.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
const glob = require('glob');
const packageDirectory = __dirname + '/src/';
const packageTree = {};
let treePointer;
try {
let files = glob.sync(
'**/*.js',
{
'cwd': packageDirectory
}
);
if (files) {
files.forEach((file) => {
const nodeStructure = file.split('/');
// Reset pointer to the tree base
treePointer = packageTree;
for (let index = 0; index < nodeStructure.length; index++) {
let segment = nodeStructure[index];
let lastNode = nodeStructure.length - 1 === index;
// Skip empty segments
if ('' === segment) {
continue;
}
if (!(segment in treePointer)) {
if (lastNode && segment.endsWith('.js')) {
let segmentName = segment.slice(0, -3).replace('.', '_');
treePointer[segmentName] = require(packageDirectory + file);
break;
}
treePointer[segment] = {};
}
treePointer = treePointer[segment];
}
});
}
}
catch (exception) {
console.log('Kanopi Pack cannot load its dependencies, please make sure NPM installed successfully.');
console.log('Following is the internal exception found while attempting to load the Kanopi Pack environment:');
console.log(exception);
}
if (packageTree && 0 === Object.keys(packageTree).length) {
console.log('Kanopi Pack is not installed properly, its internal file structure is not found or unable to load. Check the NPM installation and permissions.');
}
module.exports = packageTree;