From a817394b8ebd627edcd90533b52213599b78becb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Thu, 15 May 2025 07:55:28 +0200 Subject: [PATCH] Print Ferric banners into generated files --- packages/ferric/src/banner.ts | 10 ++++++++++ packages/ferric/src/build.ts | 7 ++++++- packages/ferric/src/napi-rs.ts | 22 ++++++++++++++++++---- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/packages/ferric/src/banner.ts b/packages/ferric/src/banner.ts index 95edab04..c9c136f3 100644 --- a/packages/ferric/src/banner.ts +++ b/packages/ferric/src/banner.ts @@ -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) => { diff --git a/packages/ferric/src/build.ts b/packages/ferric/src/build.ts index 739c049e..6e67b772 100644 --- a/packages/ferric/src/build.ts +++ b/packages/ferric/src/build.ts @@ -30,6 +30,7 @@ import { filterTargetsByPlatform, } from "./targets.js"; import { generateTypeScriptDeclarations } from "./napi-rs.js"; +import { getBlockComment } from "./banner.js"; type EntrypointOptions = { outputPath: string; @@ -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" ); } diff --git a/packages/ferric/src/napi-rs.ts b/packages/ferric/src/napi-rs.ts index 804e38c2..7b3ada01 100644 --- a/packages/ferric/src/napi-rs.ts +++ b/packages/ferric/src/napi-rs.ts @@ -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, ".."); @@ -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( @@ -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 }); }