summaryrefslogtreecommitdiffstats
path: root/lib/fs/node.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fs/node.js')
-rw-r--r--lib/fs/node.js66
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/fs/node.js b/lib/fs/node.js
new file mode 100644
index 0000000..fc2517e
--- /dev/null
+++ b/lib/fs/node.js
@@ -0,0 +1,66 @@
+var _ = require('lodash');
+var util = require('util');
+var path = require('path');
+
+var fs = require('../utils/fs');
+var Promise = require('../utils/promise');
+var BaseFS = require('./');
+
+function NodeFS() {
+ BaseFS.call(this);
+}
+util.inherits(NodeFS, BaseFS);
+
+// Check if a file exists, run a Promise(true) if that's the case, Promise(false) otherwise
+NodeFS.prototype.exists = function(filename) {
+ return fs.exists(filename);
+};
+
+// Read a file and returns a promise with the content as a buffer
+NodeFS.prototype.read = function(filename) {
+ return fs.readFile(filename);
+};
+
+// Read stat infos about a file
+NodeFS.prototype.stat = function(filename) {
+ return fs.stat(filename);
+};
+
+// List files in a directory
+NodeFS.prototype.readdir = function(folder) {
+ return fs.readdir(folder)
+ .then(function(files) {
+ return _.chain(files)
+ .map(function(file) {
+ if (file == '.' || file == '..') return;
+
+ var stat = fs.statSync(path.join(folder, file));
+ if (stat.isDirectory()) file = file + path.sep;
+ return file;
+ })
+ .compact()
+ .value();
+ });
+};
+
+// Load a JSON/JS file
+NodeFS.prototype.loadAsObject = function(filename) {
+ return Promise()
+ .then(function() {
+ var jsFile;
+
+ try {
+ jsFile = require.resolve(filename);
+
+ // Invalidate node.js cache for livreloading
+ delete require.cache[jsFile];
+
+ return require(jsFile);
+ }
+ catch(err) {
+ return Promise.reject(err);
+ }
+ });
+};
+
+module.exports = NodeFS;