Skip to content

Commit bc0069d

Browse files
committed
fix: hidden dir path clean up corrected
1 parent fa6ed87 commit bc0069d

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

lib/normalize.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,9 @@ function unixifyPath (ref) {
127127
return ref.replace(/\\|:/g, '/')
128128
}
129129

130-
function securePath (ref) {
131-
const secured = path.join('.', path.join('/', unixifyPath(ref)))
132-
return secured.startsWith('.') ? '' : secured
133-
}
134-
135130
function secureAndUnixifyPath (ref) {
136-
return unixifyPath(securePath(ref))
131+
const secured = unixifyPath(path.join('.', path.join('/', unixifyPath(ref))))
132+
return secured.startsWith('./') ? '' : secured
137133
}
138134

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

377373
// expand "directories.bin"
378374
if (steps.includes('binDir') && data.directories?.bin && !data.bin) {
379-
const binsDir = path.resolve(pkg.path, securePath(data.directories.bin))
375+
const binsDir = path.resolve(pkg.path, secureAndUnixifyPath(data.directories.bin))
380376
const bins = await lazyLoadGlob()('**', { cwd: binsDir })
381377
data.bin = bins.reduce((acc, binFile) => {
382378
if (binFile && !binFile.startsWith('.')) {

test/prepare.js

+42
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,19 @@ for (const [name, testPrepare] of Object.entries(testMethods)) {
156156
t.strictSame(content.bin, { echo: 'bin/echo' })
157157
})
158158

159+
t.test('bin handles hidden folders', async t => {
160+
const { content } = await testPrepare(t, ({
161+
'package.json': JSON.stringify({
162+
name: 'bin-test',
163+
bin: {
164+
echo: '..\\..\\..\\.bin\\echo',
165+
},
166+
}),
167+
bin: { echo: '#!/bin/sh\n\necho "hello world"' },
168+
}))
169+
t.strictSame(content.bin, { echo: '.bin/echo' })
170+
})
171+
159172
t.test('directories.bin with bin', async t => {
160173
const { content } = await testPrepare(t, ({
161174
'package.json': JSON.stringify({
@@ -175,6 +188,25 @@ for (const [name, testPrepare] of Object.entries(testMethods)) {
175188
t.strictSame(content.bin, { echo: 'bin/echo' })
176189
})
177190

191+
t.test('directories.bin with hidden bin dir', async t => {
192+
const { content } = await testPrepare(t, ({
193+
'package.json': JSON.stringify({
194+
name: 'bin-test',
195+
directories: {
196+
bin: './.bin',
197+
},
198+
bin: {
199+
echo: './.bin/echo',
200+
},
201+
}),
202+
bin: {
203+
echo: '#!/bin/sh\n\necho "hello world"',
204+
echo2: '#!/bin/sh\n\necho "hello world2"',
205+
},
206+
}))
207+
t.strictSame(content.bin, { echo: '.bin/echo' })
208+
})
209+
178210
t.end()
179211
})
180212

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

224+
t.test('resolves hidden directory', async t => {
225+
const { content } = await testPrepare(t, ({
226+
'package.json': JSON.stringify({
227+
directories: { man: './.man' },
228+
}),
229+
'.man': { man1: { 'test.1': 'man test file' } },
230+
}))
231+
t.strictSame(content.man, ['.man/man1/test.1'])
232+
})
233+
192234
if (name === '@npmcli/package-json') {
193235
t.test('non-string', async t => {
194236
const { content } = await testPrepare(t, ({

0 commit comments

Comments
 (0)