forked from adamhalasz/diet-use
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (41 loc) · 1014 Bytes
/
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
// Use - Advanced Require v1
/*
Require Features:
- require file
- require module
- require directory
*/
var fs = require('fs');
module.exports = function(base_path, install){
this.process = function(Path){
var $path = base_path + '/' +Path;
try {
var stats = fs.lstatSync($path);
// REQUIRE DIRECTORY
if(stats.isDirectory()){
var files = fs.readdirSync($path);
for(var i = 0; i < files.length; i++){
var file = $path+'/'+files[i];
var file_stats = fs.lstatSync(file);
if(file_stats.isFile() && path.extname($path+'/'+files[i]) == '.js'){ require(file); }
}
}
// REQUIRE FILE
if(stats.isFile()){
require($path);
}
} catch (error) {
if(error.code == 'ENOENT'){
// REQUIRE MODULE
return require($path);
} else {
throw new Error(error);
return null;
}
}
}
if (install) function () {
// Install if missing
return this.process
} else return this.process
}