PowerShell脚本添加'--prod'参数以构建Angular CLI项目 [英] PowerShell script add '--prod' argument to build Angular CLI project
问题描述
我正在创建PowerShell脚本以自动构建Angular CLI项目. 当我在.ps1文件中使用此命令时:
I am creating PowerShell script to automatically build Angular CLI project. When I use this command in my .ps1 file:
Start-Process -FilePath ng build -WorkingDirectory D:\pathToAngularProject -Wait
它可以完美运行,但不能创建丑陋的javascript版本.为了能够创建生产版本,我需要添加'--prod'参数. 但是当我在上面更改示例时:
it works perfectly but create not uglified javascript build. To be able to create production build I need to add '--prod' argument. But when I change example above on :
Start-Process -FilePath ng build -ArgumentList '--prod' -WorkingDirectory D:\pathToAngularProject -Wait
我遇到了错误:
有人建议如何在PowerShell脚本中向"ng build"命令添加参数吗?
Does anyone have a suggestion how can I add an argument to 'ng build' command in PowerShell script?
推荐答案
Neko Musume的有用答案提供了一个解决方案解决您眼前的问题.
Neko Musume's helpful answer provides a solution to your immediate problem.
但是,值得退后一步:
要同步执行控制台应用程序或批处理文件,直接将其称为 (ng build ...
或& ng build ...
),不要不使用Start-Process
强>-请参见此答案和 Start-Process
帮助主题.
To synchronously execute console applications or batch files, call them directly (ng build ...
or & ng build ...
), do not use Start-Process
- see this answer and this GitHub docs issue detailing appropriate vs. non-appropiate use cases and requesting that guidance be added to the Start-Process
help topic.
因此:
# Execute ng synchronously, with its output streams connected to PowerShell's
ng build --prod D:\pathToAngularProject
关于您尝试过的事情:
要添加到Neko的答案中:
To add to Neko's answer:
第一个命令起作用的原因是:
The reason that your first command worked is that the following:
Start-Process -FilePath ng build ...
等效于:
Start-Process -FilePath -FilePath ng -ArgumentList build ...
也就是说,build
自变量在位置上已绑定,而无需显式命名其目标参数-ArgumentList
.
That is, the build
argument was bound positionally, without the need to name its target parameter, -ArgumentList
, explicitly.
利用这一点,并且-FilePath
,可以将问题的直接解决方案简化为:
Taking advantage of this, and also that -FilePath
is implied for the first positional argument, the immediate solution to your problem could be simplified to:
# 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问题.
为了保持向后兼容性,此错误可能不会得到修复(除非通过引入一个新参数 ).
That said, Start-Process
has a long-standing bug that causes it to pass arguments with embedded spaces incorrectly - see this GitHub issue.
To preserve backward compatibility, this bug will likely not get fixed (except perhaps by introducing a new parameter).
因此,最好将参数作为单个数组元素传递,有效地作为命令行(不包含可执行文件)传递,其中必要时,可以使用嵌入的"..."
引号正确表示参数之间的界限:
It is therefore preferable to pass the arguments as a single array element, effectively as a command line (without the executable), where the boundaries between the arguments can properly be signaled with embedded "..."
quoting, if necessary:
# Pass the arguments for 'ng' *as a single string*, potentially
# with embedded "..." quoting (not needed here).
Start-Process ng 'build --prod' ...
带有嵌入式引号的示例:
An example with embedded quoting:
# Project path needs embedded "..." quoting, because it contains spaces.
Start-Process ng 'build --prod "D:\Work Projects\Foo"' ...
有关引用数组元素 的一般说明:
A general note on quoting array elements:
因为在PowerShell中的命令参数是使用所谓的 argument(解析)模式(类似于shell)进行解析的,所以(隐含)的(string)元素-ArgumentList
不需要 通常需要引用.
Because command arguments in PowerShell are parsed using the so-called argument (parsing) mode (shell-like), the (string) elements of the (implied) -ArgumentList
do not generally require quoting.
也就是说,在自变量模式中的数组build, --prod
与在表达式模式(类似于编程语言)中的'build', '--prod'
等效.
That is, array build, --prod
in argument mode is the equivalent of 'build', '--prod'
in expression mode (programming-language-like).
有关PowerShell解析模式的概述,请参见此答案.
See this answer for an overview of PowerShell's parsing modes.
但是,您也可以在参数模式下使用带引号的形式,并且-根据元素值-您可能必须 进行引号,例如元素包含空格或其他shell元字符时;另外,如果 first 元素看起来像 PowerShell 参数名(例如,-prod
而不是--prod
),则也必须将其引用.
However, you may use the quoted form in argument mode too, and - depending on the element values - you may have to quote, such as when elements contain spaces or other shell metacharacter; additionally, if the first element looks like a PowerShell parameter name (e.g., -prod
rather than --prod
), it must be quoted too.
一些示例:
注意:为简单起见,示例中使用了Write-Output
,它仅在每个行上回显每个数组元素.传递给 any cmdlet的数组以参数模式进行解析.
Note: For simplicity, Write-Output
is used in the examples, which simply echoes each array element on its own line. An array passed to any cmdlet is parsed in argument mode.
# 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脚本添加'--prod'参数以构建Angular CLI项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!