Skip to content

Commit e0a6164

Browse files
committed
fix(add): respect project registry configuration
1 parent a08a775 commit e0a6164

4 files changed

Lines changed: 52 additions & 22 deletions

File tree

‎packages/nuxt-cli/src/commands/module/add.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ async function resolveModule(moduleName: string, cwd: string, modulesDB: NuxtMod
427427
// Fetch package on npm
428428
letversion=pkgVersion||'latest'
429429
constpkgScope=pkgName.startsWith('@') ? pkgName.split('/')[0]! : null
430-
constmeta: RegistryMeta=awaitdetectNpmRegistry(pkgScope)
430+
constmeta: RegistryMeta=awaitdetectNpmRegistry(pkgScope,cwd)
431431
constheaders: HeadersInit={}
432432

433433
if(meta.authToken){

‎packages/nuxt-cli/src/utils/registry.ts‎

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,8 @@ export function getRegistryFromContent(content: string, scope: string | null): s
3737
}
3838
}
3939

40-
functiongetNpmrcPaths(): string[]{
41-
constuserNpmrcPath=join(homedir(),'.npmrc')
42-
constcwdNpmrcPath=join(process.cwd(),'.npmrc')
43-
44-
return[cwdNpmrcPath,userNpmrcPath]
40+
functiongetNpmrcPaths(cwd: string): string[]{
41+
return[join(cwd,'.npmrc'),join(homedir(),'.npmrc')]
4542
}
4643

4744
asyncfunctiongetRegistryFromFile(paths: string[],scope: string|null){
@@ -68,21 +65,15 @@ async function getRegistryFromFile(paths: string[], scope: string | null) {
6865
returnnull
6966
}
7067

71-
asyncfunctiongetRegistry(scope: string|null): Promise<string>{
68+
asyncfunctiongetRegistry(scope: string|null,cwd: string): Promise<string>{
7269
if(process.env.COREPACK_NPM_REGISTRY){
7370
returnprocess.env.COREPACK_NPM_REGISTRY
7471
}
75-
constregistry=awaitgetRegistryFromFile(getNpmrcPaths(),scope)
76-
77-
if(registry){
78-
process.env.COREPACK_NPM_REGISTRY=registry
79-
}
80-
81-
returnregistry||'https://registry.npmjs.org'
72+
returnawaitgetRegistryFromFile(getNpmrcPaths(cwd),scope)||'https://registry.npmjs.org'
8273
}
8374

84-
asyncfunctiongetAuthToken(registry: RegistryMeta['registry']): Promise<RegistryMeta['authToken']>{
85-
constpaths=getNpmrcPaths()
75+
asyncfunctiongetAuthToken(registry: RegistryMeta['registry'],cwd: string): Promise<RegistryMeta['authToken']>{
76+
constpaths=getNpmrcPaths(cwd)
8677
constregistryHost=registry.replace(PROTOCOL_RE,'').replace(TRAILING_SLASH_RE,'')
8778
constauthTokenKey=`//${registryHost}/:_authToken`
8879

@@ -111,9 +102,9 @@ async function getAuthToken(registry: RegistryMeta['registry']): Promise<Registr
111102
returnnull
112103
}
113104

114-
exportasyncfunctiondetectNpmRegistry(scope: string|null): Promise<RegistryMeta>{
115-
constregistry=awaitgetRegistry(scope)
116-
constauthToken=awaitgetAuthToken(registry)
105+
exportasyncfunctiondetectNpmRegistry(scope: string|null,cwd=process.cwd()): Promise<RegistryMeta>{
106+
constregistry=awaitgetRegistry(scope,cwd)
107+
constauthToken=awaitgetAuthToken(registry,cwd)
117108

118109
return{
119110
registry,

‎packages/nuxt-cli/test/unit/commands/add.spec.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe('nuxt add command', () => {
193193
)
194194
})
195195

196-
it('should install multiple modules',async()=>{
196+
it('should install multiple modules with one modules database request',async()=>{
197197
constaddCommand=await(commandsasCommandsType).subCommands.add()
198198

199199
awaitaddCommand.setup({
@@ -203,6 +203,7 @@ describe('nuxt add command', () => {
203203
},
204204
})
205205

206+
expect(utils.fetchModules).toHaveBeenCalledTimes(1)
206207
expect(runInstall).toHaveBeenCalledWith(
207208
expect.objectContaining({
208209
cwd: '/fake-dir',
@@ -214,6 +215,17 @@ describe('nuxt add command', () => {
214215
)
215216
})
216217

218+
it('should resolve duplicate modules once',async()=>{
219+
constaddCommand=await(commandsasCommandsType).subCommands.add()
220+
221+
awaitaddCommand.setup({args: {cwd: '/fake-dir',_: ['ui','ui']}})
222+
223+
expect(mock$fetch).toHaveBeenCalledTimes(1)
224+
expect(runInstall).toHaveBeenCalledWith(expect.objectContaining({
225+
dependencies: ['@nuxt/ui@3.0.0'],
226+
}))
227+
})
228+
217229
it('should skip installation when skipInstall is true',async()=>{
218230
constaddCommand=await(commandsasCommandsType).subCommands.add()
219231

‎packages/nuxt-cli/test/unit/utils/registry.spec.ts‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
import{describe,expect,it}from'vitest'
2-
import{getRegistryFromContent}from'../../../src/utils/registry'
1+
import{mkdtemp,rm,writeFile}from'node:fs/promises'
2+
import{tmpdir}from'node:os'
3+
import{join}from'node:path'
4+
importprocessfrom'node:process'
5+
import{afterEach,describe,expect,it}from'vitest'
6+
import{detectNpmRegistry,getRegistryFromContent}from'../../../src/utils/registry'
7+
8+
constdirectories: string[]=[]
9+
10+
afterEach(async()=>{
11+
awaitPromise.all(directories.splice(0).map(directory=>rm(directory,{recursive: true,force: true})))
12+
})
313

414
describe('getRegistryFromContent',()=>{
515
it('extracts scoped registry when scope is provided',()=>{
@@ -56,3 +66,20 @@ registry=https://registry.npmjs.org/ # with comment
5666
expect(getRegistryFromContent(content,'@myorg')).toBe('https://my-registry.org/')
5767
})
5868
})
69+
70+
describe('detectNpmRegistry',()=>{
71+
it('reads registry and credentials from the project directory',async()=>{
72+
constdirectory=awaitmkdtemp(join(tmpdir(),'nuxt-registry-'))
73+
directories.push(directory)
74+
awaitwriteFile(join(directory,'.npmrc'),[
75+
'registry=https://registry.example.com/',
76+
'//registry.example.com/:_authToken=secret',
77+
].join('\n'))
78+
79+
awaitexpect(detectNpmRegistry(null,directory)).resolves.toEqual({
80+
registry: 'https://registry.example.com/',
81+
authToken: 'secret',
82+
})
83+
expect(process.env.COREPACK_NPM_REGISTRY).toBeUndefined()
84+
})
85+
})

0 commit comments

Comments
 (0)