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 7839326 commit 5ca1ba2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 56 deletions.
85 changes: 44 additions & 41 deletions index.js → lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,49 @@
// - add GET CONTENTS of bundled deps, PACKAGE=true, depth + 1

const bundled = require('npm-bundled')
const {promisify} = require('util')
const { promisify } = require('util')
const fs = require('fs')
const readFile = promisify(fs.readFile)
const readdir = promisify(fs.readdir)
const stat = promisify(fs.stat)
const lstat = promisify(fs.lstat)
const {relative, resolve, basename, dirname} = require('path')
const { relative, resolve, basename, dirname } = require('path')
const normalizePackageBin = require('npm-normalize-package-bin')

const readPackage = ({ path, packageJsonCache }) =>
packageJsonCache.has(path) ? Promise.resolve(packageJsonCache.get(path))
: readFile(path).then(json => {
const pkg = normalizePackageBin(JSON.parse(json))
packageJsonCache.set(path, pkg)
return pkg
})
const pkg = normalizePackageBin(JSON.parse(json))
packageJsonCache.set(path, pkg)
return pkg
})
.catch(er => null)

// just normalize bundle deps and bin, that's all we care about here.
const normalized = Symbol('package data has been normalized')
const rpj = ({ path, packageJsonCache }) =>
readPackage({path, packageJsonCache})
.then(pkg => {
if (!pkg || pkg[normalized])
readPackage({ path, packageJsonCache })
.then(pkg => {
if (!pkg || pkg[normalized]) {
return pkg
}
if (pkg.bundledDependencies && !pkg.bundleDependencies) {
pkg.bundleDependencies = pkg.bundledDependencies
delete pkg.bundledDependencies
}
const bd = pkg.bundleDependencies
if (bd === true) {
pkg.bundleDependencies = [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.optionalDependencies || {}),
]
}
if (typeof bd === 'object' && !Array.isArray(bd)) {
pkg.bundleDependencies = Object.keys(bd)
}
pkg[normalized] = true
return pkg
if (pkg.bundledDependencies && !pkg.bundleDependencies) {
pkg.bundleDependencies = pkg.bundledDependencies
delete pkg.bundledDependencies
}
const bd = pkg.bundleDependencies
if (bd === true) {
pkg.bundleDependencies = [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.optionalDependencies || {}),
]
}
if (typeof bd === 'object' && !Array.isArray(bd)) {
pkg.bundleDependencies = Object.keys(bd)
}
pkg[normalized] = true
return pkg
})

})

const pkgContents = async ({
path,
Expand All @@ -69,19 +69,21 @@ const pkgContents = async ({
result = null,
packageJsonCache = null,
}) => {
if (!result)
if (!result) {
result = new Set()
}

if (!packageJsonCache)
if (!packageJsonCache) {
packageJsonCache = new Map()
}

if (pkg === true) {
return rpj({ path: path + '/package.json', packageJsonCache })
.then(pkg => pkgContents({
.then(p => pkgContents({
path,
depth,
currentDepth,
pkg,
pkg: p,
result,
packageJsonCache,
}))
Expand All @@ -91,7 +93,6 @@ const pkgContents = async ({
// add all bins to result if they exist
if (pkg.bin) {
const dir = dirname(path)
const base = basename(path)
const scope = basename(dir)
const nm = /^@.+/.test(scope) ? dirname(dir) : dir

Expand Down Expand Up @@ -121,8 +122,9 @@ const pkgContents = async ({
]).catch(() => [])

// not a thing, probably a missing folder
if (!dirEntries)
if (!dirEntries) {
return result
}

// empty folder, just add the folder itself to the result
if (!dirEntries.length && !bundleDeps && currentDepth !== 0) {
Expand All @@ -140,7 +142,7 @@ const pkgContents = async ({
await Promise.all(dirEntries.map(async (name, index) => {
const p = resolve(path, name)
const st = await lstat(p)
dirEntries[index] = Object.assign(st, {name})
dirEntries[index] = Object.assign(st, { name })
}))
}

Expand Down Expand Up @@ -184,20 +186,20 @@ const pkgContents = async ({
}))
}

if (recursePromises.length)
if (recursePromises.length) {
await Promise.all(recursePromises)
}

return result
}

module.exports = ({path, depth = 1, packageJsonCache}) => pkgContents({
module.exports = ({ path, depth = 1, packageJsonCache }) => pkgContents({
path: resolve(path),
depth,
pkg: true,
packageJsonCache,
}).then(results => [...results])


if (require.main === module) {
const options = { path: null, depth: 1 }
const usage = `Usage:
Expand All @@ -214,15 +216,16 @@ Options:
process.argv.slice(2).forEach(arg => {
let match
if ((match = arg.match(/^--depth=([0-9]+|Infinity)/)) ||
(match = arg.match(/^-d([0-9]+|Infinity)/)))
(match = arg.match(/^-d([0-9]+|Infinity)/))) {
options.depth = +match[1]
else if (arg === '-h' || arg === '--help') {
} else if (arg === '-h' || arg === '--help') {
console.log(usage)
process.exit(0)
} else
} else {
options.path = arg
}
})
if (!options.path) {
if (!options.path) {
console.error('ERROR: no path provided')
console.error(usage)
process.exit(1)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.7",
"description": "Get the list of files installed in a package in node_modules, including bundled dependencies",
"author": "GitHub Inc.",
"main": "index.js",
"main": "lib/index.js",
"bin": {
"installed-package-contents": "index.js"
},
Expand All @@ -28,6 +28,7 @@
"devDependencies": {
"@npmcli/eslint-config": "^3.1.0",
"@npmcli/template-oss": "3.6.0",
"mkdirp": "^1.0.4",
"require-inject": "^1.4.4",
"tap": "^14.11.0"
},
Expand Down
21 changes: 10 additions & 11 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const requireInject = require('require-inject')
const {promisify} = require('util')
const { promisify } = require('util')
const fs = require('fs')

const realReaddir = fs.readdir
let readdirFileTypes = true
const readdir = (path, ...args) => {
if (readdirFileTypes)
if (readdirFileTypes) {
return realReaddir(path, ...args)
else
} else {
return realReaddir(path, args.pop())
}
}

const fsMock = {
Expand All @@ -23,7 +24,7 @@ const fsMock = {
const getContents = requireInject('../', { fs: fsMock })
const t = require('tap')

const {resolve} = require('path')
const { resolve } = require('path')
t.formatSnapshot = a => Array.isArray(a) ? a.sort() : a
// the \ in the paths in the strings in tcompare's output are escaped
// so we have to swap out 2 \ chars with 1, then turn into / for snapshot
Expand Down Expand Up @@ -105,28 +106,26 @@ t.test('cache race condition coverage tests', t => {
t.plan(2)
const path = resolve(fixtures, 'bundle-all')

const fs = require('fs')
const normalizePackageBin = require('npm-normalize-package-bin')
const {promisify} = require('util')

t.test('full package json cache', t => {
// cache always claims to have the content, so we always get it from there
const cache = {}
const packageJsonCache = {
has: () => true,
get: path => {
const data = fs.readFileSync(path, 'utf8')
get: p => {
const data = fs.readFileSync(p, 'utf8')
return cache[data] || (cache[data] = normalizePackageBin(JSON.parse(data)))
}
},
}
return t.resolveMatchSnapshot(getContents({path, packageJsonCache}))
return t.resolveMatchSnapshot(getContents({ path, packageJsonCache }))
})

t.test('empty package json cache', t => {
const packageJsonCache = {
has: () => false,
set: () => {},
}
return t.resolveMatchSnapshot(getContents({path, packageJsonCache}))
return t.resolveMatchSnapshot(getContents({ path, packageJsonCache }))
})
})
5 changes: 2 additions & 3 deletions test/bin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {spawn} = require('child_process')
const {resolve} = require('path')
const { spawn } = require('child_process')
const { resolve } = require('path')
const t = require('tap')
// the \ in the paths in the strings in tcompare's output are escaped
// so we have to swap out 2 \ chars with 1, then turn into / for snapshot
Expand Down Expand Up @@ -43,7 +43,6 @@ const paths = [
'node_modules/optional-only',
]


const cases = [
['-h'],
[],
Expand Down

0 comments on commit 5ca1ba2

Please sign in to comment.