Skip to content

Commit a8b1cc9

Browse files
committed
fix: secure and unixify paths discovered via directories.bin
Also tweaked logging statement to help w/ debugging
1 parent 08eae47 commit a8b1cc9

4 files changed

Lines changed: 13 additions & 16 deletions

File tree

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class PackageJson {
113113
return p.prepare(opts)
114114
}
115115

116-
// read-package-json-fast.normalize compatible behavior (distinct from just read-package-json-fast
116+
// read-package-json-fast compatible behavior
117117
static async normalize (path, opts) {
118118
const p = new PackageJson()
119119
await p.load(path)

lib/normalize.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function normalizePackageBin (pkg, changes) {
6767
changes?.push(`"bin[${binKey}]" was renamed to "bin[${base}]"`)
6868
}
6969
if (binTarget !== pkg.bin[binKey]) {
70-
changes?.push(`"bin[${base}]" script name was cleaned`)
70+
changes?.push(`"bin[${base}]" script name ${binTarget} was invalid and removed`)
7171
}
7272
pkg.bin[base] = binTarget
7373
}
@@ -373,22 +373,19 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
373373
normalizePackageMan(data, changes)
374374
}
375375

376-
if (steps.includes('bin') || steps.includes('binDir') || steps.includes('binRefs')) {
377-
normalizePackageBin(data, changes)
378-
}
379-
380376
// expand "directories.bin"
381377
if (steps.includes('binDir') && data.directories?.bin && !data.bin) {
382-
const binsDir = path.resolve(pkg.path, secureAndUnixifyPath(data.directories.bin))
383-
const bins = await lazyLoadGlob()('**', { cwd: binsDir })
378+
const binPath = secureAndUnixifyPath(data.directories.bin)
379+
const bins = await lazyLoadGlob()('**', { cwd: path.resolve(pkg.path, binPath) })
384380
data.bin = bins.reduce((acc, binFile) => {
385381
if (binFile && !binFile.startsWith('.')) {
386382
const binName = path.basename(binFile)
387-
acc[binName] = path.join(data.directories.bin, binFile)
383+
// binPath is already cleaned and unixified, no need to path.join here.
384+
acc[binName] = `${binPath}/${secureAndUnixifyPath(binFile)}`
388385
}
389386
return acc
390387
}, {})
391-
// *sigh*
388+
} else if (steps.includes('bin') || steps.includes('binDir') || steps.includes('binRefs')) {
392389
normalizePackageBin(data, changes)
393390
}
394391

tap-snapshots/test/fix.js.test.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ exports[`test/fix.js TAP with changes binRefs scoped name > must match snapshot
2929
Array [
3030
"\\"bin\\" was converted to an object",
3131
"\\"bin[@npmcli/test-package]\\" was renamed to \\"bin[test-package]\\"",
32-
"\\"bin[test-package]\\" script name was cleaned",
32+
"\\"bin[test-package]\\" script name @npmcli/test-package was invalid and removed",
3333
]
3434
`
3535

test/fix.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ for (const [name, testFix] of Object.entries(testMethods)) {
217217
t.test('scriptpath', async t => {
218218
t.test('non-object scripts', async t => {
219219
const testdir = {
220-
'package.json': pkg({ scripts: '@npmcil/test-package' }),
220+
'package.json': pkg({ scripts: '@npmcli/test-package' }),
221221
}
222222
const { content } = await testFix(t, testdir)
223223
t.notHas(content, 'scripts')
@@ -263,17 +263,17 @@ for (const [name, testFix] of Object.entries(testMethods)) {
263263
t.test('binRefs', async t => {
264264
t.test('scoped name', async t => {
265265
const testdir = {
266-
'package.json': pkg({ bin: '@npmcil/test-package' }),
266+
'package.json': pkg({ bin: '@npmcli/test-package' }),
267267
}
268268
const { content } = await testFix(t, testdir)
269-
t.strictSame(content.bin, { 'test-package': '@npmcil/test-package' })
269+
t.strictSame(content.bin, { 'test-package': '@npmcli/test-package' })
270270
})
271271
t.test('array', async t => {
272272
const testdir = {
273-
'package.json': pkg({ bin: ['@npmcil/test-package'] }),
273+
'package.json': pkg({ bin: ['@npmcli/test-package'] }),
274274
}
275275
const { content } = await testFix(t, testdir)
276-
t.strictSame(content.bin, { 'test-package': '@npmcil/test-package' })
276+
t.strictSame(content.bin, { 'test-package': '@npmcli/test-package' })
277277
})
278278
t.test('no bin target', async t => {
279279
const testdir = {

0 commit comments

Comments
 (0)