diff options
author | Andy Scherzinger <info@andy-scherzinger.de> | 2024-06-18 23:32:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-18 23:32:58 +0200 |
commit | ee6ddb9ab0fca97748b113cfdeef5f67268a1b15 (patch) | |
tree | 2c7cc77f9f55fd1ea8fe8550cecb6802a7d16e84 /build/WebpackSPDXPlugin.js | |
parent | a3587175f17281790281b5bca1e5d7ca057e2a67 (diff) | |
parent | ef8937d45a468fe564bc98d86a6bea0fe42c4323 (diff) | |
download | nextcloud-server-ee6ddb9ab0fca97748b113cfdeef5f67268a1b15.tar.gz nextcloud-server-ee6ddb9ab0fca97748b113cfdeef5f67268a1b15.zip |
Merge pull request #45952 from nextcloud/fix/noid/spdxLicenseIdentifier
Fix license information extracted from built assets
Diffstat (limited to 'build/WebpackSPDXPlugin.js')
-rw-r--r-- | build/WebpackSPDXPlugin.js | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/build/WebpackSPDXPlugin.js b/build/WebpackSPDXPlugin.js index e9eeccca0f1..eeb338c032c 100644 --- a/build/WebpackSPDXPlugin.js +++ b/build/WebpackSPDXPlugin.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict' /** * Party inspired by https://github.com/FormidableLabs/webpack-stats-plugin @@ -13,6 +13,7 @@ const path = require('node:path') const webpack = require('webpack') class WebpackSPDXPlugin { + #options /** @@ -24,16 +25,16 @@ class WebpackSPDXPlugin { } apply(compiler) { - compiler.hooks.thisCompilation.tap("spdx-plugin", (compilation) => { + compiler.hooks.thisCompilation.tap('spdx-plugin', (compilation) => { // `processAssets` is one of the last hooks before frozen assets. // We choose `PROCESS_ASSETS_STAGE_REPORT` which is the last possible // stage after which to emit. compilation.hooks.processAssets.tapPromise( { - name: "spdx-plugin", - stage: compilation.constructor.PROCESS_ASSETS_STAGE_REPORT + name: 'spdx-plugin', + stage: compilation.constructor.PROCESS_ASSETS_STAGE_REPORT, }, - () => this.emitLicenses(compilation) + () => this.emitLicenses(compilation), ) }) } @@ -64,13 +65,11 @@ class WebpackSPDXPlugin { } /** - * - * @param {webpack.Compilation} compilation - * @param {*} callback - * @returns + * Emit licenses found in compilation to '.license' files + * @param {webpack.Compilation} compilation Webpack compilation object + * @param {*} callback Callback for old webpack versions */ async emitLicenses(compilation, callback) { - const moduleNames = (module) => module.modules?.map(moduleNames) ?? [module.name] const logger = compilation.getLogger('spdx-plugin') // cache the node packages const packageInformation = new Map() @@ -93,7 +92,7 @@ class WebpackSPDXPlugin { /** @type {Set<webpack.Module>} */ const modules = new Set() /** - * @param {webpack.Module} module + * @param {webpack.Module} module */ const addModule = (module) => { if (module && !modules.has(module)) { @@ -161,7 +160,7 @@ class WebpackSPDXPlugin { // Get the information from the package const { author: packageAuthor, name, version, license: packageLicense, licenses } = JSON.parse(await fs.readFile(pkg)) // Handle legacy packages - let license = !packageLicense && licenses + let license = !packageLicense && licenses ? licenses.map((entry) => entry.type ?? entry).join(' OR ') : packageLicense if (license?.includes(' ') && !license?.startsWith('(')) { @@ -196,10 +195,15 @@ class WebpackSPDXPlugin { } licenses.add(license || 'unknown') authors.add(pkg.author) - output += `\n- ${pkg.name}\n\t- version: ${pkg.version}\n\t- license: ${license}` + output += `- ${pkg.name}\n\t- version: ${pkg.version}\n\t- license: ${license}\n` + } + output = `\n\n${output}` + for (const author of [...authors].sort()) { + output = `SPDX-FileCopyrightText: ${author}\n${output}` + } + for (const license of [...licenses].sort()) { + output = `SPDX-License-Identifier: ${license}\n${output}` } - output += `\n\nSPDX-License-Identifier: ${[...licenses].sort().join(' AND ')}\n` - output += [...authors].sort().map((author) => `SPDX-FileCopyrightText: ${author}`).join('\n'); compilation.emitAsset( asset.split('?', 2)[0] + '.license', @@ -208,9 +212,10 @@ class WebpackSPDXPlugin { } if (callback) { - return void callback() + return callback() } } + } -module.exports = WebpackSPDXPlugin; +module.exports = WebpackSPDXPlugin |