@@ -4,17 +4,17 @@ import PropTypes from 'prop-types';
4
4
import Spinner from 'ink-spinner' ;
5
5
import readLine from 'readline' ;
6
6
7
- import Conditional from './components/Conditional' ;
8
- import Container from './components/Container' ;
9
7
import FileUtils from './util/File' ;
10
8
import ZipUtils from './util/Zip' ;
11
9
10
+ import Cli from './components' ;
11
+
12
12
const Types = {
13
13
EXIT_WITH_SUCCESS : 0 ,
14
14
EXIT_WITH_ERROR : 1
15
15
} ;
16
16
17
- export default class Cli extends Component {
17
+ export default class Command extends Component {
18
18
static propTypes = {
19
19
projectName : PropTypes . string . isRequired
20
20
} ;
@@ -31,35 +31,40 @@ export default class Cli extends Component {
31
31
} ;
32
32
33
33
async componentDidMount ( ) {
34
- readLine . emitKeypressEvents ( process . stdin ) ;
35
- process . stdin . setRawMode ( true ) ;
36
-
34
+ await this . enableKeyPress ( ) ;
37
35
await this . createScaffold ( this . props . projectName ) ;
38
36
}
39
37
40
38
render ( props , state ) {
41
- //TODO Review
39
+ const isFetchingFromGitHub = ! state . error && ! state . result && ! state . progress ;
40
+ const hasNoErrors = state . error !== null ;
41
+ const hasResults = state . result === 'ok' ;
42
+ const isInProgress = state . progress ;
43
+
44
+ const projectName = state . projectName || props . projectName ;
45
+
46
+ //TODO Review If we can use progress
42
47
//<Progress character="=" left={0} right={0} green />
43
48
44
49
return (
45
- < Container >
46
- < Conditional expression = { ! state . error && ! state . result && ! state . progress } >
50
+ < Cli . Container >
51
+ < Cli . Conditional expression = { isFetchingFromGitHub } >
47
52
< Spinner green /> Fetching base project from GitHub.
48
- </ Conditional >
49
- < Conditional expression = { state . progress } >
50
- < Spinner green /> Unzipping starter project to { props . projectName }
51
- </ Conditional >
52
- < Conditional expression = { state . error !== null } >
53
+ </ Cli . Conditional >
54
+ < Cli . Conditional expression = { isInProgress } >
55
+ < Spinner green /> Unzipping starter project to { projectName }
56
+ </ Cli . Conditional >
57
+ < Cli . Conditional expression = { hasNoErrors } >
53
58
< Text red >
54
59
{ state . error && state . error . message }
55
60
</ Text >
56
- </ Conditional >
57
- < Conditional expression = { state . result === 'ok' } >
61
+ </ Cli . Conditional >
62
+ < Cli . Conditional expression = { hasResults } >
58
63
< Text green >
59
- Project { state . projectName } has been created successfully.
64
+ Project { projectName } has been created successfully.
60
65
</ Text >
61
- </ Conditional >
62
- </ Container >
66
+ </ Cli . Conditional >
67
+ </ Cli . Container >
63
68
) ;
64
69
}
65
70
@@ -91,8 +96,8 @@ export default class Cli extends Component {
91
96
92
97
this . setState ( state => ( {
93
98
projectName : path ,
94
- result : 'ok' ,
95
- progress : false
99
+ progress : false ,
100
+ result : 'ok'
96
101
} ) ) ;
97
102
98
103
this . exitWithDelay ( Types . EXIT_WITH_SUCCESS ) ;
@@ -106,12 +111,20 @@ export default class Cli extends Component {
106
111
await FileUtils . remove ( path ) ;
107
112
108
113
this . setState ( state => ( {
109
- error : error ,
110
- progress : false
114
+ progress : false ,
115
+ error : error
111
116
} ) ) ;
112
117
113
118
this . exitWithDelay ( Types . EXIT_WITH_ERROR ) ;
114
119
} ;
115
120
116
121
exitWithDelay = ( type , delay = 1000 ) => setTimeout ( ( ) => process . exit ( type ) , delay ) ;
122
+
123
+ enableKeyPress = ( ) =>
124
+ new Promise ( resolve => {
125
+ readLine . emitKeypressEvents ( process . stdin ) ;
126
+ process . stdin . setRawMode ( true ) ;
127
+
128
+ resolve ( ) ;
129
+ } ) ;
117
130
}
0 commit comments