PowerShell 脚本添加“--prod”参数以构建 Angular CLI 项目
PowerShell script add '--prod' argument to build Angular CLI project
我正在创建 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
我收到错误:
有人建议我如何在 PowerShell 脚本中向 'ng build' 命令添加参数吗?
将 build
添加到 -argumentlist
如果它是这样的参数:
Start-Process -FilePath ng -ArgumentList 'build', '--prod' -WorkingDirectory D:\pathToAngularProject -Wait
如果它是路径的一部分,请在 -path
参数中引用它,因为有空格:
Start-Process -FilePath "ng build" -ArgumentList '--prod' -WorkingDirectory D:\pathToAngularProject -Wait
Neko Musume's helpful answer 提供了解决您眼前问题的方法。
然而,值得退一步:
要同步执行控制台应用程序或批处理文件,直接调用它们(ng build ...
或& ng build ...
),执行不使用Start-Process
- 请参阅this answer and this GitHub docs issue detailing appropriate vs. non-appropriate use cases and requesting that guidance be added to the 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
隐含在 first 位置参数中,您的问题的直接解决方案可以简化为:
# 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
有一个长期存在的 错误 导致它传递参数 时嵌入空格 错误 - 参见 GitHub issue #5576。
为了保持向后兼容性,这个错误可能不会得到修复(除了 introducing a new parameter)。
因此最好将参数作为单个数组元素传递,有效地作为命令行(没有可执行文件),其中参数之间的边界可以通过 嵌入 "..."
引用 正确地发出信号,如有必要:
# 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"' ...
使用 expandable (double-quoted) string ("..."
) 以嵌入变量/子表达式的值的示例(embedded "
字符必须转义为`"
(或""
)):
$projDir = 'D:\Work Projects'
Start-Process ng "build --prod `"$projDir\Foo`"" ...
关于引用数组元素的一般说明:
因为PowerShell中的命令参数是使用所谓的参数(解析)模式(shell-like ),(隐含的)-ArgumentList
的(字符串)元素做 not 通常需要 quoting.
也就是说,参数模式中的数组build, --prod
等价于表达式模式中的'build', '--prod'
(类似编程语言)。
有关 PowerShell 解析模式的概述,请参阅 。
但是,您也可以在参数模式下使用引号形式,并且 - 根据元素值 - 您可能 必须 引号,例如当元素包含空格或其他内容时shell 元字符;此外,如果 first 元素看起来像 PowerShell 参数名称(例如,-prod
而不是 --prod
),它也必须引用。
几个例子:
注意:为简单起见,示例中使用了 Write-Output
,它只是在其自己的行上简单地回显每个数组元素。传递给 any cmdlet 的数组在 参数模式 .
中进行解析
# 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
我正在创建 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
我收到错误:
有人建议我如何在 PowerShell 脚本中向 'ng build' 命令添加参数吗?
将 build
添加到 -argumentlist
如果它是这样的参数:
Start-Process -FilePath ng -ArgumentList 'build', '--prod' -WorkingDirectory D:\pathToAngularProject -Wait
如果它是路径的一部分,请在 -path
参数中引用它,因为有空格:
Start-Process -FilePath "ng build" -ArgumentList '--prod' -WorkingDirectory D:\pathToAngularProject -Wait
Neko Musume's helpful answer 提供了解决您眼前问题的方法。
然而,值得退一步:
要同步执行控制台应用程序或批处理文件,直接调用它们(ng build ...
或& ng build ...
),执行不使用Start-Process
- 请参阅this answer and this GitHub docs issue detailing appropriate vs. non-appropriate use cases and requesting that guidance be added to the 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
隐含在 first 位置参数中,您的问题的直接解决方案可以简化为:
# 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
有一个长期存在的 错误 导致它传递参数 时嵌入空格 错误 - 参见 GitHub issue #5576。
为了保持向后兼容性,这个错误可能不会得到修复(除了 introducing a new parameter)。
因此最好将参数作为单个数组元素传递,有效地作为命令行(没有可执行文件),其中参数之间的边界可以通过 嵌入 "..."
引用 正确地发出信号,如有必要:
# 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"' ...
使用 expandable (double-quoted) string ("..."
) 以嵌入变量/子表达式的值的示例(embedded "
字符必须转义为`"
(或""
)):
$projDir = 'D:\Work Projects'
Start-Process ng "build --prod `"$projDir\Foo`"" ...
关于引用数组元素的一般说明:
因为PowerShell中的命令参数是使用所谓的参数(解析)模式(shell-like ),(隐含的)-ArgumentList
的(字符串)元素做 not 通常需要 quoting.
也就是说,参数模式中的数组build, --prod
等价于表达式模式中的'build', '--prod'
(类似编程语言)。
有关 PowerShell 解析模式的概述,请参阅
但是,您也可以在参数模式下使用引号形式,并且 - 根据元素值 - 您可能 必须 引号,例如当元素包含空格或其他内容时shell 元字符;此外,如果 first 元素看起来像 PowerShell 参数名称(例如,-prod
而不是 --prod
),它也必须引用。
几个例子:
注意:为简单起见,示例中使用了 Write-Output
,它只是在其自己的行上简单地回显每个数组元素。传递给 any cmdlet 的数组在 参数模式 .
# 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