From 92f7aee6731a5bb5e1ebe7d5405850aa56b5e6b1 Mon Sep 17 00:00:00 2001 From: Milan Meva Date: Wed, 11 Dec 2024 11:08:26 -0500 Subject: [PATCH] fix: correct main entry for relative path --- lib/index.js | 3 +++ lib/normalize.js | 12 +++++++++++- tap-snapshots/test/normalize.js.test.cjs | 13 +++++++++++++ test/normalize.js | 23 +++++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 23f326d..a3f87cd 100644 --- a/lib/index.js +++ b/lib/index.js @@ -34,6 +34,7 @@ class PackageJson { 'scripts', 'funding', 'bin', + 'main', ]) // npm pkg fix @@ -47,6 +48,7 @@ class PackageJson { 'fixDependencies', 'devDependencies', 'scriptpath', + 'main', ]) static prepareSteps = Object.freeze([ @@ -66,6 +68,7 @@ class PackageJson { 'fillTypes', 'normalizeData', 'binRefs', + 'main', ]) // create a new empty package.json, so we can save at the given path even diff --git a/lib/normalize.js b/lib/normalize.js index 3adec01..56bfa4a 100644 --- a/lib/normalize.js +++ b/lib/normalize.js @@ -610,6 +610,16 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase }) } } } -} + if (steps.includes('main') && data.main) { + if (typeof data.main !== 'string') { + throw new TypeError('The "main" attribute must be of type string.') + } + const main = secureAndUnixifyPath(data.main) + if (main !== data.main) { + changes?.push(`"main" was normalized to "${main}"`) + data.main = main + } + } +} module.exports = normalize diff --git a/tap-snapshots/test/normalize.js.test.cjs b/tap-snapshots/test/normalize.js.test.cjs index b626516..5d8c8b0 100644 --- a/tap-snapshots/test/normalize.js.test.cjs +++ b/tap-snapshots/test/normalize.js.test.cjs @@ -122,6 +122,19 @@ Array [ ] ` +exports[`test/normalize.js TAP @npmcli/package-json - with changes main normalize main path - no changes > must match snapshot 1`] = ` +Array [ + "Deleted incorrect \\"bundledDependencies\\"", +] +` + +exports[`test/normalize.js TAP @npmcli/package-json - with changes main normalize main to correct path > must match snapshot 1`] = ` +Array [ + "Deleted incorrect \\"bundledDependencies\\"", + "\\"main\\" was normalized to \\"index.js\\"", +] +` + exports[`test/normalize.js TAP @npmcli/package-json - with changes normalize bin > must match snapshot 1`] = ` Array [ "Deleted incorrect \\"bundledDependencies\\"", diff --git a/test/normalize.js b/test/normalize.js index ea2aa3d..7d467c7 100644 --- a/test/normalize.js +++ b/test/normalize.js @@ -338,6 +338,29 @@ for (const [name, testNormalize] of Object.entries(testMethods)) { t.has(content, { bin: undefined }) }) + t.test('main', t => { + if (isLegacy) { + return t.skip('rpj does not have configurable steps for main') + } + t.test('normalize main to correct path', async t => { + const { content: { main } } = await testNormalize(t, ({ + 'package.json': JSON.stringify({ main: './index.js' }), + })) + t.strictSame(main, 'index.js') + }) + t.test('normalize main path - no changes', async t => { + const { content: { main } } = await testNormalize(t, ({ + 'package.json': JSON.stringify({ main: '.pkg/index.js' }), + })) + t.strictSame(main, '.pkg/index.js') + }) + t.test('normalize main path - not a string', async t => { + t.rejects(testNormalize(t, ({ + 'package.json': JSON.stringify({ main: ['.pkg/index.js'] }), + })), { name: 'TypeError' }) + }) + t.end() + }) t.test('skipping steps', async t => { if (isLegacy) { return t.skip('rpj does not have configurable steps')