mirror of
https://github.com/pnpm/pnpm.git
synced 2026-07-25 15:07:06 -04:00
5
.changeset/spicy-ads-brush.md
Normal file
5
.changeset/spicy-ads-brush.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@pnpm/plugin-commands-env": patch
|
||||
---
|
||||
|
||||
Use the package manager's network and proxy configuration when making requests for Node.js.
|
||||
@@ -2,6 +2,7 @@ import { promises as fs } from 'fs'
|
||||
import path from 'path'
|
||||
import { docsUrl } from '@pnpm/cli-utils'
|
||||
import PnpmError from '@pnpm/error'
|
||||
import { createFetchFromRegistry } from '@pnpm/fetch'
|
||||
import cmdShim from '@zkochan/cmd-shim'
|
||||
import renderHelp from 'render-help'
|
||||
import { getNodeDir, NvmNodeCommandOptions } from './node'
|
||||
@@ -56,11 +57,12 @@ export async function handler (opts: NvmNodeCommandOptions, params: string[]) {
|
||||
if (!opts.global) {
|
||||
throw new PnpmError('NOT_IMPLEMENTED_YET', '"pnpm env use <version>" can only be used with the "--global" option currently')
|
||||
}
|
||||
const { version: nodeVersion, releaseDir } = await resolveNodeVersion(params[1])
|
||||
const fetch = createFetchFromRegistry(opts)
|
||||
const { version: nodeVersion, releaseDir } = await resolveNodeVersion(fetch, params[1])
|
||||
if (!nodeVersion) {
|
||||
throw new PnpmError('COULD_NOT_RESOLVE_NODEJS', `Couldn't find Node.js version matching ${params[1]}`)
|
||||
}
|
||||
const nodeDir = await getNodeDir({
|
||||
const nodeDir = await getNodeDir(fetch, {
|
||||
...opts,
|
||||
useNodeVersion: nodeVersion,
|
||||
releaseDir,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { Config } from '@pnpm/config'
|
||||
import fetch, { createFetchFromRegistry, FetchFromRegistry } from '@pnpm/fetch'
|
||||
import { createFetchFromRegistry, FetchFromRegistry } from '@pnpm/fetch'
|
||||
import { FilesIndex } from '@pnpm/fetcher-base'
|
||||
import { createCafsStore } from '@pnpm/package-store'
|
||||
import storePath from '@pnpm/store-path'
|
||||
@@ -37,11 +37,12 @@ export type NvmNodeCommandOptions = Pick<Config,
|
||||
> & Partial<Pick<Config, 'configDir'>>
|
||||
|
||||
export async function getNodeBinDir (opts: NvmNodeCommandOptions) {
|
||||
const nodeDir = await getNodeDir(opts)
|
||||
const fetch = createFetchFromRegistry(opts)
|
||||
const nodeDir = await getNodeDir(fetch, opts)
|
||||
return process.platform === 'win32' ? nodeDir : path.join(nodeDir, 'bin')
|
||||
}
|
||||
|
||||
export async function getNodeDir (opts: NvmNodeCommandOptions & { releaseDir?: string }) {
|
||||
export async function getNodeDir (fetch: FetchFromRegistry, opts: NvmNodeCommandOptions & { releaseDir?: string }) {
|
||||
const nodesDir = path.join(opts.pnpmHomeDir, 'nodejs')
|
||||
let wantedNodeVersion = opts.useNodeVersion ?? (await readNodeVersionsManifest(nodesDir))?.default
|
||||
await fs.promises.mkdir(nodesDir, { recursive: true })
|
||||
@@ -57,21 +58,20 @@ export async function getNodeDir (opts: NvmNodeCommandOptions & { releaseDir?: s
|
||||
}
|
||||
const versionDir = path.join(nodesDir, wantedNodeVersion)
|
||||
if (!fs.existsSync(versionDir)) {
|
||||
await installNode(wantedNodeVersion, versionDir, opts)
|
||||
await installNode(fetch, wantedNodeVersion, versionDir, opts)
|
||||
}
|
||||
return versionDir
|
||||
}
|
||||
|
||||
async function installNode (wantedNodeVersion: string, versionDir: string, opts: NvmNodeCommandOptions & { releaseDir?: string }) {
|
||||
async function installNode (fetch: FetchFromRegistry, wantedNodeVersion: string, versionDir: string, opts: NvmNodeCommandOptions & { releaseDir?: string }) {
|
||||
await fs.promises.mkdir(versionDir, { recursive: true })
|
||||
const { tarball, pkgName } = getNodeJSTarball(wantedNodeVersion, opts.releaseDir ?? 'release')
|
||||
const fetchFromRegistry = createFetchFromRegistry(opts)
|
||||
if (tarball.endsWith('.zip')) {
|
||||
await downloadAndUnpackZip(fetchFromRegistry, tarball, versionDir, pkgName)
|
||||
await downloadAndUnpackZip(fetch, tarball, versionDir, pkgName)
|
||||
return
|
||||
}
|
||||
const getCredentials = () => ({ authHeaderValue: undefined, alwaysAuth: undefined })
|
||||
const fetch = createFetcher(fetchFromRegistry, getCredentials, {
|
||||
const { tarball: fetchTarball } = createFetcher(fetch, getCredentials, {
|
||||
retry: {
|
||||
maxTimeout: opts.fetchRetryMaxtimeout,
|
||||
minTimeout: opts.fetchRetryMintimeout,
|
||||
@@ -83,7 +83,7 @@ async function installNode (wantedNodeVersion: string, versionDir: string, opts:
|
||||
const storeDir = await storePath(process.cwd(), opts.storeDir)
|
||||
const cafsDir = path.join(storeDir, 'files')
|
||||
const cafs = createCafsStore(cafsDir)
|
||||
const { filesIndex } = await fetch.tarball(cafs, { tarball }, {
|
||||
const { filesIndex } = await fetchTarball(cafs, { tarball }, {
|
||||
lockfileDir: process.cwd(),
|
||||
})
|
||||
await cafs.importPackage(versionDir, {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import fetch from '@pnpm/fetch'
|
||||
import { FetchFromRegistry } from '@pnpm/fetch'
|
||||
import semver from 'semver'
|
||||
import versionSelectorType from 'version-selector-type'
|
||||
|
||||
@@ -7,7 +7,7 @@ interface NodeVersion {
|
||||
lts: false | string
|
||||
}
|
||||
|
||||
export default async function resolveNodeVersion (rawVersionSelector: string) {
|
||||
export default async function resolveNodeVersion (fetch: FetchFromRegistry, rawVersionSelector: string) {
|
||||
const { releaseDir, version } = parseNodeVersionSelector(rawVersionSelector)
|
||||
const response = await fetch(`https://nodejs.org/download/${releaseDir}/index.json`)
|
||||
const allVersions = (await response.json()) as NodeVersion[]
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { createFetchFromRegistry } from '@pnpm/fetch'
|
||||
import resolveNodeVersion from '@pnpm/plugin-commands-env/lib/resolveNodeVersion'
|
||||
|
||||
const fetch = createFetchFromRegistry({})
|
||||
|
||||
test.each([
|
||||
['6', '6.17.1', 'release'],
|
||||
['16.0.0-rc.0', '16.0.0-rc.0', 'rc'],
|
||||
@@ -9,7 +12,7 @@ test.each([
|
||||
['argon', '4.9.1', 'release'],
|
||||
['latest', /.+/, 'release'],
|
||||
])('Node.js %s is resolved', async (spec, version, releaseDir) => {
|
||||
const node = await resolveNodeVersion(spec)
|
||||
const node = await resolveNodeVersion(fetch, spec)
|
||||
expect(node.version).toMatch(version)
|
||||
expect(node.releaseDir).toBe(releaseDir)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user