48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
"use strict";
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
// Patch fetch before loading the module so the WASM loader never reaches it
|
|
const wasmPath = path.join(
|
|
path.dirname(require.resolve("@pybricks/mpy-cross-v6/build/mpy-cross-v6.js")),
|
|
"mpy-cross-v6.wasm",
|
|
);
|
|
const wasmBinary = fs.readFileSync(wasmPath);
|
|
|
|
// Monkey-patch the Emscripten module to inject wasmBinary
|
|
const originalMpyCross = require("@pybricks/mpy-cross-v6/build/mpy-cross-v6");
|
|
const MpyCross = (opts) => originalMpyCross({ ...opts, wasmBinary });
|
|
|
|
function compile(fileName, fileContents, options) {
|
|
return new Promise(function (resolve, reject) {
|
|
try {
|
|
const args = [...(options || []), fileName];
|
|
MpyCross({
|
|
arguments: ["-o", "main.mpy", "__main__.py"],
|
|
inputFileContents: fileContents,
|
|
callback: function (status, mpy, out, err) {
|
|
resolve({ status, mpy, out, err });
|
|
},
|
|
});
|
|
} catch (err) {
|
|
reject(err);
|
|
}
|
|
});
|
|
}
|
|
|
|
const myCode = `
|
|
from pybricks.hubs import PrimeHub
|
|
from pybricks.parameters import Color
|
|
hub = PrimeHub()
|
|
hub.light.on(Color.GREEN)
|
|
`;
|
|
|
|
compile("main.py", myCode, []).then((result) => {
|
|
if (result.status === 0) {
|
|
fs.writeFileSync("main.mpy", result.mpy);
|
|
console.log("Success! Written to main.mpy");
|
|
} else {
|
|
console.error("Compiler error:", result.err);
|
|
}
|
|
});
|