Skip to content

Commit e61eb54

Browse files
committed
Revert "fix: revert "feat: use spawn instead of exec""
This reverts commit 824ed6c.
1 parent efa6fd3 commit e61eb54

File tree

3 files changed

+66
-34
lines changed

3 files changed

+66
-34
lines changed

src/install.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { makeRunCommand } from "./utils";
1+
import { HardhatPluginError } from "hardhat/plugins";
2+
import { makeRunCommand, PLUGIN_NAME } from "./utils";
23

34
const installationSeparator = "hardhat";
45

@@ -9,9 +10,10 @@ async function installNoirup() {
910
if (!fs.existsSync(noirupBinary)) {
1011
const runCommand = makeRunCommand();
1112
console.log("Installing noirup");
12-
await runCommand(
13-
"curl -L https://raw.githubusercontent.com/noir-lang/noirup/refs/heads/main/install | bash",
13+
const installScript = await downloadScript(
14+
"https://raw.githubusercontent.com/noir-lang/noirup/refs/heads/main/install",
1415
);
16+
await runCommand("bash", ["-c", installScript]);
1517
}
1618
return noirupBinary;
1719
}
@@ -33,9 +35,9 @@ export async function installNargo(version: string) {
3335
const nargoBinDir = path.dirname(nargoBinary);
3436
fs.mkdirSync(path.join(nargoBinDir), { recursive: true });
3537
console.log(`Installing nargo@${version} in ${nargoBinDir}`);
36-
await runCommand(
37-
`NARGO_HOME=${path.dirname(nargoBinDir)} ${noirupBinary} -v ${version}`,
38-
);
38+
await runCommand(noirupBinary, ["-v", version], {
39+
env: { NARGO_HOME: path.dirname(nargoBinDir) },
40+
});
3941
}
4042
return nargoBinary;
4143
}
@@ -45,3 +47,11 @@ async function getNargoHome() {
4547
const path = await import("path");
4648
return path.join(os.homedir(), ".nargo");
4749
}
50+
51+
async function downloadScript(url: string) {
52+
const res = await fetch(url);
53+
if (!res.ok) {
54+
throw new HardhatPluginError(PLUGIN_NAME, `Failed to download ${url}`);
55+
}
56+
return await res.text();
57+
}

src/tasks.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ task(TASK_COMPILE, "Compile and generate circuits and contracts").setAction(
2727
const cache = await NoirCache.fromConfig(config);
2828
if ((await cache.haveSourceFilesChanged()) || force) {
2929
console.log("Compiling Noir circuits...");
30-
await runCommand(`${nargoBinary} compile`);
30+
await runCommand(nargoBinary, ["compile"]);
3131
await cache.saveSourceFilesHash();
3232
console.log("Compiled Noir circuits");
3333
}
@@ -81,9 +81,11 @@ task("noir-new", "Create a new Noir package")
8181
const nargoBinary = await installNargo(config.noir.version);
8282
const runCommand = makeRunCommand(config.paths.noir);
8383
fs.mkdirSync(config.paths.noir, { recursive: true });
84-
await runCommand(
85-
`${nargoBinary} new ${args.name} ${args.lib ? "--lib" : ""}`,
86-
);
84+
const cmdArgs = ["new", args.name];
85+
if (args.lib) {
86+
cmdArgs.push("--lib");
87+
}
88+
await runCommand(nargoBinary, cmdArgs);
8789
});
8890

8991
task(

src/utils.ts

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,49 @@
11
import { HardhatPluginError } from "hardhat/plugins";
2+
import type { SpawnOptions } from "node:child_process";
23

34
export const PLUGIN_NAME = "hardhat-plugin-noir";
45

5-
export const makeRunCommand = (cwd?: string) => async (command: string) => {
6-
const { exec } = await import("child_process");
7-
const { promisify } = await import("util");
8-
const execAsync = promisify(exec);
9-
// TODO(security): escape command arguments (use template strings)
10-
try {
11-
const { stdout, stderr } = await execAsync(command, {
12-
cwd,
13-
maxBuffer: Infinity,
14-
});
15-
if (stdout) {
16-
console.log(stdout);
17-
}
18-
if (stderr) {
19-
console.error(stderr);
20-
}
21-
} catch (error) {
22-
console.error(`Error executing command: ${command}`);
23-
console.error((error as any).stderr || (error as any).message); // Log only error messages
24-
throw new HardhatPluginError(
25-
PLUGIN_NAME,
26-
`Error executing command: ${command}`,
6+
export const makeRunCommand =
7+
(cwd?: string) =>
8+
async (
9+
command: string,
10+
args: (string | number)[],
11+
options?: Pick<SpawnOptions, "env">,
12+
) => {
13+
const { spawn } = await import("node:child_process");
14+
15+
const spawned = spawn(
16+
command,
17+
args.map((arg) => arg.toString()),
18+
{ ...options, cwd },
2719
);
28-
}
29-
};
20+
spawned.stdout.on("data", (data) => {
21+
process.stdout.write(data);
22+
});
23+
24+
spawned.stderr.on("data", (data) => {
25+
process.stderr.write(data);
26+
});
27+
28+
return await new Promise<void>((resolve, reject) => {
29+
spawned.on("close", (code: number) => {
30+
if (code !== 0) {
31+
reject(new Error(`Process exited with code ${code}`));
32+
return;
33+
}
34+
35+
resolve();
36+
});
37+
38+
spawned.on("error", (err) => {
39+
reject(
40+
new HardhatPluginError(
41+
PLUGIN_NAME,
42+
`Error executing command \`${
43+
command + " " + args.join(" ")
44+
}\`: ${err.message}`,
45+
),
46+
);
47+
});
48+
});
49+
};

0 commit comments

Comments
 (0)