diff options
-rw-r--r-- | lib/book.js | 6 | ||||
-rw-r--r-- | lib/fs/node.js | 2 | ||||
-rw-r--r-- | lib/output.js | 25 |
3 files changed, 29 insertions, 4 deletions
diff --git a/lib/book.js b/lib/book.js index 913df2f..fd20a94 100644 --- a/lib/book.js +++ b/lib/book.js @@ -13,6 +13,12 @@ var Page = require('./backbone/page'); var pathUtil = require('./utils/path'); var Promise = require('./utils/promise'); + +/* +The Book class is an interface for parsing books content. +It does not require to run on Node.js, isnce it only depends on the fs implementation +*/ + function Book(opts) { if (!(this instanceof Book)) return new Book(opts); diff --git a/lib/fs/node.js b/lib/fs/node.js index 28383cf..5994c2e 100644 --- a/lib/fs/node.js +++ b/lib/fs/node.js @@ -1,7 +1,7 @@ var _ = require('lodash'); var util = require('util'); var path = require('path'); -var fs = require('fs'); +var fs = require('graceful-fs'); var mkdirp = require('mkdirp'); var Promise = require('../utils/promise'); diff --git a/lib/output.js b/lib/output.js index 01610c6..2fb3938 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1,6 +1,8 @@ var _ = require('lodash'); -var fs = require('fs'); +var fs = require('graceful-fs'); +var mkdirp = require('mkdirp'); var Ignore = require('ignore'); +var path = require('path'); var Promise = require('./utils/promise'); var pathUtil = require('./utils/path'); @@ -29,16 +31,28 @@ function Output(book, type) { ])); } +// Return path to the root folder +Output.prototype.root = function(filename) { + return path.resolve(process.cwd(), this.book.config.get('output')); +}; // Resolve a file in the output directory Output.prototype.resolve = function(filename) { - return pathUtil.resolveInRoot.apply(null, [this.book.config.get('output')].concat(_.toArray(arguments))); + return pathUtil.resolveInRoot.apply(null, [this.root()].concat(_.toArray(arguments))); }; // Write a file/buffer to the output folder Output.prototype.writeFile = function(filename, buf) { filename = this.resolve(filename); - return Promise.nfcall(fs.writeFile, filename, buf); + var folder = path.dirname(filename); + + // Ensure fodler exists + return Promise.nfcall(mkdirp, folder) + + // Write the file + .then(function() { + return Promise.nfcall(fs.writeFile, filename, buf); + }); }; // Start the generation, for a parsed book @@ -57,6 +71,11 @@ Output.prototype.generate = function() { return that.plugins.load(plugins); }) + // Create the output folder + .then(function() { + return Promise.nfcall(mkdirp, that.root()); + }) + // Initialize the generation .then(function() { return that.generator.prepare(); |