Why some NPM script argument syntax in echo message work and others don't?

Viewed 51

I wanted to use a dynamic message print call to trim NPM scripts, but stumbled on some curious differences.

My package.json looks like following:

"scripts": {
    "foo": "echo \"\033[0;31m****\n This is foo message \n****\" && npm run test",
    "bar": "echo \"\033[0;31m****\n This is bar message \n****\" && npm run compile",
    "baz": "echo \"\033[0;31m****\n This is baz message \n****\" && npm run dev"
}

To avoid repetition and shorten the scripts I implemented new script with $npm_config_argument to make package.json scripts as below:

"scripts": {
    "echomsg": "echo \"\033[0;31m****\n This is $npm_config_msg message \n****\",
    "foo": "npm run echomsg --msg=foo && npm run test",
    "bar": ...
}

as per this article.

Now I should be able to call npm run foo, but this didn't work. Instead, it prints

> echo "\033[0;31m****************
 This is $npm_config_msg message
****************" "silent" "foo"

****************
 This is  message
**************** silent foo

I found out that the issue was with $npm_config_<ARGUMENT>, because with

    "echomsg": "echo \"\033[0;31m****\n This is $npm_config_messag message \n****\",
    "foo": "npm run echomsg --messag=foo && npm run test",

I get

> echo "\033[0;31m****************
 This is $npm_config_messag message
****************"

****************
 This is  message
****************

and with

    "echomsg": "echo \"\033[0;31m****\n This is $npm_config_message message \n****\",
    "foo": "npm run echomsg --message=foo && npm run test",

or with

    "echomsg": "echo \"\033[0;31m****\n This is $npm_config_myvar message \n****\",
    "foo": "npm run echomsg --myvar=foo && npm run test",

I finally get what I wanted:

> echo "\033[0;31m****************
 This is $npm_config_message/myvar message
****************"

****************
 This is foo message
****************

So the question is: Why this happens?
What's with the argument syntax that does not accept "msg" or "messag" but some other strings are fine?

0 Answers
Related