import { spawnSync } from "child_process"; import { openZeppelinProcessEnvironment } from "../config/openzeppelin"; import { resolveCliTool } from "./lib/cliTool"; import { validateExecutionId } from "./lib/executionId"; type ArgValue = string | boolean; const WORKFLOW_ARG_KEYS = new Set([ "mode", "target", "step", "from", "executionId", "useLock", "sourceSuperTaskId", "mergeCommit", "parameterOverrides", "existingDeploymentPolicies", "simulationSigner", "releaseKind", ]); const parseArgs = (argv: string[]): Record => { const args: Record = {}; for (let index = 0; index < argv.length; index += 1) { const item = argv[index]; if (!item.startsWith("--")) continue; const equalIndex = item.indexOf("="); if (equalIndex > 2) { args[item.slice(2, equalIndex)] = item.slice(equalIndex + 1); continue; } const key = item.slice(2); const next = argv[index + 1]; if (!next || next.startsWith("--")) { args[key] = true; } else { args[key] = next; index += 1; } } return args; }; const stringArg = ( args: Record, key: string, ): string | undefined => { const value = args[key]; return typeof value === "string" && value.length > 0 ? value : undefined; }; const args = parseArgs(process.argv.slice(2)); if (args.checkcode === true) { const forwardedArgs = process.argv.slice(2).filter((item) => item !== "--checkcode"); const tsNode = resolveCliTool({ root: process.cwd(), binary: "ts-node" }); const result = spawnSync( tsNode.command, [...tsNode.argsPrefix, "scripts/workflow/checkCode.ts", ...forwardedArgs], { env: process.env, stdio: "inherit" }, ); if (result.error) console.error(result.error); process.exit(result.error ? 1 : result.status ?? 1); } if (Object.hasOwn(args, "executionId")) { validateExecutionId(args.executionId); } const network = stringArg(args, "network") || "hardhat"; const task = stringArg(args, "task") || process.env.WORKFLOW_TASK; const workflowArgs = Object.fromEntries( Object.entries(args).filter(([key]) => WORKFLOW_ARG_KEYS.has(key)), ); const hardhatArgs = ["run", "scripts/workflow/run.ts", "--network", network]; if (args["no-compile"] === true) { hardhatArgs.push("--no-compile"); } const manifestEnv = openZeppelinProcessEnvironment({ root: process.cwd(), network, mode: stringArg(args, "mode"), target: stringArg(args, "target"), env: process.env, }); const env = { ...manifestEnv, WORKFLOW_ARGS_JSON: JSON.stringify(workflowArgs), ...(task ? { WORKFLOW_TASK: task } : {}), }; const hardhat = resolveCliTool({ root: process.cwd(), binary: "hardhat" }); const result = spawnSync(hardhat.command, [...hardhat.argsPrefix, ...hardhatArgs], { env, stdio: "inherit", }); if (result.error) { console.error(result.error); process.exitCode = 1; } else { process.exitCode = result.status ?? 1; }