Skip to content

Commit cdf50b4

Browse files
committed
fix: auto install works with yarn berry, close #454
1 parent bc37e9d commit cdf50b4

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

  • packages

packages/devtools-kit/src/_types/rpc.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { StorageMounts } from 'nitropack'
33
import type { StorageValue } from 'unstorage'
44
import type { ModuleOptions, NuxtDevToolsOptions } from './options'
55
import type { ModuleCustomTab } from './custom-tabs'
6-
import type { AssetEntry, AssetInfo, AutoImportsWithMetadata, ComponentRelationship, HookInfo, ImageMeta, NpmCommandOptions, NpmCommandType, PackageManagerName, PackageUpdateInfo, ServerRouteInfo } from './integrations'
6+
import type { AssetEntry, AssetInfo, AutoImportsWithMetadata, ComponentRelationship, HookInfo, ImageMeta, NpmCommandOptions, NpmCommandType, PackageUpdateInfo, ServerRouteInfo } from './integrations'
77
import type { TerminalAction, TerminalInfo } from './terminals'
88
import type { GetWizardArgs, WizardActions } from './wizard'
99
import type { AnalyzeBuildsInfo } from './analyze-build'
@@ -31,7 +31,6 @@ export interface ServerFunctions {
3131

3232
// Updates
3333
checkForUpdateFor(name: string): Promise<PackageUpdateInfo | undefined>
34-
getPackageManager(): Promise<PackageManagerName>
3534
getNpmCommand(command: NpmCommandType, packageName: string, options?: NpmCommandOptions): Promise<string[] | undefined>
3635
runNpmCommand(token: string, command: NpmCommandType, packageName: string, options?: NpmCommandOptions): Promise<{ processId: string } | undefined>
3736

packages/devtools/src/server-rpc/npm.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import fs from 'node:fs/promises'
22
import { startSubprocess } from '@nuxt/devtools-kit'
33
import isInstalledGlobally from 'is-installed-globally'
4+
import type { PackageManager } from 'nypm'
45
import { detectPackageManager } from 'nypm'
56
import { parseModule } from 'magicast'
67
import { addNuxtModule, getDefaultExportOptions } from 'magicast/helpers'
78
import { 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

1011
export 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

Comments
 (0)