From fc3003360b58df1f5c658f2aac61bd5a1bf99140 Mon Sep 17 00:00:00 2001 From: regevbr Date: Thu, 7 Sep 2023 16:01:25 +0300 Subject: [PATCH] fix #1325 - setting errorLevel in workspaces doesn't work as expected --- src/index.ts | 21 ++++++++++++++------- src/lib/runLocal.ts | 7 ------- test/workspaces.test.ts | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/index.ts b/src/index.ts index 67a4bdc2..6dbbffcf 100755 --- a/src/index.ts +++ b/src/index.ts @@ -72,6 +72,15 @@ const getPackageManagerForInstall = async (options: Options, pkgFile: string) => return pnpmDetected ? 'pnpm' : 'npm' } +/** Returns if analysis contains upgrades */ +const someUpgraded = (pkgs: string[], analysis: Index | PackageFile) => { + // deep mode analysis is of type Index + // non-deep mode analysis is of type , so we normalize it to Index + const analysisNormalized: Index = + pkgs.length === 1 ? { [pkgs[0]]: analysis as PackageFile } : (analysis as Index) + return Object.values(analysisNormalized).some(upgrades => Object.keys(upgrades).length > 0) +} + /** Either suggest an install command based on the package manager, or in interactive mode, prompt to auto-install. */ const npmInstall = async ( pkgs: string[], @@ -83,15 +92,9 @@ const npmInstall = async ( return } - // deep mode analysis is of type Index - // non-deep mode analysis is of type , so we normalize it to Index - const analysisNormalized: Index = - pkgs.length === 1 ? { [pkgs[0]]: analysis as PackageFile } : (analysis as Index) - const someUpgraded = Object.values(analysisNormalized).some(upgrades => Object.keys(upgrades).length > 0) - // if no packages were upgraded (i.e. all dependencies deselected in interactive mode), then bail without suggesting an install. // normalize the analysis for one or many packages - if (!someUpgraded) return + if (!someUpgraded(pkgs, analysis)) return // for the purpose of the install hint, just use the package manager used in the first sub-project // if auto-installing, the actual package manager in each sub-project will be used @@ -238,6 +241,10 @@ async function runUpgrades(options: Options, timeout?: NodeJS.Timeout): Promise< } clearTimeout(timeout) + if (options.errorLevel === 2 && someUpgraded(packageFilepaths, analysis)) { + programError(options, '\nDependencies not up-to-date') + } + // suggest install command or auto-install if (options.upgrade) { // deno does not have an install command diff --git a/src/lib/runLocal.ts b/src/lib/runLocal.ts index 7b14059e..d107363e 100644 --- a/src/lib/runLocal.ts +++ b/src/lib/runLocal.ts @@ -281,13 +281,6 @@ async function runLocal( print(options, upgradeHint) } } - - // if errorLevel is 2, exit with non-zero error code - if (options.errorLevel === 2) { - writePromise.then(() => { - programError(options, '\nDependencies not up-to-date') - }) - } } await writePromise diff --git a/test/workspaces.test.ts b/test/workspaces.test.ts index 656d6ba6..85569cea 100644 --- a/test/workspaces.test.ts +++ b/test/workspaces.test.ts @@ -367,6 +367,29 @@ describe('workspaces', () => { } }) + it('update root project and workspaces if errorLevel=2', async () => { + const tempDir = await setup() + try { + await spawn('node', [bin, '--upgrade', '--workspaces', '--root', '--errorLevel', '2'], { + cwd: tempDir, + }).should.eventually.be.rejectedWith('Dependencies not up-to-date') + const upgradedPkg = JSON.parse(await fs.readFile(path.join(tempDir, 'package.json'), 'utf-8')) + upgradedPkg.should.have.property('dependencies') + upgradedPkg.dependencies.should.have.property('ncu-test-v2') + upgradedPkg.dependencies['ncu-test-v2'].should.not.equal('1.0.0') + const upgradedPkgA = JSON.parse(await fs.readFile(path.join(tempDir, 'packages/a/package.json'), 'utf-8')) + upgradedPkgA.should.have.property('dependencies') + upgradedPkgA.dependencies.should.have.property('ncu-test-tag') + upgradedPkgA.dependencies['ncu-test-tag'].should.not.equal('1.0.0') + const upgradedPkgB = JSON.parse(await fs.readFile(path.join(tempDir, 'packages/b/package.json'), 'utf-8')) + upgradedPkgB.should.have.property('dependencies') + upgradedPkgB.dependencies.should.have.property('ncu-test-return-version') + upgradedPkgB.dependencies['ncu-test-return-version'].should.not.equal('1.0.0') + } finally { + await fs.rm(tempDir, { recursive: true, force: true }) + } + }) + it('do not update non-workspace subpackages', async () => { const tempDir = await setup() await fs.mkdir(path.join(tempDir, 'other'), { recursive: true })