Skip to content

Commit

Permalink
backmerge changeset for new release (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
escottalexander authored Jan 16, 2025
2 parents f3ed109 + 88ff791 commit cc0ae64
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-frogs-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eth-tech-tree": patch
---

Fixed a small bug when showing tree history, Also removed the (Y/n) that showed when prompted with "Press Enter to continue"
52 changes: 34 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync, rmSync } from "fs";
import { confirm } from "@inquirer/prompts";
import { confirm, input } from "@inquirer/prompts";
import { IUserChallenge, IChallenge, TreeNode, IUser, Actions } from "./types";
import chalk from "chalk";
import { loadChallenges, loadUserState, saveUserState } from "./utils/state-manager";
Expand Down Expand Up @@ -290,6 +290,17 @@ Open up the challenge in your favorite code editor and follow the instructions i
return existsSync(targetDir);
}

rebuildHistory():void {
// Rebuild Global tree
this.globalTree = this.buildTree();
// For each node in the current history, find the new node in the newly created tree
const newHistory = this.history.map(({ node, selection }) => {
const newNode = this.findNode(this.globalTree, node.name);
return { node: newNode as TreeNode, selection };
});
this.history = newHistory;
}

getChallengeActions(challenge: TreeNode): Actions {
const actions: Actions = {};
const { address, installLocation } = this.userState;
Expand All @@ -298,19 +309,17 @@ Open up the challenge in your favorite code editor and follow the instructions i
actions["Setup Challenge Repository"] = async () => {
this.clearView();
await setupChallenge(name, installLocation);
// Rebuild the tree
this.globalTree = this.buildTree();
// Rebuild the tree and history
this.rebuildHistory();
// Wait for enter key
await this.pressEnterToContinue();
this.history.pop(); // Remove the old node from history since it has different actions
// Return to challenge menu
const challengeNode = this.findNode(this.globalTree, name) as TreeNode;
await this.navigate(challengeNode);
await this.goBack();
};
} else {
actions["Reset Challenge"] = async () => {
this.clearView();
const confirmReset = await this.pressEnterToContinue("Are you sure you want to reset this challenge? This will remove the challenge from your local machine and re-install it.", false);
const confirmReset = await this.yesOrNo("Are you sure you want to reset this challenge? This will remove the challenge from your local machine and re-install it.", false);
if (!confirmReset) {
await this.goBack();
} else {
Expand All @@ -319,12 +328,11 @@ Open up the challenge in your favorite code editor and follow the instructions i
rmSync(targetDir, { recursive: true, force: true });
console.log(`Installing fresh copy of challenge...`);
await setupChallenge(name, installLocation);
this.globalTree = this.buildTree();
// Rebuild the tree and history
this.rebuildHistory();
await this.pressEnterToContinue();
this.history.pop(); // Remove the old node from history since it has different actions
// Return to challenge menu
const challengeNode = this.findNode(this.globalTree, name) as TreeNode;
await this.navigate(challengeNode);
await this.goBack();
}
};
actions["Submit Completed Challenge"] = async () => {
Expand All @@ -336,22 +344,20 @@ Open up the challenge in your favorite code editor and follow the instructions i
this.userState.challenges = newUserState.challenges;
// Save the new user state locally
await saveUserState(this.userState);
// Rebuild the tree
this.globalTree = this.buildTree();
// Rebuild the tree and history
this.rebuildHistory();
// Wait for enter key
await this.pressEnterToContinue();
this.history.pop(); // Remove the old node from history since it has different actions
// Return to challenge menu
const challengeNode = this.findNode(this.globalTree, name) as TreeNode;
await this.navigate(challengeNode);
await this.goBack();
};
}
return actions;
};

async pressEnterToContinue(customMessage?: string, defaultAnswer: boolean = true) {
async yesOrNo(message: string, defaultAnswer: boolean = true) {
const answer = await confirm({
message: typeof customMessage === "string" ? customMessage : 'Press Enter to continue...',
message,
default: defaultAnswer,
theme: {
prefix: "",
Expand All @@ -360,6 +366,16 @@ Open up the challenge in your favorite code editor and follow the instructions i
return answer;
}

async pressEnterToContinue(customMessage?: string) {
const answer = await input({
message: typeof customMessage === "string" ? customMessage : 'Press Enter to continue...',
theme: {
prefix: "",
}
});
return answer;
}

private clearView(): void {
process.stdout.moveCursor(0, this.getMaxViewHeight());
console.clear();
Expand Down

0 comments on commit cc0ae64

Please sign in to comment.