-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce complexity of src/cli/index.js #2887
Changes from all commits
1c3bd30
123b5bf
6ec7f97
f268378
c9affc7
04c3ce1
89cb665
0f268f3
89fdcbc
c227e40
fbd0c88
3314951
0899ac7
71072db
4d930e9
045bd60
2fb8a3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,13 @@ 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'; | ||
import {sortAlpha, hyphenate, camelCase} from '../../util/misc.js'; | ||
const chalk = require('chalk'); | ||
|
||
export function hasWrapper(): boolean { | ||
return false; | ||
} | ||
|
||
export function run( | ||
config: Config, | ||
reporter: Reporter, | ||
|
@@ -17,24 +21,47 @@ export function run( | |
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; | ||
const commandName = camelCase(args.shift()); | ||
|
||
if (commandName) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't have to check this, since it won't pass the following test ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have to check this because commandName can be null, sorry I lost this comment! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah ... it seems this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. YES T_T XD |
||
const command = commands[commandName]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably should validate every such access with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we validate here maybe we should validate in src/cli/index.js. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep! It will fix this:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a pre-existent error, but i prefer to fix here right now. If I am correctly after this part, we will finish this review. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @arcanis done! |
||
|
||
if (command) { | ||
if (typeof command.setFlags === 'function') { | ||
command.setFlags(commander); | ||
} | ||
|
||
const examples: Array<string> = (command && command.examples) || []; | ||
if (examples.length) { | ||
commander.on('--help', () => { | ||
console.log(' Examples:\n'); | ||
for (const example of examples) { | ||
console.log(` $ yarn ${example}`); | ||
} | ||
console.log(); | ||
}); | ||
} | ||
commander.on('--help', () => console.log(' ' + getDocsInfo(commandName) + '\n')); | ||
|
||
console.log(` - ${hyphenate(name)}`); | ||
commander.help(); | ||
return Promise.resolve(); | ||
} | ||
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.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(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to check what happened when i type yarn unknown-command --help. It should ignore everything and display standard help. Maybe this behaviour is wrong
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK I was right. In detail: yarn unknown --help
before:
after pr:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! I'd probably put them on a single line, comma-separate, restricted to 80 columns. Otherwise they take a lot of vertical space.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean the
commands
part? I can do that if you confirmThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't overwrite the commands part without doing other horrible things for what I understand because it is a feature of commander.js -.-. In detail the function help.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I think it's done userland, more precisely here. That being said, I've checked locally and it seems that this display (one command each line) was already printed this way in recent Yarn releases, so let's keep it this way. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right! As you wish :)