UNPKG

1.07 kBJavaScriptView Raw
1//
2// # Paths
3//
4// Application path helper. Makes sure vital paths exists and provides a
5// helper function for getting at those paths.
6//
7// * **root**, root directory.
8// * **callback**, called when done.
9//
10var fs = require('fs');
11var path = require('path');
12var Paths = function (root, callback) {
13 this.paths = {};
14 var self = this;
15 var expected = ['controllers', 'views', 'models', 'helpers', 'config', 'assets'];
16 function check(dir, cb) {
17 if (typeof dir !== 'string') {
18 cb();
19 return;
20 }
21
22 var target = path.join(root, dir);
23 fs.stat(target, function (err, stat) {
24 if (err) {
25 cb(err);
26 return;
27 }
28 else {
29 self.paths[dir] = target;
30 var next = expected.shift();
31 check(next, cb);
32 }
33 });
34 }
35
36 check(expected.shift(), callback);
37};
38
39//
40// ## Get path
41//
42// Get path for specified directory.
43//
44// * **name**, name of directory.
45//
46// **Returns** path or false.
47//
48Paths.prototype.get = function (name) {
49 return this.paths[name] || false;
50};
51
52module.exports = Paths;
53