|
| 1 | +import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import path from "node:path"; |
| 4 | +import process from "node:process"; |
| 5 | + |
| 6 | +const root = process.cwd(); |
| 7 | +const frontendDir = path.join(root, "frontend"); |
| 8 | +const backendDir = path.join(root, "backend"); |
| 9 | +const reportDir = path.join(root, "reports", "licenses"); |
| 10 | +const npmLicenseTool = "[email protected]"; |
| 11 | + |
| 12 | +function resolvePythonCommand(workingDir) { |
| 13 | + const localPython = |
| 14 | + process.platform === "win32" |
| 15 | + ? path.join(workingDir, ".venv", "Scripts", "python.exe") |
| 16 | + : path.join(workingDir, ".venv", "bin", "python"); |
| 17 | + |
| 18 | + return existsSync(localPython) ? localPython : "python"; |
| 19 | +} |
| 20 | + |
| 21 | +function runAndCapture(command, args, cwd) { |
| 22 | + const result = spawnSync(command, args, { |
| 23 | + cwd, |
| 24 | + encoding: "utf8", |
| 25 | + stdio: "pipe", |
| 26 | + shell: process.platform === "win32", |
| 27 | + }); |
| 28 | + |
| 29 | + if (result.error) { |
| 30 | + throw result.error; |
| 31 | + } |
| 32 | + |
| 33 | + if (result.status !== 0) { |
| 34 | + const details = [result.stdout, result.stderr].filter(Boolean).join("\n").trim(); |
| 35 | + throw new Error( |
| 36 | + details |
| 37 | + ? `Command failed: ${command} ${args.join(" ")}\n${details}` |
| 38 | + : `Command failed: ${command} ${args.join(" ")}`, |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + return result.stdout; |
| 43 | +} |
| 44 | + |
| 45 | +function writeReport(filename, content) { |
| 46 | + const outputPath = path.join(reportDir, filename); |
| 47 | + const normalized = content.endsWith("\n") ? content : `${content}\n`; |
| 48 | + writeFileSync(outputPath, normalized, "utf8"); |
| 49 | +} |
| 50 | + |
| 51 | +const backendPython = resolvePythonCommand(backendDir); |
| 52 | + |
| 53 | +rmSync(reportDir, { recursive: true, force: true }); |
| 54 | +mkdirSync(reportDir, { recursive: true }); |
| 55 | + |
| 56 | +writeReport( |
| 57 | + "root-npm.json", |
| 58 | + runAndCapture( |
| 59 | + "npx", |
| 60 | + [ |
| 61 | + "--yes", |
| 62 | + npmLicenseTool, |
| 63 | + "--json", |
| 64 | + "--relativeLicensePath", |
| 65 | + "--relativeModulePath", |
| 66 | + ], |
| 67 | + root, |
| 68 | + ), |
| 69 | +); |
| 70 | + |
| 71 | +writeReport( |
| 72 | + "root-npm-summary.txt", |
| 73 | + runAndCapture("npx", ["--yes", npmLicenseTool, "--summary"], root), |
| 74 | +); |
| 75 | + |
| 76 | +writeReport( |
| 77 | + "frontend-npm.json", |
| 78 | + runAndCapture( |
| 79 | + "npx", |
| 80 | + [ |
| 81 | + "--yes", |
| 82 | + npmLicenseTool, |
| 83 | + "--json", |
| 84 | + "--relativeLicensePath", |
| 85 | + "--relativeModulePath", |
| 86 | + ], |
| 87 | + frontendDir, |
| 88 | + ), |
| 89 | +); |
| 90 | + |
| 91 | +writeReport( |
| 92 | + "frontend-npm-summary.txt", |
| 93 | + runAndCapture("npx", ["--yes", npmLicenseTool, "--summary"], frontendDir), |
| 94 | +); |
| 95 | + |
| 96 | +writeReport( |
| 97 | + "backend-python.json", |
| 98 | + runAndCapture( |
| 99 | + backendPython, |
| 100 | + ["-m", "piplicenses", "--from=mixed", "--with-urls", "--format=json"], |
| 101 | + backendDir, |
| 102 | + ), |
| 103 | +); |
| 104 | + |
| 105 | +writeReport( |
| 106 | + "backend-python-summary.txt", |
| 107 | + runAndCapture( |
| 108 | + backendPython, |
| 109 | + ["-m", "piplicenses", "--from=mixed", "--summary", "--order=license"], |
| 110 | + backendDir, |
| 111 | + ), |
| 112 | +); |
| 113 | + |
| 114 | +writeReport( |
| 115 | + "README.md", |
| 116 | + [ |
| 117 | + "# License Reports", |
| 118 | + "", |
| 119 | + `Generated from \`${path.basename(root)}\` on ${new Date().toISOString()}.`, |
| 120 | + "", |
| 121 | + "Files:", |
| 122 | + "", |
| 123 | + "- `root-npm.json`: root workspace npm dependency license inventory.", |
| 124 | + "- `root-npm-summary.txt`: aggregated license counts for the root workspace.", |
| 125 | + "- `frontend-npm.json`: frontend workspace npm dependency license inventory.", |
| 126 | + "- `frontend-npm-summary.txt`: aggregated license counts for the frontend workspace.", |
| 127 | + "- `backend-python.json`: backend Python dependency license inventory.", |
| 128 | + "- `backend-python-summary.txt`: aggregated license counts for the backend Python environment.", |
| 129 | + "", |
| 130 | + "These reports are generated artifacts and should not be committed.", |
| 131 | + ].join("\n"), |
| 132 | +); |
| 133 | + |
| 134 | +console.log(`Created license reports in ${path.relative(root, reportDir)}`); |
0 commit comments