ここ1週間自己紹介用のCLIアプリを書くのが流行ってる。
はじまりはいつものSindre Sorhus。
nodeが入っていれば
npx sindresorhus
すると自己紹介CLIアプリが起動。
基本的には、このレポジトリを適当に書き換えれば簡単にできる。
はじめてのnpmライブラリにちょうどいいかも知れない。
https://github.com/sindresorhus/sindresorhus
また、使っている技術がちょっとおもしろくて、ink
を使っている。
これは何かとCLIのためのReactだ。
https://github.com/vadimdemedes/ink
普通にReactでCLIを書くことができる。
なお、v0.5
がちょっと壊れている雰囲気あってエラーが出るのでv0.4
推奨です。
const {h, render, Component, Color } = require('ink');
class Counter extends Component {
constructor() {
super();
this.state = {
i: 0
};
}
render() {
return (
<Color green>
{this.state.i} tests passed
</Color>
);
}
componentDidMount() {
this.timer = setInterval(() => {
this.setState({
i: this.state.i + 1
});
}, 100);
}
componentWillUnmount() {
clearInterval(this.timer);
}
}
render(<Counter/>);
また、本家では、import-jsx
を使って実行時にばべっているのだけど、これだとインストールに時間かかるので、普通にバベるのがいいと思う。
それでは、よいCLIライフを。