-
Notifications
You must be signed in to change notification settings - Fork 48
/
moduleLoader.js
89 lines (83 loc) · 2.4 KB
/
moduleLoader.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
const fs = require("fs");
//
// Function to detect if given path was file or folder and load files(s)
//
function loadModulesFromFolder(folder) {
const returnArray = [];
try {
if (fs.statSync(folder).isDirectory()) {
const files = fs.readdirSync(folder);
for (let x = 0; x < files.length; x++) {
const temp = requireModule(`${folder}/${files[x]}`);
if (temp) {
console.log(`Successfully required module ${files[x]}`);
returnArray.push(temp);
}
}
} else {
const temp = requireModule(folder);
if (temp) {
console.log(`Successfully required module ${folder}`);
returnArray.push(temp);
}
}
} catch (e) {
console.log(`Failed to get modules from ${folder} with error ${e}`);
}
return returnArray;
}
//
// Function to check if file given is compatible to NodeFuzz.
//
// Each module is required to export at least fuzz() method. Also init-method is recommended if module needs some initializing.
// If module exports init() method then the init is executed when detected.
//
function requireModule(fileName) {
if (fs.statSync(fileName).isDirectory()) {
console.log(
`Will not handle directories in module-dir. Skipping ${fileName}`,
);
return null;
}
const temp = require(fileName);
if (temp.fuzz) {
if (temp.init) {
console.log(`Found property init() from module ${fileName}`);
temp.init();
}
return temp;
}
console.log(`Module ${fileName} has no exported property fuzz.`);
return null;
}
function loadModules() {
let returnArray;
if (
process.argv.indexOf("-m") !== -1 ||
process.argv.indexOf("--module") !== -1
) {
if (process.argv.indexOf("-m") !== -1)
returnArray = loadModulesFromFolder(
process.argv[process.argv.indexOf("-m") + 1],
);
else
returnArray = loadModulesFromFolder(
process.argv[process.argv.indexOf("--module") + 1],
);
} else {
console.log(
`No module folder given. Defaulting to ${config.defaultModuleDirectory} from config.js`,
);
returnArray = loadModulesFromFolder(config.defaultModuleDirectory);
}
if (returnArray.length === 0) {
console.log("No usable modules were detected.\nExiting...");
process.exit(1);
}
console.log(`We have ${returnArray.length} modules available.`);
return returnArray;
}
module.exports = {
loadModulesFromFolder,
loadModules,
};