|  | 
|  | 1 | +import assert from "node:assert/strict"; | 
| 1 | 2 | import fs from "node:fs"; | 
| 2 | 3 | import path from "node:path"; | 
| 3 | 4 | 
 | 
| 4 | 5 | import { spawn } from "bufout"; | 
|  | 6 | +import { getBlockComment } from "./banner.js"; | 
| 5 | 7 | 
 | 
| 6 | 8 | const PACKAGE_ROOT = path.join(import.meta.dirname, ".."); | 
| 7 | 9 | 
 | 
| @@ -29,6 +31,7 @@ export async function generateTypeScriptDeclarations({ | 
| 29 | 31 |   const tempPath = fs.realpathSync( | 
| 30 | 32 |     fs.mkdtempSync(path.join(PACKAGE_ROOT, "dts-tmp-")) | 
| 31 | 33 |   ); | 
|  | 34 | +  const finalOutputPath = path.join(outputPath, outputFilename); | 
| 32 | 35 |   try { | 
| 33 | 36 |     // Write a dummy package.json file to avoid errors from napi-rs | 
| 34 | 37 |     await fs.promises.writeFile( | 
| @@ -56,11 +59,22 @@ export async function generateTypeScriptDeclarations({ | 
| 56 | 59 |         cwd: tempPath, | 
| 57 | 60 |       } | 
| 58 | 61 |     ); | 
| 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() | 
| 63 | 72 |     ); | 
|  | 73 | +    // Copy out the generated TypeScript declarations | 
|  | 74 | +    await fs.promises.writeFile(finalOutputPath, patchedContents, { | 
|  | 75 | +      encoding: "utf8", | 
|  | 76 | +    }); | 
|  | 77 | +    return finalOutputPath; | 
| 64 | 78 |   } finally { | 
| 65 | 79 |     await fs.promises.rm(tempPath, { recursive: true, force: true }); | 
| 66 | 80 |   } | 
|  | 
0 commit comments