10
10
import chalk from 'chalk' ;
11
11
import childProcess from 'child_process' ;
12
12
import commander from 'commander' ;
13
- import minimist from 'minimist' ;
14
13
import path from 'path' ;
15
14
import type { CommandT , ContextT } from './tools/types.flow' ;
16
15
import getLegacyConfig from './tools/getLegacyConfig' ;
17
16
import { getCommands } from './commands' ;
18
17
import init from './commands/init/init' ;
19
18
import assertRequiredOptions from './tools/assertRequiredOptions' ;
20
19
import logger from './tools/logger' ;
21
- import pkg from '../package.json' ;
20
+ import pkgJson from '../package.json' ;
22
21
23
22
commander
24
- . version ( pkg . version )
23
+ . option ( '-- version' , 'Print CLI version' )
25
24
. option ( '--projectRoot [string]' , 'Path to the root of the project' )
26
25
. option ( '--reactNativePath [string]' , 'Path to React Native' ) ;
27
26
@@ -39,41 +38,33 @@ const handleError = err => {
39
38
40
39
// Custom printHelpInformation command inspired by internal Commander.js
41
40
// one modified to suit our needs
42
- function printHelpInformation ( ) {
41
+ function printHelpInformation ( examples , pkg ) {
43
42
let cmdName = this . _name ;
44
43
if ( this . _alias ) {
45
44
cmdName = `${ cmdName } |${ this . _alias } ` ;
46
45
}
47
46
48
- const sourceInformation = this . pkg
49
- ? [ ` ${ chalk . bold ( 'Source:' ) } ${ this . pkg . name } @${ this . pkg . version } ` , '' ]
47
+ const sourceInformation = pkg
48
+ ? [ `${ chalk . bold ( 'Source:' ) } ${ pkg . name } @${ pkg . version } ` , '' ]
50
49
: [ ] ;
51
50
52
51
let output = [
53
- '' ,
54
- chalk . bold ( chalk . cyan ( ` react-native ${ cmdName } ${ this . usage ( ) } ` ) ) ,
55
- this . _description ? ` ${ this . _description } ` : '' ,
56
- '' ,
52
+ chalk . bold ( `react-native ${ cmdName } ${ this . usage ( ) } ` ) ,
53
+ this . _description ? `\n${ this . _description } \n` : '' ,
57
54
...sourceInformation ,
58
- ` ${ chalk . bold ( 'Options:' ) } ` ,
59
- '' ,
60
- this . optionHelp ( ) . replace ( / ^ / gm, ' ' ) ,
61
- '' ,
55
+ `${ chalk . bold ( 'Options:' ) } ` ,
56
+ this . optionHelp ( ) . replace ( / ^ / gm, ' ' ) ,
62
57
] ;
63
58
64
- if ( this . examples && this . examples . length > 0 ) {
65
- const formattedUsage = this . examples
66
- . map ( example => ` ${ example . desc } : \n ${ chalk . cyan ( example . cmd ) } ` )
59
+ if ( examples && examples . length > 0 ) {
60
+ const formattedUsage = examples
61
+ . map ( example => ` ${ example . desc } : \n ${ chalk . cyan ( example . cmd ) } ` )
67
62
. join ( '\n\n' ) ;
68
63
69
- output = output . concat ( [
70
- chalk . bold ( ' Example usage:' ) ,
71
- '' ,
72
- formattedUsage ,
73
- ] ) ;
64
+ output = output . concat ( [ chalk . bold ( '\nExample usage:' ) , formattedUsage ] ) ;
74
65
}
75
66
76
- return output . concat ( [ '' , '' ] ) . join ( '\n' ) ;
67
+ return output . join ( '\n' ) ;
77
68
}
78
69
79
70
function printUnknownCommand ( cmdName ) {
@@ -93,9 +84,7 @@ const addCommand = (command: CommandT, ctx: ContextT) => {
93
84
const options = command . options || [ ] ;
94
85
95
86
const cmd = commander
96
- . command ( command . name , undefined , {
97
- noHelp : ! command . description ,
98
- } )
87
+ . command ( command . name , undefined , { noHelp : ! command . description } )
99
88
. description ( command . description )
100
89
. action ( function handleAction ( ...args ) {
101
90
const passedOptions = this . opts ( ) ;
@@ -109,10 +98,12 @@ const addCommand = (command: CommandT, ctx: ContextT) => {
109
98
.catch(handleError);
110
99
} ) ;
111
100
112
- cmd . helpInformation = printHelpInformation . bind ( cmd ) ;
113
- cmd . examples = command . examples ;
114
- // $FlowFixMe: This is either null or not
115
- cmd . pkg = command . pkg ;
101
+ cmd . helpInformation = printHelpInformation . bind (
102
+ cmd ,
103
+ command . examples ,
104
+ // $FlowFixMe - we know pkg may be missing...
105
+ command . pkg ,
106
+ ) ;
116
107
117
108
options . forEach ( opt =>
118
109
cmd . option (
@@ -123,7 +114,11 @@ const addCommand = (command: CommandT, ctx: ContextT) => {
123
114
) ,
124
115
) ;
125
116
126
- // Redefined here to appear in the `--help` section
117
+ /**
118
+ * We want every command (like "start", "link") to accept below options.
119
+ * To achieve that we append them to regular options of each command here.
120
+ * This way they'll be displayed in the commands --help menus.
121
+ */
127
122
cmd
128
123
. option ( '--projectRoot [string]' , 'Path to the root of the project' )
129
124
. option ( '--reactNativePath [string]' , 'Path to React Native' ) ;
@@ -157,20 +152,12 @@ async function setupAndRun() {
157
152
}
158
153
}
159
154
160
- /**
161
- * Read passed `options` and take the "global" settings
162
- *
163
- * @todo (grabbou): Consider unifying this by removing either `commander`
164
- * or `minimist`
165
- */
166
- const options = minimist ( process . argv . slice ( 2 ) ) ;
167
-
168
- const root = options . projectRoot
169
- ? path . resolve ( options . projectRoot )
155
+ const root = commander . projectRoot
156
+ ? path . resolve ( commander . projectRoot )
170
157
: process . cwd ( ) ;
171
158
172
- const reactNativePath = options . reactNativePath
173
- ? path . resolve ( options . reactNativePath )
159
+ const reactNativePath = commander . reactNativePath
160
+ ? path . resolve ( commander . reactNativePath )
174
161
: ( ( ) => {
175
162
try {
176
163
return path . dirname (
@@ -198,9 +185,16 @@ async function setupAndRun() {
198
185
199
186
commander . parse ( process . argv ) ;
200
187
201
- if ( ! options . _ . length ) {
188
+ if ( commander . rawArgs . length === 2 ) {
202
189
commander . outputHelp ( ) ;
203
190
}
191
+
192
+ // We handle --version as a special case like this because both `commander`
193
+ // and `yargs` append it to every command and we don't want to do that.
194
+ // E.g. outside command `init` has --version flag and we want to preserve it.
195
+ if ( commander . args . length === 0 && commander . version === true ) {
196
+ console . log ( pkgJson . version ) ;
197
+ }
204
198
}
205
199
206
200
export default {
0 commit comments