Skip to content

Commit

Permalink
Fix --target newest. Closes #734.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Sep 11, 2020
1 parent 5ec2e18 commit 19a2f3e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
3 changes: 1 addition & 2 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ const cliOptions = [
},
{
name: '-t, --target <value>',
description: 'Target version to upgrade to: latest, newest, greatest, minor, patch.',
default: 'latest'
description: 'Target version to upgrade to: latest, newest, greatest, minor, patch.'
},
{
name: '--timeout <ms>',
Expand Down
16 changes: 11 additions & 5 deletions lib/npm-check-updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ function initOptions(options) {
programError(options, chalk.red('Cannot specify both --greatest and --newest. --greatest is an alias for "--target greatest" and --newest is an alias for "--target newest".'))
}

const target = options.newest ? 'newest'
: options.greatest ? 'greatest'
: options.target || options.semverLevel || 'latest'

const autoPre = target === 'newest' || target === 'greatest'

return {
...options,
concurrency: options.concurrency || 8,
Expand All @@ -250,11 +256,11 @@ function initOptions(options) {
// convert silent option to loglevel silent
loglevel: options.silent ? 'silent' : options.loglevel || 'warn',
minimal: options.minimal === undefined ? false : options.minimal,
// default to 0, except when newest or greatest are set
pre: options.pre != null ? Boolean(Number(options.pre)) : options.newest || options.greatest,
target: options.newest ? 'newest'
: options.greatest ? 'greatest'
: options.target || options.semverLevel || 'latest',
// default to false, except when newest or greatest are set
...options.pre != null || autoPre
? { pre: options.pre != null ? !!options.pre : autoPre }
: null,
target,
// imply upgrade in interactive mode when json is not specified as the output
upgrade: options.interactive && options.upgrade === undefined ? !json : options.upgrade
}
Expand Down
56 changes: 30 additions & 26 deletions test/test-ncu.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('run', function () {
it('should not upgrade prereleases to newer prereleases with --pre 0', () => {

return ncu.run({
pre: false,
pre: 0,
packageData: JSON.stringify({
dependencies: {
'ncu-test-alpha-latest': '1.0.0-alpha.1'
Expand Down Expand Up @@ -187,7 +187,7 @@ describe('run', function () {
})
})

it('should not require --pre with --newest option', () => {
it('should not require --pre with --target newest', () => {

return ncu.run({
jsonAll: true,
Expand All @@ -196,7 +196,7 @@ describe('run', function () {
'ncu-mock-pre': '1.0.0'
}
}),
newest: true
target: 'newest'
}).then(data => {
return data.should.eql({
dependencies: {
Expand All @@ -206,7 +206,7 @@ describe('run', function () {
})
})

it('should not require --pre with --greatest option', () => {
it('should not require --pre with --target greatest', () => {

return ncu.run({
jsonAll: true,
Expand All @@ -215,7 +215,7 @@ describe('run', function () {
'ncu-mock-pre': '1.0.0'
}
}),
greatest: true
target: 'greatest'
}).then(data => {
return data.should.eql({
dependencies: {
Expand All @@ -225,7 +225,7 @@ describe('run', function () {
})
})

it('should allow --pre 0 with --newest option to exclude prereleases', () => {
it('should allow --pre 0 with --target newest to exclude prereleases', () => {

return ncu.run({
jsonAll: true,
Expand All @@ -234,8 +234,8 @@ describe('run', function () {
'ncu-mock-pre': '1.0.0'
}
}),
newest: true,
pre: '0'
target: 'newest',
pre: 0
}).then(data => {
return data.should.eql({
dependencies: {
Expand All @@ -245,11 +245,11 @@ describe('run', function () {
})
})

it('should work with --newest option with any invalid or wildcard range', () => {
it('should work with --target newest with any invalid or wildcard range', () => {
return Promise.all([
ncu.run({
jsonAll: true,
newest: true,
target: 'newest',
packageData: JSON.stringify({
dependencies: {
del: ''
Expand All @@ -258,7 +258,7 @@ describe('run', function () {
}),
ncu.run({
jsonAll: true,
newest: true,
target: 'newest',
packageData: JSON.stringify({
dependencies: {
del: 'invalid range'
Expand All @@ -267,7 +267,7 @@ describe('run', function () {
}),
ncu.run({
jsonAll: true,
newest: true,
target: 'newest',
packageData: JSON.stringify({
dependencies: {
del: '*'
Expand All @@ -276,7 +276,7 @@ describe('run', function () {
}),
ncu.run({
jsonAll: true,
newest: true,
target: 'newest',
packageData: JSON.stringify({
dependencies: {
del: '~'
Expand Down Expand Up @@ -365,22 +365,26 @@ describe('run', function () {
})
})

it('should not allow --greatest and --newest together', async () => {
ncu.run({ greatest: true, newest: true })
.should.eventually.be.rejectedWith('Cannot specify both')
})
describe('target', () => {

it('should not allow --target and --greatest together', async () => {
ncu.run({ target: 'greatest', newest: true })
.should.eventually.be.rejectedWith('Cannot specify both')
})
it('should not allow --greatest and --newest together', async () => {
ncu.run({ greatest: true, target: 'newest' })
.should.eventually.be.rejectedWith('Cannot specify both')
ncu.run({ target: 'greatest', newest: true })
.should.eventually.be.rejectedWith('Cannot specify both')
ncu.run({ greatest: true, newest: true })
.should.eventually.be.rejectedWith('Cannot specify both')
})

it('should not allow --target and --newest together', async () => {
ncu.run({ target: 'newest', greatest: true })
.should.eventually.be.rejectedWith('Cannot specify both')
})
it('should not allow --target and --greatest together', async () => {
ncu.run({ target: 'greatest', greatest: true })
.should.eventually.be.rejectedWith('Cannot specify both')
})

describe('target', () => {
it('should not allow --target and --newest together', async () => {
ncu.run({ target: 'newest', newest: true })
.should.eventually.be.rejectedWith('Cannot specify both')
})

it('should not update major versions with --target minor', async () => {
const pkgData = await ncu.run({ target: 'minor', packageData: '{ "dependencies": { "chalk": "3.0.0" } }' })
Expand Down

0 comments on commit 19a2f3e

Please sign in to comment.