Skip to content

Commit

Permalink
fix: inline bin normalization code
Browse files Browse the repository at this point in the history
Changes are also now properly reported
  • Loading branch information
wraithgar committed Jul 17, 2023
1 parent 4743d41 commit 3c1cb66
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 4 deletions.
62 changes: 59 additions & 3 deletions lib/normalize.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,70 @@
const semver = require('semver')
const fs = require('fs/promises')
const { glob } = require('glob')
const normalizePackageBin = require('npm-normalize-package-bin')
const legacyFixer = require('normalize-package-data/lib/fixer.js')
const legacyMakeWarning = require('normalize-package-data/lib/make_warning.js')
const path = require('path')
const log = require('proc-log')
const git = require('@npmcli/git')
const hostedGitInfo = require('hosted-git-info')

// used to be npm-normalize-package-bin
function normalizePackageBin (pkg, changes) {
if (pkg.bin) {
if (typeof pkg.bin === 'string' && pkg.name) {
changes?.push('"bin" was converted to an object')
pkg.bin = { [pkg.name]: pkg.bin }
} else if (Array.isArray(pkg.bin)) {
changes?.push('"bin" was converted to an object')
pkg.bin = pkg.bin.reduce((acc, k) => {
acc[path.basename(k)] = k
return acc
}, {})
}
if (typeof pkg.bin === 'object') {
for (const binKey in pkg.bin) {
if (typeof pkg.bin[binKey] !== 'string') {
delete pkg.bin[binKey]
changes?.push(`removed invalid "bin[${binKey}]"`)
continue
}
const base = path.join('/', path.basename(binKey.replace(/\\|:/g, '/'))).slice(1)
if (!base) {
delete pkg.bin[binKey]
changes?.push(`removed invalid "bin[${binKey}]"`)
continue
}

const binTarget = path.join('/', pkg.bin[binKey].replace(/\\/g, '/'))
.replace(/\\/g, '/').slice(1)

if (!binTarget) {
delete pkg.bin[binKey]
changes?.push(`removed invalid "bin[${binKey}]"`)
continue
}

if (base !== binKey) {
delete pkg.bin[binKey]
changes?.push(`"bin[${binKey}]" was renamed to "bin[${base}]"`)
}
if (binTarget !== pkg.bin[binKey]) {
changes?.push(`"bin[${base}]" script name was cleaned`)
}
pkg.bin[base] = binTarget
}

if (Object.keys(pkg.bin).length === 0) {
changes?.push('empty "bin" was removed')
delete pkg.bin
}

return pkg
}
}
delete pkg.bin
}

function isCorrectlyEncodedName (spec) {
return !spec.match(/[/@\s+%:]/) &&
spec === encodeURIComponent(spec)
Expand Down Expand Up @@ -257,7 +313,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
}

if (steps.includes('bin') || steps.includes('binDir') || steps.includes('binRefs')) {
normalizePackageBin(data)
normalizePackageBin(data, changes)
}

// expand "directories.bin"
Expand All @@ -272,7 +328,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
return acc
}, {})
// *sigh*
normalizePackageBin(data)
normalizePackageBin(data, changes)
}

// populate "gitHead" attribute
Expand Down
26 changes: 25 additions & 1 deletion tap-snapshots/test/fix.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,32 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/fix.js TAP with changes binRefs array > must match snapshot 1`] = `
Array [
"\\"bin\\" was converted to an object",
]
`

exports[`test/fix.js TAP with changes binRefs empty bin name > must match snapshot 1`] = `
Array [
"removed invalid \\"bin[/]\\"",
"empty \\"bin\\" was removed",
]
`

exports[`test/fix.js TAP with changes binRefs no bin target > must match snapshot 1`] = `
Array [
"removed invalid \\"bin[test-package]\\"",
"empty \\"bin\\" was removed",
]
`

exports[`test/fix.js TAP with changes binRefs scoped name > must match snapshot 1`] = `
Array []
Array [
"\\"bin\\" was converted to an object",
"\\"bin[@npmcli/test-package]\\" was renamed to \\"bin[test-package]\\"",
"\\"bin[test-package]\\" script name was cleaned",
]
`

exports[`test/fix.js TAP with changes bundleDependencies null > must match snapshot 1`] = `
Expand Down
3 changes: 3 additions & 0 deletions tap-snapshots/test/normalize.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ exports[`test/normalize.js TAP @npmcli/package-json - with changes cleanup bins
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"Removed invalid \\"scripts\\"",
"\\"bin\\" was converted to an object",
]
`

Expand All @@ -99,6 +100,8 @@ exports[`test/normalize.js TAP @npmcli/package-json - with changes cleanup bins
Array [
"Deleted incorrect \\"bundledDependencies\\"",
"Removed invalid \\"scripts\\"",
"removed invalid \\"bin[y]\\"",
"removed invalid \\"bin[z]\\"",
]
`

Expand Down
21 changes: 21 additions & 0 deletions test/fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,27 @@ for (const [name, testFix] of Object.entries(testMethods)) {
const { content } = await testFix(t, testdir)
t.strictSame(content.bin, { 'test-package': '@npmcil/test-package' })
})
t.test('array', async t => {
const testdir = {
'package.json': pkg({ bin: ['@npmcil/test-package'] }),
}
const { content } = await testFix(t, testdir)
t.strictSame(content.bin, { 'test-package': '@npmcil/test-package' })
})
t.test('no bin target', async t => {
const testdir = {
'package.json': pkg({ bin: { 'test-package': '/' } }),
}
const { content } = await testFix(t, testdir)
t.notHas(content, 'bin')
})
t.test('empty bin name', async t => {
const testdir = {
'package.json': pkg({ bin: { '/': 'test-slash' } }),
}
const { content } = await testFix(t, testdir)
t.notHas(content, 'bin')
})
})
t.test('fixDependencies', async t => {
t.test('string dependencies', async t => {
Expand Down

0 comments on commit 3c1cb66

Please sign in to comment.