かもメモ

自分の落ちた落とし穴に何度も落ちる人のメモ帳

npm / yarn npm script でオプションを渡す方法の違いのメモ

npm script で実行するアクションにオプションをコマンドラインから渡す方法が npm と yarn で微妙に違って戸惑ったのでメモ

npm script

{
  "scripts": {
    "test": "jest"
  }
}

jest を実行する test コマンドがあり、GitHub actions では coverage を表示させるようにしたい

npm (npm@2.0.0以降)なら -- を付けてその後にオプションを書く

jobs:
  - name: Run test
    run: npm run test -- --ci --json --coverage --coverageReporters=\"json-summary\" --outputFile=coverage.json

-- を書き忘れるとその後のオプションは無視される

npm run-script <command> [-- <args>]
Use -- to pass --prefixed flags and options which would otherwise be parsed by npm.
For example: npm run test -- --grep="pattern"
cf. npm-run-script | npm Docs

As of npm@2.0.0, you can use custom arguments when executing scripts. The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script:
cf. npm-run-script | npm Docs: Version 6.14.18 (Legacy Release)

yarn ならそのままオプションを書いて OK

jobs:
  - name: Run test
    run: yarn test --ci --json --coverage --coverageReporters=\"json-summary\" --outputFile=coverage.json

yarn と npm が混在してるプロジェクト混乱する…
おわり


[参考]

葬送のフリーレンって さよならの朝に約束の花をかざろう 系?