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

feat(cli): group by license in licenses list (#5074) #5110

Merged
merged 1 commit into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion __tests__/commands/__snapshots__/licenses.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`lists all licenses of the dependencies with the --json argument 1`] = `
"{\\"type\\":\\"warning\\",\\"data\\":\\"package.json: No license field\\"}
{\\"type\\":\\"warning\\",\\"data\\":\\"No license field\\"}
{\\"type\\":\\"tree\\",\\"data\\":{\\"type\\":\\"licenses\\",\\"trees\\":[{\\"name\\":\\"[email protected]\\",\\"children\\":[{\\"name\\":\\"License: MIT\\"},{\\"name\\":\\"URL: https://github.com/sindresorhus/is-plain-obj.git\\"}]}]}}
{\\"type\\":\\"tree\\",\\"data\\":{\\"type\\":\\"licenses\\",\\"trees\\":[{\\"name\\":\\"MIT\\",\\"children\\":[{\\"name\\":\\"[email protected]\\",\\"children\\":[{\\"name\\":\\"URL: https://github.com/sindresorhus/is-plain-obj.git\\"},{\\"name\\":\\"VendorUrl: sindresorhus.com\\"},{\\"name\\":\\"VendorName: Sindre Sorhus\\"}]}]}]}}
"
`;

Expand Down
77 changes: 52 additions & 25 deletions src/cli/commands/licenses.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,71 @@ async function getManifests(config: Config, flags: Object): Promise<Array<Manife

async function list(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
const manifests: Array<Manifest> = await getManifests(config, flags);
const manifestsByLicense = new Map();

for (const {name, version, license, repository, homepage, author} of manifests) {
const licenseKey = license || 'UNKNOWN';
const url = repository ? repository.url : homepage;
const vendorUrl = homepage || (author && author.url);
const vendorName = author && author.name;

if (!manifestsByLicense.has(licenseKey)) {
manifestsByLicense.set(licenseKey, new Map());
}

const byLicense = manifestsByLicense.get(licenseKey);
invariant(byLicense, 'expected value');
byLicense.set(`${name}@${version}`, {
name,
version,
url,
vendorUrl,
vendorName,
});
}

if (flags.json) {
const body = [];

for (const {name, version, license, repository, homepage, author} of manifests) {
const url = repository ? repository.url : homepage;
const vendorUrl = homepage || (author && author.url);
const vendorName = author && author.name;
body.push([
name,
version,
license || 'Unknown',
url || 'Unknown',
vendorUrl || 'Unknown',
vendorName || 'Unknown',
]);
}
manifestsByLicense.forEach((license, licenseKey) => {
license.forEach(({name, version, url, vendorUrl, vendorName}) => {
body.push([name, version, licenseKey, url || 'Unknown', vendorUrl || 'Unknown', vendorName || 'Unknown']);
});
});

reporter.table(['Name', 'Version', 'License', 'URL', 'VendorUrl', 'VendorName'], body);
} else {
const trees = [];

for (const {name, version, license, repository, homepage} of manifests) {
const children = [];
children.push({
name: `${reporter.format.bold('License:')} ${license || reporter.format.red('UNKNOWN')}`,
});
manifestsByLicense.forEach((license, licenseKey) => {
const licenseTree = [];

const url = repository ? repository.url : homepage;
if (url) {
children.push({name: `${reporter.format.bold('URL:')} ${url}`});
}
license.forEach(({name, version, url, vendorUrl, vendorName}) => {
const children = [];

if (url) {
children.push({name: `${reporter.format.bold('URL:')} ${url}`});
}

if (vendorUrl) {
children.push({name: `${reporter.format.bold('VendorUrl:')} ${vendorUrl}`});
}

if (vendorName) {
children.push({name: `${reporter.format.bold('VendorName:')} ${vendorName}`});
}

licenseTree.push({
name: `${name}@${version}`,
children,
});
});

trees.push({
name: `${name}@${version}`,
children,
name: licenseKey,
children: licenseTree,
});
}
});

reporter.tree('licenses', trees);
}
Expand Down