Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
findIssues used to recurse, but no longer has to.
  • Loading branch information
kemitchell committed Aug 23, 2022
1 parent 30b2cab commit 3117239
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ function licensee (configuration, path, callback) {
var arborist = new Arborist({ path })
arborist.loadActual({ forceActual: true })
.then(function (tree) {
var children = Array.from(tree.children.values())
var dependencies = Array.from(tree.children.values())
if (configuration.filterPackages) {
children = configuration.filterPackages(children)
dependencies = configuration.filterPackages(dependencies)
}
callback(null, findIssues(configuration, children, []))
callback(null, findIssues(configuration, dependencies))
})
.catch(function (error) {
return callback(error)
Expand Down Expand Up @@ -73,33 +73,32 @@ function isString (argument) {
return typeof argument === 'string'
}

function findIssues (configuration, children, results) {
if (children) {
children.forEach(function (child) {
if (
configuration.productionOnly &&
child.dev
) return
var result = resultForPackage(configuration, child)
// Deduplicate.
var existing = results.find(function (existing) {
return (
existing.name === result.name &&
existing.version === result.version
)
})
if (existing) {
if (existing.duplicates) {
existing.duplicates.push(result)
} else {
existing.duplicates = [result]
}
function findIssues (configuration, dependencies) {
var results = []
dependencies.forEach(function (dependency) {
if (
configuration.productionOnly &&
dependency.dev
) return
var result = resultForPackage(configuration, dependency)
// Deduplicate.
var existing = results.find(function (existing) {
return (
existing.name === result.name &&
existing.version === result.version
)
})
if (existing) {
if (existing.duplicates) {
existing.duplicates.push(result)
} else {
results.push(result)
existing.duplicates = [result]
}
})
return results
} else return results
} else {
results.push(result)
}
})
return results
}

function resultForPackage (configuration, tree) {
Expand Down

0 comments on commit 3117239

Please # to comment.