feat(env): installing the latest Node.js version (#3879)

ref https://github.com/pnpm/pnpm/discussions/3857
This commit is contained in:
Zoltan Kochan
2021-10-16 16:15:36 +03:00
committed by GitHub
parent 3882510d52
commit 1a6cc7ee77
3 changed files with 27 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/plugin-commands-env": minor
---
Allow to install the latest Node.js version by running `pnpm env use -g latest`.

View File

@@ -43,6 +43,7 @@ export function help () {
'pnpm env use --global 16',
'pnpm env use --global lts',
'pnpm env use --global argon',
'pnpm env use --global latest',
],
})
}
@@ -105,6 +106,9 @@ interface NodeVersion {
async function resolveNodeVersion (rawVersionSelector: string) {
const response = await fetch('https://nodejs.org/download/release/index.json')
const allVersions = (await response.json()) as NodeVersion[]
if (rawVersionSelector === 'latest') {
return allVersions[0].version.substring(1)
}
const { versions, versionSelector } = filterVersions(allVersions, rawVersionSelector)
const pickedVersion = semver.maxSatisfying(versions.map(({ version }) => version), versionSelector)
if (!pickedVersion) return null

View File

@@ -112,6 +112,24 @@ test('install Node by its LTS name', async () => {
expect(dirs).toEqual([version.substring(1)])
})
test('install the latest Node.js', async () => {
tempDir()
await env.handler({
bin: process.cwd(),
global: true,
pnpmHomeDir: process.cwd(),
rawConfig: {},
}, ['use', 'latest'])
const { stdout } = execa.sync('node', ['-v'], {
env: {
[PATH]: `${process.cwd()}${path.delimiter}${process.env[PATH] as string}`,
},
})
expect(stdout.toString()).toMatch(/^v/)
})
test('fail if a non-existend Node.js version is tried to be installed', async () => {
tempDir()