Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1325 - setting errorLevel in workspaces doesn't work as expected #1326

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> | PackageFile) => {
// deep mode analysis is of type Index<PackageFile>
// non-deep mode analysis is of type <PackageFile>, so we normalize it to Index<PackageFile>
const analysisNormalized: Index<PackageFile> =
pkgs.length === 1 ? { [pkgs[0]]: analysis as PackageFile } : (analysis as Index<PackageFile>)
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[],
Expand All @@ -83,15 +92,9 @@ const npmInstall = async (
return
}

// deep mode analysis is of type Index<PackageFile>
// non-deep mode analysis is of type <PackageFile>, so we normalize it to Index<PackageFile>
const analysisNormalized: Index<PackageFile> =
pkgs.length === 1 ? { [pkgs[0]]: analysis as PackageFile } : (analysis as Index<PackageFile>)
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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions src/lib/runLocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/workspaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
Loading