Node.js/npm/版本号码:修订间差异
来自康健生活
| (未显示同一用户的2个中间版本) | |||
| 第49行: | 第49行: | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
npm view <pkg> --no-workspaces versions --json | grep -E '^ *"' | sort -rV | head -n 1 | sed -E 's/^ *"(.+)",*$/\1/'</syntaxhighlight>|$|/home/kwy}} | npm view <pkg> --no-workspaces versions --json \ | ||
| grep -E '^ *"' \ | |||
| sort -rV \ | |||
| head -n 1 \ | |||
| sed -E 's/^ *"(.+)",*$/\1/'</syntaxhighlight>|$|/home/kwy}} | |||
测试 | 测试 | ||
{{Shc_out|<syntaxhighlight lang="bash"> | {{Shc_out|<syntaxhighlight lang="bash"> | ||
| 第154行: | 第158行: | ||
npm list --depth=0 --no-workspaces <pkg> | grep <pkg> | cut -d '@' -f2 | npm list --depth=0 --no-workspaces <pkg> | grep <pkg> | cut -d '@' -f2 | ||
npm list --depth=0 --no-workspaces | grep <pkg>| awk '{ print $2; }' | cut -d '@' -f 2</syntaxhighlight>|$|/home/kwy}} | npm list --depth=0 --no-workspaces | grep <pkg>| awk '{ print $2; }' | cut -d '@' -f 2</syntaxhighlight>|$|/home/kwy}} | ||
== 版本升级 == | |||
脚本 | |||
{{hc|head=update.sh|output=<syntaxhighlight lang="bash"> | |||
#!/bin/bash | |||
# @link{https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425?permalink_comment_id=4387764} | |||
# -e 指示 bash 在任何命令 [1] 具有非零退出状态时立即退出 | |||
# -u 未定义的任何变量的引用($*和$@除外)都是错误的并立即退出 | |||
# -o 防止掩盖管道中的错误 | |||
set -euo pipefail | |||
# IFS 变量(代表内部字段分隔符)控制 Bash 所说的单词拆分 | |||
# 当设置为字符串时,Bash 会将字符串中的每个字符视为分隔单词 | |||
IFS=$'\n\t' | |||
runCheckFun(){ | |||
pkgstr="" | |||
# 接收数组不能使用 $1 | |||
# 传入数组也不能使用 "${pks[*]}" 会被转为字符串 | |||
arr=($*) | |||
var_path=`pwd` | |||
for(( i=0;i<${#arr[@]};i++)) do | |||
# ${#arr[@]}获取数组长度用于循环 | |||
# --no-workspaces 解决wran: npm warn config ignoring workspace config at | |||
# @link{https://github.com/npm/cli/issues/4605} | |||
cur_v=$(node -p "try {require('./node_modules/${arr[i]}/package.json').version} catch (e){''}") | |||
next_v=$(npm view ${arr[i]} dist-tags.latest --no-workspaces) | |||
echo "组件"${arr[i]}"的当前版本:"$cur_v",注册表版本:"$next_v; | |||
if [[ $cur_v != $next_v ]]; then | |||
pkgstr="$pkgstr ${arr[i]}@$next_v" | |||
fi | |||
done; | |||
if [[ -n $pkgstr ]];then | |||
echo "当前目录: "$var_path | |||
echo "运行安装命令: pnpm add $pkgstr" | |||
# 字符串作为命令执行, 避免 $pkgstr 中空格存在导致整体变为目录结构被安装 | |||
str="pnpm i $pkgstr" | |||
eval $str | |||
else | |||
echo "版本相同跳过" | |||
fi | |||
} | |||
# npm config set proxy http://192.168.51.34:808 | |||
# npm config set https-proxy http://192.168.51.34:808 | |||
pks=( | |||
[0]="@postlab/types" | |||
[1]="@postlab/platform" | |||
[2]="@postlab/js-sandbox" | |||
[3]="@postlab/component-rest" | |||
[4]="@postlab/codemirror-input" | |||
[5]="@postlab/component-mqtt" | |||
[6]="@postlab/component-socketio" | |||
[7]="@postlab/component-sse" | |||
[8]="@postlab/component-websocket" | |||
) | |||
cd './packages/postlab-common' | |||
runCheckFun ${pks[*]}; | |||
exit 0 | |||
## npm run-script updateself | |||
</syntaxhighlight>}} | |||
添加一些简单的缓存功能,减少查询次数 | |||
{{hc|head=update.sh|output=<syntaxhighlight lang="bash"> | |||
#!/bin/bash | |||
# @link{https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425?permalink_comment_id=4387764} | |||
# -e 指示 bash 在任何命令 [1] 具有非零退出状态时立即退出 | |||
# -u 未定义的任何变量的引用($*和$@除外)都是错误的并立即退出 | |||
# -o 防止掩盖管道中的错误 | |||
set -euo pipefail | |||
# IFS 变量(代表内部字段分隔符)控制 Bash 所说的单词拆分 | |||
# 当设置为字符串时,Bash 会将字符串中的每个字符视为分隔单词 | |||
# IFS=$'\n\t' | |||
temp=("0.0.14=@postlab/component-websocket") | |||
runCheckFun(){ | |||
pkgstr="" | |||
# 接收数组不能使用 $1 | |||
# 传入数组也不能使用 "${pks[*]}" 会被转为字符串 | |||
arr=($*) | |||
var_path=`pwd` | |||
echo "#### 当前目录: "$var_path | |||
# ${#arr[@]}获取数组长度用于循环 | |||
for(( i=0;i<${#arr[@]};i++)) do | |||
cur_pkg=${arr[i]} | |||
# --no-workspaces 解决wran: npm warn config ignoring workspace config at | |||
# @link{https://github.com/npm/cli/issues/4605} | |||
cur_v=$(node -p "try {require('./node_modules/$cur_pkg/package.json').version} catch (e){''}") | |||
# 查找缓存是否存在 | |||
has_canche_v=$(echo "${temp[@]}" | grep -w ".*=$cur_pkg" && echo "yes" || echo "") | |||
if [[ -n $has_canche_v ]];then | |||
# 遍历缓存数组 | |||
for item in "${temp[@]}" | |||
do | |||
# 检查数组元素是否命中 pkg | |||
# has_canche_v 重新赋值 or 赋予原来的值 | |||
has_canche_v=$(echo "$item" | grep -w ".*=$cur_pkg" | cut -f1 -d '=' || echo $has_canche_v) | |||
done | |||
fi | |||
# 容错处理 | |||
if [[ $has_canche_v == "yes" ]];then | |||
has_canche_v="" | |||
fi | |||
next_v=$(test -n "$has_canche_v" && echo $has_canche_v || npm view $cur_pkg dist-tags.latest --no-workspaces) | |||
# next_v=$(npm view $cur_pkg dist-tags.latest --no-workspaces) | |||
echo " 组件"$cur_pkg"的当前版本:"$cur_v",注册表版本:"$next_v; | |||
# if [[ "" != $next_v ]]; then | |||
if [[ $cur_v != $next_v ]]; then | |||
pkgstr="$pkgstr $cur_pkg@$next_v" | |||
fi | |||
# 被添加的元素 eq: 0.0.12=@postlab/types | |||
canche_item="$next_v=$cur_pkg" | |||
# if [[ ${temp[@]/${canche_item}/} == ${temp[@]} ]];then | |||
if [[ "${temp[@]}" =~ "$canche_item" ]];then | |||
# 已经缓存此组件 | |||
echo " 已经缓存此组件" | |||
else | |||
# 根据数组长度添加元素 | |||
temp[${#temp[@]}]=$canche_item | |||
fi | |||
done; | |||
# 容错处理 | |||
if [[ ${#temp[@]} != 0 ]];then | |||
echo " 以缓存组件: ${temp[@]}" | |||
fi | |||
if [[ -n $pkgstr ]];then | |||
echo "##### 运行安装命令: pnpm add $pkgstr" | |||
# 字符串作为命令执行, 避免 $pkgstr 中空格存在导致整体变为目录结构被安装 | |||
str="pnpm i $pkgstr" | |||
eval $str | |||
else | |||
echo "##### 版本相同跳过" | |||
fi | |||
} | |||
# npm config set proxy http://192.168.51.34:808 | |||
# npm config set https-proxy http://192.168.51.34:808 | |||
cd './packages/postlab-common' | |||
com_pks=( | |||
[0]="@postlab/types" | |||
[1]="@postlab/platform" | |||
[2]="@postlab/js-sandbox" | |||
[3]="@postlab/component-rest" | |||
[4]="@postlab/codemirror-input" | |||
[5]="@postlab/component-mqtt" | |||
[6]="@postlab/component-socketio" | |||
[7]="@postlab/component-sse" | |||
[8]="@postlab/component-websocket" | |||
) | |||
runCheckFun ${com_pks[*]}; | |||
cd '../postlab-vip-types' | |||
types_pks=( | |||
[0]="@postlab/types" | |||
) | |||
runCheckFun ${types_pks[*]}; | |||
cd '../postlab-vip-platform' | |||
plat_pks=( | |||
[0]="@postlab/types" | |||
[1]="@postlab/platform" | |||
) | |||
runCheckFun ${plat_pks[*]}; | |||
cd '../postlab-di-ioc' | |||
diioc_pks=( | |||
[0]="@postlab/types" | |||
[1]="@postlab/platform" | |||
[2]="@postlab/js-sandbox" | |||
[4]="@postlab/codemirror-input" | |||
[5]="@postlab/component-mqtt" | |||
[6]="@postlab/component-socketio" | |||
[7]="@postlab/component-sse" | |||
[8]="@postlab/component-websocket" | |||
) | |||
runCheckFun ${diioc_pks[*]}; | |||
# 离开 | |||
exit 0 | |||
## npm run-script updateself</syntaxhighlight>}} | |||