UNPKG

4.79 kBJavaScriptView Raw
1/**
2 * Copyright 2014 Skytap Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 **/
16
17var _ = require('underscore'),
18 Promise = require('bluebird'),
19 fs = require('fs'),
20 Wrench = require('wrench'),
21 Logger = require('./logger');
22
23/**
24 * Module for interacting with the filesystem.
25 **/
26var Filesystem = {
27
28 //////////////////////////////////////////////////////////////////////////
29 // Public methods ///////////////////////////////////////////////////////
30 ////////////////////////////////////////////////////////////////////////
31
32 /**
33 * Filesystem.recurseDirectory(path, filter) -> Object
34 * - path (String): Directory to recurse
35 * - filter (Function): Function to filter results
36 *
37 * Recursivly gets a list of all files in a directory.
38 **/
39 recurseDirectory : function (path, filter) {
40 var self = this;
41
42 return new Promise(function (resolve, reject) {
43 var results = [];
44
45 Wrench.readdirRecursive(path, function getAllFiles (error, files) {
46 results = self._getAllFiles(resolve, reject, results, filter, error, files);
47 });
48 });
49 },
50
51 /**
52 * Filesystem.requireFilesInDirectory(path, annotate) -> Object
53 * - path (String): Directory to recurse
54 * - annotate (Boolean): Whether to add the module path and name to the object
55 **/
56 requireFilesInDirectory : function (path, annotate) {
57 var self = this,
58 annotate = annotate || false;
59
60 return Promise.promisify(fs.readdir)(path)
61 .then(function (files) {
62 return self._requireAllFiles(files, path, annotate);
63 });
64 },
65
66 /**
67 * Filesystem.recurseDirectorySync(path, filter) -> Object
68 * - path (String): Directory to recurse
69 * - filter (Function): Function to filter results
70 *
71 * Synchronously, recursivly gets a list of all files in a directory.
72 **/
73 recurseDirectorySync : function (path, filter) {
74 var files = Wrench.readdirSyncRecursive(path);
75 return _.filter(files, filter);
76 },
77
78 //////////////////////////////////////////////////////////////////////////
79 // Psuedo-private methods ///////////////////////////////////////////////
80 ////////////////////////////////////////////////////////////////////////
81
82 /**
83 * Filesystem._getAllFiles(resolve, reject, results, filter, error, files)
84 * - resolve (Object)
85 * - reject (Object)
86 * - results (Array)
87 * - filter (Function): Function to filter results.
88 * - error (Object): Error object.
89 * - files (Array): Array of files.
90 **/
91 _getAllFiles : function (resolve, reject, results, filter, error, files) {
92 if (error) {
93 reject(error);
94 }
95
96 if (files === null) {
97 resolve(results);
98 return;
99 }
100
101 files = _.filter(files, filter);
102
103 return results.concat(files);
104 },
105
106 /**
107 * Filesystem._requireAllFiles(files, path, annotate) -> Object
108 * - files (Array): Array of files to require
109 * - path (String): Path to the file
110 * - annotate (Boolean): Whether to add the module path and name to the object
111 **/
112 _requireAllFiles : function (files, path, annotate) {
113 var self = this,
114 results = {};
115
116 files.forEach(function loadFile (file) {
117 self._requireFile(results, file, path, annotate);
118 });
119
120 return results;
121 },
122
123 /**
124 * Filesystem._requireFile(results, file, path, annotate) -> Object
125 * - results (Object): Hash of modules
126 * - file (String): File to load
127 * - path (String): Path to the file
128 * - annotate (Boolean): Whether to add the module path and name to the object
129 *
130 * Require a module.
131 **/
132 _requireFile : function (results, file, path, annotate) {
133 var start = Date.now(),
134 name = file.replace(/\.(js|coffee)/, ''),
135 Module = require(path + '/' + name);
136
137 if (annotate) {
138 Module.modulePath = path;
139 Module.moduleName = name;
140 }
141
142 results[name] = Module;
143
144 Logger.profile('Require module ' + name, start);
145 }
146};
147
148module.exports = Filesystem;
\No newline at end of file