-
Notifications
You must be signed in to change notification settings - Fork 1
/
Terminal.js
72 lines (60 loc) · 1.96 KB
/
Terminal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React, {Component, PropTypes} from 'react';
import {merge, pick, compose, uniq, concat} from 'ramda';
import Wrapper from './components/Wrapper';
import {initFileSystem} from './utils/fs';
import bindCommands from './utils/bindCommands';
import {commandsType} from './utils/customPropTypes';
import {splitPath} from './utils/pathUtils';
import defaultCommands from './commands';
const mergeDefault = merge(defaultCommands);
// props we want to pass to wrapper from this.state
const pickProps = pick(['visibles', 'currentPath']);
class Terminal extends Component {
constructor(props) {
super(props);
const {initialPath, directories, files, username} = props;
const homePath = username === 'root' ? `/${username}` : `/home/${username}`;
const currentPath = splitPath(initialPath);
const finalDirs = compose(uniq, concat(directories))([initialPath, homePath]);
this.state = {
username,
// A list of commands <String> can be access with up-arrow
history: [],
// An ordered list of {command: '', outputs: []} visible on to the user
visibles: [],
// {directories: [], files: []}
fileSystem: initFileSystem(finalDirs, files),
// an array representation of current path
currentPath,
homePath: splitPath(homePath),
};
}
getChildContext() {
const commands = bindCommands(mergeDefault(this.props.commands), this);
return {commands};
}
render() {
return (
<Wrapper {...pickProps(this.state)} />
);
}
}
Terminal.childContextTypes = {
commands: commandsType,
};
Terminal.propTypes = {
username: PropTypes.string,
// a list of directory paths
directories: PropTypes.arrayOf(PropTypes.string),
// file content mapped to file absolute path
files: PropTypes.objectOf(PropTypes.string),
initialPath: PropTypes.string,
commands: commandsType,
};
Terminal.defaultProps = {
username: 'root',
initialPath: '/',
files: {},
directories: [],
};
export default Terminal;