Skip to content

Commit 37ebc81

Browse files
committed
npm install in root project only when workspaces are specified (#1182)
1 parent 253b3d2 commit 37ebc81

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/index.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const getPackageManagerForInstall = async (options: Options, pkgFile: string) =>
6868
}
6969

7070
/** Either suggest an install command based on the package manager, or in interactive mode, prompt to autoinstall. */
71-
const npmInstallHint = async (
71+
const npmInstall = async (
7272
pkgs: string[],
7373
analysis: Index<PackageFile> | PackageFile,
7474
options: Options,
@@ -80,16 +80,16 @@ const npmInstallHint = async (
8080
const someUpgraded = Object.values(analysisNormalized).some(upgrades => Object.keys(upgrades).length > 0)
8181
if (!someUpgraded) return
8282

83-
let showInstallHint = true
84-
8583
// for the purpose of the install hint, just use the package manager used in the first subproject
8684
// if autoinstalling, the actual package manager in each subproject will be used
8785
const packageManager = await getPackageManagerForInstall(options, pkgs[0])
8886

8987
// by default, show an install hint after upgrading
9088
// this will be disabled in interactive mode if the user chooses to have npm-check-updates execute the install command
91-
const installHint = `${chalk.cyan(packageManager + ' install')}${
92-
pkgs.length > 1 ? ' in each project directory' : ''
89+
const installHint = `Run ${chalk.cyan(packageManager + ' install')}${
90+
pkgs.length > 1 && !options.withWorkspace && !options.workspace && !options.withWorkspaces && !options.workspaces
91+
? ' in each project directory'
92+
: ''
9393
} to install new versions`
9494

9595
// prompt the user if they want ncu to run "npm install"
@@ -110,7 +110,6 @@ const npmInstallHint = async (
110110

111111
// autoinstall
112112
if (response.value) {
113-
showInstallHint = false
114113
pkgs.forEach(async pkgFile => {
115114
const packageManager = await getPackageManagerForInstall(options, pkgFile)
116115
const cmd = packageManager + (process.platform === 'win32' ? '.cmd' : '')
@@ -122,8 +121,8 @@ const npmInstallHint = async (
122121
}
123122

124123
// show the install hint unless autoinstall occurred
125-
if (showInstallHint) {
126-
print(options, `\nRun ${installHint}.`)
124+
else {
125+
print(options, `\n${installHint}.`)
127126
}
128127
}
129128

@@ -252,8 +251,9 @@ export async function run(
252251
}
253252

254253
// enable --deep if multiple package files are found
255-
options.deep =
256-
options.deep || options.withWorkspaces || options.workspaces || !!options.workspace || pkgs.length > 1
254+
const isWorkspace =
255+
options.withWorkspaces || options.workspaces || !!options.withWorkspace?.length || !!options.workspace?.length
256+
options.deep = options.deep || isWorkspace || pkgs.length > 1
257257

258258
let analysis: Index<PackageFile> | PackageFile | void
259259
if (options.global) {
@@ -302,7 +302,8 @@ export async function run(
302302

303303
// suggest install command or autoinstall
304304
if (options.upgrade) {
305-
await npmInstallHint(pkgs, analysis, options)
305+
// if workspaces, install from root project folder
306+
await npmInstall(isWorkspace ? ['package.json'] : pkgs, analysis, options)
306307
}
307308

308309
return analysis

0 commit comments

Comments
 (0)