Skip to content

Commit 4579dbe

Browse files
committed
fix: respect env variable if it exists
1 parent 343d812 commit 4579dbe

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

src/commands/module/add.ts

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fs from 'node:fs'
22
import { homedir } from 'node:os'
33
import { join } from 'node:path'
4+
import type { FileHandle } from 'node:fs/promises'
45
import { defineCommand } from 'citty'
56
import { resolve } from 'pathe'
67
import consola from 'consola'
@@ -219,11 +220,8 @@ async function resolveModule(
219220

220221
// Fetch package on npm
221222
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}`)
227225
const pkgDependencies = Object.assign(
228226
pkg.dependencies || {},
229227
pkg.devDependencies || {},
@@ -254,25 +252,39 @@ async function resolveModule(
254252
}
255253
}
256254

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+
}
258260
const userNpmrcPath = join(homedir(), '.npmrc')
259261
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'
261267
}
262268

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()
269287
}
270288
}
271289
return null
272290
}
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

Comments
 (0)