|
1 | 1 | import * as fs from 'node:fs'
|
2 | 2 | import { homedir } from 'node:os'
|
3 | 3 | import { join } from 'node:path'
|
| 4 | +import type { FileHandle } from 'node:fs/promises' |
4 | 5 | import { defineCommand } from 'citty'
|
5 | 6 | import { resolve } from 'pathe'
|
6 | 7 | import consola from 'consola'
|
@@ -219,11 +220,8 @@ async function resolveModule(
|
219 | 220 |
|
220 | 221 | // Fetch package on npm
|
221 | 222 | pkgVersion = pkgVersion || 'latest'
|
222 |
| - const registry = getRegistryFromNpmrc() |
223 |
| - setCorepackNpmRegistry(registry) |
224 |
| - const pkg = await $fetch( |
225 |
| - `${registry}/${pkgName}/${pkgVersion}`, |
226 |
| - ) |
| 223 | + const registry = await detectNpmRegistry() |
| 224 | + const pkg = await $fetch(`${registry}/${pkgName}/${pkgVersion}`) |
227 | 225 | const pkgDependencies = Object.assign(
|
228 | 226 | pkg.dependencies || {},
|
229 | 227 | pkg.devDependencies || {},
|
@@ -254,25 +252,39 @@ async function resolveModule(
|
254 | 252 | }
|
255 | 253 | }
|
256 | 254 |
|
257 |
| -function getRegistryFromNpmrc() { |
| 255 | +async function detectNpmRegistry() { |
| 256 | + console.log(process.env.COREPACK_NPM_REGISTRY) |
| 257 | + if (process.env.COREPACK_NPM_REGISTRY) { |
| 258 | + return process.env.COREPACK_NPM_REGISTRY |
| 259 | + } |
258 | 260 | const userNpmrcPath = join(homedir(), '.npmrc')
|
259 | 261 | const cwdNpmrcPath = join(process.cwd(), '.npmrc')
|
260 |
| - return getRegistryFromFile(cwdNpmrcPath) || getRegistryFromFile(userNpmrcPath) || 'https://registry.npmjs.org' |
| 262 | + const registry = await getRegistryFromFile([cwdNpmrcPath, userNpmrcPath]) |
| 263 | + if (registry) { |
| 264 | + process.env.COREPACK_NPM_REGISTRY = registry |
| 265 | + } |
| 266 | + return registry || 'https://registry.npmjs.org' |
261 | 267 | }
|
262 | 268 |
|
263 |
| -function getRegistryFromFile(npmrcPath: string) { |
264 |
| - if (fs.existsSync(npmrcPath)) { |
265 |
| - const npmrcContent = fs.readFileSync(npmrcPath, 'utf-8') |
266 |
| - const registryMatch = npmrcContent.match(/registry=(.*)/) |
267 |
| - if (registryMatch) { |
268 |
| - return registryMatch[1].trim() |
| 269 | +async function getRegistryFromFile(paths: string[]) { |
| 270 | + for (const npmrcPath of paths) { |
| 271 | + let fd: FileHandle | undefined |
| 272 | + try { |
| 273 | + fd = await fs.promises.open(npmrcPath, 'r') |
| 274 | + if (await fd.stat().then(r => r.isFile())) { |
| 275 | + const npmrcContent = await fd.readFile('utf-8') |
| 276 | + const registryMatch = npmrcContent.match(/registry=(.*)/) |
| 277 | + if (registryMatch) { |
| 278 | + return registryMatch[1].trim() |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + catch { |
| 283 | + // swallow errors as file does not exist |
| 284 | + } |
| 285 | + finally { |
| 286 | + await fd?.close() |
269 | 287 | }
|
270 | 288 | }
|
271 | 289 | return null
|
272 | 290 | }
|
273 |
| - |
274 |
| -function setCorepackNpmRegistry(registry: string) { |
275 |
| - if (!process.env.COREPACK_NPM_REGISTRY) { |
276 |
| - process.env.COREPACK_NPM_REGISTRY = registry ? registry : '' |
277 |
| - } |
278 |
| -} |
|
0 commit comments