我的 .bashrc 中有一个 bunyan 的别名:
$ alias bsh
alias bsh='bunyan -o short'
这一行在 bash 中运行良好:
$ coffee src/index.coffee | bsh
但是如果我把同样的东西放在'脚本'中
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"coffee":"coffee src/index.coffee | bsh"
},
npm run coffee
,它失败了:
> coffee src/index.coffee | bsh
sh: bsh: command not found
events.js:141
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at exports._errnoException (util.js:870:11)
at WriteWrap.afterWrite (net.js:769:14)
所以我随机尝试放入 ||
而不是 |
并且它起作用了。我不知道为什么。据我所知,我不必转义 JSON 中的管道字符。
但是它实际上并没有将输出通过管道传递给 bsh 别名。
实际的解决方法是使用 "coffee":"coffee src/index.coffee | bunyan -o short"
—— 完全去掉别名。
如何在 npm 脚本中使用 bash 别名?
最佳答案
您可以创建函数而不是别名。
function bsh() {
bunyan -o short
}
export -f bsh
导出将使其可供子进程使用。
关于json - 如何从 npm 脚本通过管道传输到 bash 别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37308870/