Skip to content

Commit

Permalink
fix(build): run cargo using childProcess.spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Thomas committed Oct 9, 2024
1 parent cd54871 commit 1ddae81
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions packages/critters/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "node:fs/promises";
import path from "node:path";
import util from "node:util";
import url from "node:url";
import childProcess from "node:child_process";
import { spawn } from "node:child_process";
import { NapiCli } from "@napi-rs/cli";
import { rimraf } from "rimraf";
import { consola as log } from "consola";
Expand All @@ -22,7 +22,6 @@ const CRATE_PATH = path.resolve(JS_DIR, "../..");
const RUST_OUT_DIR = path.join(JS_DIR, "./pkg");

const cli = new NapiCli();
const exec = util.promisify(childProcess.exec);
const args = util.parseArgs({
options: {
target: {
Expand Down Expand Up @@ -52,10 +51,35 @@ log.success("Build complete!");

// fix types
log.start("Updating bindings...");
await exec("cargo test export_bindings --lib --features typegen", {
cwd: CRATE_PATH,
env: { TS_RS_EXPORT_DIR: RUST_OUT_DIR },
await new Promise<void>((resolve, reject) => {
const cargo = process.env.CARGO ?? "cargo";
const bindingsProcess = spawn(
cargo,
"test export_bindings --lib --features typegen".split(" "),
{
cwd: CRATE_PATH,
env: { TS_RS_EXPORT_DIR: RUST_OUT_DIR },
stdio: "inherit",
},
);

bindingsProcess.once("exit", (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`Bindings generation failed with exit code ${code}`));
}
});

bindingsProcess.once("error", (e) => {
reject(
new Error(`Bindings generation failed with error: ${e.message}`, {
cause: e,
}),
);
});
});

const declarationFile = path.join(RUST_OUT_DIR, "index.d.ts");
const declaration = await fs
.readFile(declarationFile)
Expand Down

0 comments on commit 1ddae81

Please sign in to comment.