PowerShell脚本为构建Angular CLI项目添加“--prod”参数

3

我正在创建PowerShell脚本以自动构建Angular CLI项目。当我在我的.ps1文件中使用以下命令:

Start-Process -FilePath ng build -WorkingDirectory D:\pathToAngularProject -Wait

它运行正常,但不会生成混淆的JavaScript构建文件。为了创建生产构建文件,我需要添加“--prod”参数。 但是当我将上面的示例更改为:

Start-Process -FilePath ng build -ArgumentList '--prod' -WorkingDirectory D:\pathToAngularProject -Wait

我遇到了错误: enter image description here 有没有人有建议如何在PowerShell脚本中添加参数到'ng build'命令?

4
ng build 是什么?如果它是一个文件,请用引号将其包裹起来 "ng build";如果它是一个参数,请将它添加到参数列表 -ArgumentList '--prod','build' - Nico Nekoru
2个回答

5

Neko Musume的有用回答提供了解决您当前问题的方法。

然而,值得退一步思考:

要同步执行控制台应用程序或批处理文件,请直接调用它们(ng build ...& ng build ...),不要使用Start-Process - 请参阅此答案此GitHub文档问题详细说明适当与非适当的使用情况,并请求将指导添加到Start-Process帮助主题中。

因此:

# Execute ng synchronously, with its output streams connected to PowerShell's
ng build --prod D:\pathToAngularProject

关于你尝试的事情:
补充一下Neko的回答:
你的第一个命令能够成功执行的原因是以下这些:
Start-Process -FilePath ng build

等同于:

Start-Process -FilePath -FilePath ng -ArgumentList build

也就是说,build 参数在绑定时是按位置的方式进行的,无需显式命名其目标参数 -ArgumentList

利用这一点,还有第一个位置参数隐含地指定了 -FilePath,你的问题的即时解决方案可以简化为:

# The 1st positional argument, 'ng', binds to -FilePath
# The 2nd positional argument, the *array* of arguments to pass to 'ng',
# 'build' and '--prod', binds to -ArgumentList
Start-Process ng  build, --prod

尽管如此,Start-Process存在一个长期存在的bug,导致它错误地传递带有嵌入空格的参数 - 请参见GitHub问题#5576。 为了保持向后兼容性,这个bug可能不会被修复(除非通过引入一个新参数)。

因此,最好将参数作为一个单一数组元素进行传递,实际上作为一个命令行(不包括可执行文件),以编码所有参数,如果需要,可以使用嵌入的双引号("..."正确地标示参数之间的边界:

# Pass the arguments for 'ng' *as a single string*, potentially
# with embedded "..." quoting (not needed here).
Start-Process ng  'build --prod'

一个带有嵌入引用的例子:
# Project path needs embedded "..." quoting, because it contains spaces.
Start-Process ng  'build --prod "D:\Work Projects\Foo"' ...

一个使用可展开(双引号)字符串("..."的示例,以便嵌入变量/子表达式的值(嵌入 "字符必须转义为`"(或"")):
$projDir = 'D:\Work Projects'
Start-Process ng  "build --prod `"$projDir\Foo`"" ...

关于“引用数组元素”的一般说明:
由于PowerShell中的“命令参数”是使用所谓的“参数(解析)模式”(类似于shell)进行解析的,因此(隐含的)-ArgumentList的(字符串)元素通常不需要引号。
也就是说,在“参数模式”中,数组build, --prod等同于在“表达式模式”(类似编程语言)中的'build', '--prod'。
有关PowerShell解析模式的概述,请参阅this answer
但是,在参数模式下,您也可以使用带引号的形式,并且根据元素值的不同,您可能需要引号,例如当元素包含空格或其他shell元字符时;另外,如果第一个元素看起来像是PowerShell参数名称(例如-prod而不是--prod),那么它也必须被引号引起来。
一些例子:
注意:为了简单起见,在这些例子中使用了Write-Output,它只是将每个数组元素打印在自己的行上。传递给任何命令的数组在参数模式下解析。
# No quoting needed.
# Elements contain no PowerShell metacharacters.
Write-Output one, two

# Quoting needed for the 2nd array element, due to containing
# PowerShell metacharacters (space, parentheses)
Write-Output one, 'two (2)'

# Quoting needed for the 1st array element, because it looks
# like a PowerShell parameter name.
# (Of course, you may choose to quote *both* elements in this case,
# for consistency).
Write-Output '-one', two

# If the parameter-like argument isn't the *first* array element,
# the need for quoting goes away
Write-Output one, -two

3
如果-argumentlist是一个参数,那么请加上build
Start-Process -FilePath ng -ArgumentList 'build', '--prod' -WorkingDirectory D:\pathToAngularProject -Wait 

如果路径中包含空格,请在 -path 参数中将其引用起来:

Start-Process -FilePath "ng build" -ArgumentList '--prod' -WorkingDirectory D:\pathToAngularProject -Wait

2
可执行文件本身就是 ng,它是 Angular CLI。然而,值得指出的是,Start-Process 通常不是调用 CLIs(控制台应用程序)的正确工具。 - mklement0
1
此外,虽然-ArgumentList应该是逐个传递参数的正确方式,但由于长期存在的错误(请参见此GitHub问题),在一般情况下不幸地并非如此。但由于此特定情况中的参数不包含空格,因此该命令可以正常工作。 - mklement0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接