"npm run XXX" でOS依存処理 (Win/Mac/Linux)
Package.json の npm-scripts に、OSごとの処理を別々に書く手順。
(1) "run-script-os" パッケージをインストールする
$ npm install -D run-script-os
(2) package.json の scripts に処理を追加
Windowsでしか動作しない例
{ "scripts": { "test": "cmd /c start index.html", } }
下記のコマンドを打つと、Windows上ならindex.htmlがブラウザで開く。でも他のOSだとコマンドが存在しないのでエラーが出る。
$ npm run test
Win/Mac/Linuxで動作する例
ステージ名にコロン+OS名をつけたものを作り、そこにOSごとの処理を追加する。
{ "scripts": { "test": "run-script-os", "test:win32": "cmd /c start index.html", "test:darwin": "open index.html", "test:linux": "xdg-open index.html" } }
下記のコマンドを打つと、どのOSでもindex.htmlがブラウザで開く。
$ npm run test
やったー😊🎉✨