UNPKG

zksync-cli

Version:

CLI tool that simplifies the process of developing applications and interacting with the ZKsync network

82 lines 3.4 kB
import path from "path"; import { executeCommand } from "./helpers.js"; import Logger from "./logger.js"; let dockerInstalled = false; const checkDockerInstallation = async () => { if (dockerInstalled) return; try { await executeCommand("docker --version", { silent: true }); dockerInstalled = true; } catch { throw new Error("Docker is not installed. Download: https://www.docker.com/get-started/"); } }; const getComposeCommandBase = (dockerComposePath, projectDir) => { return `docker compose -f ${dockerComposePath} --project-directory ${projectDir ?? path.dirname(dockerComposePath)}`; }; const createComposeCommand = (action) => async (dockerComposePath, projectDir, additionalArgs) => { await checkDockerInstallation(); const baseCommand = getComposeCommandBase(dockerComposePath, projectDir); const args = additionalArgs ? `${additionalArgs.join(" ")}` : ""; return await executeCommand(`${baseCommand} ${action} ${args}`.trim()); }; var ContainerStatus; (function (ContainerStatus) { ContainerStatus["Running"] = "running"; ContainerStatus["Exited"] = "exited"; ContainerStatus["Paused"] = "paused"; ContainerStatus["Restarting"] = "restarting"; ContainerStatus["Dead"] = "dead"; ContainerStatus["Unknown"] = "unknown"; })(ContainerStatus || (ContainerStatus = {})); export const composeStatus = async (dockerComposePath, projectDir) => { await checkDockerInstallation(); let statusJson = (await executeCommand(`${getComposeCommandBase(dockerComposePath, projectDir)} ps --format json --all`, { silent: true, })).trim(); // trim to remove leading and trailing whitespace // if no containers are mounted, docker compose returns an empty string if (!statusJson.length) { return []; } // on windows, docker compose returns json objects separated by newlines if (statusJson.startsWith("{") && statusJson.endsWith("}")) { statusJson = "[" + statusJson.split("\n").join(",") + "]"; } try { const containers = JSON.parse(statusJson); return containers.map((container) => ({ name: container.Name, isRunning: container.State === ContainerStatus.Running || container.State === ContainerStatus.Restarting, })); } catch (error) { Logger.debug(`Failed to JSON.parse compose status ${dockerComposePath}: ${error?.toString()}`); Logger.debug(statusJson); return []; } }; export const composeLogs = async (dockerComposePath, projectDir, totalLines = 15) => { await checkDockerInstallation(); const response = (await executeCommand(`${getComposeCommandBase(dockerComposePath, projectDir)} logs --tail=${totalLines}`, { silent: true, })).trim(); // trim to remove leading and trailing whitespace try { return response.split("\n"); } catch (error) { Logger.debug(`Failed to split compose logs ${dockerComposePath}: ${error?.toString()}`); return []; } }; export const compose = { build: createComposeCommand("build"), create: createComposeCommand("create"), up: createComposeCommand("up -d"), stop: createComposeCommand("stop"), down: createComposeCommand("down --rmi all --volumes --remove-orphans"), logs: composeLogs, status: composeStatus, }; //# sourceMappingURL=docker.js.map