【问题标题】:Get the next major version of an npm package (NOT necessarily the latest!)获取 npm 包的下一个主要版本(不一定是最新的!)
【发布时间】:2021-07-30 22:45:41
【问题描述】:

我正在编写一个 bash 脚本来批量更新和测试包。但是,我希望脚本不要跳转到最新版本,只要下一个主要版本存在并通过测试。

我正在研究与此类似的东西(为简洁起见,将较长的代码段简化为伪代码)

#!/bin/bash

# Assign package name to $packageName

# START MAIN LOOP

# Start code that does a preliminary test
...
# End code that does preliminary test
# Assign current package version to $current

npm install $packagename@#[next?]
# # The above line is the one I can't figure out. 
# Say, for example, $current is 3.4.7. and 
# $packagename@latest is 5.2.3. This should 
# update to 4.0.0, but if latest is 3.5.8, it would update
# to 3.5.8, but then not update if latest is 3.4.7

# Do post tests
# # If fails post tests
       # alert user to breaking version
       npm install $packagename@$current # revert to previous version

# # If succeeds post tests, check if now at @latest
# # # If not at @latest, repeat MAIN LOOP
# # # If at @latest, exit 0.

【问题讨论】:

  • @markp-fuso 这不是掩饰。关键部分是我不知道该怎么做的部分。我不想识别各种方案,我只想选择下一个主要版本而不考虑方案,因为 npm 支持主要/次要版本。

标签: bash npm package-managers


【解决方案1】:
#!/usr/bin/bash
arr=('npm' 'yarn' 'puppetter' 'express')
for pkg in "${arr[@]}"; do
    pkg_noinstl=$(npm -g ls "$pkg" | grep -o '(empty)')
    if [[ "$pkg_noinstl" ]]; then
        echo "$pkg: Not Installed"
    else
        myversion=$(npm -g list "$pkg" | grep "@" | cut -d@ -f2)
        int_myversion=$(echo "$myversion" | cut -f3 -d.)
        reg=$(echo "$myversion" | cut -f1,2 -d.)
        newversion=$(npm view "$pkg" versions | grep -oP "$reg.[0-9]{1,2}")
        int_newversion=$(echo "$newversion" | cut -f3 -d. | tail -1)

        if [[ "$int_newversion" -gt "$int_myversion" ]]; then
            n=$(echo "$newversion" | tail -1)
            echo "$pkg:$myversion you have an upgrade-->$n"
        else
            echo "$pkg: OK $myversion"
        fi
    fi
done

返回:

如果你想 +1 发布 6.14.8-->6.14.9 然后添加:
 next_release=$(((int_myversion + 1)))
 intnr=$(echo "$newversion" | cut -d. -f3 | grep "$next_release")
 n=$(echo "$newversion" | tail -1)
 echo "$pkg:$myversion you have an upgrade-->$n, available-->$reg.$intnr"

【讨论】:

  • 您可以在脚本中替换很多东西,例如数组$arr,您可以创建它并作为参数传递并作为您全局安装的所有程序的file。该命令是npm -g ls --depth=0 >installed.txt,然后清理文件
猜你喜欢
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 2016-06-02
  • 2018-07-31
  • 1970-01-01
  • 2014-11-17
相关资源
最近更新 更多