Changesets · Branches · Tags · Compare
| Changeset | 7d65ae85126726d262f882b37857d0cc0182bfa1631917776e78b5b56c77726f |
|---|---|
| Author | Nate Sesti <33237525+sestinj@users.noreply.github.com> · 2025-09-22 14:34 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-09-22 14:34 -07:00 |
| Parents | f2b29e6c0c99 |
8 files changed
modified .github/workflows/main.yaml +17 −17
| @@ -26,8 +26,24 @@ | ||
| 26 | 26 | echo "should_run=false" >> $GITHUB_OUTPUT |
| 27 | 27 | fi |
| 28 | 28 | |
| 29 | + check-version: | |
| 30 | + runs-on: ubuntu-latest | |
| 31 | + steps: | |
| 32 | + - name: Checkout | |
| 33 | + uses: actions/checkout@v5 | |
| 34 | + with: | |
| 35 | + fetch-depth: 0 # This ensures all tags are fetched | |
| 36 | + | |
| 37 | + - name: Checkout tag | |
| 38 | + run: git checkout ${GITHUB_REF#refs/tags/} | |
| 39 | + | |
| 40 | + - name: Make sure version isn't odd | |
| 41 | + run: | | |
| 42 | + cd extensions/vscode | |
| 43 | + node scripts/versionCheck.js | |
| 44 | + | |
| 29 | 45 | build: |
| 30 | - needs: check_release_name | |
| 46 | + needs: [check_release_name, check-version] | |
| 31 | 47 | if: needs.check_release_name.outputs.should_run == 'true' || github.event_name == 'workflow_dispatch' |
| 32 | 48 | runs-on: ${{ matrix.os }} |
| 33 | 49 | strategy: |
| @@ -88,22 +104,6 @@ | ||
| 88 | 104 | name: ${{ matrix.platform }}-${{ matrix.arch }}-vsix |
| 89 | 105 | path: "extensions/vscode/*.vsix" |
| 90 | 106 | |
| 91 | - check-version: | |
| 92 | - runs-on: ubuntu-latest | |
| 93 | - steps: | |
| 94 | - - name: Checkout | |
| 95 | - uses: actions/checkout@v5 | |
| 96 | - with: | |
| 97 | - fetch-depth: 0 # This ensures all tags are fetched | |
| 98 | - | |
| 99 | - - name: Checkout tag | |
| 100 | - run: git checkout ${GITHUB_REF#refs/tags/} | |
| 101 | - | |
| 102 | - - name: Make sure version isn't odd | |
| 103 | - run: | | |
| 104 | - cd extensions/vscode | |
| 105 | - node scripts/versionCheck.js | |
| 106 | - | |
| 107 | 107 | release: |
| 108 | 108 | permissions: |
| 109 | 109 | contents: write |
modified extensions/cli/package-lock.json +0 −0
Not shown: the file is larger than the hub shows, or this page holds as much as it shows. View the file.
modified extensions/cli/package.json +0 −2
| @@ -77,7 +77,6 @@ | ||
| 77 | 77 | "@types/jszip": "^3.4.0", |
| 78 | 78 | "@types/nock": "^10.0.3", |
| 79 | 79 | "@types/react": "^19.1.8", |
| 80 | - "@types/readline-sync": "^1.4.8", | |
| 81 | 80 | "@types/shell-quote": "^1.7.5", |
| 82 | 81 | "@types/supertest": "^6.0.3", |
| 83 | 82 | "@types/turndown": "^5.0.5", |
| @@ -122,7 +121,6 @@ | ||
| 122 | 121 | "prettier": "^3.6.2", |
| 123 | 122 | "prettier-plugin-tailwindcss": "^0.6.14", |
| 124 | 123 | "react": "^19.1.0", |
| 125 | - "readline-sync": "^1.4.10", | |
| 126 | 124 | "semantic-release": "^24.2.7", |
| 127 | 125 | "shell-quote": "^1.8.3", |
| 128 | 126 | "supertest": "^7.1.3", |
modified extensions/cli/src/commands/chat.ts +3 −3
| @@ -1,8 +1,7 @@ | ||
| 1 | 1 | import { ModelConfig } from "@continuedev/config-yaml"; |
| 2 | 2 | import chalk from "chalk"; |
| 3 | -import type { ChatHistoryItem, Session } from "core/index.js"; | |
| 3 | +import { ChatHistoryItem, Session } from "core"; | |
| 4 | 4 | import { ChatDescriber } from "core/util/chatDescriber.js"; |
| 5 | -import * as readlineSync from "readline-sync"; | |
| 6 | 5 | |
| 7 | 6 | import { compactChatHistory, findCompactionIndex } from "../compaction.js"; |
| 8 | 7 | import { processCommandFlags } from "../flags/flagProcessor.js"; |
| @@ -25,6 +24,7 @@ | ||
| 25 | 24 | import { gracefulExit } from "../util/exit.js"; |
| 26 | 25 | import { formatAnthropicError, formatError } from "../util/formatError.js"; |
| 27 | 26 | import { logger } from "../util/logger.js"; |
| 27 | +import { question } from "../util/prompt.js"; | |
| 28 | 28 | import { |
| 29 | 29 | calculateContextUsagePercentage, |
| 30 | 30 | countChatHistoryTokens, |
| @@ -454,7 +454,7 @@ | ||
| 454 | 454 | const userInput = |
| 455 | 455 | isFirstMessage && initialUserInput |
| 456 | 456 | ? initialUserInput |
| 457 | - : readlineSync.question(`\n${chalk.bold.green("You:")} `); | |
| 457 | + : await question(`\n${chalk.bold.green("You:")} `); | |
| 458 | 458 | |
| 459 | 459 | isFirstMessage = false; |
| 460 | 460 |
modified extensions/cli/src/freeTrialTransition.ts +10 −11
| @@ -4,14 +4,14 @@ | ||
| 4 | 4 | |
| 5 | 5 | import chalk from "chalk"; |
| 6 | 6 | import open from "open"; |
| 7 | -import * as readlineSync from "readline-sync"; | |
| 8 | 7 | |
| 9 | 8 | import { env } from "./env.js"; |
| 10 | 9 | import { |
| 11 | - isValidAnthropicApiKey, | |
| 12 | 10 | getApiKeyValidationError, |
| 11 | + isValidAnthropicApiKey, | |
| 13 | 12 | } from "./util/apiKeyValidation.js"; |
| 14 | 13 | import { gracefulExit } from "./util/exit.js"; |
| 14 | +import { question, questionWithChoices } from "./util/prompt.js"; | |
| 15 | 15 | import { updateAnthropicModelInYaml } from "./util/yamlConfigUpdater.js"; |
| 16 | 16 | |
| 17 | 17 | const CONFIG_PATH = path.join(env.continueHome, "config.yaml"); |
| @@ -52,10 +52,12 @@ | ||
| 52 | 52 | console.log(chalk.white("1. 💳 Sign up for models add-on")); |
| 53 | 53 | console.log(chalk.white("2. 🔑 Enter your Anthropic API key")); |
| 54 | 54 | |
| 55 | - const choice = readlineSync.question(chalk.yellow("\nEnter choice (1): "), { | |
| 56 | - limit: ["1", "2", ""], | |
| 57 | - limitMessage: chalk.dim("Please enter 1 or 2"), | |
| 58 | - }); | |
| 55 | + const choice = await questionWithChoices( | |
| 56 | + chalk.yellow("\nEnter choice (1): "), | |
| 57 | + ["1", "2", ""], | |
| 58 | + "1", | |
| 59 | + chalk.dim("Please enter 1 or 2"), | |
| 60 | + ); | |
| 59 | 61 | |
| 60 | 62 | if (choice === "1" || choice === "") { |
| 61 | 63 | // Option 1: Open models setup page |
| @@ -81,15 +83,12 @@ | ||
| 81 | 83 | } |
| 82 | 84 | |
| 83 | 85 | // Wait for user to acknowledge |
| 84 | - readlineSync.question(chalk.dim("\nPress Enter to exit...")); | |
| 86 | + await question(chalk.dim("\nPress Enter to exit...")); | |
| 85 | 87 | await gracefulExit(0); |
| 86 | 88 | } else if (choice === "2") { |
| 87 | 89 | // Option 2: Enter Anthropic API key |
| 88 | - const apiKey = readlineSync.question( | |
| 90 | + const apiKey = await question( | |
| 89 | 91 | chalk.white("\nEnter your Anthropic API key: "), |
| 90 | - { | |
| 91 | - hideEchoBack: true, | |
| 92 | - }, | |
| 93 | 92 | ); |
| 94 | 93 | |
| 95 | 94 | if (!isValidAnthropicApiKey(apiKey)) { |
modified extensions/cli/src/onboarding.ts +8 −9
| @@ -5,7 +5,6 @@ | ||
| 5 | 5 | import { BaseLlmApi } from "@continuedev/openai-adapters"; |
| 6 | 6 | import { DefaultApiInterface } from "@continuedev/sdk/dist/api/dist/index.js"; |
| 7 | 7 | import chalk from "chalk"; |
| 8 | -import * as readlineSync from "readline-sync"; | |
| 9 | 8 | |
| 10 | 9 | import { processPromptOrRule } from "./args.js"; |
| 11 | 10 | import { AuthConfig, isAuthenticated, login } from "./auth/workos.js"; |
| @@ -16,6 +15,7 @@ | ||
| 16 | 15 | getApiKeyValidationError, |
| 17 | 16 | isValidAnthropicApiKey, |
| 18 | 17 | } from "./util/apiKeyValidation.js"; |
| 18 | +import { question, questionWithChoices } from "./util/prompt.js"; | |
| 19 | 19 | import { updateAnthropicModelInYaml } from "./util/yamlConfigUpdater.js"; |
| 20 | 20 | |
| 21 | 21 | const CONFIG_PATH = path.join(env.continueHome, "config.yaml"); |
| @@ -97,10 +97,12 @@ | ||
| 97 | 97 | console.log(chalk.white("1. ⏩ Log in with Continue")); |
| 98 | 98 | console.log(chalk.white("2. 🔑 Enter your Anthropic API key")); |
| 99 | 99 | |
| 100 | - const choice = readlineSync.question(chalk.yellow("\nEnter choice (1): "), { | |
| 101 | - limit: ["1", "2", ""], | |
| 102 | - limitMessage: chalk.dim("Please enter 1 or 2"), | |
| 103 | - }); | |
| 100 | + const choice = await questionWithChoices( | |
| 101 | + chalk.yellow("\nEnter choice (1): "), | |
| 102 | + ["1", "2", ""], | |
| 103 | + "1", | |
| 104 | + chalk.dim("Please enter 1 or 2"), | |
| 105 | + ); | |
| 104 | 106 | |
| 105 | 107 | if (choice === "1" || choice === "") { |
| 106 | 108 | const newAuthConfig = await login(); |
| @@ -111,11 +113,8 @@ | ||
| 111 | 113 | const result = await initialize(finalAuthConfig, undefined); |
| 112 | 114 | return { ...result, wasOnboarded: true }; |
| 113 | 115 | } else if (choice === "2") { |
| 114 | - const apiKey = readlineSync.question( | |
| 116 | + const apiKey = await question( | |
| 115 | 117 | chalk.white("\nEnter your Anthropic API key: "), |
| 116 | - { | |
| 117 | - hideEchoBack: true, | |
| 118 | - }, | |
| 119 | 118 | ); |
| 120 | 119 | |
| 121 | 120 | if (!isValidAnthropicApiKey(apiKey)) { |
added extensions/cli/src/util/prompt.test.ts +277 −0
| @@ -0,0 +1,277 @@ | ||
| 1 | +import * as readline from "readline"; | |
| 2 | + | |
| 3 | +import { | |
| 4 | + describe, | |
| 5 | + expect, | |
| 6 | + it, | |
| 7 | + vi, | |
| 8 | + beforeEach, | |
| 9 | + afterEach, | |
| 10 | + MockInstance, | |
| 11 | +} from "vitest"; | |
| 12 | + | |
| 13 | +// Mock readline module | |
| 14 | +vi.mock("readline", () => ({ | |
| 15 | + createInterface: vi.fn(), | |
| 16 | +})); | |
| 17 | + | |
| 18 | +import { question, questionWithChoices } from "./prompt.js"; | |
| 19 | + | |
| 20 | +describe("prompt utilities", () => { | |
| 21 | + let mockInterface: any; | |
| 22 | + let consoleLogSpy: MockInstance<typeof console.log>; | |
| 23 | + let processExitSpy: MockInstance<typeof process.exit>; | |
| 24 | + | |
| 25 | + beforeEach(() => { | |
| 26 | + // Create a mock readline interface | |
| 27 | + mockInterface = { | |
| 28 | + question: vi.fn(), | |
| 29 | + close: vi.fn(), | |
| 30 | + on: vi.fn(), | |
| 31 | + }; | |
| 32 | + | |
| 33 | + // Mock readline.createInterface to return our mock interface | |
| 34 | + (readline.createInterface as any).mockReturnValue(mockInterface); | |
| 35 | + | |
| 36 | + // Mock console.log for limit messages | |
| 37 | + consoleLogSpy = vi.spyOn(console, "log").mockImplementation(() => {}); | |
| 38 | + | |
| 39 | + // Mock process.exit | |
| 40 | + processExitSpy = vi.spyOn(process, "exit").mockImplementation((() => { | |
| 41 | + throw new Error("Process exit"); | |
| 42 | + }) as any); | |
| 43 | + }); | |
| 44 | + | |
| 45 | + afterEach(() => { | |
| 46 | + vi.clearAllMocks(); | |
| 47 | + consoleLogSpy.mockRestore(); | |
| 48 | + processExitSpy.mockRestore(); | |
| 49 | + }); | |
| 50 | + | |
| 51 | + describe("question", () => { | |
| 52 | + it("should return user input", async () => { | |
| 53 | + const promptText = "What is your name? "; | |
| 54 | + const userInput = "John Doe"; | |
| 55 | + | |
| 56 | + // Setup mock to call the callback with user input | |
| 57 | + mockInterface.question.mockImplementation( | |
| 58 | + (prompt: string, callback: (answer: string) => void) => { | |
| 59 | + setTimeout(() => callback(userInput), 0); | |
| 60 | + }, | |
| 61 | + ); | |
| 62 | + | |
| 63 | + const result = await question(promptText); | |
| 64 | + | |
| 65 | + expect(mockInterface.question).toHaveBeenCalledWith( | |
| 66 | + promptText, | |
| 67 | + expect.any(Function), | |
| 68 | + ); | |
| 69 | + expect(mockInterface.close).toHaveBeenCalled(); | |
| 70 | + expect(result).toBe(userInput); | |
| 71 | + }); | |
| 72 | + | |
| 73 | + it("should handle empty input", async () => { | |
| 74 | + const promptText = "Enter something: "; | |
| 75 | + | |
| 76 | + mockInterface.question.mockImplementation( | |
| 77 | + (prompt: string, callback: (answer: string) => void) => { | |
| 78 | + setTimeout(() => callback(""), 0); | |
| 79 | + }, | |
| 80 | + ); | |
| 81 | + | |
| 82 | + const result = await question(promptText); | |
| 83 | + | |
| 84 | + expect(result).toBe(""); | |
| 85 | + }); | |
| 86 | + | |
| 87 | + it("should handle special characters in input", async () => { | |
| 88 | + const promptText = "Enter special chars: "; | |
| 89 | + const userInput = "!@#$%^&*()_+-=[]{}|;':\",./<>?"; | |
| 90 | + | |
| 91 | + mockInterface.question.mockImplementation( | |
| 92 | + (prompt: string, callback: (answer: string) => void) => { | |
| 93 | + setTimeout(() => callback(userInput), 0); | |
| 94 | + }, | |
| 95 | + ); | |
| 96 | + | |
| 97 | + const result = await question(promptText); | |
| 98 | + | |
| 99 | + expect(result).toBe(userInput); | |
| 100 | + }); | |
| 101 | + | |
| 102 | + it.skip("should handle SIGINT (Ctrl+C) by exiting process", async () => { | |
| 103 | + const promptText = "Enter something: "; | |
| 104 | + let sigintHandler: (() => void) | null = null; | |
| 105 | + | |
| 106 | + // Setup mock to capture SIGINT handler | |
| 107 | + mockInterface.on.mockImplementation( | |
| 108 | + (event: string, handler: () => void) => { | |
| 109 | + if (event === "SIGINT") { | |
| 110 | + sigintHandler = handler; | |
| 111 | + } | |
| 112 | + return mockInterface; | |
| 113 | + }, | |
| 114 | + ); | |
| 115 | + | |
| 116 | + // Setup question mock that doesn't resolve immediately | |
| 117 | + mockInterface.question.mockImplementation(() => { | |
| 118 | + // Trigger SIGINT after question is called | |
| 119 | + setTimeout(() => { | |
| 120 | + if (sigintHandler) { | |
| 121 | + sigintHandler(); | |
| 122 | + } | |
| 123 | + }, 10); | |
| 124 | + }); | |
| 125 | + | |
| 126 | + // The promise should reject when SIGINT is triggered | |
| 127 | + await expect(question(promptText)).rejects.toThrow("Process exit"); | |
| 128 | + expect(processExitSpy).toHaveBeenCalledWith(0); | |
| 129 | + }); | |
| 130 | + }); | |
| 131 | + | |
| 132 | + describe("questionWithChoices", () => { | |
| 133 | + it("should accept valid choice", async () => { | |
| 134 | + const promptText = "Choose (1): "; | |
| 135 | + const choices = ["1", "2", "3"]; | |
| 136 | + const userChoice = "2"; | |
| 137 | + | |
| 138 | + mockInterface.question.mockImplementationOnce( | |
| 139 | + (prompt: string, callback: (answer: string) => void) => { | |
| 140 | + setTimeout(() => callback(userChoice), 0); | |
| 141 | + }, | |
| 142 | + ); | |
| 143 | + | |
| 144 | + const result = await questionWithChoices(promptText, choices); | |
| 145 | + expect(result).toBe(userChoice); | |
| 146 | + }); | |
| 147 | + | |
| 148 | + it("should use default choice when empty input is provided", async () => { | |
| 149 | + const promptText = "Choose (1): "; | |
| 150 | + const choices = ["1", "2", "3"]; | |
| 151 | + const defaultChoice = "1"; | |
| 152 | + | |
| 153 | + mockInterface.question.mockImplementationOnce( | |
| 154 | + (prompt: string, callback: (answer: string) => void) => { | |
| 155 | + setTimeout(() => callback(""), 0); | |
| 156 | + }, | |
| 157 | + ); | |
| 158 | + | |
| 159 | + const result = await questionWithChoices( | |
| 160 | + promptText, | |
| 161 | + choices, | |
| 162 | + defaultChoice, | |
| 163 | + ); | |
| 164 | + expect(result).toBe(defaultChoice); | |
| 165 | + }); | |
| 166 | + | |
| 167 | + it("should reject invalid choice and ask again", async () => { | |
| 168 | + const promptText = "Choose (1 or 2): "; | |
| 169 | + const choices = ["1", "2"]; | |
| 170 | + const limitMessage = "Please enter 1 or 2"; | |
| 171 | + | |
| 172 | + let callCount = 0; | |
| 173 | + mockInterface.question.mockImplementation( | |
| 174 | + (prompt: string, callback: (answer: string) => void) => { | |
| 175 | + setTimeout(() => { | |
| 176 | + if (callCount === 0) { | |
| 177 | + callCount++; | |
| 178 | + callback("3"); // Invalid choice | |
| 179 | + } else { | |
| 180 | + callback("1"); // Valid choice | |
| 181 | + } | |
| 182 | + }, 0); | |
| 183 | + }, | |
| 184 | + ); | |
| 185 | + | |
| 186 | + const result = await questionWithChoices( | |
| 187 | + promptText, | |
| 188 | + choices, | |
| 189 | + undefined, | |
| 190 | + limitMessage, | |
| 191 | + ); | |
| 192 | + expect(result).toBe("1"); | |
| 193 | + expect(consoleLogSpy).toHaveBeenCalledWith(limitMessage); | |
| 194 | + }); | |
| 195 | + | |
| 196 | + it("should handle multiple invalid attempts before valid choice", async () => { | |
| 197 | + const promptText = "Choose: "; | |
| 198 | + const choices = ["yes", "no"]; | |
| 199 | + const limitMessage = "Invalid choice"; | |
| 200 | + | |
| 201 | + let callCount = 0; | |
| 202 | + const invalidAnswers = ["maybe", "perhaps"]; | |
| 203 | + mockInterface.question.mockImplementation( | |
| 204 | + (prompt: string, callback: (answer: string) => void) => { | |
| 205 | + setTimeout(() => { | |
| 206 | + if (callCount < invalidAnswers.length) { | |
| 207 | + callback(invalidAnswers[callCount++]); | |
| 208 | + } else { | |
| 209 | + callback("yes"); | |
| 210 | + } | |
| 211 | + }, 0); | |
| 212 | + }, | |
| 213 | + ); | |
| 214 | + | |
| 215 | + const result = await questionWithChoices( | |
| 216 | + promptText, | |
| 217 | + choices, | |
| 218 | + undefined, | |
| 219 | + limitMessage, | |
| 220 | + ); | |
| 221 | + expect(result).toBe("yes"); | |
| 222 | + expect(consoleLogSpy).toHaveBeenCalledTimes(2); | |
| 223 | + }); | |
| 224 | + | |
| 225 | + it("should not show limit message when none provided", async () => { | |
| 226 | + const promptText = "Choose: "; | |
| 227 | + const choices = ["a", "b"]; | |
| 228 | + | |
| 229 | + let callCount = 0; | |
| 230 | + mockInterface.question.mockImplementation( | |
| 231 | + (prompt: string, callback: (answer: string) => void) => { | |
| 232 | + setTimeout(() => { | |
| 233 | + if (callCount === 0) { | |
| 234 | + callCount++; | |
| 235 | + callback("c"); // Invalid | |
| 236 | + } else { | |
| 237 | + callback("a"); // Valid | |
| 238 | + } | |
| 239 | + }, 0); | |
| 240 | + }, | |
| 241 | + ); | |
| 242 | + | |
| 243 | + const result = await questionWithChoices(promptText, choices); | |
| 244 | + expect(result).toBe("a"); | |
| 245 | + expect(consoleLogSpy).not.toHaveBeenCalled(); | |
| 246 | + }); | |
| 247 | + | |
| 248 | + it("should handle empty string as valid choice", async () => { | |
| 249 | + const promptText = "Press Enter to continue or type 'cancel': "; | |
| 250 | + const choices = ["", "cancel"]; | |
| 251 | + | |
| 252 | + mockInterface.question.mockImplementationOnce( | |
| 253 | + (prompt: string, callback: (answer: string) => void) => { | |
| 254 | + setTimeout(() => callback(""), 0); | |
| 255 | + }, | |
| 256 | + ); | |
| 257 | + | |
| 258 | + const result = await questionWithChoices(promptText, choices); | |
| 259 | + expect(result).toBe(""); | |
| 260 | + }); | |
| 261 | + | |
| 262 | + it("should handle choice with spaces", async () => { | |
| 263 | + const promptText = "Choose an option: "; | |
| 264 | + const choices = ["option one", "option two", "option three"]; | |
| 265 | + const userChoice = "option two"; | |
| 266 | + | |
| 267 | + mockInterface.question.mockImplementationOnce( | |
| 268 | + (prompt: string, callback: (answer: string) => void) => { | |
| 269 | + setTimeout(() => callback(userChoice), 0); | |
| 270 | + }, | |
| 271 | + ); | |
| 272 | + | |
| 273 | + const result = await questionWithChoices(promptText, choices); | |
| 274 | + expect(result).toBe(userChoice); | |
| 275 | + }); | |
| 276 | + }); | |
| 277 | +}); |
added extensions/cli/src/util/prompt.ts +60 −0
| @@ -0,0 +1,60 @@ | ||
| 1 | +import * as readline from "readline"; | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Creates a promise-based question prompt using readline | |
| 5 | + * Properly handles backspace and Ctrl+C | |
| 6 | + */ | |
| 7 | +export function question(prompt: string): Promise<string> { | |
| 8 | + return new Promise((resolve) => { | |
| 9 | + const rl = readline.createInterface({ | |
| 10 | + input: process.stdin, | |
| 11 | + output: process.stdout, | |
| 12 | + terminal: true, | |
| 13 | + }); | |
| 14 | + | |
| 15 | + // Handle Ctrl+C properly | |
| 16 | + rl.on("SIGINT", () => { | |
| 17 | + console.log("\n"); | |
| 18 | + rl.close(); | |
| 19 | + process.exit(0); | |
| 20 | + }); | |
| 21 | + | |
| 22 | + rl.question(prompt, (answer) => { | |
| 23 | + rl.close(); | |
| 24 | + resolve(answer); | |
| 25 | + }); | |
| 26 | + }); | |
| 27 | +} | |
| 28 | + | |
| 29 | +/** | |
| 30 | + * Creates a prompt with limited choices | |
| 31 | + * @param prompt The prompt to display | |
| 32 | + * @param choices Array of valid choices | |
| 33 | + * @param defaultChoice Default choice if user presses enter | |
| 34 | + * @param limitMessage Message to show when invalid choice is entered | |
| 35 | + */ | |
| 36 | +export async function questionWithChoices( | |
| 37 | + prompt: string, | |
| 38 | + choices: string[], | |
| 39 | + defaultChoice?: string, | |
| 40 | + limitMessage?: string, | |
| 41 | +): Promise<string> { | |
| 42 | + while (true) { | |
| 43 | + const answer = await question(prompt); | |
| 44 | + | |
| 45 | + // Handle default choice | |
| 46 | + if (answer === "" && defaultChoice !== undefined) { | |
| 47 | + return defaultChoice; | |
| 48 | + } | |
| 49 | + | |
| 50 | + // Check if answer is valid | |
| 51 | + if (choices.includes(answer)) { | |
| 52 | + return answer; | |
| 53 | + } | |
| 54 | + | |
| 55 | + // Show limit message if provided | |
| 56 | + if (limitMessage) { | |
| 57 | + console.log(limitMessage); | |
| 58 | + } | |
| 59 | + } | |
| 60 | +} |