覚えたら書く

IT関係のデベロッパとして日々覚えたことを書き残したいです。twitter: @yyoshikaw

Yarnを使ってみる

最近になって今更感かなりありますが、electronNode.js を触り始めました。
パッケージを環境に導入するには、npmコマンドを利用するのが当然だと思ってたんですが、
Yarnというツールも存在している様ですね。いやー、この界隈全然詳しくないです。

Yarnの方が高速だとかという話もあるので、Yarn使っていこうかと思います。


インストール

Macへのイントールならhomebrew使えば簡単ですね。以下実行!

brew install yarn


実行結果は以下の通りです。

$ brew install yarn
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (ethereum/ethereum and homebrew/core).
==> New Formulae
azure-storage-cpp   gitmoji             i386-elf-gcc        mesa                ruby@2.4            um
fluxctl             healpix             kubespy             node@10             shellz
fx                  i386-elf-binutils   maven@3.5           resin-cli           sourcedocs
==> Updated Formulae

・・・(中略)・・・

==> Renamed Formulae
mat -> mat2
==> Deleted Formulae
corebird    heroku      kibana@4.4  maven@3.0   maven@3.1   nethack4    pxz         ruby@2.2    taylor      tcptrack

==> Installing dependencies for yarn: icu4c and node
==> Installing yarn dependency: icu4c
==> Downloading https://homebrew.bintray.com/bottles/icu4c-62.1.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring icu4c-62.1.sierra.bottle.tar.gz
==> Caveats
icu4c is keg-only, which means it was not symlinked into /usr/local,
because macOS provides libicucore.dylib (but nothing else).

If you need to have icu4c first in your PATH run:
  echo 'export PATH="/usr/local/opt/icu4c/bin:$PATH"' >> ~/.bash_profile
  echo 'export PATH="/usr/local/opt/icu4c/sbin:$PATH"' >> ~/.bash_profile

For compilers to find icu4c you may need to set:
  export LDFLAGS="-L/usr/local/opt/icu4c/lib"
  export CPPFLAGS="-I/usr/local/opt/icu4c/include"

==> Summary
🍺  /usr/local/Cellar/icu4c/62.1: 250 files, 67.3MB
==> Installing yarn dependency: node
==> Downloading https://homebrew.bintray.com/bottles/node-11.2.0.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring node-11.2.0.sierra.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Summary
🍺  /usr/local/Cellar/node/11.2.0: 3,936 files, 50.1MB
==> Installing yarn
==> Downloading https://yarnpkg.com/downloads/1.12.3/yarn-v1.12.3.tar.gz
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.com/49970642/a4875000-e25c-11e8-88b4-45
######################################################################## 100.0%
🍺  /usr/local/Cellar/yarn/1.12.3: 14 files, 4.7MB, built in 9 seconds
==> Caveats
==> icu4c
icu4c is keg-only, which means it was not symlinked into /usr/local,
because macOS provides libicucore.dylib (but nothing else).

If you need to have icu4c first in your PATH run:
  echo 'export PATH="/usr/local/opt/icu4c/bin:$PATH"' >> ~/.bash_profile
  echo 'export PATH="/usr/local/opt/icu4c/sbin:$PATH"' >> ~/.bash_profile

For compilers to find icu4c you may need to set:
  export LDFLAGS="-L/usr/local/opt/icu4c/lib"
  export CPPFLAGS="-I/usr/local/opt/icu4c/include"

==> node
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d


Windowsの場合は、Windows用のインストーラをダウンロードして実行してください。


インストール後は、一応バージョン確認を実施

$ yarn -v
1.12.3


Yarnの操作

Yarnnpmで実行できる操作は、基本的にできるようです。


例えばプロジェクトの初期化をするなら

yarn init

詳細情報の入力を省略するなら以下です

yarn init -y

これでpackage.json.jsonが作成されます。

$ cat package.json 
{
  "name": "develop",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT"
}


開発環境に必要なパッケージをローカルインストールする(devDependenciesに追加したい時)には以下を実行します

yarn add [package] --dev

electronを対象にするなら以下の通りです

$ yarn add electron --dev
yarn add v1.12.3
info No lockfile found.
[1/4] 🔍  Resolving packages...
warning electron > electron-download > nugget > progress-stream > through2 > xtend > object-keys@0.4.0: 
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 📃  Building fresh packages...
success Saved lockfile.
success Saved 124 new dependencies.
info Direct dependencies
└─ electron@3.0.10
info All dependencies
├─ @types/node@8.10.38
├─ ajv@6.5.5
├─ ansi-regex@2.1.1
├─ array-find-index@1.0.2
├─ asn1@0.2.4
├─ asynckit@0.4.0

・・・(中略)・・・

├─ xtend@2.1.2
└─ yauzl@2.4.1
✨  Done in 54.26s.


実行環境に必要なパッケージをローカルインストールする(dependenciesに追加したい時)には以下を実行します

yarn add [package]

electron-logを対象にするなら以下の通りです

$ yarn add electron-log
yarn add v1.12.3
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 📃  Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ electron-log@2.2.17
info All dependencies
└─ electron-log@2.2.17
✨  Done in 0.74s.


これらを実行した後のpackage.jsonは以下の様になっています。

{
  "name": "develop",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "devDependencies": {
    "electron": "^3.0.10"
  },
  "dependencies": {
    "electron-log": "^2.2.17"
  }
}

ディレクトリ構成は以下の通りです。

ls -l
total 88
drwxr-xr-x  138 yuki  staff   4692 11 23 21:19 node_modules
-rw-r--r--    1 yuki  staff    197 11 23 21:19 package.json
-rw-r--r--    1 yuki  staff  37898 11 23 21:19 yarn.lock


まとめ

だいぶ単純な操作しかしてませんが、yarnnpm の代わり(+それ以上の操作)ができる様です。
これからはYarnを利用していこうと思います。



関連エントリ