Cross-platform pipe command in NPM script

自闭症网瘾萝莉.ら 提交于 2019-12-06 10:55:02

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 cat command. Usage of cat will fail via Windows cmd.exe, however cat is 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 file cat.js:

cat.js

const fs = require('fs');

fs.readFile(process.argv[2], function(err, data) {
  process.stdout.write(data);
});

As you can see, it utilizes nodes builtin:

  • fs.readFile to read the contents of a file.
  • The file path of the file to read will be provided as an argument to the script and captured using process.argv.
  • The contents of the file is the written to process.stdout

Note: For the sake of brevity cat.js doesn't include any error capturing/handling, so you may wish to add some.

npm script

Then in your scripts section of your package.json we invoke cat.js and pass the path to the file (i.e. ./coverage/lcov.info) as an argument. For instance:

"scripts": {
  "coveralls": "node cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls"
},

Note: The npm-script above assumes cat.js resides 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 (|) utilizes process.stdin to read from stdin, i.e file descriptor 0, (as coveralls.js does) using the pipe cross-platform will be OK.

While I have not used Windows in some time, its CMD.EXE command line processor supports command redirection as well as “piping” the output of one program to the input of another.

See Syntax Redirection for more information.

For piping programmatically, see the pipe-operator NPM module for a Node/JavaScript implementation of shell piping.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!