Skip to content

Commit

Permalink
Upgrade packageManager field by default (#1390).
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Mar 30, 2024
1 parent 1d13783 commit 4a9a34f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ Options that take no arguments can be negated by prefixing them with `--no-`, e.
</tr>
<tr>
<td>--dep &lt;value&gt;</td>
<td>Check one or more sections of dependencies only: dev, optional, peer, prod, or packageManager (comma-delimited). (default: ["prod","dev","optional"])</td>
<td>Check one or more sections of dependencies only: dev, optional, peer, prod, or packageManager (comma-delimited). (default: ["prod","dev","optional","packageManager"])</td>
</tr>
<tr>
<td>--deprecated</td>
Expand Down
2 changes: 1 addition & 1 deletion src/cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ const cliOptions: CLIOption[] = [
arg: 'value',
description:
'Check one or more sections of dependencies only: dev, optional, peer, prod, or packageManager (comma-delimited).',
default: ['prod', 'dev', 'optional'],
default: ['prod', 'dev', 'optional', 'packageManager'],
parse: value => (value && typeof value === 'string' ? value.split(',') : value),
type: 'string | string[]',
},
Expand Down
2 changes: 1 addition & 1 deletion src/types/RunOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
"type": "string"
}
],
"default": ["prod", "dev", "optional"],
"default": ["prod", "dev", "optional", "packageManager"],
"description": "Check one or more sections of dependencies only: dev, optional, peer, prod, or packageManager (comma-delimited)."
},
"deprecated": {
Expand Down
2 changes: 1 addition & 1 deletion src/types/RunOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface RunOptions {

/** Check one or more sections of dependencies only: dev, optional, peer, prod, or packageManager (comma-delimited).
*
* @default ["prod","dev","optional"]
* @default ["prod","dev","optional","packageManager"]
*/
dep?: string | string[]

Expand Down
89 changes: 85 additions & 4 deletions test/dep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('--dep', () => {
})

describe('packageManager field', () => {
it('support packageManager field', async () => {
it('upgrade packageManager field by default', async () => {
const stub = stubVersions({
'ncu-test-tag': '1.0.0',
npm: '9.0.0',
Expand All @@ -224,7 +224,6 @@ describe('--dep', () => {
packageFile: pkgFile,
jsonUpgraded: false,
upgrade: true,
dep: 'prod,packageManager',
})
const pkgDataNew = await fs.readFile(pkgFile, 'utf-8')
const pkgNew = JSON.parse(pkgDataNew)
Expand All @@ -241,6 +240,48 @@ describe('--dep', () => {
}
})

it('do not upgrade packageManager field if missing from --dep', async () => {
const stub = stubVersions({
'ncu-test-tag': '1.0.0',
npm: '9.0.0',
})
const packageData = JSON.stringify(
{
packageManager: '[email protected]',
dependencies: {
'ncu-test-tag': '0.1.0',
},
},
null,
2,
)

const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(pkgFile, packageData)

try {
await ncu({
packageFile: pkgFile,
jsonUpgraded: false,
upgrade: true,
dep: ['prod'],
})
const pkgDataNew = await fs.readFile(pkgFile, 'utf-8')
const pkgNew = JSON.parse(pkgDataNew)

pkgNew.should.deep.equal({
packageManager: '[email protected]',
dependencies: {
'ncu-test-tag': '1.0.0',
},
})
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
stub.restore()
}
})

it('do nothing if no packageManager field is present', async () => {
const stub = stubVersions({
'ncu-test-tag': '1.0.0',
Expand All @@ -265,7 +306,6 @@ describe('--dep', () => {
packageFile: pkgFile,
jsonUpgraded: false,
upgrade: true,
dep: 'prod,packageManager',
})
const pkgDataNew = await fs.readFile(pkgFile, 'utf-8')
const pkgNew = JSON.parse(pkgDataNew)
Expand All @@ -281,6 +321,48 @@ describe('--dep', () => {
}
})

it('upgrade packageManager field if specified in --dep', async () => {
const stub = stubVersions({
'ncu-test-tag': '1.0.0',
npm: '9.0.0',
})
const packageData = JSON.stringify(
{
packageManager: '[email protected]',
dependencies: {
'ncu-test-tag': '0.1.0',
},
},
null,
2,
)

const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(pkgFile, packageData)

try {
await ncu({
packageFile: pkgFile,
jsonUpgraded: false,
upgrade: true,
dep: ['prod', 'packageManager'],
})
const pkgDataNew = await fs.readFile(pkgFile, 'utf-8')
const pkgNew = JSON.parse(pkgDataNew)

pkgNew.should.deep.equal({
packageManager: '[email protected]',
dependencies: {
'ncu-test-tag': '1.0.0',
},
})
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
stub.restore()
}
})

it('do nothing if packageManager is up-to-date', async () => {
const stub = stubVersions({
'ncu-test-tag': '1.0.0',
Expand All @@ -306,7 +388,6 @@ describe('--dep', () => {
packageFile: pkgFile,
jsonUpgraded: false,
upgrade: true,
dep: 'prod,packageManager',
})
const pkgDataNew = await fs.readFile(pkgFile, 'utf-8')
const pkgNew = JSON.parse(pkgDataNew)
Expand Down

0 comments on commit 4a9a34f

Please sign in to comment.