![0ee472c44bf38b840df9a25910fa21be.png](https://i-blog.csdnimg.cn/blog_migrate/15e2287de8db3b625f7e1257a5483892.jpeg)
React可以写命令行,你知道吗?
一、介绍
命令行工具,大家应该比较熟悉了。比如我们平常使用的vue-cli、create-react-app、angular-cli等工具,这类都是命令行工具。
一个好的命令行工具,能给对应的框架加分,也同时显示出了框架开发者的水平。
那么,如何写出一个好看的命令行呢?
本文给大家介绍一些,如何使用React来写命令行,来达到更好用的命令行的效果。
二、入门实践
2.1 新建工程
// 创建一个工程路径
mkdir helloworld
// 进入这个目录
cd helloworld
// 初始化npm
npm init -y
// 安装依赖
npm install ink react import-jsx -s
2.2 编写代码
我们创建index.js文件
'use strict';
require('import-jsx')('./hello');
然后我们再新建一个hello.js:
'use strict';
const React = require('react');
const {render, Color} = require('ink');
class Counter extends React.Component {
constructor() {
super();
this.state = {
counter: 0
};
}
render() {
return (
<Color green>
{this.state.counter} tests passed
</Color>
);
}
componentDidMount() {
this.timer = setInterval(() => {
this.setState(prevState => ({
counter: prevState.counter + 1
}));
}, 100);
}
componentWillUnmount() {
clearInterval(this.timer);
}
}
render(<Counter/>);
文件结构如下:
![11c77c5d99ff3ea8692bc40297a1ffa1.png](https://i-blog.csdnimg.cn/blog_migrate/34140813f04dbe61644a357746ef404c.jpeg)
2.3 运行代码
我们运行我们的代码
node ./index.js
然后我们看到:
![ec56778b3a17bdf00f3c38488d1e2bec.png](https://i-blog.csdnimg.cn/blog_migrate/1530b0ef3fe2b8c2bf55593285d71248.png)
然后tests前面的数字不停地在变化,然后按ctrl + c 可以退出我们的命令行。
2.4 解释
写过react的很好理解,render函数里面就是会输出原先的界面信息,内部用到了state.counter的变量。
由于在componentDidMount这个生命周期创建了一个定时器,每100ms修改一下这个变量。
最终我们看到了tests passed前面的数字不停的在变化。
如果我们想要让他输出一下,就退出,那就把这个定时器删除就好了。
三、进阶实践
前面我们看到了一个基本的react写一个命令行程序的例子。下面让我们深入学习
4.1 文档地址
我们看到上面我们用的最核心的是ink,大家可以打开ink的地址:https://github.com/vadimdemedes/ink
4.2 普通组件
进阶篇,我们可以首先看一下作者给我们提供了哪些组件:
- Box
- Color
- Text
4.3 生态组件
- Input组件:https://github.com/vadimdemedes/ink-text-input
![db0c91f2d86e8de19ce49a60398aaf5a.gif](https://i-blog.csdnimg.cn/blog_migrate/3a6ba3fa1dd176a120a90e30d3403ad8.gif)
2. 旋转Spinner组件: https://github.com/vadimdemedes/ink-spinner
![2c0006114896161b1f86c19987b60adc.gif](https://i-blog.csdnimg.cn/blog_migrate/38121812889f8c28b1f4789d5875ab8e.gif)
3. 选择组件:https://github.com/vadimdemedes/ink-select-input
![d217a40548e05708ef9bfe0e30eee8e5.gif](https://i-blog.csdnimg.cn/blog_migrate/d6dc9b98d8bdb22d9fed04247b261387.gif)
4. 超链接组件:https://github.com/sindresorhus/ink-link
![bf0222697de08d303347800eb316c594.png](https://i-blog.csdnimg.cn/blog_migrate/6a48a251023cdb723dab387e2f9e7120.jpeg)
5. 大banner组件:https://github.com/sindresorhus/ink-box
![094bf6de113fef5a5cc76cac7a7e4447.png](https://i-blog.csdnimg.cn/blog_migrate/aeb4d7a18f7401afe51d6938e9ae7882.jpeg)
6.颜色渐进组件:https://github.com/sindresorhus/ink-gradient
![9a9e8047e32f90d9e4448a6a90d6a213.png](https://i-blog.csdnimg.cn/blog_migrate/8622f236ec04d371d795d01df28cdd51.jpeg)
7. 大字组件:https://github.com/sindresorhus/ink-big-text
![9a9e8047e32f90d9e4448a6a90d6a213.png](https://i-blog.csdnimg.cn/blog_migrate/8622f236ec04d371d795d01df28cdd51.jpeg)
8. 图片显示组件:https://github.com/kevva/ink-image
![9ecbab3250a49d86457f5b5219c0acb5.png](https://i-blog.csdnimg.cn/blog_migrate/e0247a442375ce7240dcd595de546e8f.jpeg)
9.tab选择组件:https://github.com/jdeniau/ink-tab
![167e082876eeeba7ed3a14ef642dcc70.png](https://i-blog.csdnimg.cn/blog_migrate/b44f1ca7d9e9f631c4aef2d97bcab5ce.jpeg)
10.带背景色的组件:https://github.com/LitoMore/ink-color-pipe
![48f19063ad73b0045c0ab80a01b29bc9.png](https://i-blog.csdnimg.cn/blog_migrate/f5e1db5c494ebf32fc8b9bb76bb18c39.jpeg)
11. 多选组件:https://github.com/karaggeorge/ink-multi-select
![9b472873cc9a8e6b6730d115117c1115.gif](https://i-blog.csdnimg.cn/blog_migrate/3f2590b8ba29ce75be5e843998d3d3e0.gif)
![9b472873cc9a8e6b6730d115117c1115.gif](https://i-blog.csdnimg.cn/blog_migrate/3f2590b8ba29ce75be5e843998d3d3e0.gif)
12. 分割线组件:https://github.com/JureSotosek/ink-divider
![d00754aa8f36f0b9e217377726eee1f2.png](https://i-blog.csdnimg.cn/blog_migrate/0c5ec909952cf3f5fec0c8978839d614.jpeg)
四、可学习的软件
4.1 命令行输出emoji的工具
代码地址:https://github.com/sindresorhus/emoj
效果:
![52a6b5556d0cf0635271e36161e8665d.gif](https://i-blog.csdnimg.cn/blog_migrate/e3d018f7673b9dcb40c819ed00e03822.gif)
4.2 命令行查找npm包的工具
代码地址:https://github.com/maticzav/emma-cli
效果:
![18d91c584ca70a887fb7252f7ffcb78e.gif](https://i-blog.csdnimg.cn/blog_migrate/a85238047aba11788cb94a5820a55766.gif)
4.3 命令行查找changelog工具
代码地址:https://github.com/jdeniau/changelog-view
![11f57d20b653d2f05f1e79f3ca290bbd.png](https://i-blog.csdnimg.cn/blog_migrate/c2d41162ffb57a0ed7b11f9fa537ff0c.jpeg)
总结
所以本文主要介绍了如何使用React去开发炫酷的命令行工具。
如果听了之后,对你有所启发和帮助,希望能帮忙转发一下。本公众号特点:分享Node相关好玩好用的东西,希望各位看官关注下面公众号,如果有问题,也可以发送问题给我。
![bb605269d999c9fe77e1f58ca308e8aa.png](https://i-blog.csdnimg.cn/blog_migrate/b49c4fae1d4e843b6a9694bb76e6353e.jpeg)