Skip to content

Commit

Permalink
added interactive commands to tail for dev
Browse files Browse the repository at this point in the history
  • Loading branch information
timbrinded committed Sep 11, 2023
1 parent 0a7d7c1 commit f129987
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 66 deletions.
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"debug": "^4.3.4",
"dotenv": "^16.3.1",
"ethers": "^6.7.1",
"inquirer": "^8.2.6",
"inquirer": "^9.2.11",
"inquirer-press-to-continue": "^1.2.0",
"minimatch": "^9.0.3",
"node-fetch": "^3.3.2",
Expand Down
108 changes: 79 additions & 29 deletions packages/cli/src/cmds/runNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ApiPromise } from "@polkadot/api";
import chalk from "chalk";
import clear from "clear";
import { watch } from "fs";
import fs from "fs/promises";
import fs, { promises as fsPromises } from "fs";
import inquirer from "inquirer";
import PressToContinuePrompt from "inquirer-press-to-continue";
import { createReadStream, stat } from "node:fs";
Expand Down Expand Up @@ -152,7 +152,7 @@ export async function runNetworkCmd(args) {
switch (choice.MenuChoice) {
case 1:
clear();
await resolveTailChoice();
await resolveTailChoice(env);
lastSelected = 0;
clear();
break;
Expand Down Expand Up @@ -208,7 +208,7 @@ const reportServicePorts = async () => {
portsList.push(
...(await Promise.all(
config.foundation.launchSpec.map(async ({ configPath, name }) => {
const yaml = parse((await fs.readFile(configPath)).toString());
const yaml = parse((await fsPromises.readFile(configPath)).toString());
return { name, port: yaml.port || "8000" };
})
))
Expand Down Expand Up @@ -257,7 +257,7 @@ const resolveCommandChoice = async () => {
config.foundation.type == "chopsticks"
? await Promise.all(
config.foundation.launchSpec.map(async ({ configPath }) => {
const yaml = parse((await fs.readFile(configPath)).toString());
const yaml = parse((await fsPromises.readFile(configPath)).toString());
return yaml.port || "8000";
})
)
Expand Down Expand Up @@ -331,29 +331,42 @@ const resolveInfoChoice = async (env: Environment) => {
);
};

const resolveGrepChoice = async (env: Environment) => {
const resolveGrepChoice = async (env: Environment, silent: boolean = false) => {
const choice = await inquirer.prompt({
name: "grep",
type: "input",
message: `What pattern would you like to filter for (ID/Title): `,
default: process.env.MOON_GREP || "D01T01",
});
process.env.MOON_RECYCLE = "true";

console.log(`Running tests with grep pattern: ${await choice.grep}`);
process.env.MOON_GREP = await choice.grep;
return await executeTests(env, { testNamePattern: await choice.grep });
const opts = { testNamePattern: await choice.grep, silent };
if (silent) {
opts["reporters"] = ["dot"];
}
return await executeTests(env, opts);
};

const resolveTestChoice = async (env: Environment) => {
const resolveTestChoice = async (env: Environment, silent: boolean = false) => {
process.env.MOON_RECYCLE = "true";
return await executeTests(env);
const opts = { silent };
if (silent) {
opts["reporters"] = ["dot"];
}
return await executeTests(env, opts);
};

const resolveTailChoice = async () => {
const ui = new inquirer.ui.BottomBar();
const resolveTailChoice = async (env: Environment) => {

await new Promise((resolve) => {
// TODO: Add Pause/Continue tail toggle
// TODO: Add
const ui = new inquirer.ui.BottomBar({
bottomBar: `📜 Tailing Logs, commands: ${chalk.bgWhite.black(
"[q]"
)} - quit, ${chalk.bgWhite.black("[t]")} - test, ${chalk.bgWhite.black("[g]")} - grep test\n`,
});
process.stdin.setEncoding("utf8");
await new Promise(async (resolve) => {
const ctx = MoonwallContext.getContext();
const onData = (chunk: any) => ui.log.write(chunk.toString());

Expand Down Expand Up @@ -402,23 +415,60 @@ const resolveTailChoice = async () => {
resolve("");
});
} else {
const runningNode = ctx.nodes[0];
runningNode.stderr!.on("data", onData);
runningNode.stdout!.on("data", onData);
inquirer
.prompt({
name: "exitTail",
type: "press-to-continue",
anyKey: true,
pressToContinueMessage: " Press any key to stop tailing logs and go back ↩️",
})
.then(() => {
runningNode.stderr!.off("data", onData);
runningNode.stdout!.off("data", onData);
resolve("");
const logFilePath = reportLogLocation(true);
// eslint-disable-next-line prefer-const
let currentReadPosition = 0;
process.stdin.resume();
const printLogs = (newReadPosition: number, currentReadPosition: number) => {
const stream = fs.createReadStream(logFilePath, {
start: currentReadPosition,
end: newReadPosition,
});
}
stream.on("data", onData);
stream.on("end", () => {
currentReadPosition = newReadPosition;
});
};

// TODO: Extend W.I.P below so support interactive tests whilst tailing logs
const readLog = () => {
const stats = fs.statSync(logFilePath);
const newReadPosition = stats.size;

if (newReadPosition > currentReadPosition) {
printLogs(newReadPosition, currentReadPosition);
}
process.stdin.resume();
};

printLogs(fs.statSync(logFilePath).size, 0);

process.stdin.on("data", async (key) => {
process.stdin.pause();
const char = key.toString().trim();

if (char === "q") {
fs.unwatchFile(logFilePath);
process.stdin.pause();
resolve("");
}

if (char === "t") {
await resolveTestChoice(env, true);
process.stdin.resume();
}

if (char === "g") {
process.stdin.pause();
await resolveGrepChoice(env, true);
process.stdin.resume();
}

process.stdin.resume();
});

fs.watchFile(logFilePath, () => {
readLog();
});
}
});
};
16 changes: 9 additions & 7 deletions packages/cli/src/internal/cmdFunctions/tempLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ export function clearNodeLogs() {
}
}

export function reportLogLocation() {
export function reportLogLocation(silent: boolean = false) {
const dirPath = path.join(process.cwd(), "tmp", "node_logs");
const result = fs.readdirSync(dirPath);
let consoleMessage = "";
let filePath = "";
try {
filePath = ` 🪵 Log location: ${path.join(
dirPath,
result.find((file) => path.extname(file) == ".log")!
)}`;
filePath = path.join(dirPath, result.find((file) => path.extname(file) == ".log")!);
consoleMessage = ` 🪵 Log location: ${filePath}`;
} catch (e) {
console.error(e);
}

console.log(filePath);
return filePath;
if (!silent) {
console.log(consoleMessage);
}

return filePath.trim();
}
2 changes: 1 addition & 1 deletion packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"debug": "^4.3.4",
"dotenv": "^16.3.1",
"ethers": "^6.7.1",
"inquirer": "^8.2.6",
"inquirer": "^9.2.11",
"inquirer-press-to-continue": "^1.2.0",
"node-fetch": "^3.3.2",
"rlp": "^3.0.0",
Expand Down
68 changes: 40 additions & 28 deletions pnpm-lock.yaml

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

0 comments on commit f129987

Please sign in to comment.