如何在 shell (sh) 脚本中获取最新的 yarn 包版本?

How to get the latest yarn package version in a shell (sh) script?

到目前为止我有以下内容:

#!/bin/sh 

getVersionInfo() {
    yarn info my-package version
}

getVersion() {
    VERSION_REGEX='^'
     =~ $VERSION_REGEX
}

VERSION_INFO=$(getVersionInfo)
VERSION=$(getVersion "$VERSION_INFO")
echo $VERSION

我最终想要 运行 脚本并能够在一个命令中将我的所有项目升级到最新版本 my-package

我坚持两件事:

  1. 为什么控制台会打印两次?
  2. 我如何计算出正则表达式(或使用其他技术)来获取版本号?

yarn info my-package version 输出为:

└─ my-package@workspace:shared
   ├─ Version: 0.0.28
   │
   └─ Dependencies
      ├─ @grpc/grpc-js@npm:^1.5.3 → npm:1.5.9
      ├─ @types/dinero.js@npm:^1.9.0 → npm:1.9.0
      ├─ @types/gulp-sourcemaps@npm:^0.0.35 → npm:0.0.35

我使用的是 yarn 版本 3.2.0

感谢@kj-crypto,解决方案不需要函数,可以是 one-liner:

VERSION="$(yarn info my-package version | grep -oE 'Version: (?[0-9.]*)' | grep -oE '(?[0-9.]*)')