Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: block explorer module, command improvements #65

Merged
merged 16 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Or you can install the CLI globally with `npm i -g zksync-cli` and run the comma
## πŸ’» Commands

### Local development commands
`zksync-cli dev` - Manage local zkSync development environment. It allows to easily start zkSync stack locally, for example: local Ethereum and zkSync nodes, Wallet and Bridge.
`zksync-cli dev` - Manage local zkSync development environment. It allows to easily start zkSync stack locally, for example: local Ethereum and zkSync nodes, Block Explorer, Wallet and Bridge.

**General:**
- `zksync-cli dev start` - start local development environment (will ask to configure if starting for the first time)
Expand Down
207 changes: 198 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"ethers": "5.7.2",
"inquirer": "^8.1.4",
"winston": "^3.10.0",
"zkcli-dockerized-node": "^1.0.2",
"zkcli-block-explorer": "^1.0.2",
"zkcli-dockerized-node": "^1.0.4",
"zkcli-in-memory-node": "^1.0.3",
"zkcli-portal": "^1.0.1",
"zksync-web3": "^0.14.4"
Expand Down
24 changes: 20 additions & 4 deletions src/commands/dev/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,30 @@ export const cleanModule = async (module: Module) => {
if (!isInstalled) {
return;
}
module.removeDataDir();
await module.clean();
module.removeDataDir();
} catch (error) {
Logger.error(`There was an error while cleaning module "${module.name}":`);
Logger.error(error);
}
};

export const handler = async () => {
export const handler = async (modulePackageNames: string[]) => {
try {
const modules = await configHandler.getConfigModules();
const modules = [];
if (modulePackageNames.length) {
const allModules = await configHandler.getAllModules();
for (const moduleName of modulePackageNames) {
const module = allModules.find((m) => m.package.name === moduleName);
if (!module) {
throw new Error(`Module "${moduleName}" not found`);
}
modules.push(module);
}
} else {
const configModules = await configHandler.getConfigModules();
modules.push(...configModules);
}
Logger.info(`Cleaning: ${modules.map((module) => module.name).join(", ")}...`);
await Promise.all(modules.map((module) => cleanModule(module)));
} catch (error) {
Expand All @@ -29,4 +42,7 @@ export const handler = async () => {
}
};

Program.command("clean").description("Clean data for all config modules").action(handler);
Program.command("clean")
.description("Clean data for all config modules")
.argument("[module...]", "NPM package names of the modules to clean")
.action(handler);
6 changes: 4 additions & 2 deletions src/commands/dev/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ import chalk from "chalk";
import { Option } from "commander";

import Program from "./command.js";
import { modulesPath } from "./modules/Module.js";
import { createModulesFolder, modulesPath } from "./modules/Module.js";
import { executeCommand } from "../../utils/helpers.js";
import Logger from "../../utils/logger.js";

const linkOption = new Option("--link", "Use `npm link` instead of `npm install` (useful during module development)");

export const handler = async (moduleNames: string[], options: { link: boolean }) => {
try {
createModulesFolder();

const command = options.link ? "npm link" : "npm install";
const fullCommand = `${command}${moduleNames.length ? ` ${moduleNames.join(" ")}` : ""}`;
await executeCommand(fullCommand, { cwd: modulesPath });

if (moduleNames.length) {
Logger.info(
`Add module${moduleNames.length > 1 ? "s" : ""} to your configuration with \`${chalk.magentaBright(
`\nAdd module${moduleNames.length > 1 ? "s" : ""} to your configuration with \`${chalk.magentaBright(
"zksync-cli dev config"
)}\``
);
Expand Down
10 changes: 10 additions & 0 deletions src/commands/dev/modules/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export type DefaultModuleFields = {

export const modulesPath = getLocalPath("modules");

export const createModulesFolder = () => {
if (fileOrDirExists(modulesPath)) {
return;
}
fs.mkdirSync(modulesPath, { recursive: true });
};

type ModuleConfigDefault = Record<string, unknown>;
abstract class Module<TModuleConfig = ModuleConfigDefault> {
configHandler: ConfigHandler;
Expand All @@ -44,6 +51,9 @@ abstract class Module<TModuleConfig = ModuleConfigDefault> {
abstract install(): Promise<void>;

abstract isRunning(): Promise<boolean>;
get startAfterNode(): boolean {
return false;
}
abstract start(): Promise<void>;
getStartupInfo(): LogEntry[] | Promise<LogEntry[]> {
return [];
Expand Down
7 changes: 7 additions & 0 deletions src/commands/dev/modules/utils/packages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "fs";
import { createRequire } from "module";
import path from "path";
import ModuleBlockExplorer from "zkcli-block-explorer";
import ModuleDockerizedNode from "zkcli-dockerized-node";
import ModuleInMemoryNode from "zkcli-in-memory-node";
import ModulePortal from "zkcli-portal";
Expand Down Expand Up @@ -101,6 +102,7 @@ export const findDefaultModules = async (): Promise<Package[]> => {
const packages = {
"zkcli-in-memory-node": require("zkcli-in-memory-node/package.json") as PackageJSON,
"zkcli-dockerized-node": require("zkcli-dockerized-node/package.json") as PackageJSON,
"zkcli-block-explorer": require("zkcli-block-explorer/package.json") as PackageJSON,
"zkcli-portal": require("zkcli-portal/package.json") as PackageJSON,
} as const;

Expand All @@ -115,6 +117,11 @@ export const findDefaultModules = async (): Promise<Package[]> => {
name: packages["zkcli-dockerized-node"].name,
version: packages["zkcli-dockerized-node"].version,
},
{
module: ModuleBlockExplorer as unknown as Module,
name: packages["zkcli-block-explorer"].name,
version: packages["zkcli-block-explorer"].version,
},
{
module: ModulePortal as unknown as Module,
name: packages["zkcli-portal"].name,
Expand Down
Loading