-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlayers.js
147 lines (126 loc) · 4.09 KB
/
layers.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
var fs = require('fs'),
_ = require('underscore'),
defaults = {
excludePrefix: "Base",
rootPath: "./layers/"
};
/**
* Loads the 'layers' of an application by loading their constituant
* files, instanciating them and adding them to the server object.
*
* @param server The server object (i.e. Express app).
* @param options An optional options object.
*/
module.exports = function(server, options) {
_.defaults(options, defaults);
var layers = options.layers || getLayers(options.rootPath),
excludePrefix = options.excludePrefix,
callbacks = options.callbacks || {};
layers.forEach(function(layer) {
server[layer] = loadComponents(options.rootPath + "/" + layer, layer);
});
/**
* Returns a list of layer names.
*
* Finds the directories in the root path supplied
* and returns their names as the layers this application
* makes use of.
*/
function getLayers(rootPath) {
var layers = getDirectoryFileListSync(rootPath),
directories = [],
hasControllers = false;
layers.forEach(function(layer) {
if (layer === "controllers") {
hasControllers = true;
} else {
var fullPathToLayerDirectory = rootPath + "/" + layer;
if (isDirectory(fullPathToLayerDirectory)) {
directories.push(layer.toLowerCase());
}
}
});
directories.sort();
// Ensure handlers are always loaded last.
if (hasControllers) {
directories.push("controllers");
}
return directories;
}
/**
* Recursively loads and instantiates javascript files within a path
* adding references to them under the current layer namespace on the
* supplied server object.
*/
function loadComponents(path, layer) {
var files = getDirectoryFileListSync(path),
components = {};
files.forEach(function(fileName) {
var fullPathToFile = path + "/" + fileName;
if (isDirectory(fullPathToFile)) {
loadComponents(fullPathToFile, layer);
} else if (fileName.indexOf(excludePrefix) != 0 && fileName.indexOf(".js") === fileName.length - 3) {
var name = getRequireName(fileName),
item = require(path + "/" + name);
if (typeof item === "function") {
item = item(server);
}
if (item) {
console.log("Attaching", layer + "." + getInstanceName(name));
components[getInstanceName(name)] = item;
}
}
});
if(callbacks[layer]){
callbacks[layer]();
}
return components;
}
/**
* Synchronously returns a file list from a directory.
*/
function getDirectoryFileListSync(directory) {
var files = [];
if (dirExistsSync(directory) && isDirectory(directory)) {
files = fs.readdirSync(directory);
}
return files;
}
/**
* Synchronously checks to see whether a directory exists or not.
*/
function dirExistsSync (directory) {
try {
fs.statSync(directory);
return true;
} catch (err) {
return false;
}
}
/**
* Synchronously checks whether the file at the path specified is a
* directory or not.
*/
function isDirectory(file) {
try {
var stats = fs.statSync(file);
return stats.isDirectory();
} catch (err) {
return false;
}
}
/**
* Synchronously returns the name of the file sans .js suffix.
*/
function getRequireName(file) {
return file.substr(0, file.lastIndexOf(".js"));
}
/**
* Synchronously returns the name of an instance.
*
* i.e. The name of the file with the first letter lower case.
*/
function getInstanceName(name) {
return name[0].toLowerCase() + name.substr(1);
}
};