Files
oh-my-pi/packages/natives/scripts/embed-native.ts
T
can1357 2a131d201e feat(natives): fixed binary installs with embedded native addon extraction
- Added automatic extraction of embedded native addon payload to ~/.omp/natives/<version> on first run of compiled binaries.
- Added embed:native build script for generating platform-specific native addon payloads embedded in compiled binaries.
- Enhanced native addon loading to prioritize extracted embedded addon over downloaded versions.
- Improved error messages with platform-specific guidance for compiled vs npm-installed binaries.
- Refactored CI workflow to use reusable build_binary() function for cross-platform binary compilation.
2026-02-01 17:16:53 +01:00

25 lines
1.2 KiB
TypeScript

import * as path from "node:path";
const reset = process.argv.includes("--reset");
const outputPath = path.join(import.meta.dir, "../src/embedded-addon.ts");
const packageJsonPath = path.join(import.meta.dir, "../package.json");
const stubContent = "export interface EmbeddedAddon {\n\tplatform: string;\n\tversion: string;\n\tfilePath: string;\n}\n\nexport const embeddedAddon: EmbeddedAddon | null = null;\n";
if (reset) {
await Bun.write(outputPath, stubContent);
process.exit(0);
}
const targetPlatform = process.env.TARGET_PLATFORM || process.platform;
const targetArch = process.env.TARGET_ARCH || process.arch;
const platformTag = `${targetPlatform}-${targetArch}`;
const addonFilename = `pi_natives.${platformTag}.node`;
const packageJson = (await Bun.file(packageJsonPath).json()) as { version: string };
const content = `import addonPath from ${JSON.stringify("../native/" + addonFilename)} with { type: \"file\" };\n\nexport interface EmbeddedAddon {\n\tplatform: string;\n\tversion: string;\n\tfilePath: string;\n}\n\nexport const embeddedAddon: EmbeddedAddon | null = {\n\tplatform: ${JSON.stringify(platformTag)},\n\tversion: ${JSON.stringify(packageJson.version)},\n\tfilePath: addonPath,\n};\n`;
await Bun.write(outputPath, content);