Skip to content

Commit

Permalink
fix: hidden dir path clean up corrected
Browse files Browse the repository at this point in the history
  • Loading branch information
milaninfy committed Sep 6, 2024
1 parent fa6ed87 commit bc0069d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
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

0 comments on commit bc0069d

Please sign in to comment.