11import fs from 'node:fs/promises'
22import { startSubprocess } from '@nuxt/devtools-kit'
33import isInstalledGlobally from 'is-installed-globally'
4+ import type { PackageManager } from 'nypm'
45import { detectPackageManager } from 'nypm'
56import { parseModule } from 'magicast'
67import { addNuxtModule , getDefaultExportOptions } from 'magicast/helpers'
78import { checkForUpdateOf } from '../npm'
8- import type { NpmCommandOptions , NpmCommandType , NuxtDevtoolsServerContext , PackageManagerName , PackageUpdateInfo , ServerFunctions } from '../types'
9+ import type { NpmCommandOptions , NpmCommandType , NuxtDevtoolsServerContext , PackageUpdateInfo , ServerFunctions } from '../types'
910
1011export function setupNpmRPC ( { nuxt, ensureDevAuthToken } : NuxtDevtoolsServerContext ) {
11- let detectPromise : Promise < PackageManagerName > | undefined
12+ let detectPromise : Promise < PackageManager | undefined > | undefined
1213 const updatesPromise = new Map < string , Promise < PackageUpdateInfo | undefined > > ( )
1314
1415 function getPackageManager ( ) {
15- detectPromise ||= detectPackageManager ( nuxt . options . rootDir ) . then ( r => r ?. name || 'npm' )
16+ detectPromise ||= detectPackageManager ( nuxt . options . rootDir )
1617 return detectPromise
1718 }
1819
@@ -23,12 +24,29 @@ export function setupNpmRPC({ nuxt, ensureDevAuthToken }: NuxtDevtoolsServerCont
2324 } = options
2425 const agent = await getPackageManager ( )
2526
27+ const name = agent ?. name || 'npm'
28+
2629 // TODO: smartly detect dev/global installs as default
27- if ( command === 'install' || command === 'update' )
28- return [ agent , agent === 'npm' ? 'install' : 'add' , `${ packageName } @latest` , dev ? '-D' : '' , global ? '-g' : '' , '--ignore-scripts' ] . filter ( Boolean )
30+ if ( command === 'install' || command === 'update' ) {
31+ return [
32+ name ,
33+ name === 'npm' ? 'install' : 'add' ,
34+ `${ packageName } @latest` ,
35+ dev ? '-D' : '' ,
36+ global ? '-g' : '' ,
37+ // In yarn berry, `--ignore-scripts` is removed
38+ ( name === 'yarn' && ! agent ?. version ?. startsWith ( '1.' ) ) ? '' : '--ignore-scripts' ,
39+ ] . filter ( Boolean )
40+ }
2941
30- if ( command === 'uninstall' )
31- return [ agent , agent === 'npm' ? 'uninstall' : 'remove' , packageName , global ? '-g' : '' ] . filter ( Boolean )
42+ if ( command === 'uninstall' ) {
43+ return [
44+ name ,
45+ name === 'npm' ? 'uninstall' : 'remove' ,
46+ packageName ,
47+ global ? '-g' : '' ,
48+ ] . filter ( Boolean )
49+ }
3250 }
3351
3452 async function runNpmCommand ( command : NpmCommandType , packageName : string , options : NpmCommandOptions = { } ) {
@@ -63,7 +81,6 @@ export function setupNpmRPC({ nuxt, ensureDevAuthToken }: NuxtDevtoolsServerCont
6381 updatesPromise . set ( name , checkForUpdateOf ( name , undefined , nuxt ) )
6482 return updatesPromise . get ( name ) !
6583 } ,
66- getPackageManager,
6784 getNpmCommand,
6885 async runNpmCommand ( token , ...args ) {
6986 await ensureDevAuthToken ( token )
0 commit comments