From 671ab12899e8ce4121c2805f327f61510e47167c Mon Sep 17 00:00:00 2001 From: Simon Vocella Date: Wed, 1 Feb 2017 23:37:59 +0100 Subject: [PATCH] extract help command --- src/cli/commands/help.js | 40 +++++++++++++++++++++++++++++++++++++++ src/cli/commands/index.js | 1 + src/cli/index.js | 32 +++++-------------------------- 3 files changed, 46 insertions(+), 27 deletions(-) create mode 100644 src/cli/commands/help.js diff --git a/src/cli/commands/help.js b/src/cli/commands/help.js new file mode 100644 index 0000000000..c2398cf712 --- /dev/null +++ b/src/cli/commands/help.js @@ -0,0 +1,40 @@ +/* @flow */ + +import * as commands from './index.js'; +import * as constants from '../../constants.js'; +import type {Reporter} from '../../reporters/index.js'; +import type Config from '../../config.js'; +import {sortAlpha, hyphenate} from '../../util/misc.js'; +const chalk = require('chalk'); + +export function run( + config: Config, + reporter: Reporter, + commander: Object, + args: Array, +): Promise { + const getDocsLink = (name) => `${constants.YARN_DOCS}${name || ''}`; + const getDocsInfo = (name) => 'Visit ' + chalk.bold(getDocsLink(name)) + ' for documentation about this command.'; + + if (args.length) { + const helpCommand = hyphenate(args[0]); + if (commands[helpCommand]) { + commander.on('--help', () => console.log(' ' + getDocsInfo(helpCommand) + '\n')); + } + } else { + commander.on('--help', () => { + console.log(' Commands:\n'); + for (const name of Object.keys(commands).sort(sortAlpha)) { + if (commands[name].useless) { + continue; + } + + console.log(` - ${hyphenate(name)}`); + } + console.log('\n Run `' + chalk.bold('yarn help COMMAND') + '` for more information on specific commands.'); + console.log(' Visit ' + chalk.bold(getDocsLink()) + ' to learn more about Yarn.\n'); + }); + } + commander.help(); + return Promise.resolve(); +} diff --git a/src/cli/commands/index.js b/src/cli/commands/index.js index 3b303d7479..44669bdb57 100644 --- a/src/cli/commands/index.js +++ b/src/cli/commands/index.js @@ -9,6 +9,7 @@ import * as clean from './clean.js'; export {clean}; import * as config from './config.js'; export {config}; import * as generateLockEntry from './generate-lock-entry.js'; export {generateLockEntry}; import * as global from './global.js'; export {global}; +import * as help from './help.js'; export {help}; import * as import_ from './import.js'; export {import_ as import}; import * as info from './info.js'; export {info}; import * as init from './init.js'; export {init}; diff --git a/src/cli/index.js b/src/cli/index.js index 103f304438..505afe325f 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -1,7 +1,6 @@ /* @flow */ import {ConsoleReporter, JSONReporter} from '../reporters/index.js'; -import {sortAlpha} from '../util/misc.js'; import {registries, registryNames} from '../registries/index.js'; import * as commands from './commands/index.js'; import * as constants from '../constants.js'; @@ -9,7 +8,7 @@ import * as network from '../util/network.js'; import {MessageError} from '../errors.js'; import aliases from './aliases.js'; import Config from '../config.js'; -import {hyphenate, camelCase} from '../util/misc.js'; +import {camelCase} from '../util/misc.js'; const chalk = require('chalk'); const commander = require('commander'); @@ -99,27 +98,8 @@ const getDocsLink = (name) => `${constants.YARN_DOCS}${name || ''}`; const getDocsInfo = (name) => 'Visit ' + chalk.bold(getDocsLink(name)) + ' for documentation about this command.'; // -if (commandName === 'help' || commandName === '--help' || commandName === '-h') { +if (commandName === '--help' || commandName === '-h') { commandName = 'help'; - if (args.length) { - const helpCommand = hyphenate(args[0]); - if (commands[helpCommand]) { - commander.on('--help', () => console.log(' ' + getDocsInfo(helpCommand) + '\n')); - } - } else { - commander.on('--help', () => { - console.log(' Commands:\n'); - for (const name of Object.keys(commands).sort(sortAlpha)) { - if (commands[name].useless) { - continue; - } - - console.log(` - ${hyphenate(name)}`); - } - console.log('\n Run `' + chalk.bold('yarn help COMMAND') + '` for more information on specific commands.'); - console.log(' Visit ' + chalk.bold(getDocsLink()) + ' to learn more about Yarn.\n'); - }); - } } // if no args or command name looks like a flag then default to `install` @@ -160,7 +140,7 @@ if (command && typeof command.setFlags === 'function') { command.setFlags(commander); } -if (commandName === 'help' || args.indexOf('--help') >= 0 || args.indexOf('-h') >= 0) { +if (args.indexOf('--help') >= 0 || args.indexOf('-h') >= 0) { const examples: Array = (command && command.examples) || []; if (examples.length) { commander.on('--help', () => { @@ -171,9 +151,7 @@ if (commandName === 'help' || args.indexOf('--help') >= 0 || args.indexOf('-h') console.log(); }); } - if (commandName !== 'help') { - commander.on('--help', () => console.log(' ' + getDocsInfo(commandName) + '\n')); - } + commander.on('--help', () => console.log(' ' + getDocsInfo(commandName) + '\n')); commander.parse(startArgs.concat(args)); commander.help(); @@ -220,7 +198,7 @@ if (typeof command.hasWrapper === 'function') { if (commander.json) { outputWrapper = false; } -if (outputWrapper) { +if (outputWrapper && commandName !== 'help') { reporter.header(commandName, pkg); }