Skip to content

fix: correct main entry for relative path #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class PackageJson {
'scripts',
'funding',
'bin',
'main',
])

// npm pkg fix
Expand All @@ -47,6 +48,7 @@ class PackageJson {
'fixDependencies',
'devDependencies',
'scriptpath',
'main',
])

static prepareSteps = Object.freeze([
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion lib/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions tap-snapshots/test/normalize.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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\\"",
Expand Down
23 changes: 23 additions & 0 deletions test/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading