Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/ferric/src/banner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ const LINES = [
"╰─────────────────────────╯",
];

export function getBlockComment() {
return (
"/**\n" +
["This file was generated by", ...LINES, "Powered by napi.rs"]
.map((line) => ` * ${line}`)
.join("\n") +
"\n */"
);
}

export function printBanner() {
console.log(
LINES.map((line, lineNumber, lines) => {
Expand Down
7 changes: 6 additions & 1 deletion packages/ferric/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
filterTargetsByPlatform,
} from "./targets.js";
import { generateTypeScriptDeclarations } from "./napi-rs.js";
import { getBlockComment } from "./banner.js";

type EntrypointOptions = {
outputPath: string;
Expand All @@ -41,7 +42,11 @@ async function generateEntrypoint({
}: EntrypointOptions) {
await fs.promises.writeFile(
outputPath,
"module.exports = require('./" + libraryName + ".node');",
[
"/* eslint-disable */",
getBlockComment(),
`module.exports = require('./${libraryName}.node');`,
].join("\n\n") + "\n",
"utf8"
);
}
Expand Down
22 changes: 18 additions & 4 deletions packages/ferric/src/napi-rs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";

import { spawn } from "bufout";
import { getBlockComment } from "./banner.js";

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

Expand Down Expand Up @@ -29,6 +31,7 @@ export async function generateTypeScriptDeclarations({
const tempPath = fs.realpathSync(
fs.mkdtempSync(path.join(PACKAGE_ROOT, "dts-tmp-"))
);
const finalOutputPath = path.join(outputPath, outputFilename);
try {
// Write a dummy package.json file to avoid errors from napi-rs
await fs.promises.writeFile(
Expand Down Expand Up @@ -56,11 +59,22 @@ export async function generateTypeScriptDeclarations({
cwd: tempPath,
}
);
// Copy out the generated TypeScript declarations
await fs.promises.copyFile(
path.join(tempPath, outputFilename),
path.join(outputPath, outputFilename)
// Override the banner
const tempOutputPath = path.join(tempPath, outputFilename);
assert(
fs.existsSync(tempOutputPath),
`Expected napi.rs to emit ${tempOutputPath}`
);
const contents = await fs.promises.readFile(tempOutputPath, "utf8");
const patchedContents = contents.replace(
"/* auto-generated by NAPI-RS */",
getBlockComment()
);
// Copy out the generated TypeScript declarations
await fs.promises.writeFile(finalOutputPath, patchedContents, {
encoding: "utf8",
});
return finalOutputPath;
} finally {
await fs.promises.rm(tempPath, { recursive: true, force: true });
}
Expand Down