summaryrefslogtreecommitdiffstats
path: root/lib/output.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-02-11 09:43:36 +0100
committerSamy Pessé <samypesse@gmail.com>2016-02-11 09:43:36 +0100
commit44ad70320a0d0b907702c57e560c6a60373c9bca (patch)
treecf73f56e4cbb5de5d5d5feafb8651a66baff7ab1 /lib/output.js
parent2b8e1c0b11c9245886abf9aa0a990c4f5db0c213 (diff)
downloadgitbook-44ad70320a0d0b907702c57e560c6a60373c9bca.zip
gitbook-44ad70320a0d0b907702c57e560c6a60373c9bca.tar.gz
gitbook-44ad70320a0d0b907702c57e560c6a60373c9bca.tar.bz2
Correctly setup folder when outputting
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();