-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.js
151 lines (139 loc) · 4.06 KB
/
loader.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
// isText, camelize, camelizeVar, and the enum routes are based on code and courtesy of Vitaly Tomilov
// https://github.com/vitaly-t/
// The includes
var n = {
fs: require('fs'),
path: require('path'),
isText: isText
};
/**
* isText - test to see if something is text in a more reliabe way
*
* @param {String} t Text to test
* @return {Boolean} true or false
*/
function isText(t) {
return t && typeof t === 'string' && /\S/.test(t);
}
/**
* camelize - Camelcase a string based on regex triggers
*
* @param {String} text The string to camelCase
* @return {String} The camelCased string
*/
function camelize(text) {
text = text.replace(/[\-_\s\.]+(.)?/g, function (match, chr) {
return chr ? chr.toUpperCase() : '';
});
return text.substr(0, 1).toLowerCase() + text.substr(1);
}
/**
* camelizeVar - A variable safe version of the camelize function.
* Any string that comes out of this function is a valid property name
* Useful for Object[text] declerations
*
* @param {String} text The string to camelCase safely
* @return {String} The camelCased, property safe string
*/
function camelizeVar(text) {
text = text.replace(/[^a-zA-Z0-9\$_\-\s\.]/g, '').replace(/^[0-9_\-\s\.]+/, '');
return camelize(text);
}
/**
* _enumJS - Pass over a folder creating a Tree Object
* The sub folders of the folder enumerated over are required into the Tree
* Non recursive
*
* @param {String} dir the path to go over
* @return {Object} the Tree Object containing the properties
*/
function _enumJS(dir) {
var tree = {};
if (!n.fs.existsSync(dir)) { return tree; }
n.fs.readdirSync(dir).forEach(function (file) {
var stat, name, fullPath = n.path.join(dir, file);
stat = n.fs.statSync(fullPath);
if (stat.isDirectory()) {
name = camelizeVar(file);
if (!name.length || name in tree) {
throw new Error("Empty or duplicate camelized folder name: " + fullPath);
}
tree[name] = require(fullPath);
} else if (n.path.extname(file).toLowerCase() === '.js') {
name = camelizeVar(file.replace(/\.[^/.]+$/, ''));
if (!name.length || name in tree) {
throw new Error("Empty or duplicate camelized file name: " + fullPath);
}
tree[name] = require(fullPath);
}
});
return tree;
}
/**
* _enumRoutes - A specific enum function to passover a specific pattern
* Designed only to work with folders containing express based routers
*
* @param {String} dir The directory to traverse
* @return {Array} An Array of object containing the route and routers
*/
function _enumRoutes(dir) {
var tree = [];
if (!n.fs.existsSync(dir)) { return tree; }
n.fs.readdirSync(dir).forEach(function (file) {
var stat, name, fullPath = n.path.join(dir, file);
stat = n.fs.statSync(fullPath);
if (stat.isDirectory()) {
//we want to push the object and it's routes
tree.push(require(fullPath));
} else if (n.path.extname(file).toLowerCase() === '.js') {
name = camelizeVar(file.replace(/\.[^/.]+$/, ''));
if (!name.length) {
throw new Error("Empty file name: " + fullPath);
}
tree.push(require(fullPath));
}
});
return tree;
}
/**
* modules - Pre flight
*
* @param {String} dir The directory to traverse
* @return {Object} The Tree Object
*/
function modules(dir) {
if (!n.isText(dir)) {
throw new TypeError("Parameter 'dir' must be a non-empty text string.");
}
return _enumJS(dir);
}
/**
* routes - Pre Flight
*
* @param {String} dir The directory to Traverse
* @return {Array} The Array of Objects
*/
function routes(dir) {
if (!n.isText(dir)) {
throw new TypeError("Parameter 'dir' must be a non-empty text string.");
}
return _enumRoutes(dir);
}
/**
* middleware - Pre Flight
*
* @param {String} dir The directory to Traverse
* @return {Array} The Array of Objects
*/
function middleware(dir) {
if (!n.isText(dir)) {
throw new TypeError("Parameter 'dir' must be a non-empty text string.");
}
return _enumRoutes(dir);
}
module.exports = {
modules,
routes,
middleware
};