From 74e408f91dcb66db9a0a5e2df8799a15c6476c00 Mon Sep 17 00:00:00 2001 From: Jonathan Budzenski Date: Wed, 13 Jan 2021 09:50:51 -0600 Subject: [PATCH] [build] Migrate grunt license:csv_report to script (#86001) Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- scripts/licenses_csv_report.js | 21 +++++ src/dev/run_licenses_csv_report.js | 128 +++++++++++++++++++++++++++++ tasks/licenses_csv_report.js | 116 -------------------------- 3 files changed, 149 insertions(+), 116 deletions(-) create mode 100644 scripts/licenses_csv_report.js create mode 100644 src/dev/run_licenses_csv_report.js delete mode 100644 tasks/licenses_csv_report.js diff --git a/scripts/licenses_csv_report.js b/scripts/licenses_csv_report.js new file mode 100644 index 0000000000000..6fe813a472745 --- /dev/null +++ b/scripts/licenses_csv_report.js @@ -0,0 +1,21 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +require('../src/setup_node_env'); +require('../src/dev/run_licenses_csv_report'); diff --git a/src/dev/run_licenses_csv_report.js b/src/dev/run_licenses_csv_report.js new file mode 100644 index 0000000000000..791f296085deb --- /dev/null +++ b/src/dev/run_licenses_csv_report.js @@ -0,0 +1,128 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { writeFileSync } from 'fs'; +import { resolve } from 'path'; +import { isNull, isUndefined } from 'lodash'; + +import { run } from '@kbn/dev-utils'; + +import { getInstalledPackages } from './npm'; +import { engines } from '../../package'; +import { LICENSE_OVERRIDES } from './license_checker'; + +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(','); +} + +run( + async ({ log, flags }) => { + const fields = ['name', 'version', 'url', 'license', 'sourceURL']; + + const file = flags.csv; + const directory = flags.directory; + const dev = flags.dev; + + const root = resolve(__dirname, '..', '..'); + const packages = await getInstalledPackages({ + directory: directory ? resolve(directory) : root, + licenseOverrides: LICENSE_OVERRIDES, + dev, + }); + + packages.unshift( + { + name: 'Node.js', + version: engines.node, + repository: 'https://nodejs.org', + licenses: ['MIT'], + }, + { + name: 'Red Hat Universal Base Image minimal', + version: '8', + repository: + 'https://catalog.redhat.com/software/containers/ubi8/ubi-minimal/5c359a62bed8bd75a2c3fba8', + licenses: [ + 'Custom;https://www.redhat.com/licenses/EULA_Red_Hat_Universal_Base_Image_English_20190422.pdf', + ], + sourceURL: 'https://oss-dependencies.elastic.co/redhat/ubi/ubi-minimal-8-source.tar.gz', + } + ); + + 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(','), + sourceURL: pkg.sourceURL, + }; + + return formatCsvValues(fields, data); + }) + .join('\n'); + + if (file) { + writeFileSync(file, `${fields.join(',')}\n${csv}`); + log.success(`wrote to ${file}`); + } else { + log.success(csv); + log.debug('\nspecify "--csv [filepath]" to write the data to a specific file'); + } + }, + { + description: ` + Report of 3rd party dependencies + `, + flags: { + boolean: ['dev'], + string: ['csv', 'directory'], + default: { + dev: false, + }, + help: ` + --dev Include development dependencies + --csv Write csv report to file + --directory Directory to check for licenses + `, + }, + } +); diff --git a/tasks/licenses_csv_report.js b/tasks/licenses_csv_report.js deleted file mode 100644 index f504cbcaa5fc8..0000000000000 --- a/tasks/licenses_csv_report.js +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { writeFileSync } from 'fs'; -import { resolve } from 'path'; -import { getInstalledPackages } from '../src/dev/npm'; -import { engines } from '../package'; -import { LICENSE_OVERRIDES } from '../src/dev/license_checker'; - -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', 'sourceURL']; - const done = this.async(); - - try { - const file = grunt.option('csv'); - const directory = grunt.option('directory'); - const dev = Boolean(grunt.option('dev')); - - const packages = await getInstalledPackages({ - directory: directory ? resolve(directory) : grunt.config.get('root'), - licenseOverrides: LICENSE_OVERRIDES, - dev, - }); - - packages.unshift( - { - name: 'Node.js', - version: engines.node, - repository: 'https://nodejs.org', - licenses: ['MIT'], - }, - { - name: 'Red Hat Universal Base Image minimal', - version: '8', - repository: - 'https://catalog.redhat.com/software/containers/ubi8/ubi-minimal/5c359a62bed8bd75a2c3fba8', - licenses: [ - 'Custom;https://www.redhat.com/licenses/EULA_Red_Hat_Universal_Base_Image_English_20190422.pdf', - ], - sourceURL: 'https://oss-dependencies.elastic.co/redhat/ubi/ubi-minimal-8-source.tar.gz', - } - ); - - 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(','), - sourceURL: pkg.sourceURL, - }; - - 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); - } - }); -}