如何在 Windows 上的 npm 脚本中使用变量 [英] How to use variables in npm scripts on windows

查看:121
本文介绍了如何在 Windows 上的 npm 脚本中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个裸仓库,我们可以从中开始新项目.在这个存储库中是另一个必须单独更新的存储库.每个项目都从这两个存储库开始,但有时在项目期间不应该同时更新它们.

I've got a bare repository from which we start new projects. Inside this repository is another repository that has to be updated seperately. Every project starts with these two repositories, but sometimes during the project they should not both be updated.

我正在尝试创建一个克隆父存储库的 Npm 脚本,然后它会打开文件夹并克隆子存储库.

I'm trying to create an Npm script that clones the parent repository, after that it opens the folder and clones the child repository.

脚本有一个变量代表项目和文件夹名称.

The script has a variable that represents the project and thus folder name.

基于这个答案,我想出了以下内容:

Based on this answer, I came up with the following:

"new_project": "git clone x:/parent_repo $PROJECT & cd $PROJECT & git clone x:/child_repo"

我像这样运行命令:PROJECT=new_project_name npm run new_project也试过 PROJECT="new_project_name" npm run new_project

I run the command like so: PROJECT=new_project_name npm run new_project Have also tried PROJECT="new_project_name" npm run new_project

不幸的是,这会创建一个名为 $PROJECT 的文件夹,而不是实际读取变量.

Unfortunately this creates a folder named $PROJECT in stead of actualy reading the variable.

也试过了:

"new_project": "git clone x:/parent_repo $PROJECT & cd $PROJECT & git clone x:/child_repo"

npm run new_project --PROJECT new_project_name

或运行

npm run new_project -- --PROJECT=new_project

同样的结果.

这些解决方案中的大多数似乎适用于 linux 机器,但不适用于 Windows.

Most of these sollutions seem to work on linux machines but not for Windows.

字符串中的变量似乎没有被读取.我试过

It appears the variables in the string are not being read as such. I tried

"git clone x:/parent_repo "+$PROJECT+" & cd "+$PROJECT+" & git clone x:/child_repo" 

但这只是给了我语法错误.

But that just gave me syntax errors.

如何从命令行成功传递一个可以在我的 npm 脚本中使用的变量?

How do I succesfully pass a variable from the command line that I can use in my npm script?

命令必须是一行.

我在 Windows 上使用 Git bash 作为 cli

npm 版本:2.14.20

推荐答案

所以,我链接的答案工作,由于操作系统的不同,似乎在我的机器上失败了.

So, the answer I linked which was not working, seemed to fail on my machine because of a difference in operating systems.

这适用于 Linux:

This works on Linux:

"new_project": "git clone x:/parent_repo $PROJECT & cd $PROJECT & git clone x:/child_repo"

PROJECT=new_project_name npm run new_project

要使其在 Windows 上运行,您需要使用 % % 而不是 $ 对变量进行转义.您还需要在 windows cli 上专门设置变量.要与脚本结合,需要用&&链接脚本和设置变量命令.

To get this to work on Windows, you need to escape the variable with % % in stead of $. Also you need to specificaly SET the variable on the windows cli. To combine with the script, you need to link the script and the set variable command with &&.

所以上面在 Windows 上变成了:

So the above on Windows becomes:

"new_project": "git clone x:/parent_repo %PROJECT% & cd %PROJECT% & git clone x:/child_repo"

SET PROJECT=new_project_name &&npm run new_project

另请注意,如果您想引用配置变量,您必须以相同的方式转义该变量.所以 $npm_package_config_yourvariable 在 Windows 上变成了 %npm_package_config_yourvariable%.

Also note, if you want to refer to the config variables you have to escape that variable in the same way. So $npm_package_config_yourvariable becomes %npm_package_config_yourvariable% on Windows.

这篇关于如何在 Windows 上的 npm 脚本中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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