From 129fde710c89d1537971cd5bdf430af50bea3907 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Tue, 27 May 2014 20:24:03 -0400 Subject: [PATCH] allow loading a config from .tendrc --- bin/tend | 34 +++++++++++++++++++++++++++------- lib/index.js | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 9 deletions(-) diff --git a/bin/tend b/bin/tend index a985a08..b470ee9 100755 --- a/bin/tend +++ b/bin/tend @@ -5,8 +5,9 @@ var tend = require('../'); var doc = [ 'Usage:', + ' tend', ' tend (--help | --version)', - ' tend [--restart] [--ignoreHidden] []', + ' tend [--restart] [--ignoreHidden] [ ] []', '', 'Options:', ' -h --help Show this help text', @@ -20,9 +21,28 @@ var args = docopt.docopt(doc, { version: 'tend ' + require('../package.json').version, }); -var options = { - pattern: args[''], - ignoreHidden: args['--ignoreHidden'], - restart: args['--restart'], -}; -tend.tend(args[''], args[''], options); +var config = tend.parseConfig(); +if (!config && !args[''] && !args['']) { + console.error('No .tendrc file found, must run with " "'); + process.exit(1); +} + +if (!config) { + config = {}; +} + +if (args[''] && args['']) { + config['cli'] = { + filter: args[''], + ignoreHidden: args['--ignoreHidden'], + restart: args['--restart'], + command: args[''], + directory: args[''], + } +} + +for (var key in config) { + console.log('Starting listener: ' + key); + var options = config[key]; + tend.tend(options.directory, options.command, options); +} diff --git a/lib/index.js b/lib/index.js index 6d07d99..87e16c7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,12 +1,13 @@ var colors = require('colors'); -var path = require('path'); var parse = require('shell-quote').parse; +var path = require('path'); +var rc = require('rc'); var spawn = require('child_process').spawn; var watch = require('watch'); module.exports.tend = function(dir, rawCommand, options) { options = options || {}; - var pattern = options.pattern || '.'; + var pattern = options.filter || '.'; var filter = new RegExp(pattern, 'i'); var ignoreHidden = options.ignoreHidden === true; @@ -72,3 +73,40 @@ module.exports.tend = function(dir, rawCommand, options) { timeout = setTimeout(startCommand, 300); }); }; + +module.exports.parseConfig = function() { + var config = {}; + var rawConfig = rc('tend', {}, {}); + + var defaults = { + filter: rawConfig.filter || '.', + ignoreHidden: (rawConfig.ignoreHidden === undefined) ? false : rawConfig.ignoreHidden, + restart: (rawConfig.restart === undefined) ? false : rawConfig.restart, + }; + + for (var key in rawConfig) { + if (!(rawConfig[key] instanceof Object)) { + continue; + } + var section = rawConfig[key]; + if (!section.directory) { + console.error('Section[' + key + '] missing "directory" setting'); + continue; + } else if (!section.command) { + console.error('Section[' + key + '] missing "command" setting'); + continue; + } + config[key] = { + filter: (section.filter === undefined) ? defaults.filter : section.filter, + ignoreHidden: (section.ignoreHidden === undefined) ? defaults.ignoreHidden : section.ignoreHidden, + restart: (section.restart === undefined) ? defaults.restart : section.restart, + directory: section.directory, + command: section.command + }; + } + + if (!Object.keys(config).length) { + return false; + } + return config; +};