Skip to content

Commit f476de4

Browse files
committed
fix parsing bug for .npmrc files with comments
1 parent 074cfa3 commit f476de4

File tree

4 files changed

+570
-379
lines changed

4 files changed

+570
-379
lines changed

packages/nuxi/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"chokidar": "^4.0.3",
4949
"citty": "^0.1.6",
5050
"clipboardy": "^4.0.0",
51+
"confbox": "^0.2.2",
5152
"consola": "^3.4.2",
5253
"defu": "^6.1.4",
5354
"fuse.js": "^7.1.0",

packages/nuxi/src/commands/module/_utils.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parseINI } from 'confbox'
12
import { $fetch } from 'ofetch'
23
import { readPackageJSON } from 'pkg-types'
34
import { coerce, satisfies } from 'semver'
@@ -126,20 +127,23 @@ export async function getNuxtVersion(cwd: string) {
126127
}
127128

128129
export function getRegistryFromContent(content: string, scope: string | null) {
129-
if (scope) {
130-
const scopedRegex = new RegExp(`^${scope}:registry=(.+)$`, 'm')
131-
const scopedMatch = content.match(scopedRegex)?.[1]
132-
if (scopedMatch) {
133-
return scopedMatch.trim()
130+
try {
131+
const npmConfig = parseINI<Record<string, string | undefined>>(content)
132+
133+
if (scope) {
134+
const scopeKey = `${scope}:registry`
135+
if (npmConfig[scopeKey]) {
136+
return npmConfig[scopeKey].trim()
137+
}
134138
}
135-
}
136139

137-
// If no scoped registry found or no scope provided, look for the default registry
138-
const defaultRegex = /^\s*registry=(.+)$/m
139-
const defaultMatch = content.match(defaultRegex)?.[1]
140-
if (defaultMatch) {
141-
return defaultMatch.trim()
142-
}
140+
if (npmConfig.registry) {
141+
return npmConfig.registry.trim()
142+
}
143143

144-
return null
144+
return null
145+
}
146+
catch {
147+
return null
148+
}
145149
}

packages/nuxi/test/unit/commands/module/_utils.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,14 @@ some-other-setting=value
4545
expect(getRegistryFromContent('', null)).toBeNull()
4646
expect(getRegistryFromContent('', '@myorg')).toBeNull()
4747
})
48+
49+
it('extracts registry from line with comments', () => {
50+
const content = `
51+
registry=https://registry.npmjs.org/ # with comment
52+
@myorg:registry=https://my-registry.org/ # another comment
53+
`
54+
55+
expect(getRegistryFromContent(content, null)).toBe('https://registry.npmjs.org/')
56+
expect(getRegistryFromContent(content, '@myorg')).toBe('https://my-registry.org/')
57+
})
4858
})

0 commit comments

Comments
 (0)