Skip to content

Commit 7a3c8b0

Browse files
committed
build: update dist/licenses.txt automatically
This was previously done by ncc but esbuild can't do it out of the box. This simple script does pretty much the same as before.
1 parent 06a6b57 commit 7a3c8b0

1 file changed

Lines changed: 53 additions & 2 deletions

File tree

‎esbuild.config.mjs‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,65 @@
11
import * as esbuild from "esbuild";
2+
import { readFileSync, writeFileSync } from "fs";
3+
import { join } from "path";
24

3-
await esbuild.build({
5+
const result = await esbuild.build({
46
entryPoints: ["src/main.ts"],
57
bundle: true,
68
platform: "node",
79
target: "node24",
810
format: "esm",
911
minify: true,
12+
metafile: true,
1013
banner: {
1114
js: "import{createRequire}from'module';const require=createRequire(import.meta.url);",
1215
},
1316
outfile: "dist/index.js",
14-
});
17+
});
18+
19+
// Collect licenses from all bundled node_modules packages
20+
const packages = new Map();
21+
for (const input of Object.keys(result.metafile.inputs)) {
22+
// Find the last node_modules/ segment to handle nested dependencies
23+
const lastIdx = input.lastIndexOf("node_modules/");
24+
if (lastIdx === -1) continue;
25+
const afterNodeModules = input.slice(lastIdx + "node_modules/".length);
26+
const match = afterNodeModules.match(/^(@[^/]+\/[^/]+|[^/]+)/);
27+
if (match) {
28+
const pkg = match[1];
29+
const pkgDir = input.slice(0, lastIdx + "node_modules/".length + match[0].length);
30+
packages.set(pkg, pkgDir);
31+
}
32+
}
33+
34+
let licenseText = "";
35+
for (const [pkg, pkgDir] of [...packages.entries()].sort(([a], [b]) =>
36+
a.localeCompare(b),
37+
)) {
38+
const pkgJson = JSON.parse(
39+
readFileSync(join(pkgDir, "package.json"), "utf8"),
40+
);
41+
42+
const licenseFiles = [
43+
"LICENSE",
44+
"license",
45+
"LICENCE",
46+
"LICENSE.md",
47+
"License.md",
48+
"LICENSE.txt",
49+
"license.txt",
50+
];
51+
let license = "";
52+
for (const f of licenseFiles) {
53+
try {
54+
license = readFileSync(join(pkgDir, f), "utf8");
55+
break;
56+
} catch {
57+
// try next candidate
58+
}
59+
}
60+
61+
licenseText += `${pkg}\n${pkgJson.license || "Unknown"}\n${license}\n\n`;
62+
}
63+
64+
writeFileSync("dist/licenses.txt", licenseText.trimEnd() + "\n");
65+
console.log(`Collected licenses for ${packages.size} bundled packages`);

0 commit comments

Comments
 (0)