简体   繁体   English

如何从npm脚本管道到bash别名?

[英]How can I pipe to a bash alias from an npm script?

I have an alias in my .bashrc for bunyan: 我的.bashrc中有别名为bunyan:

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

This line runs fine in bash: 这行在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"
  },

And npm run coffee , it fails: 并且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)

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. 据我所知,我不必在JSON中转义管道字符。

However it doesn't actually pipe the output to the bsh alias. 但是它实际上并没有将输出传递给bsh别名。

The actual fix is to use "coffee":"coffee src/index.coffee | bunyan -o short" -- get rid of the alias completely. 实际的解决方法是使用"coffee":"coffee src/index.coffee | bunyan -o short" - 彻底摆脱别名。

How can I use a bash alias in an npm script? 如何在npm脚本中使用bash别名?

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. 出口将使其可用于儿童流程。

So I had a whole response typed up about using 所以我对使用打了一个完整的回复

. ~/.bash_aliases && coffee src/index.coffee | bsh

But it turns out that aliases are barely, if at all, supported in bash scripts. 但事实证明,在bash脚本中,别名几乎不受支持。 From what I have read, aliases are deprecated in favor of functions... 从我所读到的,别名被弃用以支持函数...

See this discussion for what convinced me to use functions instead of aliases. 请参阅此讨论,以确定我使用函数而不是别名。 I tried for an hour or two to get aliases to work by testing with /bin/bash -c as well as npm run, with no luck. 我尝试了一两个小时来通过使用/ bin / bash -c以及npm run进行测试来获取别名,但没有运气。 However, using a function as suggested by Diego worked immediately and without problems. 但是,使用Diego建议的功能可以立即工作并且没有问题。

I am including this even though the question is already marked as answered in case someone as stubborn as me winds up here from google and decides to try to make aliases work instead of just using a function. 即使这个问题已被标记为已回答,以防有人像我一样顽固地从谷歌到这里,并决定尝试使别名工作而不仅仅是使用一个函数,我也包括这个。

However, I did run into a problem specifically when trying to use this with npm scripts. 但是,在尝试使用npm脚本时,我确实遇到了一个问题。 Even with the export -f, my functions aren't recognized - I still had to manually include the bash_aliases file, and even then, I got an error about the -f option for export. 即使使用导出-f,我的函数也无法识别 - 我仍然需要手动包含bash_aliases文件,即使这样,我也会出现关于导出的-f选项的错误。

In order to actually get this working, I had to take out the function export line and manually include the bash_aliases file... 为了实际使这个工作,我不得不取出函数导出行并手动包含bash_aliases文件...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM