Skip to content

Commit

Permalink
fix: linting
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Sep 20, 2022
1 parent 4ceacc3 commit 307fc2c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 42 deletions.
51 changes: 31 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {promisify} = require('util')
const { promisify } = require('util')
const fs = require('fs')
const readFile = promisify(fs.readFile)
const lstat = promisify(fs.lstat)
Expand All @@ -18,12 +18,14 @@ const normalizePackageBin = require('npm-normalize-package-bin')

// load the directories.bin folder as a 'bin' object
const readBinDir = async (path, data) => {
if (data.bin)
if (data.bin) {
return data
}

const m = data.directories && data.directories.bin
if (!m || typeof m !== 'string')
if (!m || typeof m !== 'string') {
return data
}

// cut off any monkey business, like setting directories.bin
// to ../../../etc/passwd or /etc/passwd or something like that.
Expand All @@ -36,30 +38,33 @@ const readBinDir = async (path, data) => {
const walkBinDir = async (root, dir, obj) => {
const entries = await readdir(resolve(root, dir)).catch(() => [])
for (const entry of entries) {
if (entry.charAt(0) === '.')
if (entry.charAt(0) === '.') {
continue
}
const f = resolve(root, dir, entry)
// ignore stat errors, weird file types, symlinks, etc.
const st = await lstat(f).catch(() => null)
if (!st)
if (!st) {
continue
else if (st.isFile())
} else if (st.isFile()) {
obj[entry] = relative(root, f)
else if (st.isDirectory())
} else if (st.isDirectory()) {
await walkBinDir(root, join(dir, entry), obj)
}
}
return obj
}

// do not preserve _fields set in files, they are sus
const stripUnderscores = data => {
for (const key of Object.keys(data).filter(k => /^_/.test(k)))
for (const key of Object.keys(data).filter(k => /^_/.test(k))) {
delete data[key]
}
return data
}

const normalize = data => {
add_id(data)
addId(data)
fixBundled(data)
pruneRepeatedOptionals(data)
fixScripts(data)
Expand All @@ -70,9 +75,10 @@ const normalize = data => {

rpj.normalize = normalize

const add_id = data => {
if (data.name && data.version)
const addId = data => {
if (data.name && data.version) {
data._id = `${data.name}@${data.version}`
}
return data
}

Expand All @@ -88,8 +94,9 @@ const pruneRepeatedOptionals = data => {
delete dd[name]
}
}
if (Object.keys(dd).length === 0)
if (Object.keys(dd).length === 0) {
delete data.dependencies
}
return data
}

Expand All @@ -98,17 +105,19 @@ const fixBundled = data => {
const bd = data.bundleDependencies === undefined ? bdd
: data.bundleDependencies

if (bd === false)
if (bd === false) {
data.bundleDependencies = []
else if (bd === true)
} else if (bd === true) {
data.bundleDependencies = Object.keys(data.dependencies || {})
else if (bd && typeof bd === 'object') {
if (!Array.isArray(bd))
} else if (bd && typeof bd === 'object') {
if (!Array.isArray(bd)) {
data.bundleDependencies = Object.keys(bd)
else
} else {
data.bundleDependencies = bd
} else
}
} else {
delete data.bundleDependencies
}

delete data.bundledDependencies
return data
Expand All @@ -121,15 +130,17 @@ const fixScripts = data => {
}

for (const [name, script] of Object.entries(data.scripts)) {
if (typeof script !== 'string')
if (typeof script !== 'string') {
delete data.scripts[name]
}
}
return data
}

const fixFunding = data => {
if (data.funding && typeof data.funding === 'string')
if (data.funding && typeof data.funding === 'string') {
data.funding = { url: data.funding }
}
return data
}

Expand Down
45 changes: 23 additions & 22 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ t.test('clean up bundleddddddDependencies', async t => {
}),
}) + '/package.json'), { bundleDependencies: [] }))


