1- import type { FileHandle } from 'node:fs/promises'
21import type { PackageManager } from 'nypm'
32import type { PackageJson } from 'pkg-types'
43
4+ import type { RegistryMeta } from '../../utils/registry'
55import type { NuxtModule } from './_utils'
6- import * as fs from 'node:fs'
7- import { homedir } from 'node:os'
8- import { join } from 'node:path'
96import process from 'node:process'
107
118import { cancel , confirm , isCancel , select , spinner } from '@clack/prompts'
@@ -23,22 +20,15 @@ import { runCommandDef as runCommand } from '../../run'
2320import { createInstallLog , resolvePackageManagerDescriptor , runInstall , takeUnreportedIgnoredBuilds } from '../../utils/install'
2421import { logger } from '../../utils/logger'
2522import { logNetworkError } from '../../utils/network'
23+ import { detectNpmRegistry } from '../../utils/registry'
2624import { getNuxtVersion } from '../../utils/versions'
2725import { cwdArgs , logLevelArgs } from '../_shared'
2826import prepareCommand from '../prepare'
2927import { selectModulesAutocomplete } from './_autocomplete'
30- import { checkNuxtCompatibility , ensureNuxtDependency , fetchModules , forwardCommandArgs , getProjectDependencies , getRegistryFromContent , isPnpmWorkspace , MODULES_API_URL } from './_utils'
28+ import { checkNuxtCompatibility , ensureNuxtDependency , fetchModules , forwardCommandArgs , getProjectDependencies , isPnpmWorkspace , MODULES_API_URL } from './_utils'
3129
32- const PROTOCOL_RE = / ^ h t t p s ? : \/ \/ /
33- const TRAILING_SLASH_RE = / \/ $ /
34- const REGEX_SPECIAL_RE = / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g
3530const WHITESPACE_RE = / \s /
3631
37- interface RegistryMeta {
38- registry : string
39- authToken : string | null
40- }
41-
4232interface ResolvedModule {
4333 nuxtModule ?: NuxtModule
4434 pkg : string
@@ -472,86 +462,3 @@ async function resolveModule(moduleName: string, cwd: string): Promise<ModuleRes
472462 . map ( ( [ name ] ) => name ) ,
473463 }
474464}
475-
476- function getNpmrcPaths ( ) : string [ ] {
477- const userNpmrcPath = join ( homedir ( ) , '.npmrc' )
478- const cwdNpmrcPath = join ( process . cwd ( ) , '.npmrc' )
479-
480- return [ cwdNpmrcPath , userNpmrcPath ]
481- }
482-
483- async function getAuthToken ( registry : RegistryMeta [ 'registry' ] ) : Promise < RegistryMeta [ 'authToken' ] > {
484- const paths = getNpmrcPaths ( )
485- const registryHost = registry . replace ( PROTOCOL_RE , '' ) . replace ( TRAILING_SLASH_RE , '' ) . replace ( REGEX_SPECIAL_RE , '\\$&' )
486- const authTokenRegex = new RegExp ( `^//${ registryHost } /:_authToken=(.+)$` , 'm' )
487-
488- for ( const npmrcPath of paths ) {
489- let fd : FileHandle | undefined
490- try {
491- fd = await fs . promises . open ( npmrcPath , 'r' )
492- if ( await fd . stat ( ) . then ( r => r . isFile ( ) ) ) {
493- const npmrcContent = await fd . readFile ( 'utf-8' )
494- const authTokenMatch = npmrcContent . match ( authTokenRegex ) ?. [ 1 ]
495-
496- if ( authTokenMatch ) {
497- return authTokenMatch . trim ( )
498- }
499- }
500- }
501- catch {
502- // swallow errors as file does not exist
503- }
504- finally {
505- await fd ?. close ( )
506- }
507- }
508-
509- return null
510- }
511-
512- async function detectNpmRegistry ( scope : string | null ) : Promise < RegistryMeta > {
513- const registry = await getRegistry ( scope )
514- const authToken = await getAuthToken ( registry )
515-
516- return {
517- registry,
518- authToken,
519- }
520- }
521-
522- async function getRegistry ( scope : string | null ) : Promise < string > {
523- if ( process . env . COREPACK_NPM_REGISTRY ) {
524- return process . env . COREPACK_NPM_REGISTRY
525- }
526- const registry = await getRegistryFromFile ( getNpmrcPaths ( ) , scope )
527-
528- if ( registry ) {
529- process . env . COREPACK_NPM_REGISTRY = registry
530- }
531-
532- return registry || 'https://registry.npmjs.org'
533- }
534-
535- async function getRegistryFromFile ( paths : string [ ] , scope : string | null ) {
536- for ( const npmrcPath of paths ) {
537- let fd : FileHandle | undefined
538- try {
539- fd = await fs . promises . open ( npmrcPath , 'r' )
540- if ( await fd . stat ( ) . then ( r => r . isFile ( ) ) ) {
541- const npmrcContent = await fd . readFile ( 'utf-8' )
542- const registry = getRegistryFromContent ( npmrcContent , scope )
543-
544- if ( registry ) {
545- return registry
546- }
547- }
548- }
549- catch {
550- // swallow errors as file does not exist
551- }
552- finally {
553- await fd ?. close ( )
554- }
555- }
556- return null
557- }
0 commit comments