如何从npm脚本通过管道传递到bash别名? [英] How can I pipe to a bash alias from an npm script?

查看:108
本文介绍了如何从npm脚本通过管道传递到bash别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.bashrc中有一个Bunyan别名:

I have an alias in my .bashrc for bunyan:

$ alias bsh
alias bsh='bunyan -o short'

此行在bash中运行良好:

This line runs fine in bash:

$ coffee src/index.coffee | bsh

但是如果我在脚本"中放同样的东西

But if I put the same thing in 'scripts'

"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中转义管道字符.

So at random I tried putting in || instead of | and it worked. I can't figure out why though. I don't have to escape pipe characters in JSON as far as I know.

但是实际上并没有将输出通过管道传递给bsh别名.

However it doesn't actually pipe the output to the bsh alias.

实际的解决方法是使用"coffee":"coffee src/index.coffee | bunyan -o short"-完全摆脱别名.

The actual fix is to use "coffee":"coffee src/index.coffee | bunyan -o short" -- get rid of the alias completely.

如何在npm脚本中使用bash别名?

How can I use a bash alias in an npm script?

推荐答案

您可以创建函数而不是别名.

You can create a function instead of an alias.

function bsh() {
    bunyan -o short
}
export -f bsh

导出将使其可用于子进程.

The export will make it available to children processes.

这篇关于如何从npm脚本通过管道传递到bash别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