package.jsonとyarn.lockのバージョンを一致させたい

数年前に作ったプロジェクトの依存関係、更新したい!なときに。

依存を更新しつつ、package.json もアップデート

yarn のコマンドでアップグレード。
対話形式で進められます。

yarn upgrade-interactive --latest  

--latest オプションをつけると、package.json に記載されている範囲を超えてアップグレードされます。
他のモジュールとの互換性問題がでてくるのでちょっとデンジャラス。
またpackage.jsonも更新されます。

--latest オプションなし

package.jsonに記載されている範囲でアップグレードされます。
でも--latestオプションなしだとpackage.jsonが更新されないので、今どのバージョンが使用されているのかがわかりにくいです。
いちいちyarn.lock見るのもだりぃし。

syncyarnlock ツールを使う

ここで便利ツール、syncyarnlockの登場です。
実際にインストールされているモジュールのバージョンを書き出してくれます。

# グローバルインストール  
npm i -g syncyarnlock  

# プロジェクトで実行  
syncyarnlock  

実行するとpackage.json.yarnが作成され、ここにカレントのバージョン番号がリスティングされています。
リストされているだけで package.json が更新されるわけじゃないのですけれど。

syncyarnlock -h  

でヘルプが表示されました。

Usage: index [options]  

Sync `yarn.lock` package versions, into package.json  

Options:  
  -V, --version                  output the version number  
  -d, --dir <path>               directory path where the yarn.lock file is located (default to current directory)  
  -p, --dirPackageJson <path>    directory of project with target package.json, if not set, -d will be used  
  -s, --save                     By default don't override the package.json file, make a new one instead package.json.yarn  
  -k, --keepPrefix               By default the ^ or any other dynamic numbers are removed and replaced with static ones.  
  -g, --keepGit                  By default direct git repositories are also replaced by the version written in yarn.  
  -l, --keepLink                 By default direct link: repositories are also replaced by the version written in yarn.  
  -a, --keepVariable <variable>  By default everything is converted to yarn version, write a part of the type you wish not to convert, seperate by comma if more than one, to not replace git  
and link you would use +,link:  
  -h, --help                     output usage information  

package.jsonへの反映は-sオプションが必要みたいです。
-kオプションで^などのプレフィクスも残してくれるので、-s -k オプションをつけると良さそうです。

# 更新しつつ、バージョン指定のプレフィクスも残す  
syncyarnlock -s -k  

プロジェクトで「ソフトウェア一覧(バージョン付き)出して」って言われたときに便利です。
早速、今日使いました。

Thanx!!

syncyarnlock - npm

Subscribe to 猫好きが猫以外のことも書く

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe