|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import * as path from "path"; |
| 3 | +import * as fs from "fs"; |
| 4 | + |
| 5 | +export class TestCaseRecorder { |
| 6 | + active: boolean = false; |
| 7 | + outPath: string | null = null; |
| 8 | + talonCommand: string | null = null; |
| 9 | + workspacePath: string | null; |
| 10 | + workSpaceFolder: string | null; |
| 11 | + fixtureRoot: string | null; |
| 12 | + fixtureSubdirectory: string | null = null; |
| 13 | + |
| 14 | + constructor() { |
| 15 | + this.workspacePath = |
| 16 | + vscode.workspace.workspaceFolders?.[0].uri.path ?? null; |
| 17 | + |
| 18 | + this.workSpaceFolder = this.workspacePath |
| 19 | + ? path.basename(this.workspacePath) |
| 20 | + : null; |
| 21 | + |
| 22 | + this.fixtureRoot = this.workspacePath |
| 23 | + ? path.join(this.workspacePath, "src/test/suite/fixtures/recorded") |
| 24 | + : null; |
| 25 | + } |
| 26 | + |
| 27 | + start(): Promise<void> { |
| 28 | + return this.promptTalonCommand(); |
| 29 | + } |
| 30 | + |
| 31 | + private async promptTalonCommand(): Promise<void> { |
| 32 | + const result = await vscode.window.showInputBox({ |
| 33 | + prompt: "Talon Command", |
| 34 | + ignoreFocusOut: true, |
| 35 | + validateInput: (input) => (input.trim().length > 0 ? null : "Required"), |
| 36 | + }); |
| 37 | + |
| 38 | + // Inputs return undefined when a user cancels by hitting 'escape' |
| 39 | + if (result === undefined) { |
| 40 | + this.active = false; |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + this.talonCommand = result; |
| 45 | + return this.promptSubdirectory(); |
| 46 | + } |
| 47 | + |
| 48 | + private async promptSubdirectory(): Promise<void> { |
| 49 | + if ( |
| 50 | + this.workspacePath == null || |
| 51 | + this.fixtureRoot == null || |
| 52 | + this.workSpaceFolder !== "cursorless-vscode" |
| 53 | + ) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const subdirectories = fs |
| 58 | + .readdirSync(this.fixtureRoot, { withFileTypes: true }) |
| 59 | + .filter((item) => item.isDirectory()) |
| 60 | + .map((directory) => directory.name); |
| 61 | + |
| 62 | + const createNewSubdirectory = "Create new folder →"; |
| 63 | + const subdirectorySelection = await vscode.window.showQuickPick([ |
| 64 | + ...subdirectories, |
| 65 | + createNewSubdirectory, |
| 66 | + ]); |
| 67 | + |
| 68 | + if (subdirectorySelection === undefined) { |
| 69 | + return this.promptTalonCommand(); // go back a prompt |
| 70 | + } else if (subdirectorySelection === createNewSubdirectory) { |
| 71 | + return this.promptNewSubdirectory(); |
| 72 | + } else { |
| 73 | + this.fixtureSubdirectory = subdirectorySelection; |
| 74 | + return this.promptFileName(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private async promptNewSubdirectory(): Promise<void> { |
| 79 | + if (this.fixtureRoot == null) { |
| 80 | + throw new Error("Missing fixture root. Not in cursorless workspace?"); |
| 81 | + } |
| 82 | + |
| 83 | + const subdirectory = await vscode.window.showInputBox({ |
| 84 | + prompt: "New Folder Name", |
| 85 | + ignoreFocusOut: true, |
| 86 | + validateInput: (input) => (input.trim().length > 0 ? null : "Required"), |
| 87 | + }); |
| 88 | + |
| 89 | + if (subdirectory === undefined) { |
| 90 | + return this.promptSubdirectory(); // go back a prompt |
| 91 | + } |
| 92 | + |
| 93 | + this.fixtureSubdirectory = subdirectory; |
| 94 | + return this.promptFileName(); |
| 95 | + } |
| 96 | + |
| 97 | + private async promptFileName(): Promise<void> { |
| 98 | + if (this.fixtureRoot == null) { |
| 99 | + throw new Error("Missing fixture root. Not in cursorless workspace?"); |
| 100 | + } |
| 101 | + |
| 102 | + const filename = await vscode.window.showInputBox({ |
| 103 | + prompt: "Fixture Filename", |
| 104 | + }); |
| 105 | + |
| 106 | + if (filename === undefined || this.fixtureSubdirectory == null) { |
| 107 | + this.promptSubdirectory(); // go back a prompt |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const targetDirectory = path.join( |
| 112 | + this.fixtureRoot, |
| 113 | + this.fixtureSubdirectory, |
| 114 | + `${filename}.yml` |
| 115 | + ); |
| 116 | + |
| 117 | + if (!fs.existsSync(targetDirectory)) { |
| 118 | + fs.mkdirSync(targetDirectory); |
| 119 | + } |
| 120 | + |
| 121 | + this.outPath = path.join(targetDirectory, `${filename}.yml`); |
| 122 | + } |
| 123 | +} |
0 commit comments