Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 08eae47

Browse files
committed
feat: add binDir step to normalize function
This adds the `binDir` step to `.normalize()`. This will translate `directories.bin` into a `.bin` entry. `read-package-json-fast` technically had TWO apis. You could call it as exported, or you could call `.normalize`. The only difference between the two was that `.normalize` did not include the translation of `directories.bin` into `.bin`. We don't need another top level api for this package. Adding `binDir` to the `normalize` step is the right one. That's part of preparing a package for local consumption. I'm relatively certain we have bugs we've missed here because of this. This also means any bugs we fix in `binDir` will be fixed in all use cases. The existing `binDir` step (using glob) is also preferable to the code that `read-package-json-fast` was using (using manual recursive directory walking). Ultimately this will help us finish porting existing code that uses `read-package-json-fast` to this library.
1 parent 23c29a9 commit 08eae47

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class PackageJson {
3434
'scripts',
3535
'funding',
3636
'bin',
37+
'binDir',
3738
])
3839

3940
// npm pkg fix
@@ -112,7 +113,7 @@ class PackageJson {
112113
return p.prepare(opts)
113114
}
114115

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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ exports[`test/normalize.js TAP @npmcli/package-json - with changes dedupe option
103103
Array []
104104
`
105105

106+
exports[`test/normalize.js TAP @npmcli/package-json - with changes directories.bin > must match snapshot 1`] = `
107+
Array []
108+
`
109+
106110
exports[`test/normalize.js TAP @npmcli/package-json - with changes normalize bin > must match snapshot 1`] = `
107111
Array []
108112
`

test/normalize.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,19 @@ for (const [name, testNormalize] of Object.entries(testMethods)) {
167167
})
168168
})
169169

170+
t.test('directories.bin', async t => {
171+
const { content } = await testNormalize(t, ({
172+
'package.json': JSON.stringify({
173+
name: 'bin-test',
174+
directories: {
175+
bin: './bin',
176+
},
177+
}),
178+
bin: { echo: '#!/bin/sh\n\necho "hello world"' },
179+
}))
180+
t.strictSame(content.bin, { echo: 'bin/echo' })
181+
})
182+
170183
t.test('dedupe optional deps out of regular deps', async t => {
171184
t.test('choose optional deps in conflict, removing empty dependencies', async t => {
172185
const { content } = await testNormalize(t, ({

0 commit comments

Comments
 (0)