Considering we have NPM script with a pipe, similarly to what's suggested in Istanbul documentation:
"coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls"
It obviously doesn't take Windows into account.
How can such command with a pipe be made cross-platform in Node.js package?
The question is specific to Coveralls but isn't limited to it; this could be any command with a pipe.
Both Bash and the Windows command line (e.g. cmd.exe and PowerShell) have the pipe operator (
|) so that shouldn't be a concern regarding cross-platform compatibility.Given your example npm-script, the primary incompatibility regarding cross-platform support is usage of Bash's
catcommand. Usage ofcatwill fail via Windows cmd.exe, howevercatis supported via Windows PowerShell.To circumvent the aforementioned cross-platform issue regarding
cat, consider utilizing a nodejs utility script as follows. Let's name the filecat.js:cat.js
As you can see, it utilizes nodes builtin:
fs.readFileto read the contents of a file.process.argv.process.stdoutNote: For the sake of brevity
cat.jsdoesn't include any error capturing/handling, so you may wish to add some.npm script
Then in your
scriptssection of your package.json we invokecat.jsand pass the path to the file (i.e../coverage/lcov.info) as an argument. For instance:Note: The npm-script above assumes
cat.jsresides in the same directory and level as package.json. If you choose to locate it elsewhere the path to it will need to be redefined. E.g."node path/to/cat ./coverage/lcov.info | ..."So long as the nodejs file specified on the right hand of the pipe (
|) utilizesprocess.stdinto read fromstdin, i.e file descriptor0, (as coveralls.js does) using the pipe cross-platform will be OK.