@moonwall/cli
Version:
Testing framework for the Moon family of projects
177 lines • 6.3 kB
JavaScript
import chalk from "chalk";
import path from "node:path";
import { standardRepos } from "../lib/repoDefinitions";
import invariant from "tiny-invariant";
export function parseZombieCmd(launchSpec) {
if (launchSpec) {
return { cmd: launchSpec.configPath };
}
throw new Error(`No ZombieSpec found in config. \n Are you sure your ${chalk.bgWhiteBright.blackBright("moonwall.config.json")} file has the correct "configPath" in zombieSpec?`);
}
function fetchDefaultArgs(binName, additionalRepos = []) {
let defaultArgs;
const repos = [...standardRepos(), ...additionalRepos];
for (const repo of repos) {
const foundBin = repo.binaries.find((bin) => bin.name === binName);
if (foundBin) {
defaultArgs = foundBin.defaultArgs;
break;
}
}
if (!defaultArgs) {
defaultArgs = ["--dev"];
}
return defaultArgs;
}
export class LaunchCommandParser {
args;
cmd;
launch;
launchSpec;
additionalRepos;
launchOverrides;
constructor(options) {
const { launchSpec, additionalRepos, launchOverrides } = options;
this.launchSpec = launchSpec;
this.additionalRepos = additionalRepos;
this.launchOverrides = launchOverrides;
this.launch = !launchSpec.running ? true : launchSpec.running;
this.cmd = launchSpec.binPath;
this.args = launchSpec.options
? [...launchSpec.options]
: fetchDefaultArgs(path.basename(launchSpec.binPath), additionalRepos);
}
overrideArg(newArg) {
const newArgKey = newArg.split("=")[0];
const existingIndex = this.args.findIndex((arg) => arg.startsWith(`${newArgKey}=`));
if (existingIndex !== -1) {
this.args[existingIndex] = newArg;
}
else {
this.args.push(newArg);
}
}
withPorts() {
if (this.launchSpec.ports) {
const ports = this.launchSpec.ports;
if (ports.p2pPort) {
this.overrideArg(`--port=${ports.p2pPort}`);
}
if (ports.wsPort) {
this.overrideArg(`--ws-port=${ports.wsPort}`);
}
if (ports.rpcPort) {
this.overrideArg(`--rpc-port=${ports.rpcPort}`);
}
}
else {
const freePort = getFreePort().toString();
process.env.MOONWALL_RPC_PORT = freePort;
if (this.launchSpec.newRpcBehaviour) {
this.overrideArg(`--rpc-port=${freePort}`);
}
else {
this.overrideArg(`--ws-port=${freePort}`);
}
}
return this;
}
withDefaultForkConfig() {
const forkOptions = this.launchSpec.defaultForkConfig;
if (forkOptions) {
this.applyForkOptions(forkOptions);
}
return this;
}
withLaunchOverrides() {
if (this.launchOverrides?.forkConfig) {
this.applyForkOptions(this.launchOverrides.forkConfig);
}
return this;
}
print() {
console.log(chalk.cyan(`Command to run is: ${chalk.bold(this.cmd)}`));
console.log(chalk.cyan(`Arguments are: ${chalk.bold(this.args.join(" "))}`));
return this;
}
applyForkOptions(forkOptions) {
if (forkOptions.url) {
invariant(forkOptions.url.startsWith("http"), "Fork URL must start with http:// or https://");
this.overrideArg(`--fork-chain-from-rpc=${forkOptions.url}`);
}
if (forkOptions.blockHash) {
this.overrideArg(`--block=${forkOptions.blockHash}`);
}
if (forkOptions.stateOverridePath) {
this.overrideArg(`--fork-state-overrides=${forkOptions.stateOverridePath}`);
}
if (forkOptions.verbose) {
this.overrideArg("-llazy-loading=trace");
}
}
build() {
return {
cmd: this.cmd,
args: this.args,
launch: this.launch,
};
}
static create(options) {
const parser = new LaunchCommandParser(options);
const parsed = parser.withPorts().withDefaultForkConfig().withLaunchOverrides();
if (options.verbose) {
parsed.print();
}
return parsed.build();
}
}
export function parseChopsticksRunCmd(launchSpecs) {
const launch = !launchSpecs[0].running ? true : launchSpecs[0].running;
if (launchSpecs.length === 1) {
const chopsticksCmd = "node";
const chopsticksArgs = [
"node_modules/@acala-network/chopsticks/chopsticks.cjs",
`--config=${launchSpecs[0].configPath}`,
`--addr=${launchSpecs[0].address ?? "127.0.0.1"}`, // use old behaviour by default
];
const mode = launchSpecs[0].buildBlockMode ? launchSpecs[0].buildBlockMode : "manual";
const num = mode === "batch" ? "Batch" : mode === "instant" ? "Instant" : "Manual";
chopsticksArgs.push(`--build-block-mode=${num}`);
if (launchSpecs[0].wsPort) {
chopsticksArgs.push(`--port=${launchSpecs[0].wsPort}`);
}
if (launchSpecs[0].wasmOverride) {
chopsticksArgs.push(`--wasm-override=${launchSpecs[0].wasmOverride}`);
}
if (launchSpecs[0].allowUnresolvedImports) {
chopsticksArgs.push("--allow-unresolved-imports");
}
return {
cmd: chopsticksCmd,
args: chopsticksArgs,
launch,
};
}
const chopsticksCmd = "node";
const chopsticksArgs = ["node_modules/@acala-network/chopsticks/chopsticks.cjs", "xcm"];
for (const spec of launchSpecs) {
const type = spec.type ? spec.type : "parachain";
switch (type) {
case "parachain":
chopsticksArgs.push(`--parachain=${spec.configPath}`);
break;
case "relaychain":
chopsticksArgs.push(`--relaychain=${spec.configPath}`);
}
}
return {
cmd: chopsticksCmd,
args: chopsticksArgs,
launch,
};
}
export const getFreePort = () => {
const notionalPort = 10000 + Number(process.env.VITEST_POOL_ID || 1) * 100;
return notionalPort;
};
//# sourceMappingURL=commandParsers.js.map