Skip to content

Commit 48b4496

Browse files
committed
Print Ferric banners into generated files
1 parent 9578c63 commit 48b4496

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

packages/ferric/src/banner.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ const LINES = [
1010
"╰─────────────────────────╯",
1111
];
1212

13+
export function getBlockComment() {
14+
return (
15+
"/**\n" +
16+
["This file was generated by", ...LINES, "Powered by napi.rs"]
17+
.map((line) => ` * ${line}`)
18+
.join("\n") +
19+
"\n */"
20+
);
21+
}
22+
1323
export function printBanner() {
1424
console.log(
1525
LINES.map((line, lineNumber, lines) => {

packages/ferric/src/build.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
filterTargetsByPlatform,
3131
} from "./targets.js";
3232
import { generateTypeScriptDeclarations } from "./napi-rs.js";
33+
import { getBlockComment } from "./banner.js";
3334

3435
type EntrypointOptions = {
3536
outputPath: string;
@@ -41,7 +42,11 @@ async function generateEntrypoint({
4142
}: EntrypointOptions) {
4243
await fs.promises.writeFile(
4344
outputPath,
44-
"module.exports = require('./" + libraryName + ".node');",
45+
[
46+
"/* eslint-disable */",
47+
getBlockComment(),
48+
`module.exports = require('./${libraryName}.node');`,
49+
].join("\n\n") + "\n",
4550
"utf8"
4651
);
4752
}

packages/ferric/src/napi-rs.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import assert from "node:assert/strict";
12
import fs from "node:fs";
23
import path from "node:path";
34

45
import { spawn } from "bufout";
6+
import { getBlockComment } from "./banner.js";
57

68
const PACKAGE_ROOT = path.join(import.meta.dirname, "..");
79

@@ -29,6 +31,7 @@ export async function generateTypeScriptDeclarations({
2931
const tempPath = fs.realpathSync(
3032
fs.mkdtempSync(path.join(PACKAGE_ROOT, "dts-tmp-"))
3133
);
34+
const finalOutputPath = path.join(outputPath, outputFilename);
3235
try {
3336
// Write a dummy package.json file to avoid errors from napi-rs
3437
await fs.promises.writeFile(
@@ -56,11 +59,22 @@ export async function generateTypeScriptDeclarations({
5659
cwd: tempPath,
5760
}
5861
);
59-
// Copy out the generated TypeScript declarations
60-
await fs.promises.copyFile(
61-
path.join(tempPath, outputFilename),
62-
path.join(outputPath, outputFilename)
62+
// Override the banner
63+
const tempOutputPath = path.join(tempPath, outputFilename);
64+
assert(
65+
fs.existsSync(tempOutputPath),
66+
`Expected napi.rs to emit ${tempOutputPath}`
67+
);
68+
const contents = await fs.promises.readFile(tempOutputPath, "utf8");
69+
const patchedContents = contents.replace(
70+
"/* auto-generated by NAPI-RS */",
71+
getBlockComment()
6372
);
73+
// Copy out the generated TypeScript declarations
74+
await fs.promises.writeFile(finalOutputPath, patchedContents, {
75+
encoding: "utf8",
76+
});
77+
return finalOutputPath;
6478
} finally {
6579
await fs.promises.rm(tempPath, { recursive: true, force: true });
6680
}

0 commit comments

Comments
 (0)