var path = require('path');
var Q = require('q');
var _ = require('lodash');
var fs = require('fs');

var utils = require('./utils');
var generate = require("../lib/generate");
var parse = require("../lib/parse");
var generators = require("../lib/generate").generators;

var buildCommand = function(command) {
    return command
    .option('-o, --output <directory>', 'Path to output directory, defaults to ./_book')
    .option('-f, --format <name>', 'Change generation format, defaults to site, availables are: '+_.keys(generators).join(", "))
    .option('--config <config file>', 'Configuration file to use, defualt to book.json')
};


var makeBuildFunc = function(converter) {
    return  function(dir, options) {
        dir = dir || process.cwd();
        outputDir = options.output;

        console.log('Starting build ...');
        return converter(
            _.extend({}, options || {}, {
                input: dir,
                output: outputDir,
                generator: options.format,
                configFile: options.config
            })
        )
        .then(function(output) {
            console.log("Successfuly built !");
            return output;
        }, utils.logError);
    };   
};

module.exports = {
    folder: makeBuildFunc(generate.folder),
    file: makeBuildFunc(generate.file),
    command: buildCommand
};

var path = require('path');
var Q = require('q');
var _ = require('lodash');
var fs = require('fs');

var utils = require('./utils');
var generate = require("../lib/generate");
var parse = require("../lib/parse");
var generators = require("../lib/generate").generators;

var buildCommand = function(command) {
    return command
    .option('-o, --output <directory>', 'Path to output directory, defaults to ./_book')
    .option('-f, --format <name>', 'Change generation format, defaults to site, availables are: '+_.keys(generators).join(", "))
    .option('--config <config file>', 'Configuration file to use, defualt to book.json')
};


var makeBuildFunc = function(converter) {
    return  function(dir, options) {
        dir = dir || process.cwd();
        outputDir = options.output;

        console.log('Starting build ...');
        return converter(
            _.extend({}, options || {}, {
                input: dir,
                output: outputDir,
                generator: options.format,
                configFile: options.config
            })
        )
        .then(function(output) {
            console.log("Successfuly built !");
            return output;
        }, utils.logError)
        .fail(function() {
            process.exit(-1);
        });
    };
};

module.exports = {
    folder: makeBuildFunc(generate.folder),
    file: makeBuildFunc(generate.file),
    command: buildCommand
};

Exit process with -1 when error occurs
Exit process with -1 when error occurs
