Search code examples
bashnpmtslintnpm-scripts

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.


Solution

  • 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'",
    }