0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-11-03

事象

タイトルの通りです。

実行環境

$ 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

> vpass_api_spec_document@1.0.0 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

> vpass_api_spec_document@1.0.0 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! vpass_api_spec_document@1.0.0 hoge:b: `if TRUE; then echo hogehoge; else; fi`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the vpass_api_spec_document@1.0.0 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

> vpass_api_spec_document@1.0.0 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 とは書いてはいけない。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?