如何以编程方式检查已安装 node.js 版本的发布状态?
How do I programmatically check the release status of the installed node.js version?
我想在安装脚本中包含一个检查,以验证安装的 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/ 上获取可用信息的某种方式,但在脚本中。如果在 JSON 中有一些 HTTP 端点提供这些东西,那也可以。
我查看了 node
、npm
、nvm
和 yarn
CLI 工具中的可用选项,但其中 none 看起来像他们可以做到这一点。
有关 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 发行版本,您可以将其与本地安装的进行比较。
这是一段获取 JSON 的 nodejs 代码,过滤掉不是 lts
的项目,然后按版本排序。结果数组的顶部将是最新的 LTS 发布版本号。然后,您可以通过编程方式将其与本地安装的进行比较。您可以仅在此脚本中使用 process.version
,也可以 运行 捕获 node -v
.
输出的 child_process
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
我想在安装脚本中包含一个检查,以验证安装的 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/ 上获取可用信息的某种方式,但在脚本中。如果在 JSON 中有一些 HTTP 端点提供这些东西,那也可以。
我查看了 node
、npm
、nvm
和 yarn
CLI 工具中的可用选项,但其中 none 看起来像他们可以做到这一点。
有关 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 发行版本,您可以将其与本地安装的进行比较。
这是一段获取 JSON 的 nodejs 代码,过滤掉不是 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