-
Notifications
You must be signed in to change notification settings - Fork 6
/
lib.js
41 lines (37 loc) · 1.63 KB
/
lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const promisify = require('util').promisify
const npmLs = require('./helpers/npm-list')
const getPackageDetails = require('./helpers/get-package-details')
const getExpandedLicName = require('./helpers/get-spdx-full-name')
const extractLicenseText = require('./helpers/extract-license-id')
const glob = promisify(require('glob'))
/**
* Get a list of licenses for any installed project dependencies
* @param {Object} options
* @returns {Promise<[]>}
*/
module.exports = async function (options = {}) {
const pathList = await npmLs(options)
return await Promise.all(pathList.map(async (path, index) => {
const pkg = await getPackageDetails(path)
const licShortName = extractLicenseText(pkg.license || pkg.licenses || pkg.licence || pkg.licences)
const licLongName = getExpandedLicName(licShortName) || 'unknown'
// find any local licences files and build a path to them
const licFilePath = await glob('+(license**|licence**)', {cwd: path, nocase: true, nodir: true})
.then(files => files.map(file => `${path}/${file}`))
return {
id: index,
name: pkg.name,
version: pkg.version,
licenseId: licShortName,
licenseFullName: licLongName,
licenseFilePath: licFilePath || [],
license: `${licLongName} (${licShortName || '?'})`,
repository: (pkg.repository || {}).url,
author: (pkg.author || {}).name,
homepage: pkg.homepage,
path,
dependencyLevel: pkg._development ? 'development' : 'production',
description: pkg.description
}
}))
}