From 687df92392337e3a4ad1585a4d941de2f4244825 Mon Sep 17 00:00:00 2001 From: kiameow Date: Thu, 5 Sep 2024 16:40:28 +0800 Subject: [PATCH 1/5] feat: user now can download package from the registry that specified in npmrc --- src/commands/module/add.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index e07b5318..137e5d8b 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -1,3 +1,6 @@ +import * as fs from 'node:fs' +import { homedir } from 'node:os' +import { join } from 'node:path' import { defineCommand } from 'citty' import { resolve } from 'pathe' import consola from 'consola' @@ -216,8 +219,9 @@ async function resolveModule( // Fetch package on npm pkgVersion = pkgVersion || 'latest' + const registry = getRegistry() const pkg = await $fetch( - `https://registry.npmjs.org/${pkgName}/${pkgVersion}`, + `${registry}/${pkgName}/${pkgVersion}`, ) const pkgDependencies = Object.assign( pkg.dependencies || {}, @@ -248,3 +252,13 @@ async function resolveModule( pkgVersion, } } + +function getRegistry() { + const npmrcPath = join(homedir(), '.npmrc') + if (fs.existsSync(npmrcPath)) { + const npmrcContent = fs.readFileSync(npmrcPath, 'utf-8') + const registryMatch = npmrcContent.match(/registry=(.*)/) + return registryMatch ? registryMatch[1].trim() : 'https://registry.npmjs.org' + } + return 'https://registry.npmjs.org' // default registry +} From acd4fa2b741b55a13601e5b3cb12ef877ab0de2e Mon Sep 17 00:00:00 2001 From: kiameow Date: Thu, 5 Sep 2024 18:14:24 +0800 Subject: [PATCH 2/5] feat: add user-specified registry support (from npmrc) --- src/commands/module/add.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index 137e5d8b..d290e502 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -219,7 +219,8 @@ async function resolveModule( // Fetch package on npm pkgVersion = pkgVersion || 'latest' - const registry = getRegistry() + const registry = getRegistryFromNpmrc() + setCorepackNpmRegistry(registry) const pkg = await $fetch( `${registry}/${pkgName}/${pkgVersion}`, ) @@ -253,7 +254,7 @@ async function resolveModule( } } -function getRegistry() { +function getRegistryFromNpmrc() { const npmrcPath = join(homedir(), '.npmrc') if (fs.existsSync(npmrcPath)) { const npmrcContent = fs.readFileSync(npmrcPath, 'utf-8') @@ -262,3 +263,9 @@ function getRegistry() { } return 'https://registry.npmjs.org' // default registry } + +function setCorepackNpmRegistry(registry: string) { + if (!process.env.COREPACK_NPM_REGISTRY) { + process.env.COREPACK_NPM_REGISTRY = registry ? registry : '' + } +} From db4527c9c3fc66785f2795ba035c695b80157277 Mon Sep 17 00:00:00 2001 From: kiameow Date: Sat, 14 Sep 2024 22:41:32 +0800 Subject: [PATCH 3/5] feat: scan the npmrc in working dir first --- src/commands/module/add.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index d290e502..e635cb51 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -255,13 +255,20 @@ async function resolveModule( } function getRegistryFromNpmrc() { - const npmrcPath = join(homedir(), '.npmrc') + const userNpmrcPath = join(homedir(), '.npmrc') + const cwdNpmrcPath = join(process.cwd(), '.npmrc') + return getRegistryFromFile(cwdNpmrcPath) || getRegistryFromFile(userNpmrcPath) || 'https://registry.npmjs.org' +} + +function getRegistryFromFile(npmrcPath: string) { if (fs.existsSync(npmrcPath)) { const npmrcContent = fs.readFileSync(npmrcPath, 'utf-8') const registryMatch = npmrcContent.match(/registry=(.*)/) - return registryMatch ? registryMatch[1].trim() : 'https://registry.npmjs.org' + if (registryMatch) { + return registryMatch[1].trim() + } } - return 'https://registry.npmjs.org' // default registry + return null } function setCorepackNpmRegistry(registry: string) { From 4579dbe12f0d75b1a3ecd3524f253aae94dddc49 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 19 Sep 2024 15:59:49 +0100 Subject: [PATCH 4/5] fix: respect env variable if it exists --- src/commands/module/add.ts | 50 +++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index e635cb51..750ff1bf 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -1,6 +1,7 @@ import * as fs from 'node:fs' import { homedir } from 'node:os' import { join } from 'node:path' +import type { FileHandle } from 'node:fs/promises' import { defineCommand } from 'citty' import { resolve } from 'pathe' import consola from 'consola' @@ -219,11 +220,8 @@ async function resolveModule( // Fetch package on npm pkgVersion = pkgVersion || 'latest' - const registry = getRegistryFromNpmrc() - setCorepackNpmRegistry(registry) - const pkg = await $fetch( - `${registry}/${pkgName}/${pkgVersion}`, - ) + const registry = await detectNpmRegistry() + const pkg = await $fetch(`${registry}/${pkgName}/${pkgVersion}`) const pkgDependencies = Object.assign( pkg.dependencies || {}, pkg.devDependencies || {}, @@ -254,25 +252,39 @@ async function resolveModule( } } -function getRegistryFromNpmrc() { +async function detectNpmRegistry() { + console.log(process.env.COREPACK_NPM_REGISTRY) + if (process.env.COREPACK_NPM_REGISTRY) { + return process.env.COREPACK_NPM_REGISTRY + } const userNpmrcPath = join(homedir(), '.npmrc') const cwdNpmrcPath = join(process.cwd(), '.npmrc') - return getRegistryFromFile(cwdNpmrcPath) || getRegistryFromFile(userNpmrcPath) || 'https://registry.npmjs.org' + const registry = await getRegistryFromFile([cwdNpmrcPath, userNpmrcPath]) + if (registry) { + process.env.COREPACK_NPM_REGISTRY = registry + } + return registry || 'https://registry.npmjs.org' } -function getRegistryFromFile(npmrcPath: string) { - if (fs.existsSync(npmrcPath)) { - const npmrcContent = fs.readFileSync(npmrcPath, 'utf-8') - const registryMatch = npmrcContent.match(/registry=(.*)/) - if (registryMatch) { - return registryMatch[1].trim() +async function getRegistryFromFile(paths: string[]) { + for (const npmrcPath of paths) { + let fd: FileHandle | undefined + try { + fd = await fs.promises.open(npmrcPath, 'r') + if (await fd.stat().then(r => r.isFile())) { + const npmrcContent = await fd.readFile('utf-8') + const registryMatch = npmrcContent.match(/registry=(.*)/) + if (registryMatch) { + return registryMatch[1].trim() + } + } + } + catch { + // swallow errors as file does not exist + } + finally { + await fd?.close() } } return null } - -function setCorepackNpmRegistry(registry: string) { - if (!process.env.COREPACK_NPM_REGISTRY) { - process.env.COREPACK_NPM_REGISTRY = registry ? registry : '' - } -} From 0e9c1235a214a6e5e6470c8d9b5af2917547dfb7 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 19 Sep 2024 16:00:30 +0100 Subject: [PATCH 5/5] chore: cleanup console.log --- src/commands/module/add.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index 750ff1bf..bfd54df1 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -253,7 +253,6 @@ async function resolveModule( } async function detectNpmRegistry() { - console.log(process.env.COREPACK_NPM_REGISTRY) if (process.env.COREPACK_NPM_REGISTRY) { return process.env.COREPACK_NPM_REGISTRY }