【问题标题】:get npm process exitcode as output in powershell获取 npm 进程退出代码作为 powershell 中的输出
【发布时间】:2022-02-09 22:47:42
【问题描述】:

我是 Powershell 新手,想通过 Powershell 脚本自动化一些流程。 如果失败,有什么方法可以获取 npm run build 的状态。目前,如果我没有添加条件,那么它会按顺序运行下一个命令,无论成功还是失败。

我已经添加了$ErrorActionPreference = "Stop",但它只适用于 cmdlet。

$exitcode = npm run build
#need exitcode as output to check
if ($exitcode -eq 0) {
   Remove-Item -Path $spaNodeModulePath -Force -Recurse
   mkdir $spaNodeModulePath
   xcopy $libDistPath $spaNodeModulePath /e
   Set-Location $spaPath
}

【问题讨论】:

    标签: node.js powershell npm


    【解决方案1】:

    您可以使用包含exit code of the last native program that was run$LASTEXITCODE 变量。所以先执行程序,稍后检查$LASTEXITCODE

    npm run build 
    if ($LASTEXITCODE -eq 0) {
        Write-Output "NPM build was successful"
    }
    

    请注意$LASTEXITCODE不是用户定义的,它是created and maintained by the PowerShell runtime

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-19
      • 2017-02-04
      • 2021-03-17
      • 2010-12-06
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多