Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a license checker script #8808

Merged
merged 7 commits into from
Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
"build:packages": "cross-env EXCLUDE_PACKAGES=babel-plugin-import-jsx-pragma,jest-console,postcss-themes node ./bin/packages/build.js",
"build": "npm run build:packages && cross-env NODE_ENV=production webpack",
"check-engines": "check-node-version --package",
"check-licenses": "concurrently \"wp-scripts check-licenses --prod --gpl2\" \"wp-scripts check-licenses --dev\"",
"ci": "concurrently \"npm run lint\" \"npm run test-unit:coverage-ci\"",
"predev": "npm run check-engines",
"dev": "npm run build:packages && concurrently \"cross-env webpack --watch\" \"npm run dev:packages\"",
Expand All @@ -174,7 +175,7 @@
"lint-css": "stylelint '**/*.scss'",
"lint-css:fix": "stylelint '**/*.scss' --fix",
"package-plugin": "./bin/build-plugin-zip.sh",
"postinstall": "npm run build:packages",
"postinstall": "npm run check-licenses && npm run build:packages",
"pot-to-php": "./bin/pot-to-php.js",
"precommit": "lint-staged",
"publish:check": "npm run build:packages && lerna updated",
Expand Down
1 change: 1 addition & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@wordpress/babel-preset-default": "file:../babel-preset-default",
"@wordpress/jest-preset-default": "file:../jest-preset-default",
"@wordpress/npm-package-json-lint-config": "file:../npm-package-json-lint-config",
"chalk": "^2.4.1",
"cross-spawn": "^5.1.0",
"jest": "^23.4.2",
"npm-package-json-lint": "^3.3.0",
Expand Down
170 changes: 170 additions & 0 deletions packages/scripts/scripts/check-licenses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/**
* External dependencies
*/
const spawn = require( 'cross-spawn' );
const { existsSync, readFileSync } = require( 'fs' );
const chalk = require( 'chalk' );

/**
* Internal dependencies
*/
const { hasCliArg } = require( '../utils' );

const ERROR = chalk.reset.inverse.bold.red( ' ERROR ' );

const prod = hasCliArg( '--prod' ) || hasCliArg( '--production' );
const dev = hasCliArg( '--dev' ) || hasCliArg( '--development' );
const gpl2 = hasCliArg( '--gpl2' );

const gpl2Licenses = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick here, but it'd be nice to call this gpl2CompatibleLicenses or something like that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

'Apache-2.0 WITH LLVM-exception',
'Artistic-2.0',
'BSD',
'BSD-2-Clause',
'BSD-3-Clause',
'BSD-like',
'CC-BY-3.0',
'CC-BY-4.0',
'CC0-1.0',
'GPL-2.0',
'GPL-2.0+',
'GPL-2.0-or-later',
'ISC',
'LGPL-2.1',
'MIT',
'MIT/X11',
'MIT (http://mootools.net/license.txt)',
'MPL-2.0',
'Public Domain',
'Unlicense',
'WTFPL',
'Zlib',
'(MIT AND BSD-3-Clause)',
'(MIT AND Zlib)',
];

const ossLicenses = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this variable mean? Aren't all of these licenses open source software licenses? 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THESE ARE NOT THE FREE-EST LICENSES MATT

RICHARD STALLMAN IS JUDGING US FOR EVEN MENTIONING THEM

😁

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I CLEARLY HATE FREEDOM, MY MICROWAVE IS RUNNING APACHE 2 FIRMWARE.

'Apache-2.0',
'Apache 2.0',
'Apache License, Version 2.0',
'Apache version 2.0',
];

const licenses = [
...gpl2Licenses,
...( gpl2 ? [] : ossLicenses ),
];

const licenseFiles = [
'LICENCE',
'license',
'LICENSE',
'LICENSE.md',
'LICENSE.txt',
'LICENSE-MIT',
'MIT-LICENSE.txt',
'Readme.md',
'README.md',
];

const licenseFileStrings = {
'Apache-2.0': [
'Licensed under the Apache License, Version 2.0',
],
BSD: [
'Redistributions in binary form must reproduce the above copyright notice,',
],
MIT: [
'Permission is hereby granted, free of charge,',
'## License\n\nMIT',
'## License\n\n MIT',
],
};

const checkLicense = ( allowedLicense, licenseType ) => {
if ( ! licenseType ) {
return false;
}

const formattedAllowedLicense = allowedLicense.toLowerCase();
const formattedlicenseType = licenseType.toLowerCase();

if ( formattedAllowedLicense === formattedlicenseType ) {
return true;
}

if ( licenseType.indexOf( 'OR' ) < 0 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add an inline comment explaining what happens in here?

return false;
}

const subLicenseTypes = formattedlicenseType.replace( /^\(*/g, '' ).replace( /\)*$/, '' ).split( ' or ' ).map( ( e ) => e.trim() );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, I bet w are extracting sublicenses. However, it is not clear how it works.


return subLicenseTypes.reduce( ( satisfied, subLicenseType ) => {
if ( checkLicense( allowedLicense, subLicenseType ) ) {
return true;
}
return satisfied;
}, false );
};

const child = spawn.sync( 'npm', [
'ls',
'--parseable',
...( prod ? [ '--prod' ] : [] ),
...( dev ? [ '--dev' ] : [] ),
] );

const modules = child.stdout.toString().split( '\n' );

modules.forEach( ( path ) => {
if ( ! path ) {
return;
}

const filename = path + '/package.json';
if ( ! existsSync( filename ) ) {
process.stdout.write( `Unable to locate package.json in ${ path }.` );
process.exit( 1 );
}

const packageInfo = require( filename );
const license = packageInfo.license || ( packageInfo.licenses && packageInfo.licenses.map( ( l ) => l.type || l ).join( ' OR ' ) );
let licenseType = typeof license === 'object' ? license.type : license;

if ( licenseType === undefined ) {
licenseType = licenseFiles.reduce( ( detectedType, licenseFile ) => {
if ( detectedType ) {
return detectedType;
}

const licensePath = path + '/' + licenseFile;

if ( existsSync( licensePath ) ) {
const licenseText = readFileSync( licensePath ).toString();

return Object.keys( licenseFileStrings ).reduce( ( stringDetectedType, licenseStringType ) => {
const licenseFileString = licenseFileStrings[ licenseStringType ];

return licenseFileString.reduce( ( currentDetectedType, fileString ) => {
if ( licenseText.includes( fileString ) ) {
return licenseStringType;
}
return currentDetectedType;
}, stringDetectedType );
}, detectedType );
}
}, false );
}

const allowed = licenses.reduce( ( satisfied, allowedLicense ) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can be implemented using array.find:

const allowed = licenses.find( ( allowedLicense ) => checkLicense( allowedLicense, licenseType ) );

if ( checkLicense( allowedLicense, licenseType ) ) {
return true;
}
return satisfied;
}, false );

if ( ! allowed ) {
process.exitCode = 1;
process.stdout.write( `${ ERROR } Module ${ packageInfo.name } has an incompatible license '${ licenseType }'.\n` );
}
} );