|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import { WasmPackage } from "@polywrap/wasm-js"; |
| 4 | + |
| 5 | +async function main() { |
| 6 | + |
| 7 | + const embedsDir = path.join(__dirname, "../src/embeds"); |
| 8 | + const embedsDirents = fs.readdirSync(embedsDir, { withFileTypes: true }); |
| 9 | + |
| 10 | + const wrapperDirs: string[] = []; |
| 11 | + |
| 12 | + for (const dirent of embedsDirents) { |
| 13 | + if (dirent.isDirectory()) { |
| 14 | + wrapperDirs.push(path.join(embedsDir, dirent.name)); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + for (const wrapperDir of wrapperDirs) { |
| 19 | + const wasmBytes = fs.readFileSync( |
| 20 | + path.join(wrapperDir, "wrap.wasm") |
| 21 | + ); |
| 22 | + const infoBytes = fs.readFileSync( |
| 23 | + path.join(wrapperDir, "wrap.info") |
| 24 | + ); |
| 25 | + |
| 26 | + try { |
| 27 | + // Make sure we can load the wasm module |
| 28 | + const tryLoad = WasmPackage.from( |
| 29 | + infoBytes, |
| 30 | + wasmBytes |
| 31 | + ); |
| 32 | + const result = await tryLoad.getManifest(); |
| 33 | + if (!result.ok) throw result.error; |
| 34 | + } catch (err) { |
| 35 | + throw Error(`Unable to load wrapper at ${wrapperDir}`); |
| 36 | + } |
| 37 | + |
| 38 | + fs.writeFileSync( |
| 39 | + path.join(wrapperDir, "wrap.ts"), |
| 40 | +`// NOTE: This file is auto-generated, do not modify by hand! |
| 41 | +// See: ./scripts/embed-wrappers.ts |
| 42 | +import { WasmPackage } from "@polywrap/wasm-js"; |
| 43 | +import toUint8Array from "base64-to-uint8array"; |
| 44 | +
|
| 45 | +const wrap_wasm = toUint8Array( |
| 46 | + "${wasmBytes.toString("base64")}" |
| 47 | +); |
| 48 | +
|
| 49 | +const wrap_info = toUint8Array( |
| 50 | + "${infoBytes.toString("base64")}" |
| 51 | +); |
| 52 | +
|
| 53 | +export const wasmPackage = WasmPackage.from( |
| 54 | + wrap_info, |
| 55 | + wrap_wasm |
| 56 | +); |
| 57 | +` |
| 58 | + ); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +main() |
| 63 | + .then(() => { |
| 64 | + process.exit(); |
| 65 | + }) |
| 66 | + .catch((err) => { |
| 67 | + console.error(err); |
| 68 | + process.abort(); |
| 69 | + }); |
0 commit comments