Skip to content

Commit

Permalink
refactor: ✨ Upgraded Inquirer
Browse files Browse the repository at this point in the history
  • Loading branch information
timbrinded committed Dec 2, 2024
1 parent a506168 commit 0aa285b
Show file tree
Hide file tree
Showing 18 changed files with 1,821 additions and 1,783 deletions.
23 changes: 16 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
"devDependencies": {
"@biomejs/biome": "1.9.4",
"@changesets/cli": "2.27.9",
"@types/node": "22.9.0",
"typescript": "5.6.3"
"@types/node": "22.10.1",
"typescript": "5.7.2"
},
"dependencies": {
"@acala-network/chopsticks": "1.0.1",
"@inquirer/prompts": "7.1.0",
"@moonbeam-network/api-augment": "0.3200.3",
"@polkadot/api": "14.3.1",
"@polkadot/api-base": "14.3.1",
Expand All @@ -39,19 +40,18 @@
"@polkadot/types-codec": "14.3.1",
"@polkadot/util": "13.2.3",
"@polkadot/util-crypto": "13.2.3",
"@types/debug": "*",
"@types/debug": "^4.1.12",
"@types/semver": "^7.5.8",
"@types/ws": "^8.5.10",
"@types/yargs": "*",
"@types/yargs": "^17.0.33",
"@vitest/ui": "2.1.4",
"@zombienet/utils": "0.0.25",
"bottleneck": "2.19.5",
"chalk": "5.3.0",
"debug": "4.3.7",
"ethers": "6.13.4",
"inquirer": "9.3.3",
"inquirer-press-to-continue": "1.2.0",
"pnpm": "9.12.3",
"polkadot-api": "1.7.7",
"semver": "7.6.2",
"solc": "0.8.28",
"tiny-invariant": "^1.3.3",
Expand All @@ -65,7 +65,16 @@
},
"pnpm": {
"overrides": {
"inquirer": "9.3.3"
"@moonbeam-network/api-augment": "0.3200.3",
"@polkadot/api": "14.3.1",
"@polkadot/api-base": "14.3.1",
"@polkadot/api-derive": "14.3.1",
"@polkadot/keyring": "13.2.3",
"@polkadot/rpc-provider": "*",
"@polkadot/types": "14.3.1",
"@polkadot/types-codec": "14.3.1",
"@polkadot/util": "13.2.3",
"@polkadot/util-crypto": "13.2.3"
}
}
}
6 changes: 4 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
},
"dependencies": {
"@acala-network/chopsticks": "*",
"@inquirer/prompts": "*",
"@moonbeam-network/api-augment": "*",
"@moonwall/types": "workspace:*",
"@moonwall/util": "workspace:*",
Expand All @@ -70,6 +71,7 @@
"@polkadot/types-codec": "*",
"@polkadot/util": "*",
"@polkadot/util-crypto": "*",
"@types/react": "^18.3.12",
"@vitest/ui": "*",
"@zombienet/orchestrator": "0.0.97",
"@zombienet/utils": "*",
Expand All @@ -83,11 +85,11 @@
"dotenv": "16.4.5",
"ethers": "*",
"get-port": "^7.1.0",
"inquirer": "*",
"inquirer-press-to-continue": "*",
"ink": "^5.1.0",
"jsonc-parser": "3.3.1",
"minimatch": "9.0.5",
"polkadot-api": "^1.7.4",
"react": "^18.3.1",
"semver": "*",
"tiny-invariant": "*",
"viem": "*",
Expand Down
186 changes: 186 additions & 0 deletions packages/cli/src/cmds/components/LogViewer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import React, { useState, useEffect } from "react";
import { Box, Text, useInput, useApp } from "ink";
import chalk from "chalk";
import fs from "node:fs";

interface LogViewerProps {
env: any;
logFilePath: string;
onExit: () => void;
onTest: () => Promise<void>;
onGrep: () => Promise<void>;
onNextLog?: () => void;
onPrevLog?: () => void;
zombieInfo?: {
currentNode: string;
position: number;
total: number;
};
}

export const LogViewer: React.FC<LogViewerProps> = ({
env,
logFilePath,
onExit,
onTest,
onGrep,
onNextLog,
onPrevLog,
zombieInfo,
}) => {
const [logs, setLogs] = useState<string[]>([]);
const [tailing, setTailing] = useState(true);
const [currentReadPosition, setCurrentReadPosition] = useState(0);
const [isExecutingCommand, setIsExecutingCommand] = useState(false);
const { exit } = useApp();

const resumePauseProse = [
`, ${chalk.bgWhite.black("[p]")} Pause tail`,
`, ${chalk.bgWhite.black("[r]")} Resume tail`,
];

const bottomBarBase = `📜 Tailing Logs, commands: ${chalk.bgWhite.black(
"[q]"
)} Quit, ${chalk.bgWhite.black("[t]")} Test, ${chalk.bgWhite.black("[g]")} Grep test`;

const zombieContent = zombieInfo
? `, ${chalk.bgWhite.black("[,]")} Next Log, ${chalk.bgWhite.black(
"[.]"
)} Previous Log | CurrentLog: ${chalk.bgWhite.black(
`${zombieInfo.currentNode} (${zombieInfo.position}/${zombieInfo.total})`
)}`
: "";

const setupWatcher = () => {
fs.watchFile(logFilePath, () => {
readLog();
});
};

const removeWatcher = () => {
fs.unwatchFile(logFilePath);
};

useInput((input, key) => {
if (input === "q") {
fs.unwatchFile(logFilePath);
onExit();
exit();
}

if (input === "p") {
setTailing(false);
removeWatcher();
}

if (input === "r") {
setTailing(true);
setupWatcher();
readLog();
}

if (input === "t") {
setIsExecutingCommand(true);
removeWatcher();
onTest().finally(() => {
setIsExecutingCommand(false);
if (tailing) {
setupWatcher();
readLog();
}
});
}

if (input === "g") {
setIsExecutingCommand(true);
removeWatcher();
onGrep().finally(() => {
setIsExecutingCommand(false);
if (tailing) {
setupWatcher();
readLog();
}
});
}

if (input === "," && onNextLog) {
fs.unwatchFile(logFilePath);
onNextLog();
exit();
}

if (input === "." && onPrevLog) {
fs.unwatchFile(logFilePath);
onPrevLog();
exit();
}
});

const readLog = () => {
if (!tailing) return;

const stats = fs.statSync(logFilePath);
const newReadPosition = stats.size;

if (newReadPosition > currentReadPosition) {
const stream = fs.createReadStream(logFilePath, {
start: currentReadPosition,
end: newReadPosition,
});

let newContent = "";
stream.on("data", (chunk) => {
newContent += chunk.toString();
});

stream.on("end", () => {
setLogs((prev) => [...prev, ...newContent.split("\n")]);
setCurrentReadPosition(newReadPosition);
});
}
};

useEffect(() => {
// Initial read
const stats = fs.statSync(logFilePath);
const stream = fs.createReadStream(logFilePath);
let content = "";

stream.on("data", (chunk) => {
content += chunk.toString();
});

stream.on("end", () => {
setLogs(content.split("\n"));
setCurrentReadPosition(stats.size);
});

// Watch for changes if tailing is enabled
if (tailing) {
setupWatcher();
}

return () => {
removeWatcher();
};
}, [logFilePath, tailing]);

return (
<Box flexDirection="column" height="100%">
<Box flexGrow={1} flexDirection="column">
{logs.slice(-process.stdout.rows + 2).map((line, i) => (
<Text key={i}>{line}</Text>
))}
</Box>
{!isExecutingCommand && (
<Box height={1}>
<Text>
{bottomBarBase}
{resumePauseProse[tailing ? 0 : 1]}
{zombieContent}
</Text>
</Box>
)}
</Box>
);
};
Loading

0 comments on commit 0aa285b

Please sign in to comment.