Adds task to export a CSV of all dependencies (#15068)
* Adds task to export a CSV of all dependencies Signed-off-by: Tyler Smalley <tyler.smalley@elastic.co> * Fixes tests and changes argument name to `out` Signed-off-by: Tyler Smalley <tyler.smalley@elastic.co> * Uses csv as argument for output file Signed-off-by: Tyler Smalley <tyler.smalley@elastic.co> * Removes unused columns Signed-off-by: Tyler Smalley <tyler.smalley@elastic.co> * [license csv] Fetch all licenses by default, add release flag to only generate release dependencies
This commit is contained in:
parent
db7f9a1f64
commit
ace0173adf
6
tasks/lib/packages/__tests__/fixtures/fixture1/node_modules/dep1/package.json
generated
vendored
6
tasks/lib/packages/__tests__/fixtures/fixture1/node_modules/dep1/package.json
generated
vendored
|
@ -1,5 +1,9 @@
|
|||
{
|
||||
"name": "dep1",
|
||||
"version": "0.0.2",
|
||||
"license": "Apache-2.0"
|
||||
"license": "Apache-2.0",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "http://github.com/mycorp/dep1.git"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,6 +40,7 @@ describe('tasks/lib/packages', () => {
|
|||
name: 'dep1',
|
||||
version: '0.0.2',
|
||||
licenses: [ 'Apache-2.0' ],
|
||||
repository: 'https://github.com/mycorp/dep1',
|
||||
directory: resolve(FIXTURE1_ROOT, 'node_modules/dep1'),
|
||||
relative: 'node_modules/dep1',
|
||||
}
|
||||
|
|
|
@ -35,6 +35,7 @@ export async function getInstalledPackages(options = {}) {
|
|||
const {
|
||||
licenses: detectedLicenses,
|
||||
realPath,
|
||||
repository
|
||||
} = licenseInfo[key];
|
||||
|
||||
const licenses = [].concat(
|
||||
|
@ -46,6 +47,7 @@ export async function getInstalledPackages(options = {}) {
|
|||
return {
|
||||
name,
|
||||
version,
|
||||
repository,
|
||||
licenses,
|
||||
directory: realPath,
|
||||
relative: relative(directory, realPath)
|
||||
|
|
74
tasks/licenses_csv_report.js
Normal file
74
tasks/licenses_csv_report.js
Normal file
|
@ -0,0 +1,74 @@
|
|||
import { writeFileSync } from 'fs';
|
||||
import { getInstalledPackages } from './lib';
|
||||
|
||||
import {
|
||||
isNull,
|
||||
isUndefined
|
||||
} from 'lodash';
|
||||
|
||||
const allDoubleQuoteRE = /"/g;
|
||||
|
||||
function escapeValue(value) {
|
||||
if (isNull(value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
return `"${value.replace(allDoubleQuoteRE, '""')}"`;
|
||||
}
|
||||
|
||||
function formatCsvValues(fields, values) {
|
||||
return fields.map((field) => {
|
||||
const value = values[field];
|
||||
|
||||
if (isNull(value) || isUndefined(value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return value.toString();
|
||||
})
|
||||
.map(escapeValue)
|
||||
.join(',');
|
||||
}
|
||||
|
||||
export default function licensesCSVReport(grunt) {
|
||||
grunt.registerTask('licenses:csv_report', 'Report of 3rd party dependencies', async function () {
|
||||
const fields = ['name', 'version', 'url', 'license'];
|
||||
const done = this.async();
|
||||
|
||||
try {
|
||||
const options = grunt.config('licenses', { overrides: {} });
|
||||
const file = grunt.option('csv');
|
||||
const release = Boolean(grunt.option('release'));
|
||||
|
||||
const packages = await getInstalledPackages({
|
||||
directory: grunt.config.get('root'),
|
||||
licenseOverrides: options.overrides,
|
||||
dev: !release
|
||||
});
|
||||
|
||||
const csv = packages.map(pkg => {
|
||||
const data = {
|
||||
name: pkg.name,
|
||||
version: pkg.version,
|
||||
url: pkg.repository || `https://www.npmjs.com/package/${pkg.name}`,
|
||||
license: pkg.licenses.join(',')
|
||||
};
|
||||
|
||||
return formatCsvValues(fields, data);
|
||||
}).join('\n');
|
||||
|
||||
if (file) {
|
||||
writeFileSync(file, `${fields.join(',')}\n${csv}`);
|
||||
grunt.log.writeln(`wrote to ${file}`);
|
||||
} else {
|
||||
grunt.log.writeln(csv);
|
||||
grunt.log.writeln('\nspecify "--csv [filepath]" to write the data to a specific file');
|
||||
}
|
||||
|
||||
done();
|
||||
} catch (err) {
|
||||
grunt.fail.fatal(err);
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue