summaryrefslogtreecommitdiffstats
path: root/lib/output.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/output.js')
-rw-r--r--lib/output.js25
1 files changed, 22 insertions, 3 deletions
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();