指定Yarn用于运行脚本的shell

时间:2017-05-04 09:13:36

标签: node.js bash shell sh yarnpkg

我的package.json中有一个脚本,如下所示:

"buildTslint": "node_modules/typescript/bin/tsc node_modules/awesomeLibrary_node_tslint/{,helpers/}*.ts",

请注意{,helpers/}*.ts部分,此部分称为Brace Expansion,仅适用于bash,而非sh

运行yarn buildTslint时,我得到以下输出:

# yarn buildTslint
yarn buildTslint v0.22.0
$ node_modules/typescript/bin/tsc node_modules/awesomeLibrary_node_tslint/{,helpers/}*.ts
error TS6053: File 'node_modules/awesomeLibrary_node_tslint/{,helpers/}*.ts' not found.
error Command failed with exit code 2.

似乎Yarn使用sh来执行这些脚本,但我想使用bash来实现此功能,以便能够使用大括号扩展。

3 个答案:

答案 0 :(得分:1)

它可以使用system函数启动命令,另请参阅man 3 system

查看使用了哪个系统调用:

strace yarn ...

system使用fork + exec + wait,exec系列函数使用shell /bin/sh

使用bash命令可以更改为bash -c 'command ..'

答案 1 :(得分:1)

Yarn还没有提供配置用于运行脚本的默认shell的方法,请参阅:https://github.com/yarnpkg/yarn/issues/4248

但是,由于yarn使用Node来生成其进程,您可以通过更改Node本身使用的默认shell来解决这个问题。

在Ubuntu上,如果您拥有root权限,则可以通过将符号链接/bin/sh更改为指向dash以外的其他内容来执行此操作:

sudo ln -sf /bin/bash /bin/sh

在Windows中的Git-bash中,您可以将COMSPEC环境变量更改为C:\Windows\system32\cmd.exe以外的其他内容,但我还没有让它为我工作。

另见:

答案 2 :(得分:1)

新配置参数script-shell

纱线版本​​1.19 added support。您现在可以执行以下操作:

yarn config set script-shell /bin/bash