diff --git a/.changeset/smart-crabs-attend.md b/.changeset/smart-crabs-attend.md new file mode 100644 index 0000000000..82bea27a54 --- /dev/null +++ b/.changeset/smart-crabs-attend.md @@ -0,0 +1,5 @@ +--- +"@pnpm/plugin-commands-env": minor +--- + +Allow to install the latest Node.js version by running `pnpm env use -g latest`. diff --git a/packages/plugin-commands-env/src/env.ts b/packages/plugin-commands-env/src/env.ts index 106dcf90f7..5d6878c499 100644 --- a/packages/plugin-commands-env/src/env.ts +++ b/packages/plugin-commands-env/src/env.ts @@ -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 diff --git a/packages/plugin-commands-env/test/env.test.ts b/packages/plugin-commands-env/test/env.test.ts index 235bd57cbc..923ceba9cf 100644 --- a/packages/plugin-commands-env/test/env.test.ts +++ b/packages/plugin-commands-env/test/env.test.ts @@ -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()