LoginSignup
393

More than 5 years have passed since last update.

名刺の代わりにCLIアプリを書く

Last updated at Posted at 2018-05-15

ここ1週間自己紹介用のCLIアプリを書くのが流行ってる。
はじまりはいつものSindre Sorhus。

nodeが入っていれば

npx sindresorhus

すると自己紹介CLIアプリが起動。

基本的には、このレポジトリを適当に書き換えれば簡単にできる。
はじめてのnpmライブラリにちょうどいいかも知れない。

また、使っている技術がちょっとおもしろくて、inkを使っている。
これは何かとCLIのためのReactだ。

普通に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ライフを。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
393