问题描述
如何从package.json
脚本"执行PowerShell ps1脚本?
How can I execute PowerShell ps1 scripts from package.json
"scripts"?
我知道如何在package.json"scripts"中设置基本脚本.例如,使用以下配置,我可以执行npm run test
,它将向控制台输出这仅是测试":
I know how to set up a basic script in package.json "scripts". For example with the following configuration, I can exec npm run test
which will output "this is only a test" to the console:
"scripts": {
"test": "echo \"this is only a test\""
}
但是,在我想执行PowerShell脚本的情况下,我有一个更高级的方案.像这样:
However, I have a more advanced scenario where I'd like to execute a PowerShell script. Something like this:
"scripts": {
"buildAngular": "buildAngular.ps1"
}
我可以通过scripts对象执行这样的ps1脚本吗?是否需要任何特殊的设置/配置?还有其他限制吗?
Can I exec a ps1 script like this via the scripts object? Is any special setup/config required? Are there any additional constraints?
推荐答案
假设powershell位于您的PATH中,您可以这样称呼它:
Assuming powershell is in you PATH you can call it like this:
"scripts": {
"test": "@powershell -NoProfile -ExecutionPolicy Unrestricted -Command ./test.ps1"
}
在Windows 7和Powershell v4上进行了测试.
Tested on Windows 7 with Powershell v4.
限制是您需要安装Powershell并在PATH中.我没有在Linux的Powershell上进行过测试,所以不确定现在是否可以将该解决方案移植到其他平台上.
Limitations are that you need Powershell installed and in your PATH. I didn't test it with Powershell for Linux, so not sure if this solution will be portable to other platform for now.
这篇关于如何从package.json“脚本"执行powershell ps1脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!