diff --git a/lib/normalize.js b/lib/normalize.js index e3b3798..e400dfd 100644 --- a/lib/normalize.js +++ b/lib/normalize.js @@ -251,7 +251,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase }) // strip "node_modules/.bin" from scripts entries // remove invalid scripts entries (non-strings) - if (steps.includes('scripts') || steps.includes('scriptpath')) { + if ((steps.includes('scripts') || steps.includes('scriptpath')) && data.scripts !== undefined) { const spre = /^(\.[/\\])?node_modules[/\\].bin[\\/]/ if (typeof data.scripts === 'object') { for (const name in data.scripts) { diff --git a/tap-snapshots/test/fix.js.test.cjs b/tap-snapshots/test/fix.js.test.cjs index d6cc1f3..fc9faa8 100644 --- a/tap-snapshots/test/fix.js.test.cjs +++ b/tap-snapshots/test/fix.js.test.cjs @@ -116,6 +116,16 @@ Array [ ] ` +exports[`test/fix.js TAP with changes scriptpath falsy scripts > must match snapshot 1`] = ` +Array [ + "Removed invalid \\"scripts\\"", +] +` + +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", diff --git a/tap-snapshots/test/normalize.js.test.cjs b/tap-snapshots/test/normalize.js.test.cjs index b42599e..b626516 100644 --- a/tap-snapshots/test/normalize.js.test.cjs +++ b/tap-snapshots/test/normalize.js.test.cjs @@ -8,14 +8,12 @@ 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\\"", ] ` @@ -23,7 +21,6 @@ exports[`test/normalize.js TAP @npmcli/package-json - with changes clean up bund Array [ "Deleted incorrect \\"bundledDependencies\\"", "\\"bundleDependencies\\" was changed from an object to an array", - "Removed invalid \\"scripts\\"", ] ` @@ -31,7 +28,6 @@ exports[`test/normalize.js TAP @npmcli/package-json - with changes clean up bund Array [ "Deleted incorrect \\"bundledDependencies\\"", "\\"bundleDependencies\\" was changed from \\"false\\" to \\"[]\\"", - "Removed invalid \\"scripts\\"", ] ` @@ -39,7 +35,6 @@ exports[`test/normalize.js TAP @npmcli/package-json - with changes clean up bund Array [ "Deleted incorrect \\"bundledDependencies\\"", "\\"bundleDependencies\\" was auto-populated from \\"dependencies\\"", - "Removed invalid \\"scripts\\"", ] ` @@ -47,7 +42,6 @@ exports[`test/normalize.js TAP @npmcli/package-json - with changes clean up bund Array [ "Deleted incorrect \\"bundledDependencies\\"", "\\"bundleDependencies\\" was auto-populated from \\"dependencies\\"", - "Removed invalid \\"scripts\\"", ] ` @@ -55,7 +49,6 @@ exports[`test/normalize.js TAP @npmcli/package-json - with changes clean up bund Array [ "Deleted incorrect \\"bundledDependencies\\"", "\\"bundleDependencies\\" was auto-populated from \\"dependencies\\"", - "Removed invalid \\"scripts\\"", ] ` @@ -77,14 +70,12 @@ 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", ] ` @@ -92,14 +83,12 @@ Array [ 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]\\"", ] @@ -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", ] ` @@ -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\\"", ] ` @@ -126,21 +113,18 @@ 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\\"", ] ` @@ -148,7 +132,6 @@ exports[`test/normalize.js TAP @npmcli/package-json - with changes set _id if na Array [ "\\"_id\\" was set to a@1.2.3", "Deleted incorrect \\"bundledDependencies\\"", - "Removed invalid \\"scripts\\"", ] ` @@ -161,6 +144,5 @@ Array [ "\\"_lodash\\" was removed", "\\"_id\\" was set to underscore@1.2.3", "Deleted incorrect \\"bundledDependencies\\"", - "Removed invalid \\"scripts\\"", ] ` diff --git a/test/fix.js b/test/fix.js index 6c611ed..ed80b97 100644 --- a/test/fix.js +++ b/test/fix.js @@ -197,6 +197,20 @@ 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('falsy scripts', async t => { + const testdir = { + 'package.json': pkg({ scripts: 0 }), + } + const { content } = await testFix(t, testdir) + t.strictSame(content.scripts, undefined) + }) }) t.test('bundleDependencies', async t => { t.test('null', async t => {