Output folder is an option for FolderOutput

This commit is contained in:
Samy Pessé
2016-02-24 18:07:17 +01:00
parent 3a0636046e
commit 0b4a6b5362
7 changed files with 60 additions and 21 deletions
+5 -4
View File
@@ -48,6 +48,10 @@ function Book(opts) {
// If multi-lingual, book can have a parent
this.parent = opts.parent;
if (this.parent) {
this.language = path.relative(this.parent.root, this.root);
console.log(this.language);
}
// A book is linked to an fs, to access its content
this.fs = opts.fs;
@@ -187,10 +191,7 @@ Book.prototype.parse = function() {
that.books.push(langBook);
return langBook.parse()
.then(function() {
langBook.config.set('output', path.join(that.config.get('output'), lang.id));
});
return langBook.parse();
});
}
+40 -9
View File
@@ -4,6 +4,7 @@ var path = require('path');
var Book = require('../book');
var NodeFS = require('../fs/node');
var Logger = require('../utils/logger');
var Promise = require('../utils/promise');
var fs = require('../utils/fs');
var JSONOutput = require('../output/json');
var WebsiteOutput = require('../output/website');
@@ -54,7 +55,16 @@ function bookCmd(fn) {
function outputCmd(fn) {
return bookCmd(function(book, args, kwargs) {
var Out = FORMATS[kwargs.format];
return fn(new Out(book), args);
var outputFolder = undefined;
// Set output folder
if (args[0]) {
outputFolder = path.resolve(process.cwd(), args[0]);
}
return fn(new Out(book, {
root: outputFolder
}), args);
});
}
@@ -69,24 +79,45 @@ function ebookCmd(format) {
exec: bookCmd(function(book, args, kwargs) {
return fs.tmpDir()
.then(function(dir) {
var outputFile = path.resolve(process.cwd(), args[1] || 'book.' + format);
var ext = '.'+format;
var outputFile = path.resolve(process.cwd(), args[1] || ('book' + ext));
var output = new EBookOutput(book, {
root: dir,
format: format
});
return output.book.parse()
.then(function() {
// Set output folder
output.book.config.set('output', dir);
return output.generate();
})
// Copy the ebook file
// Copy the ebook files
.then(function() {
return fs.copy(
path.resolve(dir, 'index.' + format),
outputFile
);
if (output.book.isMultilingual()) {
return Promise.serie(output.book.langs.list(), function(lang) {
var _outputFile = path.join(
path.dirname(outputFile),
path.basename(outputFile, ext) + '_' + lang.id + ext
);
return fs.copy(
path.resolve(dir, lang.id, 'index' + ext),
_outputFile
);
})
.thenResolve(output.book.langs.count());
} else {
return fs.copy(
path.resolve(dir, 'index' + ext),
outputFile
).thenResolve(1);
}
})
.then(function(n) {
output.book.log.info.ok(n+' file(s) generated');
output.book.log.info.ln('cleaning up... ');
return output.book.log.info.promise(fs.rmDir(dir));
});
});
})
-5
View File
@@ -64,11 +64,6 @@ module.exports = {
exec: helper.outputCmd(function(output, args, kwargs) {
return output.book.parse()
.then(function() {
// Set output folder
if (args[0]) {
output.book.config.set('output', path.resolve(process.cwd(), args[0]));
}
return output.generate();
});
})
-1
View File
@@ -56,7 +56,6 @@ Config.prototype.load = function() {
}
}
that.options.output = that.options.output || that.book.resolve('_book');
that.options.plugins = plugins.toList(that.options.plugins);
that.options.defaultsPlugins = plugins.toList(that.options.defaultsPlugins || '', false);
that.options.plugins = _.union(that.options.plugins, that.options.defaultsPlugins);
+3
View File
@@ -23,6 +23,9 @@ EbookOutput.prototype.name = 'ebook';
// Finish generation, create ebook using ebook-convert
EbookOutput.prototype.finish = function() {
var that = this;
if (that.book.isMultilingual()) {
return EbookOutput.super_.prototype.finish.apply(that);
}
return Promise()
.then(function() {
+11 -1
View File
@@ -17,6 +17,8 @@ module.exports = function folderOutput(Base) {
function FolderOutput() {
Base.apply(this, arguments);
this.opts.root = path.resolve(this.opts.root || this.book.resolve('_book'));
}
util.inherits(FolderOutput, Base);
@@ -60,12 +62,20 @@ module.exports = function folderOutput(Base) {
});
};
// Prepare output for a language book
FolderOutput.prototype.onLanguageBook = function(book) {
return new this.constructor(book, _.extend(this.opts, {
// Language output should be output in sub-directory of output
root: path.resolve(this.root(), book.language)
}), this);
};
// ----- Utility methods -----
// Return path to the root folder
FolderOutput.prototype.root = function() {
return path.resolve(process.cwd(), this.book.config.get('output'));
return this.opts.root;
};
// Resolve a file in the output directory
+1 -1
View File
@@ -113,7 +113,7 @@ BookPlugin.prototype.load = function(folder) {
that.book.config.set(that.getConfigKey(), config);
});
this.log.info('loading plugin "' + this.id + '" ...');
this.log.info('loading plugin "' + this.id + '"... ');
return this.log.info.promise(p);
};