UNPKG

basecamp-guide

Version:
71 lines (61 loc) 1.89 kB
/** * Dependecies */ var fs = require('fs'); var ConsoleInputParser = require('../lib/ConsoleInputParser.js'); var _ = require('underscore'); /** * Constructs CommandLineTool-Object */ function CommandLineTool() { // configs this.config = require('../lib/Config.js'); // container for all commands this.commands = {}; // actions to do for initialisation this.setDefaultsFrom('basecamp.json'); } CommandLineTool.prototype.setDefaultsFrom = function(file) { var overrides; if (fs.existsSync(file)) { overrides = JSON.parse(fs.readFileSync(file, 'utf8')); _.mixin({ deepExtend: require('underscore-deep-extend')(_) }); _.deepExtend(this.config, overrides); } } /** * Adds commands to the tool * @param {string} name Name of the command * @param {object} commandObject Object with fire-method */ CommandLineTool.prototype.addCommand = function(name, commandObject) { // inject global config commandObject.config = this.config; this.commands[name] = commandObject; }; /** * Checks existence of an command * @param {object} consoleInput Contains command-name and -options */ CommandLineTool.prototype.hasCommand = function(consoleInput) { var commandExists = this.commands.hasOwnProperty(consoleInput.name); if (! commandExists) { console.log(('Der Befehl ' + consoleInput.name.bold + ' existiert nicht.').red); process.exit(1); } }; /** * Parses the given command and fires it. * @param {object} rawConsoleInput Receives process.argv values. */ CommandLineTool.prototype.fireCommand = function(rawConsoleInput) { var consoleInput = ConsoleInputParser.parse(rawConsoleInput); this.hasCommand(consoleInput); this.commands[consoleInput.name].fire(consoleInput.options); }; /** * Exporting class */ module.exports = CommandLineTool;