-
Notifications
You must be signed in to change notification settings - Fork 915
chore: simplify commander/minimist usage #209
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a9837b1
chore: simplify commander/minimist usage
thymikee a891600
feedback
thymikee 7bb247c
remove extra parse
thymikee 9003617
add options back
thymikee 4882a93
rephrase comment
thymikee 89b91e6
remove excess whitespace
thymikee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,18 +10,17 @@ | |
import chalk from 'chalk'; | ||
import childProcess from 'child_process'; | ||
import commander from 'commander'; | ||
import minimist from 'minimist'; | ||
import path from 'path'; | ||
import type {CommandT, ContextT} from './tools/types.flow'; | ||
import getLegacyConfig from './tools/getLegacyConfig'; | ||
import {getCommands} from './commands'; | ||
import init from './commands/init/init'; | ||
import assertRequiredOptions from './tools/assertRequiredOptions'; | ||
import logger from './tools/logger'; | ||
import pkg from '../package.json'; | ||
import pkgJson from '../package.json'; | ||
|
||
commander | ||
.version(pkg.version) | ||
.option('--version', 'Print CLI version') | ||
.option('--projectRoot [string]', 'Path to the root of the project') | ||
.option('--reactNativePath [string]', 'Path to React Native'); | ||
|
||
|
@@ -39,41 +38,33 @@ const handleError = err => { | |
|
||
// Custom printHelpInformation command inspired by internal Commander.js | ||
// one modified to suit our needs | ||
function printHelpInformation() { | ||
function printHelpInformation(examples, pkg) { | ||
let cmdName = this._name; | ||
if (this._alias) { | ||
cmdName = `${cmdName}|${this._alias}`; | ||
} | ||
|
||
const sourceInformation = this.pkg | ||
? [` ${chalk.bold('Source:')} ${this.pkg.name}@${this.pkg.version}`, ''] | ||
const sourceInformation = pkg | ||
? [`${chalk.bold('Source:')} ${pkg.name}@${pkg.version}`, ''] | ||
: []; | ||
|
||
let output = [ | ||
'', | ||
chalk.bold(chalk.cyan(` react-native ${cmdName} ${this.usage()}`)), | ||
this._description ? ` ${this._description}` : '', | ||
'', | ||
chalk.bold(`react-native ${cmdName} ${this.usage()}`), | ||
this._description ? `\n${this._description}\n` : '', | ||
...sourceInformation, | ||
` ${chalk.bold('Options:')}`, | ||
'', | ||
this.optionHelp().replace(/^/gm, ' '), | ||
'', | ||
`${chalk.bold('Options:')}`, | ||
this.optionHelp().replace(/^/gm, ' '), | ||
]; | ||
|
||
if (this.examples && this.examples.length > 0) { | ||
const formattedUsage = this.examples | ||
.map(example => ` ${example.desc}: \n ${chalk.cyan(example.cmd)}`) | ||
if (examples && examples.length > 0) { | ||
const formattedUsage = examples | ||
.map(example => ` ${example.desc}: \n ${chalk.cyan(example.cmd)}`) | ||
.join('\n\n'); | ||
|
||
output = output.concat([ | ||
chalk.bold(' Example usage:'), | ||
'', | ||
formattedUsage, | ||
]); | ||
output = output.concat([chalk.bold('\nExample usage:'), formattedUsage]); | ||
} | ||
|
||
return output.concat(['', '']).join('\n'); | ||
return output.join('\n'); | ||
} | ||
|
||
function printUnknownCommand(cmdName) { | ||
|
@@ -93,9 +84,7 @@ const addCommand = (command: CommandT, ctx: ContextT) => { | |
const options = command.options || []; | ||
|
||
const cmd = commander | ||
.command(command.name, undefined, { | ||
noHelp: !command.description, | ||
}) | ||
.command(command.name, undefined, {noHelp: !command.description}) | ||
.description(command.description) | ||
.action(function handleAction(...args) { | ||
const passedOptions = this.opts(); | ||
|
@@ -109,10 +98,12 @@ const addCommand = (command: CommandT, ctx: ContextT) => { | |
.catch(handleError); | ||
}); | ||
|
||
cmd.helpInformation = printHelpInformation.bind(cmd); | ||
cmd.examples = command.examples; | ||
// $FlowFixMe: This is either null or not | ||
cmd.pkg = command.pkg; | ||
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.
|
||
cmd.helpInformation = printHelpInformation.bind( | ||
cmd, | ||
command.examples, | ||
// $FlowFixMe - we know pkg may be missing... | ||
command.pkg, | ||
); | ||
|
||
options.forEach(opt => | ||
cmd.option( | ||
|
@@ -123,7 +114,11 @@ const addCommand = (command: CommandT, ctx: ContextT) => { | |
), | ||
); | ||
|
||
// Redefined here to appear in the `--help` section | ||
/** | ||
* We want every command (like "start", "link") to accept below options. | ||
* To achieve that we append them to regular options of each command here. | ||
* This way they'll be displayed in the commands --help menus. | ||
*/ | ||
cmd | ||
.option('--projectRoot [string]', 'Path to the root of the project') | ||
.option('--reactNativePath [string]', 'Path to React Native'); | ||
|
@@ -157,20 +152,12 @@ async function setupAndRun() { | |
} | ||
} | ||
|
||
/** | ||
* Read passed `options` and take the "global" settings | ||
* | ||
* @todo(grabbou): Consider unifying this by removing either `commander` | ||
* or `minimist` | ||
*/ | ||
const options = minimist(process.argv.slice(2)); | ||
|
||
const root = options.projectRoot | ||
? path.resolve(options.projectRoot) | ||
const root = commander.projectRoot | ||
? path.resolve(commander.projectRoot) | ||
: process.cwd(); | ||
|
||
const reactNativePath = options.reactNativePath | ||
? path.resolve(options.reactNativePath) | ||
const reactNativePath = commander.reactNativePath | ||
? path.resolve(commander.reactNativePath) | ||
: (() => { | ||
try { | ||
return path.dirname( | ||
|
@@ -198,9 +185,16 @@ async function setupAndRun() { | |
|
||
commander.parse(process.argv); | ||
|
||
if (!options._.length) { | ||
if (commander.rawArgs.length === 2) { | ||
commander.outputHelp(); | ||
} | ||
|
||
// We handle --version as a special case like this because both `commander` | ||
// and `yargs` append it to every command and we don't want to do that. | ||
// E.g. outside command `init` has --version flag and we want to preserve it. | ||
if (commander.args.length === 0 && commander.version === true) { | ||
console.log(pkgJson.version); | ||
} | ||
} | ||
|
||
export default { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
seems like it's not used anywhere, @grabbou ideas?