Skip to content
This repository was archived by the owner on Feb 9, 2020. It is now read-only.

Commit 095dbb3

Browse files
committed
added a new way to load files? something like that..
example: const Player = pocketnode.player.use("Player"); const Player = pocketnode.player.use(["Player"]); const {Player} = pocketnode.player.all();
1 parent 09140ba commit 095dbb3

1 file changed

Lines changed: 84 additions & 3 deletions

File tree

src/pocketnode/utils/methods/Globals.js

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,95 @@ const Path = require("path");
33
/**
44
* Require files from PocketNode
55
* @param path {string}
6-
* @returns {*}
6+
* @return {*}
77
*/
88
global.pocketnode = function(path){
99
return require(Path.normalize(__dirname + "/../../" + path));
1010
};
1111

12+
const SFS = pocketnode("utils/SimpleFileSystem");
13+
14+
void function(){
15+
function walk(dir){
16+
SFS.walkDir(dir).forEach(path => {
17+
if(SFS.basename(SFS.dirname(path)) === "pocketnode" && SFS.isFile(path)) return; //omit Server, PocketNode
18+
19+
let parent;
20+
if(SFS.isDir(path)){
21+
parent = trace(path);
22+
}else if(SFS.isFile(path)){
23+
parent = trace(SFS.dirname(path));
24+
}
25+
26+
27+
if(SFS.isDir(path)){
28+
walk(path);
29+
}else if(SFS.isFile(path)){
30+
parent.files[Path.basename(path, ".js")] = path;
31+
}
32+
});
33+
}
34+
35+
function trace(path){
36+
path = path.split(Path.sep);
37+
path = path.slice(path.lastIndexOf("pocketnode")+1);
38+
39+
let parent = global.pocketnode;
40+
41+
path.forEach(part => {
42+
if(part === "") return;
43+
44+
if(typeof parent[part] === "undefined"){
45+
parent[part] = {
46+
files: {},
47+
use: function(file){
48+
if(typeof file === "string"){
49+
file = file.indexOf(".js") !== -1 ? file.slice(0, -3) : file;
50+
if(Object.keys(this.files).indexOf(file) !== -1){
51+
return require(this.files[file]);
52+
}else{
53+
throw new Error(`The requested resource, ${file}, was not found!`);
54+
}
55+
}else if(file instanceof Array){
56+
let files = [];
57+
58+
file.forEach(f => {
59+
if(Object.keys(this.files).indexOf(f) !== -1){
60+
files.push(require(this.files[f]));
61+
}else{
62+
files.push(undefined);
63+
}
64+
});
65+
66+
return files;
67+
}
68+
},
69+
all(){
70+
let all = {};
71+
for(let name in this.files){
72+
all[name] = require(this.files[name]);
73+
}
74+
return all;
75+
}
76+
};
77+
}
78+
79+
parent = parent[part];
80+
});
81+
82+
return parent;
83+
}
84+
85+
walk(__dirname + "/../../");
86+
}();
87+
88+
1289
/**
1390
* PHP-like rounding added onto the Math object
1491
* @param value {number}
1592
* @param precision {number}
1693
* @param mode {string}
17-
* @returns {Number}
94+
* @return {Number}
1895
*/
1996
global.Math.round_php = function(value, precision = 0, mode = "ROUND_HALF_UP"){
2097
let m, f, isHalf, sgn;
@@ -52,7 +129,7 @@ global.Math.round_php = function(value, precision = 0, mode = "ROUND_HALF_UP"){
52129
* CheckTypes([String, 12]); // throws TypeError
53130
*
54131
* @throws {TypeError}
55-
* @returns {boolean}
132+
* @return {boolean}
56133
*/
57134
global.CheckTypes = function(...args){
58135
if(args.length === 0) throw new TypeError("Expecting at least 1 Array. Example: [Object, myObjectVar]");
@@ -97,6 +174,10 @@ String.prototype.rtrim = function(char){
97174
return str.split("").reverse().join("");
98175
};
99176

177+
String.prototype.contains = function(str){
178+
return this.indexOf(str) !== -1;
179+
};
180+
100181
/**
101182
* @author Jonas Raoni Soares Silva
102183
* @link http://jsfromhell.com/string/wordwrap

0 commit comments

Comments
 (0)