zksync-cli
Version:
CLI tool that simplifies the process of developing applications and interacting with the ZKsync network
117 lines • 4.29 kB
JavaScript
import chalk from "chalk";
import { spawn } from "child_process";
import { computeAddress, JsonRpcProvider } from "ethers";
import { Wallet, Provider } from "zksync-ethers";
import { Logger } from "../lib/index.js";
export const optionNameToParam = (input) => {
// "--l1-rpc-url" => "l1RpcUrl"
const parts = input.replace(/^--/, "").split("-");
for (let i = 1; i < parts.length; i++) {
parts[i] = parts[i].charAt(0).toUpperCase() + parts[i].slice(1);
}
return parts.join("");
};
export const getAddressFromPrivateKey = (privateKey) => {
return computeAddress(privateKey.startsWith("0x") ? privateKey : `0x${privateKey}`);
};
export const getL1Provider = (url) => {
return new JsonRpcProvider(url);
};
export const getL2Provider = (rpc) => {
return new Provider(rpc);
};
export const getL2Wallet = (privateKey, l2Provider, l1Provider) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new Wallet(privateKey, l2Provider, l1Provider);
};
export const executeCommand = (command, options = {}) => {
return new Promise((resolve, reject) => {
const [cmd, ...args] = command.split(" ");
const child = spawn(cmd === "npm" ? (/^win/.test(process.platform) ? "npm.cmd" : "npm") : cmd, args, {
stdio: options.silent ? "pipe" : "inherit",
cwd: options.cwd,
});
let output = "";
let errorOutput = "";
if (options.silent) {
child.stdout.on("data", (data) => {
if (!options.silent) {
process.stdout.write(data);
}
output += data.toString();
});
child.stderr.on("data", (data) => {
if (!options.silent) {
process.stderr.write(data);
}
errorOutput += data.toString();
});
}
child.on("close", (code) => {
if (code !== 0) {
reject(new Error(`Command exited with code ${code}: ${errorOutput}`));
}
else {
resolve(output);
}
});
child.on("error", (error) => {
reject(error);
});
});
};
export const hasColor = (text) => {
// eslint-disable-next-line no-control-regex
const colorEscapeCodePattern = /\x1B\[\d+m/g;
return colorEscapeCodePattern.test(text);
};
const findFullCommandName = (cmd) => {
let command = "";
const findCommandBase = (cmd) => {
const commandName = cmd["_name"];
if (commandName) {
command = command ? `${commandName} ${command}` : commandName;
if (cmd.parent) {
findCommandBase(cmd.parent);
}
}
};
findCommandBase(cmd);
return command;
};
export const logFullCommandFromOptions = (options, context, formattingOptions) => {
let command = findFullCommandName(context);
let comparisonCommand = command; // Unescaped command string for comparison purposes
context.options.forEach((option) => {
const optionParamName = optionNameToParam(option.long);
if (!(optionParamName in options))
return;
if (optionParamName === "privateKey")
return;
const value = options[optionParamName];
if (Array.isArray(value) && !value.length)
return;
const optionPart = ` ${option.short || option.long}`;
if (typeof value === "boolean") {
command += optionPart;
comparisonCommand += optionPart;
}
else if (Array.isArray(value)) {
const escapedValue = value.map((v) => `"${v}"`).join(" ");
const unescapedValue = value.join(" ");
command += `${optionPart} ${escapedValue}`;
comparisonCommand += `${optionPart} ${unescapedValue}`;
}
else {
command += `${optionPart} "${value}"`;
comparisonCommand += `${optionPart} ${value}`;
}
});
if (!process.argv.join(" ").endsWith(comparisonCommand)) {
if (formattingOptions?.emptyLine) {
Logger.info("");
}
Logger.info(chalk.gray(`Run this directly: npx ${command}`));
}
};
//# sourceMappingURL=helpers.js.map