Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How execute bash Command Substitution in npm script

I want to execute linter only on staged files.

For this i wrote inside package.json the command(inside scripts):

"fix-lint": "tslint $(git diff --name-only --staged) "

Execute it:

npm run fix-lint

I have an error:

error: unknown option --name-only

I tried to figure out and try simple command:

"lss": "echo $(ls)" 

And again execute it:

npm run lss

The result is printing $(ls) instead of printing all files in directory.

My question is how to fix this commands/scripts? Or maybe in general how execute bash Command Substitution in npm script.

Thanks in advance.

like image 319
Baruch Levin Avatar asked Jul 14 '26 23:07

Baruch Levin


1 Answers

Just posting @jordanm comment to mark this question as resolved.

Use:

"fix-lint": "sh -c 'tslint $(git diff --name-only --staged)'"

To store the output of a command to a datestamped file, you can use tee and date like this:

"scripts": {
  "wdio": "wdio run ./wdio.conf.ts | sh -c 'tee output/spec-reports/scan-$(date \"+%Y-%m-%d--%H-%M\").txt'",
}
like image 184
Chris Happy Avatar answered Jul 18 '26 05:07

Chris Happy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!