t.test('handle bundleDependencies: false', t =>
t.resolveMatch(rpj(t.testdir({
'package.json': JSON.stringify({
Expand Down Expand Up @@ -77,29 +76,29 @@ t.test('clean up scripts', async t => {
'package.json': JSON.stringify({
scripts: {
foo: 'bar',
bar: [ 'baz' ],
bar: ['baz'],
baz: { bar: { foo: 'barbaz' } },
},
})
}),
}) + '/package.json'), {
scripts: {
foo: 'bar',
bar: undefined,
baz: undefined,
}
},
}))
})

t.test('convert funding string to object', t =>
t.resolveMatch(rpj(t.testdir({
'package.json': JSON.stringify({ funding: 'hello' })
'package.json': JSON.stringify({ funding: 'hello' }),
}) + '/package.json'), { funding: { url: 'hello' } }))

t.test('cleanup bins', async t => {
t.test('handle string when a name is set', t =>
t.resolveMatch(rpj(t.testdir({
'package.json': JSON.stringify({ name: 'x', bin: 'y' }),
}) + '/package.json'), { bin: { x: 'y' }}))
}) + '/package.json'), { bin: { x: 'y' } }))

t.test('delete string bin when no name', t =>
t.resolveMatch(rpj(t.testdir({
Expand All @@ -117,49 +116,49 @@ t.test('cleanup bins', async t => {
x: 'y',
y: 1234,
z: { a: 'b' },
}}),
}) + '/package.json'), { bin: {x:'y', y: undefined, z: undefined }}))
} }),
}) + '/package.json'), { bin: { x: 'y', y: undefined, z: undefined } }))
})

t.test('dedupe optional deps out of regular deps', async t => {
t.test('choose optional deps in conflict', t =>
t.resolveMatch(rpj(t.testdir({
'package.json': JSON.stringify({
optionalDependencies: {
whowins: '1.2.3-optional'
whowins: '1.2.3-optional',
},
dependencies: {
whowins: '1.2.3-prod'
}
whowins: '1.2.3-prod',
},
}),
}) + '/package.json'), {
optionalDependencies: {
whowins: '1.2.3-optional'
whowins: '1.2.3-optional',
},
}))

t.test('do not create regular deps if only optional specified', t =>
t.resolveMatch(rpj(t.testdir({
'package.json': JSON.stringify({
optionalDependencies: {
whowins: '1.2.3-optional'
whowins: '1.2.3-optional',
},
}),
}) + '/package.json'), {
optionalDependencies: {
whowins: '1.2.3-optional'
whowins: '1.2.3-optional',
},
}))
})

t.test('set _id if name and version set', t =>
t.resolveMatch(rpj(t.testdir({
'package.json': JSON.stringify({name:'a', version: '1.2.3'}),
'package.json': JSON.stringify({ name: 'a', version: '1.2.3' }),
}) + '/package.json'), { _id: 'a@1.2.3' }))

t.test('exports the normalize function', async t =>
t.same(rpj.normalize({ bundledDependencies: true, dependencies: {a:'1'}}),
{ bundleDependencies: ['a'], dependencies: {a:'1'}}))
t.same(rpj.normalize({ bundledDependencies: true, dependencies: { a: '1' } }),
{ bundleDependencies: ['a'], dependencies: { a: '1' } }))

t.test('preserve indentation', async t => {
const obj = {
Expand Down Expand Up @@ -278,20 +277,22 @@ t.test('strip _fields', async t => {
t.test('load directories.bin', async t => {
const { basename } = require('path')
const fs = require('fs')
const rpj = t.mock('../', {
const rpjMock = t.mock('../', {
fs: {
...fs,
lstat: (p, cb) => {
if (basename(p) === 'staterror')
if (basename(p) === 'staterror') {
cb(new Error('stat error'))
else
} else {
return fs.lstat(p, cb)
}
},
readdir: (p, cb) => {
if (basename(p) === 'readdirerror') {
cb(new Error('readdir error'))
} else
} else {
return fs.readdir(p, cb)
}
},
},
})
Expand Down Expand Up @@ -323,7 +324,7 @@ t.test('load directories.bin', async t => {
},
},
})
t.strictSame(await rpj(`${path}/package.json`), {
t.strictSame(await rpjMock(`${path}/package.json`), {
name: 'foo',
version: '1.2.3',
_id: 'foo@1.2.3',
Expand Down

0 comments on commit 307fc2c

Please sign in to comment.