From 5c78d6b753ddb9dfa2966dea02784b836f95f0b3 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Thu, 9 Feb 2017 13:28:46 +0000 Subject: [PATCH 1/9] test: add license test --- package.json | 5 ++- scripts/test-licenses.js | 76 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 scripts/test-licenses.js diff --git a/package.json b/package.json index bd68e383d824..f1fad0eced4c 100644 --- a/package.json +++ b/package.json @@ -12,13 +12,14 @@ "build": "node ./scripts/publish/build.js", "build:patch": "node ./scripts/patch.js", "build:packages": "for PKG in packages/*; do echo Building $PKG...; tsc -p $PKG; done", - "test": "npm-run-all -c test:packages test:cli test:deps", + "test": "npm-run-all -c test:packages test:cli test:deps test:licenses", "e2e": "npm run test:e2e", "e2e:nightly": "node tests/run_e2e.js --nightly", "test:e2e": "node tests/run_e2e.js", "test:cli": "node tests/runner", "test:deps": "node scripts/publish/validate_dependencies.js", "test:inspect": "node --inspect --debug-brk tests/runner", + "test:licenses": "node scripts/test-licenses.js", "test:packages": "node scripts/run-packages-spec.js", "eslint": "eslint .", "tslint": "tslint \"**/*.ts\" -c tslint.json -e \"**/config/schema.d.ts\" -e \"**/tests/**\" -e \"**/blueprints/*/files/**/*.ts\" -e \"node_modules/**\" -e \"tmp/**\" -e \"dist/**\"", @@ -134,6 +135,7 @@ "express": "^4.14.0", "jasmine": "^2.4.1", "jasmine-spec-reporter": "^3.2.0", + "license-checker": "^8.0.3", "minimist": "^1.2.0", "mocha": "^3.2.0", "mock-fs": "^4.0.0", @@ -144,6 +146,7 @@ "resolve-bin": "^0.4.0", "rewire": "^2.5.1", "sinon": "^1.17.3", + "spdx-satisfies": "^0.1.3", "through": "^2.3.6", "tree-kill": "^1.0.0", "ts-node": "^2.0.0", diff --git a/scripts/test-licenses.js b/scripts/test-licenses.js new file mode 100644 index 000000000000..c36f715a8c9e --- /dev/null +++ b/scripts/test-licenses.js @@ -0,0 +1,76 @@ +const licenseChecker = require('license-checker'); +const spdxSatisfies = require('spdx-satisfies'); +const denodeify = require('denodeify'); +const licenseCheckerInit = denodeify(licenseChecker.init); + + +// SPDX defined licenses, see https://spdx.org/licenses/. +const acceptedSpdxLicenses = [ + 'MIT', + 'ISC', + 'Apache-2.0', + 'BSD-2-Clause', + 'BSD-3-Clause', + 'BSD-4-Clause', + 'CC-BY-3.0', + 'CC-BY-4.0', + 'Beerware', + 'Unlicense' +]; + +// Name variations of SPDX licenses that some packages have. +const ignoredLicenseVariations = [ + 'MIT*', + 'MIT/X11', + 'AFLv2.1', + 'AFLv3.0', + 'Apache-2.0', + 'Apache2', + 'BSD', + 'BSD*', + 'BSD-like', + 'NPL', + 'L/GPL', + 'Public Domain' +]; + +// Specific packages to ignore, add a reason in a comment. Format: package-name@version. +const ignoredPackages = [ + 'json-schema@0.2.3', // old license format, lists `AFLv2.1, BSD` + 'pause-stream@0.0.11', // old license format, lists `MIT, Apache2` + 'rx-lite@3.1.2' // `Apache License, Version 2.0`, but licence-checker can't handle commas +]; + +function testSpdx(licenses) { + try { + return spdxSatisfies(licenses, `(${acceptedSpdxLicenses.join(' OR ')})`) + } catch (_) { + return false; + } +} + +// Use license-checker first, it can filter more licenses. +licenseCheckerInit({ + start: './', + exclude: acceptedSpdxLicenses.concat(ignoredLicenseVariations).join(), + customFormat: { name: '', _location: '' } +}) + .then(json => { + let badPackages = Object.keys(json) + .map(key => Object.assign({}, json[key], { id: key })) + // Then run the remaining ones through spdx-satisfies. + .filter(pkg => !testSpdx(pkg.licenses)) + .filter(pkg => !ignoredPackages.find(ignored => ignored === pkg.id)); + + if (badPackages.length > 0) { + console.log('Invalid package licences found:\n'); + badPackages.forEach(pkg => console.log(`- ${pkg.id} (${pkg._location}): ${pkg.licenses}`)); + process.exit(1); + } else { + console.log('All package licenses are valid.'); + } + }) + .catch(err => { + console.log(err); + process.exit(1); + }); From 6a81697e75093421661323b7cf1896ed8e4b8266 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Fri, 10 Feb 2017 13:09:40 +0000 Subject: [PATCH 2/9] only check prod dependencies --- scripts/test-licenses.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/test-licenses.js b/scripts/test-licenses.js index c36f715a8c9e..35331dd31a67 100644 --- a/scripts/test-licenses.js +++ b/scripts/test-licenses.js @@ -53,7 +53,8 @@ function testSpdx(licenses) { licenseCheckerInit({ start: './', exclude: acceptedSpdxLicenses.concat(ignoredLicenseVariations).join(), - customFormat: { name: '', _location: '' } + customFormat: { name: '', _location: '' }, + production: true }) .then(json => { let badPackages = Object.keys(json) From bd64c5d91917f0fed877ff618872ca314111f871 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Fri, 10 Feb 2017 14:13:05 +0000 Subject: [PATCH 3/9] remove findup dep --- package.json | 1 - .../cli/ember-cli/lib/models/project.js | 44 +++++++------------ packages/@angular/cli/models/config.ts | 36 ++++----------- packages/@angular/cli/package.json | 1 - packages/@angular/cli/upgrade/version.ts | 30 ++++--------- packages/@angular/cli/utilities/find-up.ts | 23 ++++++++++ 6 files changed, 55 insertions(+), 80 deletions(-) create mode 100644 packages/@angular/cli/utilities/find-up.ts diff --git a/package.json b/package.json index f1fad0eced4c..0d7f085233ec 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,6 @@ "exports-loader": "^0.6.3", "extract-text-webpack-plugin": "^2.0.0-rc.3", "file-loader": "^0.10.0", - "findup": "0.1.5", "fs-extra": "~2.0.0", "get-caller-file": "^1.0.0", "glob": "^7.0.3", diff --git a/packages/@angular/cli/ember-cli/lib/models/project.js b/packages/@angular/cli/ember-cli/lib/models/project.js index 1905a850c49b..fb993b77cd98 100644 --- a/packages/@angular/cli/ember-cli/lib/models/project.js +++ b/packages/@angular/cli/ember-cli/lib/models/project.js @@ -5,7 +5,7 @@ */ var Promise = require('../ext/promise'); var path = require('path'); -var findup = Promise.denodeify(require('findup')); +var findUp = require('../../../utilities/find-up').findUp; var resolve = Promise.denodeify(require('resolve')); var fs = require('fs'); var find = require('lodash/find'); @@ -470,7 +470,7 @@ Project.closest = function(pathName, _ui, _cli) { return new Project(result.directory, result.pkg, ui, _cli); }) .catch(function(reason) { - handleFindupError(pathName, reason); + handleFindupError(pathName); }); }; @@ -546,7 +546,7 @@ Project.projectOrnullProject = function(_ui, _cli) { */ Project.getProjectRoot = function () { try { - var directory = findup.sync(process.cwd(), 'package.json'); + var directory = path.dirname(findUp(process.cwd(), 'package.json')); var pkg = require(path.join(directory, 'package.json')); if (pkg && pkg.name === 'ember-cli') { @@ -557,12 +557,8 @@ Project.getProjectRoot = function () { debug('getProjectRoot %s -> %s', process.cwd(), directory); return directory; } catch (reason) { - if (isFindupError(reason)) { - debug('getProjectRoot: not found. Will use cwd: %s', process.cwd()); - return process.cwd(); - } else { - throw reason; - } + debug('getProjectRoot: not found. Will use cwd: %s', process.cwd()); + return process.cwd(); } }; @@ -594,34 +590,24 @@ function ensureUI(_ui) { } function closestPackageJSON(pathName) { - return findup(pathName, 'package.json') - .then(function(directory) { - return Promise.hash({ - directory: directory, - pkg: require(path.join(directory, 'package.json')) - }); - }); + return Promise.resolve() + .then(() => findUp('package.json', pathName)) + .then(filePath => ({ + directory: path.dirname(filePath), + pkg: require(filePath) + })); } function findupPath(pathName) { try { - return findup.sync(pathName, 'package.json'); + return path.dirname(findUp('package.json', pathName)); } catch (reason) { - handleFindupError(pathName, reason); + handleFindupError(pathName); } } -function isFindupError(reason) { - // Would be nice if findup threw error subclasses - return reason && /not found/i.test(reason.message); -} - -function handleFindupError(pathName, reason) { - if (isFindupError(reason)) { - throw new NotFoundError('No project found at or up from: `' + pathName + '`'); - } else { - throw reason; - } +function handleFindupError(pathName) { + throw new NotFoundError('No project found at or up from: `' + pathName + '`'); } // Export diff --git a/packages/@angular/cli/models/config.ts b/packages/@angular/cli/models/config.ts index 8de57cce60b1..02a31ee501aa 100644 --- a/packages/@angular/cli/models/config.ts +++ b/packages/@angular/cli/models/config.ts @@ -2,31 +2,13 @@ import {CliConfig as CliConfigBase} from './config/config'; import {CliConfig as ConfigInterface} from '../lib/config/schema'; import { oneLine } from 'common-tags'; import * as chalk from 'chalk'; -import * as fs from 'fs'; import * as path from 'path'; -export const CLI_CONFIG_FILE_NAME = '.angular-cli.json'; -const CLI_CONFIG_FILE_NAME_ALT = 'angular-cli.json'; - - -function _findUp(name: string, from: string) { - let currentDir = from; - while (currentDir && currentDir !== path.parse(currentDir).root) { - const p = path.join(currentDir, name); - if (fs.existsSync(p)) { - return p; - } - - const nodeModuleP = path.join(currentDir, 'node_modules'); - if (fs.existsSync(nodeModuleP)) { - return null; - } +import {findUp} from '../utilities/find-up'; - currentDir = path.dirname(currentDir); - } - return null; -} +export const CLI_CONFIG_FILE_NAME = '.angular-cli.json'; +const CLI_CONFIG_FILE_NAME_ALT = 'angular-cli.json'; function getUserHome() { @@ -38,12 +20,12 @@ export class CliConfig extends CliConfigBase { static configFilePath(projectPath?: string): string { // Find the configuration, either where specified, in the Angular CLI project // (if it's in node_modules) or from the current process. - return (projectPath && _findUp(CLI_CONFIG_FILE_NAME, projectPath)) - || (projectPath && _findUp(CLI_CONFIG_FILE_NAME_ALT, projectPath)) - || _findUp(CLI_CONFIG_FILE_NAME, process.cwd()) - || _findUp(CLI_CONFIG_FILE_NAME_ALT, process.cwd()) - || _findUp(CLI_CONFIG_FILE_NAME, __dirname) - || _findUp(CLI_CONFIG_FILE_NAME_ALT, __dirname); + return (projectPath && findUp(CLI_CONFIG_FILE_NAME, projectPath)) + || (projectPath && findUp(CLI_CONFIG_FILE_NAME_ALT, projectPath)) + || findUp(CLI_CONFIG_FILE_NAME, process.cwd()) + || findUp(CLI_CONFIG_FILE_NAME_ALT, process.cwd()) + || findUp(CLI_CONFIG_FILE_NAME, __dirname) + || findUp(CLI_CONFIG_FILE_NAME_ALT, __dirname); } static fromGlobal(): CliConfig { diff --git a/packages/@angular/cli/package.json b/packages/@angular/cli/package.json index 9ab9bc9569a1..2df914d490a9 100644 --- a/packages/@angular/cli/package.json +++ b/packages/@angular/cli/package.json @@ -46,7 +46,6 @@ "exports-loader": "^0.6.3", "extract-text-webpack-plugin": "^2.0.0-rc.3", "file-loader": "^0.10.0", - "findup": "0.1.5", "fs-extra": "^2.0.0", "get-caller-file": "^1.0.0", "glob": "^7.0.3", diff --git a/packages/@angular/cli/upgrade/version.ts b/packages/@angular/cli/upgrade/version.ts index 1b363ab5e1a7..63c4850f5528 100644 --- a/packages/@angular/cli/upgrade/version.ts +++ b/packages/@angular/cli/upgrade/version.ts @@ -5,32 +5,18 @@ import {readFileSync, existsSync} from 'fs'; import * as path from 'path'; import {CliConfig} from '../models/config'; +import {findUp} from '../utilities/find-up'; const resolve = require('resolve'); -function _findUp(name: string, from: string) { - let currentDir = from; - while (currentDir && currentDir !== path.parse(currentDir).root) { - const p = path.join(currentDir, name); - if (existsSync(p)) { - return p; - } - - currentDir = path.dirname(currentDir); - } - - return null; -} - - function _hasOldCliBuildFile() { - return existsSync(_findUp('angular-cli-build.js', process.cwd())) - || existsSync(_findUp('angular-cli-build.ts', process.cwd())) - || existsSync(_findUp('ember-cli-build.js', process.cwd())) - || existsSync(_findUp('angular-cli-build.js', __dirname)) - || existsSync(_findUp('angular-cli-build.ts', __dirname)) - || existsSync(_findUp('ember-cli-build.js', __dirname)); + return existsSync(findUp('angular-cli-build.js', process.cwd())) + || existsSync(findUp('angular-cli-build.ts', process.cwd())) + || existsSync(findUp('ember-cli-build.js', process.cwd())) + || existsSync(findUp('angular-cli-build.js', __dirname)) + || existsSync(findUp('angular-cli-build.ts', __dirname)) + || existsSync(findUp('ember-cli-build.js', __dirname)); } @@ -112,7 +98,7 @@ export class Version { console.error(bold(red(stripIndents` This version of CLI is only compatible with angular version 2.3.1 or better. Please upgrade your angular version, e.g. by running: - + npm install @angular/core@latest ` + '\n'))); process.exit(3); diff --git a/packages/@angular/cli/utilities/find-up.ts b/packages/@angular/cli/utilities/find-up.ts new file mode 100644 index 000000000000..0d54428a3659 --- /dev/null +++ b/packages/@angular/cli/utilities/find-up.ts @@ -0,0 +1,23 @@ +import * as path from 'path'; +import { existsSync } from 'fs'; + +export function findUp(name: string, from: string, stopOnNodeModules = false) { + let currentDir = from; + while (currentDir && currentDir !== path.parse(currentDir).root) { + const p = path.join(currentDir, name); + if (existsSync(p)) { + return p; + } + + if (stopOnNodeModules) { + const nodeModuleP = path.join(currentDir, 'node_modules'); + if (existsSync(nodeModuleP)) { + return null; + } + } + + currentDir = path.dirname(currentDir); + } + + return null; +} From f933fbabecd039fe5844c65775ffa7cb39a39940 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Fri, 10 Feb 2017 14:13:13 +0000 Subject: [PATCH 4/9] also check dev deps --- scripts/test-licenses.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/test-licenses.js b/scripts/test-licenses.js index 35331dd31a67..c36f715a8c9e 100644 --- a/scripts/test-licenses.js +++ b/scripts/test-licenses.js @@ -53,8 +53,7 @@ function testSpdx(licenses) { licenseCheckerInit({ start: './', exclude: acceptedSpdxLicenses.concat(ignoredLicenseVariations).join(), - customFormat: { name: '', _location: '' }, - production: true + customFormat: { name: '', _location: '' } }) .then(json => { let badPackages = Object.keys(json) From adeb29a5ba52eef2eadb45236b53a4ddbcd0c2b1 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Fri, 10 Feb 2017 14:22:59 +0000 Subject: [PATCH 5/9] add map-stream to ignore list --- scripts/test-licenses.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/test-licenses.js b/scripts/test-licenses.js index c36f715a8c9e..a8858bbfa6c6 100644 --- a/scripts/test-licenses.js +++ b/scripts/test-licenses.js @@ -38,7 +38,8 @@ const ignoredLicenseVariations = [ const ignoredPackages = [ 'json-schema@0.2.3', // old license format, lists `AFLv2.1, BSD` 'pause-stream@0.0.11', // old license format, lists `MIT, Apache2` - 'rx-lite@3.1.2' // `Apache License, Version 2.0`, but licence-checker can't handle commas + 'rx-lite@3.1.2', // `Apache License, Version 2.0`, but licence-checker can't handle commas + 'map-stream@0.1.0', // has a MIT license but it's not listed in package.json ]; function testSpdx(licenses) { From c7d28e0f30a65e7cbd7c7f4eef29c02bd1832dea Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Fri, 10 Feb 2017 17:03:18 +0000 Subject: [PATCH 6/9] remove license-checker --- package.json | 1 - scripts/test-licenses.js | 140 ++++++++++++++++++++++++++------------- 2 files changed, 93 insertions(+), 48 deletions(-) diff --git a/package.json b/package.json index 0d7f085233ec..0da972783909 100644 --- a/package.json +++ b/package.json @@ -134,7 +134,6 @@ "express": "^4.14.0", "jasmine": "^2.4.1", "jasmine-spec-reporter": "^3.2.0", - "license-checker": "^8.0.3", "minimist": "^1.2.0", "mocha": "^3.2.0", "mock-fs": "^4.0.0", diff --git a/scripts/test-licenses.js b/scripts/test-licenses.js index a8858bbfa6c6..c5aa6a37e7ea 100644 --- a/scripts/test-licenses.js +++ b/scripts/test-licenses.js @@ -1,10 +1,14 @@ -const licenseChecker = require('license-checker'); +require('../lib/bootstrap-local'); + +const path = require('path'); +const glob = require('glob'); const spdxSatisfies = require('spdx-satisfies'); -const denodeify = require('denodeify'); -const licenseCheckerInit = denodeify(licenseChecker.init); +// TODO use logger.Logger +const logger = require('@ngtools/logger'); // SPDX defined licenses, see https://spdx.org/licenses/. +// TODO(hansl): confirm this list const acceptedSpdxLicenses = [ 'MIT', 'ISC', @@ -19,59 +23,101 @@ const acceptedSpdxLicenses = [ ]; // Name variations of SPDX licenses that some packages have. -const ignoredLicenseVariations = [ - 'MIT*', - 'MIT/X11', - 'AFLv2.1', - 'AFLv3.0', - 'Apache-2.0', - 'Apache2', - 'BSD', - 'BSD*', - 'BSD-like', - 'NPL', - 'L/GPL', - 'Public Domain' +// Licenses not included in SPDX but accepted will be converted to MIT. +// TODO(hansl): make sure all of these are ok +const licenseReplacements = [ + { name: 'Apache License, Version 2.0', replacement: 'Apache-2.0' }, + { name: 'AFLv2.1', replacement: 'AFL-2.1' }, + // I guess they are kinda the same? + { name: 'BSD', replacement: 'BSD-2-Clause' }, + { name: 'BSD-like', replacement: 'BSD-2-Clause' }, + { name: 'MIT/X11', replacement: 'MIT' }, + // Not sure how to deal with public domain. + // http://wiki.spdx.org/view/Legal_Team/Decisions/Dealing_with_Public_Domain_within_SPDX_Files + { name: 'Public Domain', replacement: 'MIT' } ]; // Specific packages to ignore, add a reason in a comment. Format: package-name@version. +// TODO(hansl): review these const ignoredPackages = [ - 'json-schema@0.2.3', // old license format, lists `AFLv2.1, BSD` - 'pause-stream@0.0.11', // old license format, lists `MIT, Apache2` - 'rx-lite@3.1.2', // `Apache License, Version 2.0`, but licence-checker can't handle commas - 'map-stream@0.1.0', // has a MIT license but it's not listed in package.json + 'async-foreach@0.1.3', // MIT, but doesn't list it in package.json + 'domelementtype@1.1.3', // Looks like MIT + 'domelementtype@1.3.0', // Looks like MIT + 'domhandler@2.1.0', // Looks like MIT + 'domutils@1.5.1', // Looks like MIT + 'domutils@1.1.6', // Looks like MIT + 'extsprintf@1.0.2', // Looks like MIT + 'formatio@1.1.1', // BSD, but doesn't list it in package.json + 'indexof@0.0.1', // MIT, but doesn't list it in package.json + 'map-stream@0.1.0', // MIT, license but it's not listed in package.json. + 'mime@1.2.11', // MIT, but doesn't list it in package.json + 'ms@0.7.1', // MIT, but doesn't list it in package.json + 'pause-stream@0.0.11', // MIT AND Apache-2.0, but broken license field in package.json lists. + 'progress@1.1.8', // MIT, but doesn't list it in package.json + 'samsam@1.1.2', // BSD, but doesn't list it in package.json + 'stdout-stream@1.4.0', // MIT, but doesn't list it in package.json + 'undefined@undefined', // Test package with no name nor version. + 'verror@1.3.6', // Looks like MIT ]; -function testSpdx(licenses) { +const root = path.resolve(__dirname, '../'); + +// Find all folder directly under a `node_modules` that have a package.json. +const allPackages = glob.sync(path.join(root, '**/node_modules/*/package.json'), { nodir: true }) + .map(packageJsonPath => { + const packageJson = require(packageJsonPath); + return { + id: `${packageJson.name}@${packageJson.version}`, + path: path.dirname(packageJsonPath), + packageJson: packageJson + }; + }) + // Figure out what kind of license the package uses. + .map(pkg => { + let license = null; + if (pkg.packageJson.license) { + // Use license field if present + if (typeof pkg.packageJson.license === 'string') { + license = replace(pkg.packageJson.license); + } else if (typeof pkg.packageJson.license === 'object' && typeof pkg.packageJson.type) { + license = replace(pkg.packageJson.license.type); + } + } else if (Array.isArray(pkg.packageJson.licenses)) { + // If there's an (outdated) licenses array use that joined by OR. + // TODO verify multiple licenses is OR and not AND + license = pkg.packageJson.licenses + .map(license => replace(license.type)) + .join(' OR '); + } + pkg.license = license; + return pkg; + }) + +// Packages with bad licenses are those that neither pass SPDX nor are ignored. +const badLicensePackages = allPackages + .filter(pkg => !passesSpdx(pkg.license, acceptedSpdxLicenses)) + .filter(pkg => !ignoredPackages.find(ignored => ignored === pkg.id)); + +// Report packages with bad licenses +if (badLicensePackages.length > 0) { + console.log('Invalid package licences found:\n'); + badLicensePackages.forEach(pkg => console.log(`${pkg.id} (${pkg.path}): ${pkg.license}`)); + process.exit(1); +} else { + console.log('All package licenses are valid.'); +} + +// Check if a license is accepted by an array of accepted licenses +function passesSpdx(license, accepted) { try { - return spdxSatisfies(licenses, `(${acceptedSpdxLicenses.join(' OR ')})`) + return spdxSatisfies(license, `(${accepted.join(' OR ')})`) } catch (_) { return false; } } -// Use license-checker first, it can filter more licenses. -licenseCheckerInit({ - start: './', - exclude: acceptedSpdxLicenses.concat(ignoredLicenseVariations).join(), - customFormat: { name: '', _location: '' } -}) - .then(json => { - let badPackages = Object.keys(json) - .map(key => Object.assign({}, json[key], { id: key })) - // Then run the remaining ones through spdx-satisfies. - .filter(pkg => !testSpdx(pkg.licenses)) - .filter(pkg => !ignoredPackages.find(ignored => ignored === pkg.id)); - - if (badPackages.length > 0) { - console.log('Invalid package licences found:\n'); - badPackages.forEach(pkg => console.log(`- ${pkg.id} (${pkg._location}): ${pkg.licenses}`)); - process.exit(1); - } else { - console.log('All package licenses are valid.'); - } - }) - .catch(err => { - console.log(err); - process.exit(1); - }); +// Apply license name replacement if any +function replace(license) { + const match = licenseReplacements.find(rpl => rpl.name === license); + return match ? match.replacement : license; +} From 1c34b55268fd1e20a65e561059caf234dd3a5b65 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Fri, 10 Feb 2017 20:06:22 +0000 Subject: [PATCH 7/9] add comment --- scripts/test-licenses.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/test-licenses.js b/scripts/test-licenses.js index c5aa6a37e7ea..a7285384e5a9 100644 --- a/scripts/test-licenses.js +++ b/scripts/test-licenses.js @@ -93,6 +93,8 @@ const allPackages = glob.sync(path.join(root, '**/node_modules/*/package.json'), return pkg; }) +// TODO: show total number of packages + // Packages with bad licenses are those that neither pass SPDX nor are ignored. const badLicensePackages = allPackages .filter(pkg => !passesSpdx(pkg.license, acceptedSpdxLicenses)) From c39d6b65c1e130eee73e31cf74451ef4b00c5ee7 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Fri, 10 Feb 2017 21:00:06 +0000 Subject: [PATCH 8/9] use logger --- scripts/test-licenses.js | 42 +++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/scripts/test-licenses.js b/scripts/test-licenses.js index a7285384e5a9..15da9b38b558 100644 --- a/scripts/test-licenses.js +++ b/scripts/test-licenses.js @@ -2,10 +2,34 @@ require('../lib/bootstrap-local'); const path = require('path'); const glob = require('glob'); +const chalk = require('chalk'); const spdxSatisfies = require('spdx-satisfies'); -// TODO use logger.Logger -const logger = require('@ngtools/logger'); +const Logger = require('@ngtools/logger').Logger; +require('rxjs/add/operator/filter'); +// Configure logger +const logger = new Logger('test-licenses'); + +logger + .subscribe((entry) => { + let color = chalk.white; + let output = process.stdout; + switch (entry.level) { + case 'info': color = chalk.white; break; + case 'warn': color = chalk.yellow; break; + case 'error': color = chalk.red; output = process.stderr; break; + case 'fatal': color = (x) => chalk.bold(chalk.red(x)); output = process.stderr; break; + } + + output.write(color(entry.message) + '\n'); + }); + +logger + .filter((entry) => entry.level == 'fatal') + .subscribe(() => { + process.stderr.write('A fatal error happened. See details above.'); + process.exit(1); + }); // SPDX defined licenses, see https://spdx.org/licenses/. // TODO(hansl): confirm this list @@ -28,7 +52,7 @@ const acceptedSpdxLicenses = [ const licenseReplacements = [ { name: 'Apache License, Version 2.0', replacement: 'Apache-2.0' }, { name: 'AFLv2.1', replacement: 'AFL-2.1' }, - // I guess they are kinda the same? + // I guess these are kinda the same? { name: 'BSD', replacement: 'BSD-2-Clause' }, { name: 'BSD-like', replacement: 'BSD-2-Clause' }, { name: 'MIT/X11', replacement: 'MIT' }, @@ -62,7 +86,7 @@ const ignoredPackages = [ const root = path.resolve(__dirname, '../'); -// Find all folder directly under a `node_modules` that have a package.json. +// Find all folders directly under a `node_modules` that have a package.json. const allPackages = glob.sync(path.join(root, '**/node_modules/*/package.json'), { nodir: true }) .map(packageJsonPath => { const packageJson = require(packageJsonPath); @@ -93,7 +117,7 @@ const allPackages = glob.sync(path.join(root, '**/node_modules/*/package.json'), return pkg; }) -// TODO: show total number of packages +logger.info(`Testing ${allPackages.length} packages.\n`) // Packages with bad licenses are those that neither pass SPDX nor are ignored. const badLicensePackages = allPackages @@ -102,11 +126,11 @@ const badLicensePackages = allPackages // Report packages with bad licenses if (badLicensePackages.length > 0) { - console.log('Invalid package licences found:\n'); - badLicensePackages.forEach(pkg => console.log(`${pkg.id} (${pkg.path}): ${pkg.license}`)); - process.exit(1); + logger.error('Invalid package licences found:'); + badLicensePackages.forEach(pkg => logger.error(`${pkg.id} (${pkg.path}): ${pkg.license}`)); + logger.fatal(`\n${badLicensePackages.length} total packages with invalid licenses.`); } else { - console.log('All package licenses are valid.'); + logger.info('All package licenses are valid.'); } // Check if a license is accepted by an array of accepted licenses From b9bb6958179f1c6905edcde53f6a567454ebe67b Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Fri, 10 Feb 2017 21:18:46 +0000 Subject: [PATCH 9/9] fix lint errors --- .eslintrc.json | 2 +- packages/@angular/cli/models/config.ts | 1 + scripts/test-licenses.js | 25 ++++++++++++------------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index c3bf0b37ad05..ad06da4e2265 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -164,7 +164,7 @@ "guard-for-in": "off", "handle-callback-err": "off", "id-length": "off", - "indent": [2,2], + "indent": [2,2, { "SwitchCase": 1 }], "init-declarations": "off", "jsx-quotes": "off", "key-spacing": [2, { diff --git a/packages/@angular/cli/models/config.ts b/packages/@angular/cli/models/config.ts index 02a31ee501aa..547823d70358 100644 --- a/packages/@angular/cli/models/config.ts +++ b/packages/@angular/cli/models/config.ts @@ -2,6 +2,7 @@ import {CliConfig as CliConfigBase} from './config/config'; import {CliConfig as ConfigInterface} from '../lib/config/schema'; import { oneLine } from 'common-tags'; import * as chalk from 'chalk'; +import * as fs from 'fs'; import * as path from 'path'; import {findUp} from '../utilities/find-up'; diff --git a/scripts/test-licenses.js b/scripts/test-licenses.js index 15da9b38b558..7cef8219198d 100644 --- a/scripts/test-licenses.js +++ b/scripts/test-licenses.js @@ -10,19 +10,18 @@ require('rxjs/add/operator/filter'); // Configure logger const logger = new Logger('test-licenses'); -logger - .subscribe((entry) => { - let color = chalk.white; - let output = process.stdout; - switch (entry.level) { - case 'info': color = chalk.white; break; - case 'warn': color = chalk.yellow; break; - case 'error': color = chalk.red; output = process.stderr; break; - case 'fatal': color = (x) => chalk.bold(chalk.red(x)); output = process.stderr; break; - } +logger.subscribe((entry) => { + let color = chalk.white; + let output = process.stdout; + switch (entry.level) { + case 'info': color = chalk.white; break; + case 'warn': color = chalk.yellow; break; + case 'error': color = chalk.red; output = process.stderr; break; + case 'fatal': color = (x) => chalk.bold(chalk.red(x)); output = process.stderr; break; + } - output.write(color(entry.message) + '\n'); - }); + output.write(color(entry.message) + '\n'); +}); logger .filter((entry) => entry.level == 'fatal') @@ -81,7 +80,7 @@ const ignoredPackages = [ 'samsam@1.1.2', // BSD, but doesn't list it in package.json 'stdout-stream@1.4.0', // MIT, but doesn't list it in package.json 'undefined@undefined', // Test package with no name nor version. - 'verror@1.3.6', // Looks like MIT + 'verror@1.3.6' // Looks like MIT ]; const root = path.resolve(__dirname, '../');