bash 带参数的纱线运行脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50010211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:54:15  来源:igfitidea点击:

yarn run script with parameters

bashyarn

提问by thatsIT

How do I pass a parameter? When I run "yarn generate" it will make both a "-p" and a "test" directory. But it works well when I run "mkdir -p test" in bash. I tried to [-p] as well but it only creates that directory.

如何传递参数?当我运行“yarn generate”时,它会同时生成“-p”和“test”目录。但是当我在 bash 中运行“mkdir -p test”时它运行良好。我也尝试 [-p] ,但它只创建该目录。

"scripts": {
    "generate": "mkdir -p test"
  }

回答by Artem Rapoport

Although I could not reproduce the issue that you mentioned (my config: node v8.11.1and yarn v1.2.1, latest MacOS), according to the yarn docs, you can pass the arguments to yarnscript by appending them normally, like so:

尽管我无法重现您提到的问题(我的配置:node v8.11.1yarn v1.2.1最新的 MacOS),但根据yarn docs,您可以通过yarn正常附加参数将参数传递给脚本,如下所示:

yarn generate -p test

In this case your npm (yarn) scripts config (in the package.json, I assume) would look like

在这种情况下,您的 npm(纱线)脚本配置(package.json我假设在 .

"scripts": {
    "generate": "mkdir"
}

If you're using Windows, you indeed won't have the mkdir -pflag (read this). In order to make what you want (check if the folder does not exist and if so, create one) you'd need to use some cmd commands. So your package.json will contain smth like

如果您使用的是 Windows,则您确实不会拥有该mkdir -p标志(请阅读本文)。为了制作您想要的东西(检查文件夹是否不存在,如果存在,请创建一个),您需要使用一些 cmd 命令。所以你的 package.json 将包含像

"scripts": { 
    "generate": "IF NOT EXIST test mkdir test" 
}