Skip to content

Instantly share code, notes, and snippets.

@arcanis
Last active April 9, 2020 12:59
Show Gist options
  • Save arcanis/2e564ff62387cd73053155c2e515b2fb to your computer and use it in GitHub Desktop.
Save arcanis/2e564ff62387cd73053155c2e515b2fb to your computer and use it in GitHub Desktop.
module.exports = {
name: `yarn-plugin-license`,
factory: require => {
const {BaseCommand} = require(`@yarnpkg/cli`);
const {Cache, Configuration, Manifest, Project, StreamReport, miscUtils, structUtils} = require(`@yarnpkg/core`);
class LicenseCommand extends BaseCommand {
async execute() {
const configuration = await Configuration.find(this.context.cwd, this.context.plugins);
const {project, workspace} = await Project.find(configuration, this.context.cwd);
const cache = await Cache.find(configuration, {immutable: true});
await project.restoreInstallState();
await StreamReport.start({
configuration,
stdout: this.context.stdout,
}, async report => {
const fetcher = configuration.makeFetcher();
const fetchOptions = {project, cache, checksums: project.storedChecksums, report, fetcher};
const seen = new Set();
const results = [];
const traverse = async descriptor => {
const resolution = project.storedResolutions.get(descriptor.descriptorHash);
if (seen.has(resolution)) {
return;
} else {
seen.add(resolution);
}
const pkg = project.storedPackages.get(resolution);
await process(pkg);
for (const dep of pkg.dependencies.values()) {
await traverse(dep);
}
};
const process = async locator => {
const fetchResult = await fetcher.fetch(locator, fetchOptions);
let manifest;
try {
manifest = await Manifest.find(fetchResult.prefixPath, {baseFs: fetchResult.packageFs});
} catch {
manifest = null;
} finally {
if (fetchResult.releaseFs) {
fetchResult.releaseFs();
}
}
if (manifest) {
results.push([locator, manifest]);
}
};
await traverse(workspace.anchoredDescriptor);
for (const [locator, manifest] of miscUtils.sortMap(results, ([locator]) => structUtils.stringifyLocator(locator))) {
report.reportInfo(null, `${structUtils.prettyLocator(configuration, locator)}: ${manifest.raw.license}`);
}
});
}
}
LicenseCommand.addPath(`licenses`);
return {
commands: [
LicenseCommand,
],
};
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment