LoginSignup
0
0

More than 3 years have passed since last update.

npm-scriptsでコマンドを並列に実行する

Posted at

準備

concurrentlyをインストールします。

$ npm install -D concurrently

npm-scriptsを書く

以下サンプルのbuildの行のように、concurrentlyを使ってコマンド実行するように記述します。

  • ここでは、シェルスクリプトを実行していますが、他のnpm-scriptsを指定しても構いません。
  • -nオプションで各コマンドに名前をつけるとこで、標準出力の出力がわかりやすくなります。
    以下では、./build1.shbuild1./build2.shbuild2という名前を付けています。
package.json
{
  "name": "poi",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "concurrently -n build1,build2 \"./build1.sh\" \"./build2.sh\""
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "concurrently": "^5.2.0"
  }
}

実行する

build1.shとbuild2.shを単純に数字を出力するシェルスクリプトとすると(後述)、buildを実行すると以下の出力例のようになります。

  • 出力が並列に実行されていることがわかります。
  • -nオプションをつけると各コマンドの出力が分かりやすくなっていることがわかります。
$ npm run build

> poi@1.0.0 build C:\Users\kazuhiro_otani\Downloads\poi
> concurrently -n build1,build2 "bash ./build1.sh" "bash ./build2.sh" || exit 0

[build2] 6
[build1] 1
[build2] 7
[build2] 8
[build1] 2
[build2] 9
[build2] 10
[build2] bash ./build2.sh exited with code 0
[build1] 3
[build1] 4
[build1] 5
[build1] bash ./build1.sh exited with code 0
build1.sh
for i in {1..5} ; do
    sleep 0.2
    echo ${i}
done
build2.sh
for i in {6..10} ; do
    sleep 0.1
    echo ${i}
done
0
0
0

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
0
0