Skip to content

Commit

Permalink
move all ask/clearline to inquirer.confirm
Browse files Browse the repository at this point in the history
  • Loading branch information
octref committed Oct 24, 2024
1 parent 7e053c1 commit 0c55eb9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 77 deletions.
2 changes: 1 addition & 1 deletion cli/src/commands/new/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as vi from "../../util/versioninfo.js";
import { execFile } from "../../util/cp.js";
import { isOnline } from "../../util/index.js";
import { GitHubOwner, GitHubRepo, MinGoVersion, MinNodeVersion, MinTinyGoVersion, ModusHomeDir, SDK, parseSDK } from "../../custom/globals.js";
import { ask, clearLine, withSpinner } from "../../util/index.js";
import { withSpinner } from "../../util/index.js";
import SDKInstallCommand from "../sdk/install/index.js";
import { getHeader } from "../../custom/header.js";
import * as inquirer from "@inquirer/prompts";
Expand Down
43 changes: 21 additions & 22 deletions cli/src/commands/runtime/remove/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import chalk from "chalk";

import * as fs from "../../../util/fs.js";
import * as vi from "../../../util/versioninfo.js";
import { ask, clearLine, withSpinner } from "../../../util/index.js";
import { withSpinner } from "../../../util/index.js";
import * as inquirer from "@inquirer/prompts";

