|
| 1 | +const licenseChecker = require('license-checker'); |
| 2 | +const spdxSatisfies = require('spdx-satisfies'); |
| 3 | +const denodeify = require('denodeify'); |
| 4 | +const licenseCheckerInit = denodeify(licenseChecker.init); |
| 5 | + |
| 6 | + |
| 7 | +// SPDX defined licenses, see https://spdx.org/licenses/. |
| 8 | +const acceptedSpdxLicenses = [ |
| 9 | + 'MIT', |
| 10 | + 'ISC', |
| 11 | + 'Apache-2.0', |
| 12 | + 'BSD-2-Clause', |
| 13 | + 'BSD-3-Clause', |
| 14 | + 'BSD-4-Clause', |
| 15 | + 'CC-BY-3.0', |
| 16 | + 'CC-BY-4.0', |
| 17 | + 'Beerware', |
| 18 | + 'Unlicense' |
| 19 | +]; |
| 20 | + |
| 21 | +// Name variations of SPDX licenses that some packages have. |
| 22 | +const ignoredLicenseVariations = [ |
| 23 | + 'MIT*', |
| 24 | + 'MIT/X11', |
| 25 | + 'AFLv2.1', |
| 26 | + 'AFLv3.0', |
| 27 | + 'Apache-2.0', |
| 28 | + 'Apache2', |
| 29 | + 'BSD', |
| 30 | + 'BSD*', |
| 31 | + 'BSD-like', |
| 32 | + 'NPL', |
| 33 | + 'L/GPL', |
| 34 | + 'Public Domain' |
| 35 | +]; |
| 36 | + |
| 37 | +// Specific packages to ignore, add a reason in a comment. Format: package-name@version. |
| 38 | +const ignoredPackages = [ |
| 39 | + '[email protected]', // old license format, lists `AFLv2.1, BSD` |
| 40 | + '[email protected]', // old license format, lists `MIT, Apache2` |
| 41 | + '[email protected]' // `Apache License, Version 2.0`, but licence-checker can't handle commas |
| 42 | +]; |
| 43 | + |
| 44 | +function testSpdx(licenses) { |
| 45 | + try { |
| 46 | + return spdxSatisfies(licenses, `(${acceptedSpdxLicenses.join(' OR ')})`) |
| 47 | + } catch (_) { |
| 48 | + return false; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// Use license-checker first, it can filter more licenses. |
| 53 | +licenseCheckerInit({ |
| 54 | + start: './', |
| 55 | + exclude: acceptedSpdxLicenses.concat(ignoredLicenseVariations).join(), |
| 56 | + customFormat: { name: '', _location: '' } |
| 57 | +}) |
| 58 | + .then(json => { |
| 59 | + let badPackages = Object.keys(json) |
| 60 | + .map(key => Object.assign({}, json[key], { id: key })) |
| 61 | + // Then run the remaining ones through spdx-satisfies. |
| 62 | + .filter(pkg => !testSpdx(pkg.licenses)) |
| 63 | + .filter(pkg => !ignoredPackages.find(ignored => ignored === pkg.id)); |
| 64 | + |
| 65 | + if (badPackages.length > 0) { |
| 66 | + console.log('Invalid package licences found:\n'); |
| 67 | + badPackages.forEach(pkg => console.log(`- ${pkg.id} (${pkg._location}): ${pkg.licenses}`)); |
| 68 | + process.exit(1); |
| 69 | + } else { |
| 70 | + console.log('All package licenses are valid.'); |
| 71 | + } |
| 72 | + }) |
| 73 | + .catch(err => { |
| 74 | + console.log(err); |
| 75 | + process.exit(1); |
| 76 | + }); |
0 commit comments