-
-
Notifications
You must be signed in to change notification settings - Fork 933
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { existsSync } from 'fs' | ||
import path from 'path' | ||
import { PnpmError } from '@pnpm/error' | ||
import { createFetchFromRegistry } from '@pnpm/fetch' | ||
import { globalInfo } from '@pnpm/logger' | ||
import { resolveNodeVersion } from '@pnpm/node.resolver' | ||
import { removeBin } from '@pnpm/remove-bins' | ||
import rimraf from '@zkochan/rimraf' | ||
import { parseNodeEditionSpecifier } from './parseNodeEditionSpecifier' | ||
import { getNodeExecPathAndTargetDir } from './utils' | ||
import { getNodeMirror } from './getNodeMirror' | ||
import { getNodeVersionsBaseDir, NvmNodeCommandOptions } from './node' | ||
|
||
export async function envRemove (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 fetch = createFetchFromRegistry(opts) | ||
const { releaseChannel, versionSpecifier } = parseNodeEditionSpecifier(params[0]) | ||
const nodeMirrorBaseUrl = getNodeMirror(opts.rawConfig, releaseChannel) | ||
const nodeVersion = await resolveNodeVersion(fetch, versionSpecifier, nodeMirrorBaseUrl) | ||
const nodeDir = getNodeVersionsBaseDir(opts.pnpmHomeDir) | ||
|
||
if (!nodeVersion) { | ||
throw new PnpmError('COULD_NOT_RESOLVE_NODEJS', `Couldn't find Node.js version matching ${params[0]}`) | ||
} | ||
|
||
const versionDir = path.resolve(nodeDir, nodeVersion) | ||
|
||
if (!existsSync(versionDir)) { | ||
throw new PnpmError('ENV_NO_NODE_DIRECTORY', `Couldn't find Node.js directory in ${versionDir}`) | ||
} | ||
|
||
const { nodePath, nodeLink } = await getNodeExecPathAndTargetDir(opts.pnpmHomeDir) | ||
|
||
if (nodeLink?.includes(versionDir)) { | ||
globalInfo(`Node.js version ${nodeVersion as string} was detected as the default one, removing ...`) | ||
|
||
const npmPath = path.resolve(opts.pnpmHomeDir, 'npm') | ||
const npxPath = path.resolve(opts.pnpmHomeDir, 'npx') | ||
|
||
try { | ||
await Promise.all([ | ||
removeBin(nodePath), | ||
removeBin(npmPath), | ||
removeBin(npxPath), | ||
]) | ||
} catch (err: any) { // eslint-disable-line | ||
if (err.code !== 'ENOENT') throw err | ||
} | ||
} | ||
|
||
await rimraf(versionDir) | ||
|
||
return `Node.js ${nodeVersion as string} is removed | ||
${versionDir}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { promises as fs } from 'fs' | ||
import path from 'path' | ||
import { PnpmError } from '@pnpm/error' | ||
import { createFetchFromRegistry } from '@pnpm/fetch' | ||
import { resolveNodeVersion } from '@pnpm/node.resolver' | ||
import cmdShim from '@zkochan/cmd-shim' | ||
import { getNodeDir, NvmNodeCommandOptions } from './node' | ||
import { getNodeMirror } from './getNodeMirror' | ||
import { parseNodeEditionSpecifier } from './parseNodeEditionSpecifier' | ||
import { getNodeExecPathInBinDir, getNodeExecPathInNodeDir } from './utils' | ||
|
||
export async function envUse (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 fetch = createFetchFromRegistry(opts) | ||
const { releaseChannel, versionSpecifier } = parseNodeEditionSpecifier(params[0]) | ||
const nodeMirrorBaseUrl = getNodeMirror(opts.rawConfig, releaseChannel) | ||
const nodeVersion = await resolveNodeVersion(fetch, versionSpecifier, nodeMirrorBaseUrl) | ||
if (!nodeVersion) { | ||
throw new PnpmError('COULD_NOT_RESOLVE_NODEJS', `Couldn't find Node.js version matching ${params[0]}`) | ||
} | ||
const nodeDir = await getNodeDir(fetch, { | ||
...opts, | ||
useNodeVersion: nodeVersion, | ||
nodeMirrorBaseUrl, | ||
}) | ||
const src = getNodeExecPathInNodeDir(nodeDir) | ||
const dest = getNodeExecPathInBinDir(opts.bin) | ||
try { | ||
await fs.unlink(dest) | ||
} catch (err) {} | ||
await fs.symlink(src, dest, 'file') | ||
try { | ||
let npmDir = nodeDir | ||
if (process.platform !== 'win32') { | ||
npmDir = path.join(npmDir, 'lib') | ||
} | ||
npmDir = path.join(npmDir, 'node_modules/npm') | ||
if (opts.configDir) { | ||
// We want the global npm settings to persist when Node.js or/and npm is changed to a different version, | ||
// so we tell npm to read the global config from centralized place that is outside of npm's directory. | ||
await fs.writeFile(path.join(npmDir, 'npmrc'), `globalconfig=${path.join(opts.configDir, 'npmrc')}`, 'utf-8') | ||
} | ||
const npmBinDir = path.join(npmDir, 'bin') | ||
const cmdShimOpts = { createPwshFile: false } | ||
await cmdShim(path.join(npmBinDir, 'npm-cli.js'), path.join(opts.bin, 'npm'), cmdShimOpts) | ||
await cmdShim(path.join(npmBinDir, 'npx-cli.js'), path.join(opts.bin, 'npx'), cmdShimOpts) | ||
} catch (err: any) { // eslint-disable-line | ||
// ignore | ||
} | ||
return `Node.js ${nodeVersion as string} is activated | ||
${dest} -> ${src}` | ||
} |