如何从相对版本中获得准确的最新npm版本?
我希望能够获得与相对语义版本匹配的最新发布的如何从相对版本中获得准确的最新npm版本?,npm,semantic-versioning,Npm,Semantic Versioning,我希望能够获得与相对语义版本匹配的最新发布的npm 例如,我希望运行类似以下内容: npm -v 6.12.x 然后返回完全匹配的最新版本: => 6.12.1 我已经研究了一些可用的文档和命令,或者认为这在npm中是可能的,但是我还没有看到任何不安装另一个全局依赖项就可以工作的东西。简短回答:npm没有提供一个内置功能来精确满足您的需求 但是,npm确实提供了该命令。其文件说明如下: 如果提供了版本范围,则将为包的每个匹配版本打印数据。这将显示每个匹配版本的yui3需要哪个版本的js
npm
例如,我希望运行类似以下内容:
npm -v 6.12.x
然后返回完全匹配的最新版本:
=> 6.12.1
我已经研究了一些可用的文档和命令,或者认为这在npm中是可能的,但是我还没有看到任何不安装另一个全局依赖项就可以工作的东西。简短回答:npm没有提供一个内置功能来精确满足您的需求 但是,npm确实提供了该命令。其文件说明如下: 如果提供了版本范围,则将为包的每个匹配版本打印数据。这将显示每个匹配版本的yui3需要哪个版本的jsdom:
npm视图yui3@>0.5.4'dependencies.jsdom
下面给出的两个解决方案演示了如何解析/操作npm view
命令的输出以满足您的需求(无需安装另一个全局依赖项)
解决方案A:*仅限Nix平台(Linux/MacOS…) 因此,可以借助一些额外的Bash实用程序来解析输出,从而实现您的需求 考虑运行以下复合命令:
npm view npm@'6.12.x' version | tail -n 1 | cut -d "'" -f2
npm view npm@'6.12.x' version | tail -n 1 | cut -d "'" -f2
echo $'\n'"npmv() { npm view \"npm@\${1}\" version | tail -n 1 | cut -d \"'\" -f2; }" >> ~/.bash_profile
node -e "var res = require('child_process').execSync('npm view npm@"\"6.12.x\"" version', {encoding: 'utf-8'}).split('\n').filter(Boolean); if (res.length) { if (res.length === 1) { res = res[0]; } else { res = res.pop().split(' ')[1]; } console.log(res.replace(/[']/g, '')); }"
解释
以下对上述复合命令的组成部分进行了说明:
npm view npm@'6.12.x' version | tail -n 1 | cut -d "'" -f2
npm view npm@'6.12.x' version | tail -n 1 | cut -d "'" -f2
echo $'\n'"npmv() { npm view \"npm@\${1}\" version | tail -n 1 | cut -d \"'\" -f2; }" >> ~/.bash_profile
node -e "var res = require('child_process').execSync('npm view npm@"\"6.12.x\"" version', {encoding: 'utf-8'}).split('\n').filter(Boolean); if (res.length) { if (res.length === 1) { res = res[0]; } else { res = res.pop().split(' ')[1]; } console.log(res.replace(/[']/g, '')); }"
npm查看npm@'6.12.x'版本
此部分使用npm view
命令返回给定范围内的所有版本,即6.12.x
独立运行这些部件将在控制台上打印以下内容:
npm@6.12.0 '6.12.0'
npm@6.12.1 '6.12.1'
| tail-n1
然后使用-n1
选项将先前显示的结果通过管道传输到,以便只打印最后一行
运行:
npm view npm@'6.12.x' version | tail -n 1
印刷品:
npm@6.12.1“6.12.1”
|cut-d''-f2
然后,使用-d
选项将前面显示的结果通过管道传输到,并指定一个引号作为分隔符。然后,-f2
部分选择第二项
运行完整复合命令:
npm view npm@'6.12.x' version | tail -n 1 | cut -d "'" -f2
npm view npm@'6.12.x' version | tail -n 1 | cut -d "'" -f2
echo $'\n'"npmv() { npm view \"npm@\${1}\" version | tail -n 1 | cut -d \"'\" -f2; }" >> ~/.bash_profile
node -e "var res = require('child_process').execSync('npm view npm@"\"6.12.x\"" version', {encoding: 'utf-8'}).split('\n').filter(Boolean); if (res.length) { if (res.length === 1) { res = res[0]; } else { res = res.pop().split(' ')[1]; } console.log(res.replace(/[']/g, '')); }"
打印您想要的结果:
6.12.1
~
)和插入符号(^
)范围。
您还可以使用前面提到的复合命令指定波浪线或插入符号范围
示例:
- 指定波浪线范围:
npm view npm@'~4.0.0' version | tail -n 1 | cut -d "'" -f2 ^^^^^^
印刷品:node -e "var res = require('child_process').execSync('npm view npm@"\"~4.0.0\"" version', {encoding: 'utf-8'}).split('\n').filter(Boolean); if (res.length) { if (res.length === 1) { res = res[0]; } else { res = res.pop().split(' ')[1]; } console.log(res.replace(/[']/g, '')); }" ^^^^^^
4.0.5
- 指定插入符号范围:
npm view npm@'^4.0.0' version | tail -n 1 | cut -d "'" -f2 ^^^^^^
印刷品:node -e "var res = require('child_process').execSync('npm view npm@"\"^4.0.0\"" version', {encoding: 'utf-8'}).split('\n').filter(Boolean); if (res.length) { if (res.length === 1) { res = res[0]; } else { res = res.pop().split(' ')[1]; } console.log(res.replace(/[']/g, '')); }" ^^^^^^
4.6.1
npm view npm@'6.12.x' version | tail -n 1 | cut -d "'" -f2
npm view npm@'6.12.x' version | tail -n 1 | cut -d "'" -f2
echo $'\n'"npmv() { npm view \"npm@\${1}\" version | tail -n 1 | cut -d \"'\" -f2; }" >> ~/.bash_profile
node -e "var res = require('child_process').execSync('npm view npm@"\"6.12.x\"" version', {encoding: 'utf-8'}).split('\n').filter(Boolean); if (res.length) { if (res.length === 1) { res = res[0]; } else { res = res.pop().split(' ')[1]; } console.log(res.replace(/[']/g, '')); }"
这将在.bash\u profile
文件的内容中添加一行,即添加一行新内容,内容如下:
npmv(){npm view“npm@${1}”version|tail-n1|cut-d''''-f2;}
npmv 6.12.x
它简洁且更类似于您问题中给出的示例命令解决方案B:跨平台(Windows/Linux/macOS…) 如果您正在使用Windows,或者想要一个跨平台的解决方案(即在Windows、Linux和macOS上成功运行的解决方案…),则需要利用NodeJ来满足您的需求 考虑运行以下复合命令:
npm view npm@'6.12.x' version | tail -n 1 | cut -d "'" -f2
npm view npm@'6.12.x' version | tail -n 1 | cut -d "'" -f2
echo $'\n'"npmv() { npm view \"npm@\${1}\" version | tail -n 1 | cut -d \"'\" -f2; }" >> ~/.bash_profile
node -e "var res = require('child_process').execSync('npm view npm@"\"6.12.x\"" version', {encoding: 'utf-8'}).split('\n').filter(Boolean); if (res.length) { if (res.length === 1) { res = res[0]; } else { res = res.pop().split(' ')[1]; } console.log(res.replace(/[']/g, '')); }"
是的,我同意它与解决方案A相比有点冗长。:)
解释
以下对上述复合命令的组成部分进行了说明:
npm view npm@'6.12.x' version | tail -n 1 | cut -d "'" -f2
npm view npm@'6.12.x' version | tail -n 1 | cut -d "'" -f2
echo $'\n'"npmv() { npm view \"npm@\${1}\" version | tail -n 1 | cut -d \"'\" -f2; }" >> ~/.bash_profile
node -e "var res = require('child_process').execSync('npm view npm@"\"6.12.x\"" version', {encoding: 'utf-8'}).split('\n').filter(Boolean); if (res.length) { if (res.length === 1) { res = res[0]; } else { res = res.pop().split(' ')[1]; } console.log(res.replace(/[']/g, '')); }"
节点-e“…”
我们调用节点
,并使用nodejs命令行选项来评估给定的内联JavaScriptvar res=require('child\u process').execSync('npm view npm@\'6.12.x\'version',{encoding:'utf-8'})
给定node.js脚本(JavaScript)的这一部分按照解决方案A,使用相同的npm view
命令,即npm view npm@“6.12.x\”版本
.split('\n').filter(布尔值)代码>
使用字符串的方法将npm view
命令的结果从字符串转换为数组-基本上,结果的每一行都被换行符\n
分割,以便每一行输出都成为数组中的一个元素
.filter(Boolean)
部分从数组中删除由npm view
命令打印的附加尾随空行生成的空元素if(res.length){if(res.length==1){res=res[0];}否则{res=res.pop().split(“”)[1];}…}
这里我们使用一些条件语句来确保从数组中获得所需的项console.log(res.replace(/[']/g')代码>
最后,我们将结果记录到控制台,并使用该方法删除包含semver值的单引号(“
)- 指定波浪线范围:
npm view npm@'~4.0.0' version | tail -n 1 | cut -d "'" -f2 ^^^^^^
印刷品:node -e "var res = require('child_process').execSync('npm view npm@"\"~4.0.0\"" version', {encoding: 'utf-8'}).split('\n').filter(Boolean); if (res.length) { if (res.length === 1) { res = res[0]; } else { res = res.pop().split(' ')[1]; } console.log(res.replace(/[']/g, '')); }" ^^^^^^
4.0.5
- 指定插入符号范围:
npm view npm@'^4.0.0' version | tail -n 1 | cut -d "'" -f2 ^^^^^^
印刷品: <node -e "var res = require('child_process').execSync('npm view npm@"\"^4.0.0\"" version', {encoding: 'utf-8'}).split('\n').filter(Boolean); if (res.length) { if (res.length === 1) { res = res[0]; } else { res = res.pop().split(' ')[1]; } console.log(res.replace(/[']/g, '')); }" ^^^^^^