Skip to content

Commit cc0ae64

Browse files
backmerge changeset for new release (#89)
2 parents f3ed109 + 88ff791 commit cc0ae64

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

.changeset/twelve-frogs-behave.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eth-tech-tree": patch
3+
---
4+
5+
Fixed a small bug when showing tree history, Also removed the (Y/n) that showed when prompted with "Press Enter to continue"

src/index.ts

+34-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { existsSync, rmSync } from "fs";
2-
import { confirm } from "@inquirer/prompts";
2+
import { confirm, input } from "@inquirer/prompts";
33
import { IUserChallenge, IChallenge, TreeNode, IUser, Actions } from "./types";
44
import chalk from "chalk";
55
import { loadChallenges, loadUserState, saveUserState } from "./utils/state-manager";
@@ -290,6 +290,17 @@ Open up the challenge in your favorite code editor and follow the instructions i
290290
return existsSync(targetDir);
291291
}
292292

293+
rebuildHistory():void {
294+
// Rebuild Global tree
295+
this.globalTree = this.buildTree();
296+
// For each node in the current history, find the new node in the newly created tree
297+
const newHistory = this.history.map(({ node, selection }) => {
298+
const newNode = this.findNode(this.globalTree, node.name);
299+
return { node: newNode as TreeNode, selection };
300+
});
301+
this.history = newHistory;
302+
}
303+
293304
getChallengeActions(challenge: TreeNode): Actions {
294305
const actions: Actions = {};
295306
const { address, installLocation } = this.userState;
@@ -298,19 +309,17 @@ Open up the challenge in your favorite code editor and follow the instructions i
298309
actions["Setup Challenge Repository"] = async () => {
299310
this.clearView();
300311
await setupChallenge(name, installLocation);
301-
// Rebuild the tree
302-
this.globalTree = this.buildTree();
312+
// Rebuild the tree and history
313+
this.rebuildHistory();
303314
// Wait for enter key
304315
await this.pressEnterToContinue();
305-
this.history.pop(); // Remove the old node from history since it has different actions
306316
// Return to challenge menu
307-
const challengeNode = this.findNode(this.globalTree, name) as TreeNode;
308-
await this.navigate(challengeNode);
317+
await this.goBack();
309318
};
310319
} else {
311320
actions["Reset Challenge"] = async () => {
312321
this.clearView();
313-
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);
322+
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);
314323
if (!confirmReset) {
315324
await this.goBack();
316325
} else {
@@ -319,12 +328,11 @@ Open up the challenge in your favorite code editor and follow the instructions i
319328
rmSync(targetDir, { recursive: true, force: true });
320329
console.log(`Installing fresh copy of challenge...`);
321330
await setupChallenge(name, installLocation);
322-
this.globalTree = this.buildTree();
331+
// Rebuild the tree and history
332+
this.rebuildHistory();
323333
await this.pressEnterToContinue();
324-
this.history.pop(); // Remove the old node from history since it has different actions
325334
// Return to challenge menu
326-
const challengeNode = this.findNode(this.globalTree, name) as TreeNode;
327-
await this.navigate(challengeNode);
335+
await this.goBack();
328336
}
329337
};
330338
actions["Submit Completed Challenge"] = async () => {
@@ -336,22 +344,20 @@ Open up the challenge in your favorite code editor and follow the instructions i
336344
this.userState.challenges = newUserState.challenges;
337345
// Save the new user state locally
338346
await saveUserState(this.userState);
339-
// Rebuild the tree
340-
this.globalTree = this.buildTree();
347+
// Rebuild the tree and history
348+
this.rebuildHistory();
341349
// Wait for enter key
342350
await this.pressEnterToContinue();
343-
this.history.pop(); // Remove the old node from history since it has different actions
344351
// Return to challenge menu
345-
const challengeNode = this.findNode(this.globalTree, name) as TreeNode;
346-
await this.navigate(challengeNode);
352+
await this.goBack();
347353
};
348354
}
349355
return actions;
350356
};
351357

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

369+
async pressEnterToContinue(customMessage?: string) {
370+
const answer = await input({
371+
message: typeof customMessage === "string" ? customMessage : 'Press Enter to continue...',
372+
theme: {
373+
prefix: "",
374+
}
375+
});
376+
return answer;
377+
}
378+
363379
private clearView(): void {
364380
process.stdout.moveCursor(0, this.getMaxViewHeight());
365381
console.clear();

0 commit comments

Comments
 (0)