我想在安装脚本中包含一个检查,以验证安装的node.js版本是LTS。像这样的东西将是理想的
$ node -vvv
v16.3.0
Release v16
Status Active LTS
Codename Gallium
Initial Release 2021-04-20
Active LTS Start 2021-10-26
Maintenance LTS Start 2022-10-18
End-Of-Life 2024-04-30
基本上是通过某种方式获取https://nodejs.org/en/about/releases/但是在脚本中。如果有一些HTTP端点在JSON中提供了这些东西,那么它也可以工作。
我已经查看了node
、npm
、nvm
和yarn
CLI工具中的可用选项,但它们似乎都无法做到这一点。
关于nodejs发布的JSON信息的一个来源是这个端点:
https://nodejs.org/download/release/index.json
在那里,你会得到一个对象数组,每个对象看起来都是这样的:
{
version: 'v16.14.1',
date: '2022-03-16',
files: [
'aix-ppc64', 'headers',
'linux-arm64', 'linux-armv7l',
'linux-ppc64le', 'linux-s390x',
'linux-x64', 'osx-arm64-tar',
'osx-x64-pkg', 'osx-x64-tar',
'src', 'win-x64-7z',
'win-x64-exe', 'win-x64-msi',
'win-x64-zip', 'win-x86-7z',
'win-x86-exe', 'win-x86-msi',
'win-x86-zip'
],
npm: '8.5.0',
v8: '9.4.146.24',
uv: '1.43.0',
zlib: '1.2.11',
openssl: '1.1.1m+quic',
modules: '93',
lts: 'Gallium',
security: false
},
如果您按lts
属性(false
以外的任何值(筛选该数组,然后按版本排序,您将找到最新的LTS版本,并且可以将其与本地安装的版本进行比较。
这里有一段nodejs代码,它获取JSON,过滤掉不是lts
的项,然后按版本排序。结果数组的顶部将是最新的LTS版本号。然后,您可以通过编程将其与本地安装的内容进行比较。您可以只在此脚本中使用process.version
,也可以运行捕获node -v
输出的子进程。
import got from 'got';
// 'v10.16.3'
let versionRegex = /v(d+).(d+).(d+)/;
// convert version string to a number for easier sorting
function calcVersion(x) {
const match = x.match(versionRegex);
if (!match) {
throw new Error(`version regex failed to match version string '${x}'`);
}
return (+match[1] * 1000000) + (+match[2] * 1000) + (+match[3]);
}
const data = await got("https://nodejs.org/download/release/index.json").json();
const lts = data.filter(item => item.lts);
// for performance reasons when sorting,
// precalculate an actual version number from the version string
lts.forEach(item => item.numVersion = calcVersion(item.version));
lts.sort((a, b) => b.numVersion - a.numVersion);
console.log("All LTS versions - sorted newest first");
console.log(lts.map(item => item.version));
console.log("Info about newest LTS version");
console.log(lts[0]);
console.log(`Newest LTS version: ${lts[0].version}`);
console.log(`Local version: ${process.version}`);
当我现在在我的系统上运行这个时,我得到的输出是:
All LTS versions - sorted newest first
[
'v16.14.1', 'v16.14.0', 'v16.13.2', 'v16.13.1', 'v16.13.0',
'v14.19.0', 'v14.18.3', 'v14.18.2', 'v14.18.1', 'v14.18.0',
'v14.17.6', 'v14.17.5', 'v14.17.4', 'v14.17.3', 'v14.17.2',
'v14.17.1', 'v14.17.0', 'v14.16.1', 'v14.16.0', 'v14.15.5',
'v14.15.4', 'v14.15.3', 'v14.15.2', 'v14.15.1', 'v14.15.0',
'v12.22.11', 'v12.22.10', 'v12.22.9', 'v12.22.8', 'v12.22.7',
'v12.22.6', 'v12.22.5', 'v12.22.4', 'v12.22.3', 'v12.22.2',
'v12.22.1', 'v12.22.0', 'v12.21.0', 'v12.20.2', 'v12.20.1',
'v12.20.0', 'v12.19.1', 'v12.19.0', 'v12.18.4', 'v12.18.3',
'v12.18.2', 'v12.18.1', 'v12.18.0', 'v12.17.0', 'v12.16.3',
'v12.16.2', 'v12.16.1', 'v12.16.0', 'v12.15.0', 'v12.14.1',
'v12.14.0', 'v12.13.1', 'v12.13.0', 'v10.24.1', 'v10.24.0',
'v10.23.3', 'v10.23.2', 'v10.23.1', 'v10.23.0', 'v10.22.1',
'v10.22.0', 'v10.21.0', 'v10.20.1', 'v10.20.0', 'v10.19.0',
'v10.18.1', 'v10.18.0', 'v10.17.0', 'v10.16.3', 'v10.16.2',
'v10.16.1', 'v10.16.0', 'v10.15.3', 'v10.15.2', 'v10.15.1',
'v10.15.0', 'v10.14.2', 'v10.14.1', 'v10.14.0', 'v10.13.0',
'v8.17.0', 'v8.16.2', 'v8.16.1', 'v8.16.0', 'v8.15.1',
'v8.15.0', 'v8.14.1', 'v8.14.0', 'v8.13.0', 'v8.12.0',
'v8.11.4', 'v8.11.3', 'v8.11.2', 'v8.11.1', 'v8.11.0',
... 74 more items
]
Info about newest LTS version
{
version: 'v16.14.1',
date: '2022-03-16',
files: [
'aix-ppc64', 'headers',
'linux-arm64', 'linux-armv7l',
'linux-ppc64le', 'linux-s390x',
'linux-x64', 'osx-arm64-tar',
'osx-x64-pkg', 'osx-x64-tar',
'src', 'win-x64-7z',
'win-x64-exe', 'win-x64-msi',
'win-x64-zip', 'win-x86-7z',
'win-x86-exe', 'win-x86-msi',
'win-x86-zip'
],
npm: '8.5.0',
v8: '9.4.146.24',
uv: '1.43.0',
zlib: '1.2.11',
openssl: '1.1.1m+quic',
modules: '93',
lts: 'Gallium',
security: false,
numVersion: 16014001
}
Newest LTS version: v16.14.1
Local version: v16.13.2