Skip to content
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

fix: hidden dir path clean up corrected #119

Merged
merged 1 commit into from
Sep 17, 2024
Merged
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
10 changes: 3 additions & 7 deletions lib/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,9 @@ function unixifyPath (ref) {
return ref.replace(/\\|:/g, '/')
}

function securePath (ref) {
const secured = path.join('.', path.join('/', unixifyPath(ref)))
return secured.startsWith('.') ? '' : secured
}

function secureAndUnixifyPath (ref) {
return unixifyPath(securePath(ref))
const secured = unixifyPath(path.join('.', path.join('/', unixifyPath(ref))))
return secured.startsWith('./') ? '' : secured
}

// We don't want the `changes` array in here by default because this is a hot
Expand Down Expand Up @@ -376,7 +372,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })

// expand "directories.bin"
if (steps.includes('binDir') && data.directories?.bin && !data.bin) {
const binsDir = path.resolve(pkg.path, securePath(data.directories.bin))
const binsDir = path.resolve(pkg.path, secureAndUnixifyPath(data.directories.bin))
const bins = await lazyLoadGlob()('**', { cwd: binsDir })
data.bin = bins.reduce((acc, binFile) => {
if (binFile && !binFile.startsWith('.')) {
Expand Down
42 changes: 42 additions & 0 deletions test/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ for (const [name, testPrepare] of Object.entries(testMethods)) {
t.strictSame(content.bin, { echo: 'bin/echo' })
})

t.test('bin handles hidden folders', async t => {
const { content } = await testPrepare(t, ({
'package.json': JSON.stringify({
name: 'bin-test',
bin: {
echo: '..\\..\\..\\.bin\\echo',
},
}),
bin: { echo: '#!/bin/sh\n\necho "hello world"' },
}))
t.strictSame(content.bin, { echo: '.bin/echo' })
})

t.test('directories.bin with bin', async t => {
const { content } = await testPrepare(t, ({
'package.json': JSON.stringify({
Expand All @@ -175,6 +188,25 @@ for (const [name, testPrepare] of Object.entries(testMethods)) {
t.strictSame(content.bin, { echo: 'bin/echo' })
})

t.test('directories.bin with hidden bin dir', async t => {
const { content } = await testPrepare(t, ({
'package.json': JSON.stringify({
name: 'bin-test',
directories: {
bin: './.bin',
},
bin: {
echo: './.bin/echo',
},
}),
bin: {
echo: '#!/bin/sh\n\necho "hello world"',
echo2: '#!/bin/sh\n\necho "hello world2"',
},
}))
t.strictSame(content.bin, { echo: '.bin/echo' })
})

t.end()
})

Expand All @@ -189,6 +221,16 @@ for (const [name, testPrepare] of Object.entries(testMethods)) {
t.strictSame(content.man, ['man/man1/test.1'])
})

t.test('resolves hidden directory', async t => {
const { content } = await testPrepare(t, ({
'package.json': JSON.stringify({
directories: { man: './.man' },
}),
'.man': { man1: { 'test.1': 'man test file' } },
}))
t.strictSame(content.man, ['.man/man1/test.1'])
})

if (name === '@npmcli/package-json') {
t.test('non-string', async t => {
const { content } = await testPrepare(t, ({
Expand Down