Files
bower/lib/commands/search.js
Mickael Daniel b544425667 completion - generic method to complete command options
at command level, attach the `shorthand` Hash to the completion
function. The completion command will then try to complete shorthands
and longer flag based on user input.

completing after just one `-` will complete the list of short flags.

completing after just two `--` will complete the list of longer flags.
2012-12-25 16:03:21 +01:00

54 lines
1.4 KiB
JavaScript

// ==========================================
// BOWER: Lookup API
// ==========================================
// Copyright 2012 Twitter, Inc
// Licensed under The MIT License
// http://opensource.org/licenses/MIT
// ==========================================
var Emitter = require('events').EventEmitter;
var nopt = require('nopt');
var template = require('../util/template');
var source = require('../core/source');
var install = require('./install');
var help = require('./help');
var optionTypes = { help: Boolean };
var shorthand = { 'h': ['--help'] };
module.exports = function (name) {
var emitter = new Emitter;
var callback = function (err, results) {
if (err) return emitter.emit('error', err);
if (results.length) {
template('search', {results: results})
.on('data', emitter.emit.bind(emitter, 'data'));
} else {
template('search-empty', {results: results})
.on('data', emitter.emit.bind(emitter, 'data'));
}
};
if (name) {
source.search(name, callback);
} else {
source.all(callback);
}
return emitter;
};
module.exports.line = function (argv) {
var options = nopt(optionTypes, shorthand, argv);
var names = options.argv.remain.slice(1);
if (options.help) return help('search');
return module.exports(names[0]);
};
module.exports.completion = install.completion;
module.exports.completion.options = shorthand;