npm-scripts の if 文ワンライナーで else の処理に何も書かなかったら怒られる件


事象

タイトルの通りです。

実行環境

$ node -v
v14.13.1

$ npm -v
6.14.8

実験

以下の3パターンを用意。

  • hoge:a
    • else の処理あり。
  • hoge:b
    • else をつけるが処理を省略。
  • hoge:c
    • else ごと省略。
package.json
{
  (省略)
  "scripts": {
    "hoge:a": "if TRUE; then echo hogehoge; else echo mogemoge; fi",
    "hoge:b": "if TRUE; then echo hogehoge; else; fi",
    "hoge:c": "if TRUE; then echo hogehoge; fi"
  },
  (省略)
}

hoge:a(else の処理あり)

もちろん怒られない。

$ npm run hoge:a

> [email protected] hoge:a /Users/sugurutakahashi/git/vpass_api_spec_document
> if TRUE; then echo hogehoge; else echo mogemoge; fi

hogehoge

hoge:b(else をつけるが処理を省略)

怒られる。

$ npm run hoge:b

> [email protected] hoge:b /Users/sugurutakahashi/git/vpass_api_spec_document
> if TRUE; then echo hogehoge; else; fi

sh: -c: line 0: syntax error near unexpected token `;'
sh: -c: line 0: `if TRUE; then echo hogehoge; else; fi'
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] hoge:b: `if TRUE; then echo hogehoge; else; fi`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the [email protected] hoge:b script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/sugurutakahashi/.npm/_logs/2020-11-03T15_03_31_621Z-debug.log

hoge:c(else ごと省略)

怒られない。

$ npm run hoge:c

> [email protected] hoge:c /Users/sugurutakahashi/git/vpass_api_spec_document
> if TRUE; then echo hogehoge; fi

hogehoge
SuguruTakahashiMBP sugurutakahashi ~/git/vpass_api_spec_document $ 

まとめ

npm-scriptsのif文ワンライナーで else の処理を省略する場合は else とは書いてはいけない。