diff options
Diffstat (limited to 'lib/output/folder.js')
-rw-r--r-- | lib/output/folder.js | 158 |
1 files changed, 80 insertions, 78 deletions
diff --git a/lib/output/folder.js b/lib/output/folder.js index 90a3351..9964ecd 100644 --- a/lib/output/folder.js +++ b/lib/output/folder.js @@ -12,104 +12,106 @@ This output requires the native fs module to output book as a directory (mapping assets and pages) */ -function FolderOutput() { - Output.apply(this, arguments); -} -util.inherits(FolderOutput, Output); - -// Copy an asset file (non-parsable), ex: images, etc -FolderOutput.prototype.onAsset = function(filename) { - return this.copyFile( - this.book.resolve(filename), - filename - ); -}; +module.exports = function folderOutput(Base) { + Base = Base || Output; -// Prepare the generation by creating the output folder -FolderOutput.prototype.prepare = function() { - return fs.mkdirp(this.root()); -}; + function FolderOutput() { + Base.apply(this, arguments); + } + util.inherits(FolderOutput, Base); + // Copy an asset file (non-parsable), ex: images, etc + FolderOutput.prototype.onAsset = function(filename) { + return this.copyFile( + this.book.resolve(filename), + filename + ); + }; -// ----- Utility methods ----- + // Prepare the generation by creating the output folder + FolderOutput.prototype.prepare = function() { + return fs.mkdirp(this.root()); + }; -// Return path to the root folder -FolderOutput.prototype.root = function() { - return path.resolve(process.cwd(), this.book.config.get('output')); -}; -// Resolve a file in the output directory -FolderOutput.prototype.resolve = function(filename) { - return pathUtil.resolveInRoot.apply(null, [this.root()].concat(_.toArray(arguments))); -}; + // ----- Utility methods ----- -// Resolve a file path from a page (in the output folder) -FolderOutput.prototype.resolveForPage = function(page, filename) { - var abs = page.resolveLocal(filename); - return this.resolve(abs); -}; + // Return path to the root folder + FolderOutput.prototype.root = function() { + return path.resolve(process.cwd(), this.book.config.get('output')); + }; + // Resolve a file in the output directory + FolderOutput.prototype.resolve = function(filename) { + return pathUtil.resolveInRoot.apply(null, [this.root()].concat(_.toArray(arguments))); + }; -// Copy a file to the output -FolderOutput.prototype.copyFile = function(from, to) { - var that = this; + // Resolve a file path from a page (in the output folder) + FolderOutput.prototype.resolveForPage = function(page, filename) { + var abs = page.resolveLocal(filename); + return this.resolve(abs); + }; - return Promise() - .then(function() { - to = that.resolve(to); - return fs.copy(from, to); - }); -}; + // Copy a file to the output + FolderOutput.prototype.copyFile = function(from, to) { + var that = this; -// Write a file/buffer to the output folder -FolderOutput.prototype.writeFile = function(filename, buf) { - var that = this; + return Promise() + .then(function() { + to = that.resolve(to); - return Promise() - .then(function() { - filename = that.resolve(filename); - var folder = path.dirname(filename); + return fs.copy(from, to); + }); + }; - // Ensure fodler exists - return fs.mkdirp(folder); - }) + // Write a file/buffer to the output folder + FolderOutput.prototype.writeFile = function(filename, buf) { + var that = this; - // Write the file - .then(function() { - return fs.writeFile(filename, buf); - }); -}; + return Promise() + .then(function() { + filename = that.resolve(filename); + var folder = path.dirname(filename); -// Return true if a file exists in the output folder -FolderOutput.prototype.hasFile = function(filename) { - var that = this; + // Ensure fodler exists + return fs.mkdirp(folder); + }) - return Promise() - .then(function() { - return fs.exists(that.resolve(filename)); - }); -}; + // Write the file + .then(function() { + return fs.writeFile(filename, buf); + }); + }; + // Return true if a file exists in the output folder + FolderOutput.prototype.hasFile = function(filename) { + var that = this; -// Create a new unique file -// Returns its filename -FolderOutput.prototype.createNewFile = function(base, filename) { - var that = this; + return Promise() + .then(function() { + return fs.exists(that.resolve(filename)); + }); + }; - if (!filename) { - filename = path.basename(filename); - base = path.dirname(base); - } + // Create a new unique file + // Returns its filename + FolderOutput.prototype.createNewFile = function(base, filename) { + var that = this; - return fs.uniqueFilename(this.resolve(base), filename) - .then(function(out) { - out = path.join(base, out); + if (!filename) { + filename = path.basename(filename); + base = path.dirname(base); + } - return fs.ensure(that.resolve(out)) - .thenResolve(out); - }); -}; + return fs.uniqueFilename(this.resolve(base), filename) + .then(function(out) { + out = path.join(base, out); + return fs.ensure(that.resolve(out)) + .thenResolve(out); + }); + }; -module.exports = FolderOutput; + return FolderOutput; +}; |