Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add normalizePackageMan helper #100

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions lib/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,36 @@ function normalizePackageBin (pkg, changes) {
delete pkg.bin
}

function normalizePackageMan (pkg, changes) {
if (pkg.man) {
const mans = []
for (const man of (Array.isArray(pkg.man) ? pkg.man : [pkg.man])) {
if (typeof man !== 'string') {
changes?.push(`removed invalid "man [${man}]"`)
} else {
mans.push(unixifyPath(securePath(unixifyPath(man))))
}
}

if (!mans.length) {
changes?.push('empty "man" was removed')
} else {
pkg.man = mans
return pkg
wraithgar marked this conversation as resolved.
Show resolved Hide resolved
}
}
delete pkg.man
}

function isCorrectlyEncodedName (spec) {
return !spec.match(/[/@\s+%:]/) &&
spec === encodeURIComponent(spec)
}

function unixifyPath (ref) {
return ref.replace(/\\|:/g, '/')
}

function isValidScopedPackageName (spec) {
if (spec.charAt(0) !== '@') {
return false
Expand Down Expand Up @@ -329,13 +354,16 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
}

// expand directories.man
if (steps.includes('mans') && !data.man && data.directories?.man) {
const manDir = data.directories.man
const cwd = path.resolve(pkg.path, securePath(manDir))
const files = await lazyLoadGlob()('**/*.[0-9]', { cwd })
data.man = files.map(man =>
path.relative(pkg.path, path.join(cwd, man)).split(path.sep).join('/')
)
if (steps.includes('mans')) {
if (data.directories?.man && !data.man) {
const manDir = unixifyPath(securePath(unixifyPath(data.directories.man)))
const cwd = path.resolve(pkg.path, manDir)
const files = await lazyLoadGlob()('**/*.[0-9]', { cwd })
data.man = files.map(man =>
path.relative(pkg.path, path.join(cwd, man)).split(path.sep).join('/')
)
}
normalizePackageMan(data, changes)
}

if (steps.includes('bin') || steps.includes('binDir') || steps.includes('binRefs')) {
Expand Down
112 changes: 99 additions & 13 deletions test/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,105 @@ for (const [name, testPrepare] of Object.entries(testMethods)) {
t.end()
})

t.test('man', t => {
t.test('resolves 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, ({
'package.json': JSON.stringify({
man: 123,
}),
}))
t.has(content, { man: undefined })
})

t.test('good', async t => {
const { content } = await testPrepare(t, ({
'package.json': JSON.stringify({
man: './man/test.1',
}),
}))
t.strictSame(content.man, ['man/test.1'])
})

t.test('empty', async t => {
const { content } = await testPrepare(t, ({
'package.json': JSON.stringify({
man: [],
}),
}))
t.has(content, { man: undefined })
})

t.test('directories.man no prefix', async t => {
const { content } = await testPrepare(t, ({
'package.json': JSON.stringify({
name: 'man-test',
directories: {
man: './man',
},
}),
man: { 'test.1': '.TH man "test man page"' },
}))
t.strictSame(content.man, ['man/test.1'])
})

t.test('directories.man trim prefix', async t => {
const { content } = await testPrepare(t, ({
'package.json': JSON.stringify({
name: 'man-test',
directories: {
man: '../../../../../man',
},
}),
man: { 'test.1': '.TH man "test man page"' },
}))
t.strictSame(content.man, ['man/test.1'])
})

t.test('directories.man handles reversed slashes', async t => {
const { content } = await testPrepare(t, ({
'package.json': JSON.stringify({
name: 'man-test',
directories: {
man: '..\\..\\man',
},
}),
man: { 'test.1': '.TH man "test man page"' },
}))
t.strictSame(content.man, ['man/test.1'])
})

t.test('directories.man with man', async t => {
const { content } = await testPrepare(t, ({
'package.json': JSON.stringify({
name: 'man-test',
directories: {
man: './man',
},
man: '../../test.2',
}),
man: {
'test.1': '.TH man "test man page 1"',
'test.2': '.TH man "test man page 2"',
},
}))
t.strictSame(content.man, ['test.2'])
})
}

t.end()
})

t.test('bundleDependencies', t => {
t.test('true', async t => {
const { content } = await testPrepare(t, ({
Expand Down Expand Up @@ -325,19 +424,6 @@ for (const [name, testPrepare] of Object.entries(testMethods)) {
t.end()
})

t.test('man', t => {
t.test('resolves 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'])
})
t.end()
})

t.test('gitHead', t => {
t.test('HEAD with no ref', async t => {
const { content } = await testPrepare(t, ({
Expand Down
Loading