Skip to content

Commit

Permalink
fix: invalid scripts warning fixed for undefined scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
milaninfy committed May 22, 2024
1 parent 90863c1 commit d7ae3c6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lib/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
}
}
} else {
changes?.push(`Removed invalid "scripts"`)
// it only makes sense to fix if package.json has scripts field
data.scripts && changes?.push(`Removed invalid "scripts"`)
delete data.scripts
}
}
Expand Down
4 changes: 4 additions & 0 deletions tap-snapshots/test/fix.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ Array [
]
`

exports[`test/fix.js TAP with changes scriptpath no scripts > must match snapshot 1`] = `
Array []
`

exports[`test/fix.js TAP with changes scriptpath non-object script entry > must match snapshot 1`] = `
Array [
"Invalid scripts.\\"test\\" was removed",
Expand Down
18 changes: 0 additions & 18 deletions tap-snapshots/test/normalize.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,47 @@
exports[`test/normalize.js TAP @npmcli/package-json - with changes clean up bundleDependencies change name if bundleDependencies is not present > must match snapshot 1`] = `
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"Removed invalid \\"scripts\\"",
]
`

exports[`test/normalize.js TAP @npmcli/package-json - with changes clean up bundleDependencies dont array-ify if its an array already > must match snapshot 1`] = `
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"Removed invalid \\"scripts\\"",
]
`

exports[`test/normalize.js TAP @npmcli/package-json - with changes clean up bundleDependencies handle bundleDependencies object > must match snapshot 1`] = `
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"\\"bundleDependencies\\" was changed from an object to an array",
"Removed invalid \\"scripts\\"",
]
`

exports[`test/normalize.js TAP @npmcli/package-json - with changes clean up bundleDependencies handle bundleDependencies: false > must match snapshot 1`] = `
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"\\"bundleDependencies\\" was changed from \\"false\\" to \\"[]\\"",
"Removed invalid \\"scripts\\"",
]
`

exports[`test/normalize.js TAP @npmcli/package-json - with changes clean up bundleDependencies handle bundleDependencies: true > must match snapshot 1`] = `
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"\\"bundleDependencies\\" was auto-populated from \\"dependencies\\"",
"Removed invalid \\"scripts\\"",
]
`

exports[`test/normalize.js TAP @npmcli/package-json - with changes clean up bundleDependencies handle bundleDependencies: true with no deps > must match snapshot 1`] = `
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"\\"bundleDependencies\\" was auto-populated from \\"dependencies\\"",
"Removed invalid \\"scripts\\"",
]
`

exports[`test/normalize.js TAP @npmcli/package-json - with changes clean up bundleDependencies handle bundledDependencies: true > must match snapshot 1`] = `
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"\\"bundleDependencies\\" was auto-populated from \\"dependencies\\"",
"Removed invalid \\"scripts\\"",
]
`

Expand All @@ -77,29 +70,25 @@ Array [
exports[`test/normalize.js TAP @npmcli/package-json - with changes cleanup bins delete string bin when no name > must match snapshot 1`] = `
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"Removed invalid \\"scripts\\"",
]
`

exports[`test/normalize.js TAP @npmcli/package-json - with changes cleanup bins handle string when a name is set > must match snapshot 1`] = `
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"Removed invalid \\"scripts\\"",
"\\"bin\\" was converted to an object",
]
`

exports[`test/normalize.js TAP @npmcli/package-json - with changes cleanup bins remove non-object bin > must match snapshot 1`] = `
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"Removed invalid \\"scripts\\"",
]
`

exports[`test/normalize.js TAP @npmcli/package-json - with changes cleanup bins remove non-string bin values > must match snapshot 1`] = `
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"Removed invalid \\"scripts\\"",
"removed invalid \\"bin[y]\\"",
"removed invalid \\"bin[z]\\"",
]
Expand All @@ -108,7 +97,6 @@ Array [
exports[`test/normalize.js TAP @npmcli/package-json - with changes convert funding string to object > must match snapshot 1`] = `
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"Removed invalid \\"scripts\\"",
"\\"funding\\" was changed to an object with a url attribute",
]
`
Expand All @@ -117,7 +105,6 @@ exports[`test/normalize.js TAP @npmcli/package-json - with changes dedupe option
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"optionalDependencies.\\"whowins\\" was removed",
"Removed invalid \\"scripts\\"",
]
`

Expand All @@ -126,29 +113,25 @@ Array [
"Deleted incorrect \\"bundledDependencies\\"",
"optionalDependencies.\\"whowins\\" was removed",
"Empty \\"optionalDependencies\\" was removed",
"Removed invalid \\"scripts\\"",
]
`

exports[`test/normalize.js TAP @npmcli/package-json - with changes dedupe optional deps out of regular deps do not create regular deps if only optional specified > must match snapshot 1`] = `
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"Removed invalid \\"scripts\\"",
]
`

exports[`test/normalize.js TAP @npmcli/package-json - with changes normalize bin > must match snapshot 1`] = `
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"Removed invalid \\"scripts\\"",
]
`

exports[`test/normalize.js TAP @npmcli/package-json - with changes set _id if name and version set > must match snapshot 1`] = `
Array [
"\\"_id\\" was set to a@1.2.3",
"Deleted incorrect \\"bundledDependencies\\"",
"Removed invalid \\"scripts\\"",
]
`

Expand All @@ -161,6 +144,5 @@ Array [
"\\"_lodash\\" was removed",
"\\"_id\\" was set to underscore@1.2.3",
"Deleted incorrect \\"bundledDependencies\\"",
"Removed invalid \\"scripts\\"",
]
`
7 changes: 7 additions & 0 deletions test/fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ for (const [name, testFix] of Object.entries(testMethods)) {
const { content } = await testFix(t, testdir)
t.strictSame(content.scripts, { test: 'test-script' })
})
t.test('no scripts', async t => {
const testdir = {
'package.json': pkg({ scripts: undefined }),
}
const { content } = await testFix(t, testdir)
t.notHas(content, 'scripts')
})
})
t.test('bundleDependencies', async t => {
t.test('null', async t => {
Expand Down

0 comments on commit d7ae3c6

Please sign in to comment.