|
1 | | -import { LATEST_VERSION, SimpleScopeTypeType } from "@cursorless/common"; |
2 | | -import { readFile } from "fs/promises"; |
3 | | -import { homedir } from "os"; |
4 | | -import { SpeakableSurroundingPairName } from "../SpokenFormMap"; |
5 | | -import * as path from "path"; |
| 1 | +import { |
| 2 | + Disposer, |
| 3 | + FileSystem, |
| 4 | + LATEST_VERSION, |
| 5 | + Notifier, |
| 6 | + isTesting, |
| 7 | +} from "@cursorless/common"; |
| 8 | +import * as crypto from "crypto"; |
| 9 | +import { mkdir, readFile } from "fs/promises"; |
| 10 | +import * as os from "os"; |
6 | 11 |
|
7 | | -export const spokenFormsPath = path.join( |
8 | | - homedir(), |
9 | | - ".cursorless", |
10 | | - "spokenForms.json", |
11 | | -); |
| 12 | +import * as path from "path"; |
| 13 | +import { |
| 14 | + NeedsInitialTalonUpdateError, |
| 15 | + SpokenFormEntry, |
| 16 | + TalonSpokenForms, |
| 17 | +} from "./SpokenFormEntry"; |
12 | 18 |
|
13 | | -export interface CustomRegexSpokenFormEntry { |
14 | | - type: "customRegex"; |
15 | | - id: string; |
16 | | - spokenForms: string[]; |
| 19 | +interface TalonSpokenFormsPayload { |
| 20 | + version: number; |
| 21 | + entries: SpokenFormEntry[]; |
17 | 22 | } |
18 | 23 |
|
19 | | -export interface PairedDelimiterSpokenFormEntry { |
20 | | - type: "pairedDelimiter"; |
21 | | - id: SpeakableSurroundingPairName; |
22 | | - spokenForms: string[]; |
23 | | -} |
| 24 | +export class TalonSpokenFormsJsonReader implements TalonSpokenForms { |
| 25 | + private disposer = new Disposer(); |
| 26 | + private notifier = new Notifier(); |
| 27 | + private spokenFormsPath; |
24 | 28 |
|
25 | | -export interface SimpleScopeTypeTypeSpokenFormEntry { |
26 | | - type: "simpleScopeTypeType"; |
27 | | - id: SimpleScopeTypeType; |
28 | | - spokenForms: string[]; |
29 | | -} |
| 29 | + constructor(private fileSystem: FileSystem) { |
| 30 | + const cursorlessDir = isTesting() |
| 31 | + ? path.join(os.tmpdir(), crypto.randomBytes(16).toString("hex")) |
| 32 | + : path.join(os.homedir(), ".cursorless"); |
30 | 33 |
|
31 | | -export type SpokenFormEntry = |
32 | | - | CustomRegexSpokenFormEntry |
33 | | - | PairedDelimiterSpokenFormEntry |
34 | | - | SimpleScopeTypeTypeSpokenFormEntry; |
| 34 | + this.spokenFormsPath = path.join(cursorlessDir, "spokenForms.json"); |
35 | 35 |
|
36 | | -export async function getSpokenFormEntries(): Promise<SpokenFormEntry[]> { |
37 | | - const payload = JSON.parse(await readFile(spokenFormsPath, "utf-8")); |
| 36 | + this.init(); |
| 37 | + } |
| 38 | + |
| 39 | + private async init() { |
| 40 | + const parentDir = path.dirname(this.spokenFormsPath); |
| 41 | + await mkdir(parentDir, { recursive: true }); |
| 42 | + this.disposer.push( |
| 43 | + this.fileSystem.watch(parentDir, () => this.notifier.notifyListeners()), |
| 44 | + ); |
| 45 | + } |
38 | 46 |
|
39 | 47 | /** |
40 | | - * This assignment is to ensure that the compiler will error if we forget to |
41 | | - * handle spokenForms.json when we bump the command version. |
| 48 | + * Registers a callback to be run when the spoken forms change. |
| 49 | + * @param callback The callback to run when the scope ranges change |
| 50 | + * @returns A {@link Disposable} which will stop the callback from running |
42 | 51 | */ |
43 | | - const latestCommandVersion: 6 = LATEST_VERSION; |
| 52 | + onDidChange = this.notifier.registerListener; |
44 | 53 |
|
45 | | - if (payload.version !== latestCommandVersion) { |
46 | | - // In the future, we'll need to handle migrations. Not sure exactly how yet. |
47 | | - throw new Error( |
48 | | - `Invalid spoken forms version. Expected ${LATEST_VERSION} but got ${payload.version}`, |
49 | | - ); |
| 54 | + async getSpokenFormEntries(): Promise<SpokenFormEntry[]> { |
| 55 | + let payload: TalonSpokenFormsPayload; |
| 56 | + try { |
| 57 | + payload = JSON.parse(await readFile(this.spokenFormsPath, "utf-8")); |
| 58 | + } catch (err) { |
| 59 | + if ((err as any)?.code === "ENOENT") { |
| 60 | + throw new NeedsInitialTalonUpdateError( |
| 61 | + `Custom spoken forms file not found at ${this.spokenFormsPath}. Using default spoken forms.`, |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + throw err; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * This assignment is to ensure that the compiler will error if we forget to |
| 70 | + * handle spokenForms.json when we bump the command version. |
| 71 | + */ |
| 72 | + const latestCommandVersion: 6 = LATEST_VERSION; |
| 73 | + |
| 74 | + if (payload.version !== latestCommandVersion) { |
| 75 | + // In the future, we'll need to handle migrations. Not sure exactly how yet. |
| 76 | + throw new Error( |
| 77 | + `Invalid spoken forms version. Expected ${LATEST_VERSION} but got ${payload.version}`, |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + return payload.entries; |
50 | 82 | } |
51 | 83 |
|
52 | | - return payload.entries; |
| 84 | + dispose() { |
| 85 | + this.disposer.dispose(); |
| 86 | + } |
53 | 87 | } |
0 commit comments