| 15 | const OUTPUT_DEFAULT = "oss-license-summary.json"; |
| 16 | |
| 17 | function main() { |
| 18 | const args = process.argv.slice(2); |
| 19 | const inputIdx = args.indexOf("--input"); |
| 20 | const outputIdx = args.indexOf("--output"); |
| 21 | const inputFile = inputIdx !== -1 && args[inputIdx + 1] ? args[inputIdx + 1] : INPUT_DEFAULT; |
| 22 | const outputFile = outputIdx !== -1 && args[outputIdx + 1] ? args[outputIdx + 1] : OUTPUT_DEFAULT; |
| 23 | |
| 24 | const inputPath = path.join(process.cwd(), inputFile); |
| 25 | if (!fs.existsSync(inputPath)) { |
| 26 | console.error(`${inputFile} not found. Run \`node scripts/fetchLicenses.mjs\` first.`); |
| 27 | process.exit(1); |
| 28 | } |
| 29 | |
| 30 | const data = JSON.parse(fs.readFileSync(inputPath, "utf-8")); |
| 31 | const packages = data.packages || []; |
| 32 | |
| 33 | // Count by license |
| 34 | const licenseCounts = {}; |
| 35 | for (const pkg of packages) { |
| 36 | const license = typeof pkg.license === "object" |
| 37 | ? JSON.stringify(pkg.license) |
| 38 | : (pkg.license || "UNKNOWN"); |
| 39 | licenseCounts[license] = (licenseCounts[license] || 0) + 1; |
| 40 | } |
| 41 | |
| 42 | // Sort descending by count |
| 43 | const sorted = Object.entries(licenseCounts) |
| 44 | .sort((a, b) => b[1] - a[1]) |
| 45 | .map(([license, count]) => ({ license, count })); |
| 46 | |
| 47 | const summary = { |
| 48 | generatedAt: new Date().toISOString(), |
| 49 | sourcedFrom: inputFile, |
| 50 | totalPackages: packages.length, |
| 51 | totalLicenseTypes: sorted.length, |
| 52 | licenses: sorted, |
| 53 | }; |
| 54 | |
| 55 | const outputPath = path.join(process.cwd(), outputFile); |
| 56 | fs.writeFileSync(outputPath, JSON.stringify(summary, null, 2) + "\n"); |
| 57 | |
| 58 | console.log(`License summary (${packages.length} packages, ${sorted.length} license types):\n`); |
| 59 | for (const { license, count } of sorted) { |
| 60 | console.log(` ${count} ${license}`); |
| 61 | } |
| 62 | console.log(`\nWritten to ${outputFile}`); |
| 63 | } |
| 64 | |
| 65 | main(); |