diff options
Diffstat (limited to 'lib/fs')
-rw-r--r-- | lib/fs/index.js | 7 | ||||
-rw-r--r-- | lib/fs/node.js | 23 |
2 files changed, 20 insertions, 10 deletions
diff --git a/lib/fs/index.js b/lib/fs/index.js index 1961b07..7b8fc46 100644 --- a/lib/fs/index.js +++ b/lib/fs/index.js @@ -1,9 +1,10 @@ -var Q = require('q'); var _ = require('lodash'); var path = require('path'); var Buffer = require('buffer').Buffer; var destroy = require('destroy'); +var Promise = require('../utils/promise'); + /* A filesystem is an interface to read/write files GitBook can works with a virtual filesystem, for example in the browser. @@ -73,7 +74,7 @@ FS.prototype.listAllFiles = function(folder) { }); } }); - }, Q([])); + }, Promise([])); }); }; @@ -88,7 +89,7 @@ FS.prototype.readAsString = function(filename) { // Write a stream to a file and returns a promise FS.prototype.writeStream = function(filename, stream) { var bufs = []; - var d = Q.defer(); + var d = Promise.defer(); var cleanup = function() { destroy(stream); diff --git a/lib/fs/node.js b/lib/fs/node.js index 0c470d7..28383cf 100644 --- a/lib/fs/node.js +++ b/lib/fs/node.js @@ -1,9 +1,10 @@ -var Q = require('q'); var _ = require('lodash'); var util = require('util'); var path = require('path'); var fs = require('fs'); +var mkdirp = require('mkdirp'); +var Promise = require('../utils/promise'); var BaseFS = require('./'); function NodeFS() { @@ -13,7 +14,7 @@ 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) { - var d = Q.defer(); + var d = Promise.defer(); fs.exists(filename, function(exists) { d.resolve(exists); @@ -24,17 +25,25 @@ NodeFS.prototype.exists = function(filename) { // Read a file and returns a promise with the content as a buffer NodeFS.prototype.read = function(filename) { - return Q.nfcall(fs.readFile, filename); + return Promise.nfcall(fs.readFile, filename); }; // Write a file and returns a promise NodeFS.prototype.write = function(filename, buffer) { - return Q.nfcall(fs.writeFile, filename, buffer); + var folder = path.dirname(filename); + return Promise() + .then(function() { + if (!folder) return; + return Promise.nfcall(mkdirp, folder); + }) + .then(function() { + return Promise.nfcall(fs.writeFile, filename, buffer); + }); }; // List files in a directory NodeFS.prototype.readdir = function(folder) { - return Q.nfcall(fs.readdir, folder) + return Promise.nfcall(fs.readdir, folder) .then(function(files) { return _.chain(files) .map(function(file) { @@ -51,7 +60,7 @@ NodeFS.prototype.readdir = function(folder) { // Load a JSON/JS file NodeFS.prototype.loadAsObject = function(filename) { - return Q() + return Promise() .then(function() { var jsFile; @@ -64,7 +73,7 @@ NodeFS.prototype.loadAsObject = function(filename) { return require(jsFile); } catch(err) { - return Q.reject(err); + return Promise.reject(err); } }); }; |