| 1 | #!/usr/bin/env node |
| 2 | import { cpSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs"; |
| 3 | import { execFileSync } from "node:child_process"; |
| 4 | import { resolve, join } from "node:path"; |
| 5 | |
| 6 | |
| 7 | const target = process.argv[2]; |
| 8 | if (!target) throw new Error("Usage: node scripts/collect-app-licenses.mjs <output-directory>"); |
| 9 | const root = resolve(import.meta.dirname, ".."); |
| 10 | mkdirSync(target, { recursive: true }); |
| 11 | for (const file of ["LICENSE", "NOTICE", "THIRD_PARTY_LICENSES"]) { |
| 12 | cpSync(join(root, file), join(target, file)); |
| 13 | } |
| 14 | cpSync(join(root, "internal/exitpolicy/data"), join(target, "exit-country-data"), { recursive: true }); |
| 15 | const lock = JSON.parse(readFileSync(join(root, "ui/package-lock.json"), "utf8")); |
| 16 | const notices = []; |
| 17 | for (const [path, entry] of Object.entries(lock.packages)) { |
| 18 | if (!path || entry.dev) continue; |
| 19 | const source = join(root, "ui", path); |
| 20 | const name = JSON.parse(readFileSync(join(source, "package.json"), "utf8")).name; |
| 21 | const dest = join(target, "npm", name); |
| 22 | const files = readdirSync(source).filter((file) => /^(licen[sc]e|copying|notice)(\.|$)/i.test(file) && statSync(join(source, file)).isFile()); |
| 23 | if (!files.length) throw new Error(`Missing license for ${name}@${entry.version}`); |
| 24 | mkdirSync(dest, { recursive: true }); |
| 25 | for (const file of files) cpSync(join(source, file), join(dest, file)); |
| 26 | notices.push(`${name}@${entry.version}: ${entry.license || "see license files"}`); |
| 27 | } |
| 28 | writeFileSync(join(target, "npm", "NOTICE.txt"), `${notices.join("\n")}\n`); |
| 29 | const novnc = join(root, "ui/vendor/novnc"); |
| 30 | for (const file of ["LICENSE.txt", "AUTHORS", "NOTICE.cage", "docs", "vendor/pako/LICENSE"]) { |
| 31 | mkdirSync(join(target, "novnc", file.includes("/") ? file.slice(0, file.lastIndexOf("/")) : ""), { recursive: true }); |
| 32 | cpSync(join(novnc, file), join(target, "novnc", file), { recursive: true }); |
| 33 | } |
| 34 | |
| 35 | cpSync(join(root, "ui/.generated/novnc"), join(target, "novnc-source"), { recursive: true }); |
| 36 | |
| 37 | |
| 38 | const tailnetModules = new Set(); |
| 39 | for (const [os, arch] of [["linux", "amd64"], ["darwin", "arm64"], ["windows", "amd64"]]) { |
| 40 | const dirs = execFileSync("go", ["list", "-deps", "-f", "{{if .Module}}{{.Module.Dir}}{{end}}", "."], { |
| 41 | cwd: join(root, "tools/tailnet"), env: { ...process.env, GOOS: os, GOARCH: arch, CGO_ENABLED: "0", GOFLAGS: "" }, encoding: "utf8", |
| 42 | }); |
| 43 | for (const dir of dirs.split("\n").filter(Boolean)) if (resolve(dir) !== join(root, "tools/tailnet")) tailnetModules.add(dir); |
| 44 | } |
| 45 | const tailnetNotices = []; |
| 46 | for (const dir of tailnetModules) { |
| 47 | const normalized = dir.replaceAll("\\", "/"); |
| 48 | const name = normalized.slice(normalized.lastIndexOf("/pkg/mod/") + 9); |
| 49 | const dest = join(target, "tailnet-go", name); |
| 50 | const licenses = readdirSync(dir).filter(file => /^(licen[sc]e|copying|notice|patents|authors)(\.|$)/i.test(file) && statSync(join(dir, file)).isFile()); |
| 51 | if (!licenses.length) throw Error(`Missing guest dependency license: ${name}`); |
| 52 | mkdirSync(dest, { recursive: true }); |
| 53 | for (const file of licenses) cpSync(join(dir, file), join(dest, file)); |
| 54 | tailnetNotices.push(name); |
| 55 | } |
| 56 | writeFileSync(join(target, "tailnet-go", "NOTICE.txt"), tailnetNotices.sort().join("\n") + "\n"); |