Skip to content

Commit

Permalink
feat: new utils, improved dev manage commands
Browse files Browse the repository at this point in the history
  • Loading branch information
JackHamer09 committed Oct 9, 2023
1 parent c83d1b5 commit ba50e16
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 10 deletions.
22 changes: 19 additions & 3 deletions src/commands/dev/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,22 @@ export const cleanModule = async (module: Module) => {
}
};

export const handler = async () => {
export const handler = async (modulePackageNames?: string[]) => {
try {
const modules = await configHandler.getConfigModules();
const modules = [];
if (modulePackageNames) {
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 @@ -31,4 +44,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);
3 changes: 3 additions & 0 deletions src/commands/dev/modules/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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
9 changes: 6 additions & 3 deletions src/commands/dev/restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { handler as stop } from "./stop.js";
import { track } from "../../utils/analytics.js";
import Logger from "../../utils/logger.js";

export const handler = async () => {
export const handler = async (modulePackageNames?: string[]) => {
try {
await stop();
await stop(modulePackageNames);
await start();
} catch (error) {
Logger.error("There was an error while restarting the testing environment:");
Expand All @@ -15,4 +15,7 @@ export const handler = async () => {
}
};

Program.command("restart").description("Restart local zkSync environment and modules").action(handler);
Program.command("restart")
.description("Restart local zkSync environment and modules")
.argument("[module...]", "NPM package names of the modules to restart")
.action(handler);
3 changes: 2 additions & 1 deletion src/commands/dev/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const installModules = async (modules: Module[]) => {

const startModules = async (modules: Module[]) => {
Logger.info(`\nStarting: ${modules.map((m) => m.name).join(", ")}...`);
await Promise.all(modules.map((m) => m.start()));
await Promise.all(modules.filter((e) => !e.startAfterNode).map((m) => m.start()));
await Promise.all(modules.filter((e) => e.startAfterNode).map((m) => m.start()));
};

const stopOtherNodes = async (currentModules: Module[]) => {
Expand Down
22 changes: 19 additions & 3 deletions src/commands/dev/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@ import configHandler from "./ConfigHandler.js";
import { track } from "../../utils/analytics.js";
import Logger from "../../utils/logger.js";

export const handler = async () => {
export const handler = async (modulePackageNames?: string[]) => {
try {
const modules = await configHandler.getConfigModules();
const modules = [];
if (modulePackageNames) {
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(`Stopping: ${modules.map((m) => m.name).join(", ")}...`);
await Promise.all(modules.map((m) => m.isInstalled().then((installed) => (installed ? m.stop() : undefined))));
} catch (error) {
Expand All @@ -15,4 +28,7 @@ export const handler = async () => {
}
};

Program.command("stop").description("Stop local zkSync environment and modules").action(handler);
Program.command("stop")
.description("Stop local zkSync environment and modules")
.argument("[module...]", "NPM package names of the modules to stop")
.action(handler);
7 changes: 7 additions & 0 deletions src/utils/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ export const writeFile = (filePath: string, data: string | NodeJS.ArrayBufferVie
// Then write file
fs.writeFileSync(filePath, data, "utf-8");
};

export const createSymlink = (targetPath: string, linkPath: string, type: "file" | "dir" | "junction" = "file") => {
if (fileOrDirExists(linkPath)) {
throw new Error(`${type} already exists at ${linkPath}`);
}
fs.symlinkSync(targetPath, linkPath, type);
};
20 changes: 20 additions & 0 deletions src/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,23 @@ export const cloneRepo = async (repoUrl: string, destination: string) => {
Logger.debug(`Cloning ${repoUrl} repository to ${destination}`);
await executeCommand(command);
};

export const getLatestReleaseVersion = async (repo: string): Promise<string> => {
const apiUrl = `https://api.github.com/repos/${repo}/releases/latest`;
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`GitHub API request failed with status: ${response.status}`);
}
const releaseInfo = await response.json();
if (typeof releaseInfo?.tag_name !== "string") {
throw new Error(`Failed to parse the latest release version: ${JSON.stringify(releaseInfo)}`);
}
return releaseInfo.tag_name;
} catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to fetch the latest release version: ${error.message}`);
}
throw error;
}
};

0 comments on commit ba50e16

Please sign in to comment.