Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kemitchell committed Jun 2, 2022
1 parent 7b1027c commit 7071128
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 23 deletions.
36 changes: 13 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,17 @@ function licensee (configuration, path, callback) {
) {
callback(new Error('No licenses or packages allowed.'))
} else {
readFilesystemTree(function (error, children) {
if (error) callback(error)
else {
if (configuration.filterPackages) {
children = configuration.filterPackages(children)
}
callback(null, findIssues(
configuration, children, []
))
}
})
}

function readFilesystemTree (done) {
var arborist = new Arborist({ path })
arborist.loadActual()
.catch(function (error) {
return done(error)
return callback(error)
})
.then(function (tree) {
return done(null, tree.children)
var children = Array.from(tree.children.values())
if (configuration.filterPackages) {
children = configuration.filterPackages(children)
}
callback(null, findIssues(configuration, children, []))
})
}
}
Expand Down Expand Up @@ -85,7 +75,7 @@ function isString (argument) {

function findIssues (configuration, children, results) {
if (children) {
Array.from(children.values()).forEach(function (child) {
children.forEach(function (child) {
if (
!configuration.productionOnly ||
!child.dev
Expand All @@ -107,10 +97,6 @@ function findIssues (configuration, children, results) {
} else {
results.push(result)
}
findIssues(configuration, child, results)
}
if (child.children) {
findIssues(configuration, child.children, results)
}
})
return results
Expand All @@ -119,10 +105,14 @@ function findIssues (configuration, children, results) {

function resultForPackage (configuration, tree) {
var packageAllowlist = configuration.packages || {}
// TODO: fetch license with path
var result = {
name: tree.name,
version: tree.version,
license: tree.package.license,
author: tree.package.author,
contributors: tree.package.contributors,
repository: tree.package.repository,
homepage: tree.package.homepage,
version: tree.package.version,
parent: tree.parent,
path: tree.realpath
}
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"licensee"
],
"devDependencies": {
"run-parallel": "^1.2.0",
"spawn-sync": "^2.0.0",
"standard": "^14.3.1",
"tap": "^14.5.0"
Expand Down
91 changes: 91 additions & 0 deletions tests/unit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
var fs = require('fs')
var runParallel = require('run-parallel')
var mod = require('../')
var os = require('os')
var path = require('path')
var tap = require('tap')

tap.test('unit', function (test) {
fs.mkdtemp(path.join(os.tmpdir(), 'licensee-test-'), function (error, directory) {
test.ifError(error, 'mkdtemp')
var nodeModulesPath = path.join(directory, 'node_modules')
fs.mkdir(nodeModulesPath, function (error) {
test.ifError(error, 'mkdir node_modules')
runParallel([
function (done) {
fs.writeFile(
path.join(directory, 'package.json'),
JSON.stringify({
package: 'licensee-test',
version: '1.0.0',
private: true,
dependencies: { a: '1.0.0', b: '1.0.0' }
}, null, 2),
done
)
},
function (done) {
writeFakePackage(nodeModulesPath, 'a', 'MIT', done)
},
function (done) {
writeFakePackage(nodeModulesPath, 'b', 'GPL-2.0-only', done)
}
], function (error) {
test.ifError(error, 'write fake deps')
var configuration = {
licenses: { blueOak: 'bronze' }
}
mod(configuration, directory, function (error, results) {
test.ifError(error, 'no invocation error')
test.assert(
results.some(function (result) {
return result.name === 'a' && result.approved === true
}),
'approves MIT'
)
test.assert(
results.some(function (result) {
return result.name === 'b' && result.approved === false
}),
'rejects GPL-2.0-only'
)
fs.rmdir(directory, { recursive: true }, function (error) {
test.ifError(error, 'rm -rf test dir')
test.end()
})
})
})
})
})
})

function writeFakePackage (nodeModulesPath, name, license, callback) {
var packagePath = path.join(nodeModulesPath, name)
fs.mkdir(packagePath, function (error) {
if (error) return callback(error)
runParallel([
function (done) {
fs.writeFile(
path.join(packagePath, 'index.js'),
`module.exports = function () { console.log("${name}") }`,
done
)
},
function (done) {
fs.writeFile(
path.join(packagePath, 'package.json'),
JSON.stringify({
name,
version: '1.0.0',
author: `Author of ${name}`,
contributors: [`Contributor to ${name}`],
repository: `http://example.com/${name}/repo`,
homepage: `http://example.com/${name}`,
license
}, null, 2),
done
)
}
], callback)
})
}

0 comments on commit 7071128

Please sign in to comment.