|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import type { Metafile } from 'esbuild'; |
| 10 | +import { readFile } from 'node:fs/promises'; |
| 11 | +import path from 'node:path'; |
| 12 | + |
| 13 | +/** |
| 14 | + * The path segment used to signify that a file is part of a package. |
| 15 | + */ |
| 16 | +const NODE_MODULE_SEGMENT = 'node_modules'; |
| 17 | + |
| 18 | +/** |
| 19 | + * String constant for the NPM recommended custom license wording. |
| 20 | + * |
| 21 | + * See: https://docs.npmjs.com/cli/v9/configuring-npm/package-json#license |
| 22 | + * |
| 23 | + * Example: |
| 24 | + * ``` |
| 25 | + * { |
| 26 | + * "license" : "SEE LICENSE IN <filename>" |
| 27 | + * } |
| 28 | + * ``` |
| 29 | + */ |
| 30 | +const CUSTOM_LICENSE_TEXT = 'SEE LICENSE IN '; |
| 31 | + |
| 32 | +/** |
| 33 | + * A list of commonly named license files found within packages. |
| 34 | + */ |
| 35 | +const LICENSE_FILES = ['LICENSE', 'LICENSE.txt', 'LICENSE.md']; |
| 36 | + |
| 37 | +/** |
| 38 | + * Header text that will be added to the top of the output license extraction file. |
| 39 | + */ |
| 40 | +const EXTRACTION_FILE_HEADER = ''; |
| 41 | + |
| 42 | +/** |
| 43 | + * The package entry separator to use within the output license extraction file. |
| 44 | + */ |
| 45 | +const EXTRACTION_FILE_SEPARATOR = '-'.repeat(80) + '\n'; |
| 46 | + |
| 47 | +/** |
| 48 | + * Extracts license information for each node module package included in the output |
| 49 | + * files of the built code. This includes JavaScript and CSS output files. The esbuild |
| 50 | + * metafile generated during the bundling steps is used as the source of information |
| 51 | + * regarding what input files where included and where they are located. A path segment |
| 52 | + * of `node_modules` is used to indicate that a file belongs to a package and its license |
| 53 | + * should be include in the output licenses file. |
| 54 | + * |
| 55 | + * The package name and license field are extracted from the `package.json` file for the |
| 56 | + * package. If a license file (e.g., `LICENSE`) is present in the root of the package, it |
| 57 | + * will also be included in the output licenses file. |
| 58 | + * |
| 59 | + * @param metafile An esbuild metafile object. |
| 60 | + * @param rootDirectory The root directory of the workspace. |
| 61 | + * @returns A string containing the content of the output licenses file. |
| 62 | + */ |
| 63 | +export async function extractLicenses(metafile: Metafile, rootDirectory: string) { |
| 64 | + let extractedLicenseContent = `${EXTRACTION_FILE_HEADER}\n${EXTRACTION_FILE_SEPARATOR}`; |
| 65 | + |
| 66 | + const seenPaths = new Set<string>(); |
| 67 | + const seenPackages = new Set<string>(); |
| 68 | + |
| 69 | + for (const entry of Object.values(metafile.outputs)) { |
| 70 | + for (const [inputPath, { bytesInOutput }] of Object.entries(entry.inputs)) { |
| 71 | + // Skip if not included in output |
| 72 | + if (bytesInOutput <= 0) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + // Skip already processed paths |
| 77 | + if (seenPaths.has(inputPath)) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + seenPaths.add(inputPath); |
| 81 | + |
| 82 | + // Skip non-package paths |
| 83 | + if (!inputPath.includes(NODE_MODULE_SEGMENT)) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + // Extract the package name from the path |
| 88 | + let baseDirectory = path.join(rootDirectory, inputPath); |
| 89 | + let nameOrScope, nameOrFile; |
| 90 | + let found = false; |
| 91 | + while (baseDirectory !== path.dirname(baseDirectory)) { |
| 92 | + const segment = path.basename(baseDirectory); |
| 93 | + if (segment === NODE_MODULE_SEGMENT) { |
| 94 | + found = true; |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + nameOrFile = nameOrScope; |
| 99 | + nameOrScope = segment; |
| 100 | + baseDirectory = path.dirname(baseDirectory); |
| 101 | + } |
| 102 | + |
| 103 | + // Skip non-package path edge cases that are not caught in the includes check above |
| 104 | + if (!found || !nameOrScope) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + const packageName = nameOrScope.startsWith('@') |
| 109 | + ? `${nameOrScope}/${nameOrFile}` |
| 110 | + : nameOrScope; |
| 111 | + const packageDirectory = path.join(baseDirectory, packageName); |
| 112 | + |
| 113 | + // Load the package's metadata to find the package's name, version, and license type |
| 114 | + const packageJsonPath = path.join(packageDirectory, 'package.json'); |
| 115 | + let packageJson; |
| 116 | + try { |
| 117 | + packageJson = JSON.parse(await readFile(packageJsonPath, 'utf-8')) as { |
| 118 | + name: string; |
| 119 | + version: string; |
| 120 | + // The object form is deprecated and should only be present in old packages |
| 121 | + license?: string | { type: string }; |
| 122 | + }; |
| 123 | + } catch { |
| 124 | + // Invalid package |
| 125 | + continue; |
| 126 | + } |
| 127 | + |
| 128 | + // Skip already processed packages |
| 129 | + const packageId = `${packageName}@${packageJson.version}`; |
| 130 | + if (seenPackages.has(packageId)) { |
| 131 | + continue; |
| 132 | + } |
| 133 | + seenPackages.add(packageId); |
| 134 | + |
| 135 | + // Attempt to find license text inside package |
| 136 | + let licenseText = ''; |
| 137 | + if ( |
| 138 | + typeof packageJson.license === 'string' && |
| 139 | + packageJson.license.toLowerCase().startsWith(CUSTOM_LICENSE_TEXT) |
| 140 | + ) { |
| 141 | + // Attempt to load the package's custom license |
| 142 | + let customLicensePath; |
| 143 | + const customLicenseFile = path.normalize( |
| 144 | + packageJson.license.slice(CUSTOM_LICENSE_TEXT.length + 1).trim(), |
| 145 | + ); |
| 146 | + if (customLicenseFile.startsWith('..') || path.isAbsolute(customLicenseFile)) { |
| 147 | + // Path is attempting to access files outside of the package |
| 148 | + // TODO: Issue warning? |
| 149 | + } else { |
| 150 | + customLicensePath = path.join(packageDirectory, customLicenseFile); |
| 151 | + try { |
| 152 | + licenseText = await readFile(customLicensePath, 'utf-8'); |
| 153 | + break; |
| 154 | + } catch {} |
| 155 | + } |
| 156 | + } else { |
| 157 | + // Search for a license file within the root of the package |
| 158 | + for (const potentialLicense of LICENSE_FILES) { |
| 159 | + const packageLicensePath = path.join(packageDirectory, potentialLicense); |
| 160 | + try { |
| 161 | + licenseText = await readFile(packageLicensePath, 'utf-8'); |
| 162 | + break; |
| 163 | + } catch {} |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + // Generate the package's license entry in the output content |
| 168 | + extractedLicenseContent += `Package: ${packageJson.name}\n`; |
| 169 | + extractedLicenseContent += `License: ${JSON.stringify(packageJson.license, null, 2)}\n`; |
| 170 | + extractedLicenseContent += `\n${licenseText}\n`; |
| 171 | + extractedLicenseContent += EXTRACTION_FILE_SEPARATOR; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return extractedLicenseContent; |
| 176 | +} |
0 commit comments