如何以编程方式检查已安装的 Node.js 版本的发布状态?

发布于 2025-01-15 04:23:35 字数 558 浏览 2 评论 0原文

我想在安装脚本中包含一个检查,以验证安装的 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 形式提供这些内容,那也可以。

我查看了 nodenpmnvmyarn CLI 工具中的可用选项,但没有一个他们似乎可以做到这一点。

I want to include a check in a setup script that validates the installed node.js version is LTS. Something like this would be ideal

$ 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

Basically some way to get the information available on https://nodejs.org/en/about/releases/ but in a script. If there's some HTTP endpoint that provides this stuff in JSON that would work too.

I've looked through available options in the node, npm, nvm, and yarn CLI tools but none of them seem like they can do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空心↖ 2025-01-22 04:23:35

有关 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 捕获输出的 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

One source of JSON info about nodejs releases is this endpoint:

https://nodejs.org/download/release/index.json

There, you get an array of objects that each look like this:

{
    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
},

If you filter that array by the lts property (any value other than false) and then sort by version, you will find the latest LTS release version and you can compare that to what is installed locally.

Here's a piece of nodejs code that gets that JSON, filters out items that aren't lts, then sorts by version. The top of the resulting array will be the latest LTS release version number. You could then programmatically compare that to what is installed locally. You could either just use process.version from within this script or you could run a child_process that captures the output from 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}`);

When I run this on my system at this moment, I get this output:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文