Skip to content

Commit 191b521

Browse files
authored
fix: invalid scripts warning fixed for undefined scripts (#102)
Fixes npm/cli#7127
1 parent 90863c1 commit 191b521

4 files changed

Lines changed: 25 additions & 19 deletions

File tree

lib/normalize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
251251

252252
// strip "node_modules/.bin" from scripts entries
253253
// remove invalid scripts entries (non-strings)
254-
if (steps.includes('scripts') || steps.includes('scriptpath')) {
254+
if ((steps.includes('scripts') || steps.includes('scriptpath')) && data.scripts !== undefined) {
255255
const spre = /^(\.[/\\])?node_modules[/\\].bin[\\/]/
256256
if (typeof data.scripts === 'object') {
257257
for (const name in data.scripts) {

tap-snapshots/test/fix.js.test.cjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ Array [
116116
]
117117
`
118118

119+
exports[`test/fix.js TAP with changes scriptpath falsy scripts > must match snapshot 1`] = `
120+
Array [
121+
"Removed invalid \\"scripts\\"",
122+
]
123+
`
124+
125+
exports[`test/fix.js TAP with changes scriptpath no scripts > must match snapshot 1`] = `
126+
Array []
127+
`
128+
119129
exports[`test/fix.js TAP with changes scriptpath non-object script entry > must match snapshot 1`] = `
120130
Array [
121131
"Invalid scripts.\\"test\\" was removed",

tap-snapshots/test/normalize.js.test.cjs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,47 @@
88
exports[`test/normalize.js TAP @npmcli/package-json - with changes clean up bundleDependencies change name if bundleDependencies is not present > must match snapshot 1`] = `
99
Array [
1010
"Deleted incorrect \\"bundledDependencies\\"",
11-
"Removed invalid \\"scripts\\"",
1211
]
1312
`
1413

1514
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`] = `
1615
Array [
1716
"Deleted incorrect \\"bundledDependencies\\"",
18-
"Removed invalid \\"scripts\\"",
1917
]
2018
`
2119

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

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

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

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

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

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

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

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

9989
exports[`test/normalize.js TAP @npmcli/package-json - with changes cleanup bins remove non-string bin values > must match snapshot 1`] = `
10090
Array [
10191
"Deleted incorrect \\"bundledDependencies\\"",
102-
"Removed invalid \\"scripts\\"",
10392
"removed invalid \\"bin[y]\\"",
10493
"removed invalid \\"bin[z]\\"",
10594
]
@@ -108,7 +97,6 @@ Array [
10897
exports[`test/normalize.js TAP @npmcli/package-json - with changes convert funding string to object > must match snapshot 1`] = `
10998
Array [
11099
"Deleted incorrect \\"bundledDependencies\\"",
111-
"Removed invalid \\"scripts\\"",
112100
"\\"funding\\" was changed to an object with a url attribute",
113101
]
114102
`
@@ -117,7 +105,6 @@ exports[`test/normalize.js TAP @npmcli/package-json - with changes dedupe option
117105
Array [
118106
"Deleted incorrect \\"bundledDependencies\\"",
119107
"optionalDependencies.\\"whowins\\" was removed",
120-
"Removed invalid \\"scripts\\"",
121108
]
122109
`
123110

@@ -126,29 +113,25 @@ Array [
126113
"Deleted incorrect \\"bundledDependencies\\"",
127114
"optionalDependencies.\\"whowins\\" was removed",
128115
"Empty \\"optionalDependencies\\" was removed",
129-
"Removed invalid \\"scripts\\"",
130116
]
131117
`
132118

133119
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`] = `
134120
Array [
135121
"Deleted incorrect \\"bundledDependencies\\"",
136-
"Removed invalid \\"scripts\\"",
137122
]
138123
`
139124

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

147131
exports[`test/normalize.js TAP @npmcli/package-json - with changes set _id if name and version set > must match snapshot 1`] = `
148132
Array [
149133
"\\"_id\\" was set to [email protected]",
150134
"Deleted incorrect \\"bundledDependencies\\"",
151-
"Removed invalid \\"scripts\\"",
152135
]
153136
`
154137

@@ -161,6 +144,5 @@ Array [
161144
"\\"_lodash\\" was removed",
162145
"\\"_id\\" was set to [email protected]",
163146
"Deleted incorrect \\"bundledDependencies\\"",
164-
"Removed invalid \\"scripts\\"",
165147
]
166148
`

test/fix.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ for (const [name, testFix] of Object.entries(testMethods)) {
197197
const { content } = await testFix(t, testdir)
198198
t.strictSame(content.scripts, { test: 'test-script' })
199199
})
200+
t.test('no scripts', async t => {
201+
const testdir = {
202+
'package.json': pkg({ scripts: undefined }),
203+
}
204+
const { content } = await testFix(t, testdir)
205+
t.notHas(content, 'scripts')
206+
})
207+
t.test('falsy scripts', async t => {
208+
const testdir = {
209+
'package.json': pkg({ scripts: 0 }),
210+
}
211+
const { content } = await testFix(t, testdir)
212+
t.strictSame(content.scripts, undefined)
213+
})
200214
})
201215
t.test('bundleDependencies', async t => {
202216
t.test('null', async t => {

0 commit comments

Comments
 (0)