react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / umd / node_modules / uglify-js / node_modules / yargs / lib / completion.js
80759 viewsvar fs = require('fs'),1path = require('path');23// add bash completions to your4// yargs-powered applications.5module.exports = function (yargs, usage) {6var self = {7completionKey: 'get-yargs-completions'8};910// get a list of completion commands.11self.getCompletion = function (done) {12var completions = [],13current = process.argv[process.argv.length - 1],14previous = process.argv.slice(process.argv.indexOf('--' + self.completionKey) + 1),15argv = yargs.parse(previous);1617// a custom completion function can be provided18// to completion().19if (completionFunction) {20if (completionFunction.length < 3) {21// synchronous completion function.22return done(completionFunction(current, argv));23} else {24// asynchronous completion function25return completionFunction(current, argv, function(completions) {26done(completions);27});28}29}3031if (!current.match(/^-/)) {32usage.getCommands().forEach(function(command) {33completions.push(command[0]);34});35}3637if (current.match(/^-/)) {38Object.keys(yargs.getOptions().key).forEach(function(key) {39completions.push('--' + key);40});41}4243done(completions);44};4546// generate the completion script to add to your .bashrc.47self.generateCompletionScript = function ($0) {48var script = fs.readFileSync(49path.resolve(__dirname, '../completion.sh.hbs'),50'utf-8'51),52name = path.basename($0);5354// add ./to applications not yet installed as bin.55if ($0.match(/\.js$/)) $0 = './' + $0;5657script = script.replace(/{{app_name}}/g, name);58return script.replace(/{{app_path}}/g, $0);59};6061// register a function to perform your own custom62// completions., this function can be either63// synchrnous or asynchronous.64var completionFunction = null;65self.registerFunction = function (fn) {66completionFunction = fn;67}6869return self;70};717273