Skip to content

Commit

Permalink
fix #1325 - setting errorLevel in workspaces doesn't work as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
Regev Brody authored and regevbr committed Sep 5, 2023
1 parent 7b2e1a0 commit ed4f1ea
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { print, printJson } from './lib/logging'
import mergeOptions from './lib/mergeOptions'
import programError from './lib/programError'
import runGlobal from './lib/runGlobal'
import runLocal from './lib/runLocal'
import runLocal, { runLocalWithUpgradedCount } from './lib/runLocal'
import packageManagers from './package-managers'
import { Index } from './types/IndexType'
import { Options } from './types/Options'
Expand Down Expand Up @@ -195,6 +195,7 @@ async function runUpgrades(options: Options, timeout?: NodeJS.Timeout): Promise<
clearTimeout(timeout)
return analysis
} else if (options.deep) {
let totalUpgradedCount = 0
analysis = await selectedPackageInfos.reduce(async (previousPromise, packageInfo: PackageInfo) => {
const packages = await previousPromise
// copy object to prevent share .ncurc options between different packageFile, to prevent unpredictable behavior
Expand All @@ -209,8 +210,11 @@ async function runUpgrades(options: Options, timeout?: NodeJS.Timeout): Promise<
...rcConfig,
packageFile: packageInfo.filepath,
workspacePackages,
errorLevel: 1,
}
const { pkgData, pkgFile } = await findPackage(pkgOptions)
const [upgradedCount, runResult] = await runLocalWithUpgradedCount(pkgOptions, pkgData, pkgFile)
totalUpgradedCount += upgradedCount
return {
...packages,
// index by relative path if cwd was specified
Expand All @@ -219,12 +223,15 @@ async function runUpgrades(options: Options, timeout?: NodeJS.Timeout): Promise<
.relative(path.resolve(pkgOptions.cwd), pkgFile!)
// convert Windows path to *nix path for consistency
.replace(/\\/g, '/')
: pkgFile!]: await runLocal(pkgOptions, pkgData, pkgFile),
: pkgFile!]: runResult,
}
}, Promise.resolve({} as Index<PackageFile> | PackageFile))
if (options.json) {
printJson(options, analysis)
}
if (totalUpgradedCount > 0 && options.errorLevel === 2) {
programError(options, '\nDependencies not up-to-date')
}
} else {
// mutate packageFile when glob pattern finds only single package
if (
Expand Down
21 changes: 16 additions & 5 deletions src/lib/runLocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ const chooseUpgrades = async (
return keyValueBy(chosenDeps, dep => ({ [dep]: newDependencies[dep] }))
}

/** Checks local project dependencies for upgrades. */
async function runLocal(
/** Checks local project dependencies for upgrades and returns upgraded count. */
export async function runLocalWithUpgradedCount(
options: Options,
pkgData?: Maybe<string>,
pkgFile?: Maybe<string>,
): Promise<PackageFile | Index<VersionSpec>> {
): Promise<[upgradeCount: number, result: PackageFile | Index<VersionSpec>]> {
print(options, '\nOptions:', 'verbose')
printOptionsSorted(options, 'verbose')

Expand Down Expand Up @@ -260,7 +260,8 @@ async function runLocal(
printJson(options, output)
}

if (Object.keys(filteredUpgraded).length > 0) {
const upgradedCount = Object.keys(filteredUpgraded).length
if (upgradedCount > 0) {
// if there is a package file, write the new package data
// otherwise, suggest ncu -u
if (pkgFile) {
Expand Down Expand Up @@ -292,7 +293,17 @@ async function runLocal(

await writePromise

return output
return [upgradedCount, output]
}

/** Checks local project dependencies for upgrades. */
async function runLocal(
options: Options,
pkgData?: Maybe<string>,
pkgFile?: Maybe<string>,
): Promise<PackageFile | Index<VersionSpec>> {
const result = await runLocalWithUpgradedCount(options, pkgData, pkgFile)
return result[1]
}

export default runLocal
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

0 comments on commit ed4f1ea

Please sign in to comment.