Skip to content

Commit 8a2f5fc

Browse files
author
jonatansalas
committed
Refactoring to get better code readability
1 parent fdd25bd commit 8a2f5fc

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

src/cli/components/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Conditional from './Conditional';
2+
import Container from './Container';
3+
4+
export default {
5+
Conditional,
6+
Container
7+
}

src/cli/index.js

+36-23
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import PropTypes from 'prop-types';
44
import Spinner from 'ink-spinner';
55
import readLine from 'readline';
66

7-
import Conditional from './components/Conditional';
8-
import Container from './components/Container';
97
import FileUtils from './util/File';
108
import ZipUtils from './util/Zip';
119

10+
import Cli from './components';
11+
1212
const Types = {
1313
EXIT_WITH_SUCCESS: 0,
1414
EXIT_WITH_ERROR: 1
1515
};
1616

17-
export default class Cli extends Component {
17+
export default class Command extends Component {
1818
static propTypes = {
1919
projectName: PropTypes.string.isRequired
2020
};
@@ -31,35 +31,40 @@ export default class Cli extends Component {
3131
};
3232

3333
async componentDidMount() {
34-
readLine.emitKeypressEvents(process.stdin);
35-
process.stdin.setRawMode(true);
36-
34+
await this.enableKeyPress();
3735
await this.createScaffold(this.props.projectName);
3836
}
3937

4038
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
4247
//<Progress character="=" left={0} right={0} green />
4348

4449
return (
45-
<Container>
46-
<Conditional expression={!state.error && !state.result && !state.progress}>
50+
<Cli.Container>
51+
<Cli.Conditional expression={isFetchingFromGitHub}>
4752
<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}>
5358
<Text red>
5459
{state.error && state.error.message}
5560
</Text>
56-
</Conditional>
57-
<Conditional expression={state.result === 'ok'}>
61+
</Cli.Conditional>
62+
<Cli.Conditional expression={hasResults}>
5863
<Text green>
59-
Project {state.projectName} has been created successfully.
64+
Project {projectName} has been created successfully.
6065
</Text>
61-
</Conditional>
62-
</Container>
66+
</Cli.Conditional>
67+
</Cli.Container>
6368
);
6469
}
6570

@@ -91,8 +96,8 @@ export default class Cli extends Component {
9196

9297
this.setState(state => ({
9398
projectName: path,
94-
result: 'ok',
95-
progress: false
99+
progress: false,
100+
result: 'ok'
96101
}));
97102

98103
this.exitWithDelay(Types.EXIT_WITH_SUCCESS);
@@ -106,12 +111,20 @@ export default class Cli extends Component {
106111
await FileUtils.remove(path);
107112

108113
this.setState(state => ({
109-
error: error,
110-
progress: false
114+
progress: false,
115+
error: error
111116
}));
112117

113118
this.exitWithDelay(Types.EXIT_WITH_ERROR);
114119
};
115120

116121
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+
});
117130
}

0 commit comments

Comments
 (0)