export default class RuntimeRemoveCommand extends Command {
static args = {
Expand All @@ -36,7 +37,7 @@ export default class RuntimeRemoveCommand extends Command {
async run(): Promise<void> {
const { args, flags } = await this.parse(RuntimeRemoveCommand);
if (!args.version) {
this.logError(`No runtime version specified! Run ${chalk.whiteBright("modus runtime remove <version>")}, or ${chalk.whiteBright("modus runtime remove all")}`);
this.logError(`No runtime version specified. Run ${chalk.whiteBright("modus runtime remove <version>")}, or ${chalk.whiteBright("modus runtime remove all")}`);
return;
}

Expand All @@ -45,9 +46,15 @@ export default class RuntimeRemoveCommand extends Command {
if (versions.length === 0) {
this.log(chalk.yellow("No Modus runtimes are installed."));
this.exit(1);
} else if (!flags.force && !(await this.confirmAction("Really, remove all Modus runtimes? [y/N]"), false)) {
this.log(chalk.dim("Aborted."));
this.exit(1);
} else if (!flags.force) {
const confirmed = await inquirer.confirm({
message: "Are you sure you want to remove all Modus runtimes?",
default: false,
});
if (!confirmed) {
this.log(chalk.dim("Aborted"));
this.exit(1);
}
}

for (const version of versions) {
Expand All @@ -62,9 +69,15 @@ export default class RuntimeRemoveCommand extends Command {
if (!isInstalled) {
this.log(chalk.yellow(runtimeText + "is not installed."));
this.exit(1);
} else if (!flags.force && !(await this.confirmAction(`Really, remove ${runtimeText} ? [y/N]`))) {
this.log(chalk.dim("Aborted."));
this.exit(1);
} else if (!flags.force) {
const confirmed = await inquirer.confirm({
message: `Are you sure you want to remove ${runtimeText}?`,
default: false,
});
if (!confirmed) {
this.log(chalk.dim("Aborted"));
this.exit(1);
}
}

await this.removeRuntime(args.version);
Expand All @@ -88,18 +101,4 @@ export default class RuntimeRemoveCommand extends Command {
private logError(message: string) {
this.log(chalk.red(" ERROR ") + chalk.dim(": " + message));
}

private async confirmAction(message: string, defaultToContinue = true): Promise<boolean> {
this.log(message);
const input = await ask(chalk.dim(" -> "));

if (input === "") {
clearLine(2);
return defaultToContinue;
}

const shouldContinue = (input || "n").toLowerCase().trim();
clearLine(2);
return shouldContinue === "yes" || shouldContinue === "y";
}
}
53 changes: 29 additions & 24 deletions cli/src/commands/sdk/remove/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import chalk from "chalk";
import * as fs from "../../../util/fs.js";
import * as vi from "../../../util/versioninfo.js";
import { parseSDK, SDK } from "../../../custom/globals.js";
import { ask, clearLine, withSpinner } from "../../../util/index.js";
import { withSpinner } from "../../../util/index.js";
import * as inquirer from "@inquirer/prompts";

export default class SDKRemoveCommand extends Command {
static args = {
Expand Down Expand Up @@ -64,9 +65,15 @@ export default class SDKRemoveCommand extends Command {
this.exit(1);
}

if (!flags.force && !(await this.confirmAction(`Really, remove all Modus SDKs${flags.runtimes ? " and runtimes" : ""}? [y/N]`, false))) {
this.log(chalk.dim("Aborted."));
this.exit(1);
if (!flags.force) {
const confirmed = inquirer.confirm({
message: "Are you sure you want to remove all Modus SDKs?",
default: false,
});
if (!confirmed) {
this.log(chalk.dim("Aborted"));
this.exit(1);
}
}

for (const sdk of Object.values(SDK)) {
Expand All @@ -89,9 +96,15 @@ export default class SDKRemoveCommand extends Command {
if (versions.length === 0) {
this.log(chalk.yellow(`No Modus ${sdk} SDKs are installed.`));
this.exit(1);
} else if (!flags.force && !(await this.confirmAction(`Really, remove all Modus ${sdk} SDKs? [y/N]`, false))) {
this.log(chalk.dim("Aborted."));
this.exit(1);
} else if (!flags.force) {
const confirmed = inquirer.confirm({
message: `Are you sure you want to remove all Modus ${sdk} SDKs?`,
default: false,
});
if (!confirmed) {
this.log(chalk.dim("Aborted"));
this.exit(1);
}
}

for (const version of versions) {
Expand All @@ -106,9 +119,15 @@ export default class SDKRemoveCommand extends Command {
if (!isInstalled) {
this.log(chalk.yellow(sdkText + "is not installed."));
this.exit(1);
} else if (!flags.force && !(await this.confirmAction(`Really, remove ${sdkText} ? [y/N]`, false))) {
this.log(chalk.dim("Aborted."));
this.exit(1);
} else if (!flags.force) {
const confirmed = inquirer.confirm({
message: `Are you sure you want to remove ${sdkText}?`,
default: false,
});
if (!confirmed) {
this.log(chalk.dim("Aborted"));
this.exit(1);
}
}

await this.removeSDK(sdk, args.version);
Expand Down Expand Up @@ -147,18 +166,4 @@ export default class SDKRemoveCommand extends Command {
private logError(message: string) {
this.log(chalk.red(" ERROR ") + chalk.dim(": " + message));
}

private async confirmAction(message: string, defaultToContinue = true): Promise<boolean> {
this.log(message);
const input = await ask(chalk.dim(" -> "));

if (input === "") {
clearLine(2);
return defaultToContinue;
}

const shouldContinue = (input || "n").toLowerCase().trim();
clearLine(2);
return shouldContinue === "yes" || shouldContinue === "y";
}
}
30 changes: 0 additions & 30 deletions cli/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,6 @@ export async function withSpinner<T>(text: string, fn: (spinner: Ora) => Promise
}
}

export async function ask(question: string, placeholder?: string): Promise<string> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

try {
return await new Promise<string>((res, _) => {
rl.question(question + (placeholder ? " " + placeholder + " " : ""), (answer) => {
res(answer);
});
});
} finally {
rl.close();
}
}

export function clearLine(n: number = 1): void {
const stream = process.stdout;
if (isatty(stream.fd)) {
for (let i = 0; i < n; i++) {
stream.write("\u001B[1A");
stream.write("\u001B[2K");
stream.write("\u001B[2K");
}
} else {
stream.write("\n");
}
}

export async function downloadFile(url: string, dest: string): Promise<boolean> {
const res = await fetch(url);
if (!res.ok) {
Expand Down

0 comments on commit 0c55eb9

Please sign in to comment.