Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"c12",
"chokidar",
"clipboardy",
"confbox",
"consola",
"defu",
"exsolve",
Expand Down
1 change: 1 addition & 0 deletions packages/nuxi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"chokidar": "^4.0.3",
"citty": "^0.1.6",
"clipboardy": "^4.0.0",
"confbox": "^0.2.2",
"consola": "^3.4.2",
"defu": "^6.1.4",
"exsolve": "^1.0.5",
Expand Down
23 changes: 23 additions & 0 deletions packages/nuxi/src/commands/module/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { parseINI } from 'confbox'
import { $fetch } from 'ofetch'
import { readPackageJSON } from 'pkg-types'
import { coerce, satisfies } from 'semver'
Expand Down Expand Up @@ -124,3 +125,25 @@ export async function getNuxtVersion(cwd: string) {
const pkgDep = pkg?.dependencies?.nuxt || pkg?.devDependencies?.nuxt
return (pkgDep && coerce(pkgDep)?.version) || '3.0.0'
}

export function getRegistryFromContent(content: string, scope: string | null) {
try {
const npmConfig = parseINI<Record<string, string | undefined>>(content)

if (scope) {
const scopeKey = `${scope}:registry`
if (npmConfig[scopeKey]) {
return npmConfig[scopeKey].trim()
}
}

if (npmConfig.registry) {
return npmConfig.registry.trim()
}

return null
}
catch {
return null
}
}
18 changes: 4 additions & 14 deletions packages/nuxi/src/commands/module/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { joinURL } from 'ufo'
import { runCommand } from '../../run'
import { logger } from '../../utils/logger'
import { cwdArgs, logLevelArgs } from '../_shared'
import { checkNuxtCompatibility, fetchModules, getNuxtVersion } from './_utils'
import { checkNuxtCompatibility, fetchModules, getNuxtVersion, getRegistryFromContent } from './_utils'

interface RegistryMeta {
registry: string
Expand Down Expand Up @@ -406,20 +406,10 @@ async function getRegistryFromFile(paths: string[], scope: string | null) {
fd = await fs.promises.open(npmrcPath, 'r')
if (await fd.stat().then(r => r.isFile())) {
const npmrcContent = await fd.readFile('utf-8')
const registry = getRegistryFromContent(npmrcContent, scope)

if (scope) {
const scopedRegex = new RegExp(`^${scope}:registry=(.+)$`, 'm')
const scopedMatch = npmrcContent.match(scopedRegex)?.[1]
if (scopedMatch) {
return scopedMatch.trim()
}
}

// If no scoped registry found or no scope provided, look for the default registry
const defaultRegex = /^\s*registry=(.+)$/m
const defaultMatch = npmrcContent.match(defaultRegex)?.[1]
if (defaultMatch) {
return defaultMatch.trim()
if (registry) {
return registry
}
}
}
Expand Down
58 changes: 58 additions & 0 deletions packages/nuxi/test/unit/commands/module/_utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { describe, expect, it } from 'vitest'
import { getRegistryFromContent } from '../../../../src/commands/module/_utils'

describe('getRegistryFromContent', () => {
it('extracts scoped registry when scope is provided', () => {
const content = `
registry=https://registry.npmjs.org/
@myorg:registry=https://my-registry.org/
@another:registry=https://another-registry.org/
`

expect(getRegistryFromContent(content, '@myorg')).toBe('https://my-registry.org/')
expect(getRegistryFromContent(content, '@another')).toBe('https://another-registry.org/')
})

it('extracts default registry when scope is not provided', () => {
const content = `
registry=https://registry.npmjs.org/
@myorg:registry=https://my-registry.org/
`

expect(getRegistryFromContent(content, null)).toBe('https://registry.npmjs.org/')
})

it('extracts default registry when scope is provided but not found', () => {
const content = `
registry=https://registry.npmjs.org/
@myorg:registry=https://my-registry.org/
`

expect(getRegistryFromContent(content, '@notfound')).toBe('https://registry.npmjs.org/')
})

it('returns null when no registry is found', () => {
const content = `
# some npmrc content without registry
some-other-setting=value
`

expect(getRegistryFromContent(content, null)).toBeNull()
expect(getRegistryFromContent(content, '@myorg')).toBeNull()
})

it('handles empty content', () => {
expect(getRegistryFromContent('', null)).toBeNull()
expect(getRegistryFromContent('', '@myorg')).toBeNull()
})

it('extracts registry from line with comments', () => {
const content = `
registry=https://registry.npmjs.org/ # with comment
@myorg:registry=https://my-registry.org/ # another comment
`

expect(getRegistryFromContent(content, null)).toBe('https://registry.npmjs.org/')
expect(getRegistryFromContent(content, '@myorg')).toBe('https://my-registry.org/')
})
})
1 change: 1 addition & 0 deletions packages/nuxt-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"chokidar": "^4.0.3",
"citty": "^0.1.6",
"clipboardy": "^4.0.0",
"confbox": "^0.2.2",
"consola": "^3.4.2",
"defu": "^6.1.4",
"exsolve": "^1.0.5",
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading