Skip to content

Commit 88875c8

Browse files
hanslfilipesilva
authored andcommitted
ci: update licenses and use promises since license is async
1 parent 47892d2 commit 88875c8

File tree

1 file changed

+50
-39
lines changed

1 file changed

+50
-39
lines changed

scripts/validate-licenses.ts

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const licensesWhitelist = [
4545
'(AFL-2.1 OR BSD-2-Clause)',
4646
'(MIT OR CC-BY-3.0)',
4747
'(MIT OR Apache-2.0)',
48+
'(MIT OR BSD-3-Clause)',
4849
];
4950

5051
// Name variations of SPDX licenses that some packages have.
@@ -53,6 +54,7 @@ const licenseReplacements: { [key: string]: string } = {
5354
// Just a longer string that our script catches. SPDX official name is the shorter one.
5455
'Apache License, Version 2.0': 'Apache-2.0',
5556
'Apache2': 'Apache-2.0',
57+
'Apache 2.0': 'Apache-2.0',
5658
'AFLv2.1': 'AFL-2.1',
5759
// BSD is BSD-2-clause by default.
5860
'BSD': 'BSD-2-Clause',
@@ -61,6 +63,7 @@ const licenseReplacements: { [key: string]: string } = {
6163
// Specific packages to ignore, add a reason in a comment. Format: package-name@version.
6264
const ignoredPackages = [
6365
'[email protected]', // CC0 but it's content only (index.json, no code) and not distributed.
66+
'[email protected]', // CC0 but it's content only (index.json, no code) and not distributed.
6467
'[email protected]', // MIT, license but it's not listed in package.json.
6568
'[email protected]', // LGPL,MIT but has a broken licenses array.
6669
'[email protected]', // Apache-2.0 but broken license in package.json
@@ -73,54 +76,62 @@ const ignoredPackages = [
7376
// TODO(filipesilva): remove this when spec_large is moved to e2e tests.
7477
'[email protected]', // (OFL-1.1 AND MIT)
7578

79+
'@webassemblyjs/[email protected]', // MIT but no LICENSE file. `license` field in package.json.
80+
'@webassemblyjs/[email protected]', // Apache 2.0 license, but get discovered as "Apache".
7681
];
7782

7883
// Find all folders directly under a `node_modules` that have a package.json.
7984
const checker = require('license-checker');
8085

8186

82-
export default function (_options: {}, logger: logging.Logger) {
83-
checker.init({ start: path.join(__dirname, '..') }, (err: Error, json: JsonObject) => {
84-
if (err) {
85-
logger.fatal(`Something happened:\n${err.message}`);
86-
} else {
87-
logger.info(`Testing ${Object.keys(json).length} packages.\n`);
88-
89-
// Packages with bad licenses are those that neither pass SPDX nor are ignored.
90-
const badLicensePackages = Object.keys(json)
91-
.map(key => ({
92-
id: key,
93-
licenses: ([] as string[])
94-
// tslint:disable-next-line:non-null-operator
95-
.concat((json[key] ! as JsonObject).licenses as string[])
96-
// `*` is used when the license is guessed.
97-
.map(x => x.replace(/\*$/, ''))
98-
.map(x => x in licenseReplacements ? licenseReplacements[x] : x),
99-
}))
100-
.filter(pkg => !passesSpdx(pkg.licenses, licensesWhitelist))
101-
.filter(pkg => !ignoredPackages.find(ignored => ignored === pkg.id));
102-
103-
// Report packages with bad licenses
104-
if (badLicensePackages.length > 0) {
105-
logger.error('Invalid package licences found:');
106-
badLicensePackages.forEach(pkg => {
107-
logger.error(`${pkg.id}: ${JSON.stringify(pkg.licenses)}`);
108-
});
109-
logger.fatal(`\n${badLicensePackages.length} total packages with invalid licenses.`);
110-
} else {
111-
logger.info('All package licenses are valid.');
112-
}
87+
// Check if a license is accepted by an array of accepted licenses
88+
function _passesSpdx(licenses: string[], accepted: string[]) {
89+
return accepted.some(l => {
90+
try {
91+
return spdxSatisfies(licenses.join(' AND '), l);
92+
} catch (_) {
93+
return false;
11394
}
11495
});
96+
}
11597

116-
// Check if a license is accepted by an array of accepted licenses
117-
function passesSpdx(licenses: string[], accepted: string[]) {
118-
return accepted.some(l => {
119-
try {
120-
return spdxSatisfies(licenses.join(' AND '), l);
121-
} catch (_) {
122-
return false;
98+
99+
export default function (_options: {}, logger: logging.Logger): Promise<number> {
100+
return new Promise(resolve => {
101+
checker.init({ start: path.join(__dirname, '..') }, (err: Error, json: JsonObject) => {
102+
if (err) {
103+
logger.fatal(`Something happened:\n${err.message}`);
104+
resolve(1);
105+
} else {
106+
logger.info(`Testing ${Object.keys(json).length} packages.\n`);
107+
108+
// Packages with bad licenses are those that neither pass SPDX nor are ignored.
109+
const badLicensePackages = Object.keys(json)
110+
.map(key => ({
111+
id: key,
112+
licenses: ([] as string[])
113+
// tslint:disable-next-line:non-null-operator
114+
.concat((json[key] ! as JsonObject).licenses as string[])
115+
// `*` is used when the license is guessed.
116+
.map(x => x.replace(/\*$/, ''))
117+
.map(x => x in licenseReplacements ? licenseReplacements[x] : x),
118+
}))
119+
.filter(pkg => !_passesSpdx(pkg.licenses, licensesWhitelist))
120+
.filter(pkg => !ignoredPackages.find(ignored => ignored === pkg.id));
121+
122+
// Report packages with bad licenses
123+
if (badLicensePackages.length > 0) {
124+
logger.error('Invalid package licences found:');
125+
badLicensePackages.forEach(pkg => {
126+
logger.error(`${pkg.id}: ${JSON.stringify(pkg.licenses)}`);
127+
});
128+
logger.fatal(`\n${badLicensePackages.length} total packages with invalid licenses.`);
129+
resolve(2);
130+
} else {
131+
logger.info('All package licenses are valid.');
132+
resolve(0);
133+
}
123134
}
124135
});
125-
}
136+
});
126137
}

0 commit comments

Comments
 (0)