Browse Source

allow loading a config from .tendrc

pull/2/head
Brett Langdon 12 years ago
parent
commit
129fde710c
2 changed files with 67 additions and 9 deletions
  1. +27
    -7
      bin/tend
  2. +40
    -2
      lib/index.js

+ 27
- 7
bin/tend View File

@ -5,8 +5,9 @@ var tend = require('../');
var doc = [
'Usage:',
' tend',
' tend (--help | --version)',
' tend [--restart] [--ignoreHidden] <dir> <command> [<filter>]',
' tend [--restart] [--ignoreHidden] [<dir> <command>] [<filter>]',
'',
'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['<filter>'],
ignoreHidden: args['--ignoreHidden'],
restart: args['--restart'],
};
tend.tend(args['<dir>'], args['<command>'], options);
var config = tend.parseConfig();
if (!config && !args['<dir>'] && !args['<command>']) {
console.error('No .tendrc file found, must run with "<dir> <command>"');
process.exit(1);
}
if (!config) {
config = {};
}
if (args['<dir>'] && args['<command>']) {
config['cli'] = {
filter: args['<filter>'],
ignoreHidden: args['--ignoreHidden'],
restart: args['--restart'],
command: args['<command>'],
directory: args['<dir>'],
}
}
for (var key in config) {
console.log('Starting listener: ' + key);
var options = config[key];
tend.tend(options.directory, options.command, options);
}

+ 40
- 2
lib/index.js View File

@ -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;
};

Loading…
Cancel
Save