11import { readFile , writeFile } from "fs/promises" ;
22import parse from "node-html-parser" ;
3- import * as vscode from "vscode" ;
4- import { Graph } from "../typings/Types" ;
53import path = require( "path" ) ;
64import produce from "immer" ;
75import { sortBy } from "lodash" ;
@@ -28,104 +26,76 @@ interface CheatSheetCommandArg {
2826 outputPath : string ;
2927}
3028
31- export default class Cheatsheet {
32- private disposables : vscode . Disposable [ ] = [ ] ;
29+ export async function showCheatsheet ( {
30+ version,
31+ spokenFormInfo,
32+ outputPath,
33+ } : CheatSheetCommandArg ) {
34+ if ( version !== 0 ) {
35+ throw new Error ( `Unsupported cheatsheet api version: ${ version } ` ) ;
36+ }
3337
34- constructor ( private graph : Graph ) {
35- ide ( ) . disposeOnExit ( this ) ;
38+ const cheatsheetPath = path . join (
39+ ide ( ) . assetsRoot ,
40+ "cursorless-nx" ,
41+ "dist" ,
42+ "apps" ,
43+ "cheatsheet-local" ,
44+ "index.html" ,
45+ ) ;
3646
37- this . showCheatsheet = this . showCheatsheet . bind ( this ) ;
38- this . updateDefaults = this . updateDefaults . bind ( this ) ;
39- }
47+ const cheatsheetContent = ( await readFile ( cheatsheetPath ) ) . toString ( ) ;
4048
41- init ( ) {
42- this . disposables . push (
43- vscode . commands . registerCommand (
44- "cursorless.showCheatsheet" ,
45- this . showCheatsheet ,
46- ) ,
47- vscode . commands . registerCommand (
48- "cursorless.internal.updateCheatsheetDefaults" ,
49- this . updateDefaults ,
50- ) ,
51- ) ;
52- }
49+ const root = parse ( cheatsheetContent ) ;
5350
54- private async showCheatsheet ( {
55- version,
51+ root . getElementById (
52+ "cheatsheet-data" ,
53+ ) . textContent = `document.cheatsheetData = ${ JSON . stringify (
5654 spokenFormInfo ,
57- outputPath,
58- } : CheatSheetCommandArg ) {
59- if ( version !== 0 ) {
60- throw new Error ( `Unsupported cheatsheet api version: ${ version } ` ) ;
61- }
62-
63- const cheatsheetPath = path . join (
64- ide ( ) . assetsRoot ,
65- "cursorless-nx" ,
66- "dist" ,
67- "apps" ,
68- "cheatsheet-local" ,
69- "index.html" ,
70- ) ;
71-
72- const cheatsheetContent = ( await readFile ( cheatsheetPath ) ) . toString ( ) ;
55+ ) } ;`;
7356
74- const root = parse ( cheatsheetContent ) ;
57+ await writeFile ( outputPath , root . toString ( ) ) ;
58+ }
7559
76- root . getElementById (
77- "cheatsheet-data" ,
78- ) . textContent = `document.cheatsheetData = ${ JSON . stringify (
79- spokenFormInfo ,
80- ) } ;`;
60+ /**
61+ * Updates the default spoken forms stored in `defaults.json` for
62+ * development.
63+ * @param spokenFormInfo The new value to use for default spoken forms.
64+ */
65+ export async function updateDefaults ( spokenFormInfo : CheatsheetInfo ) {
66+ const { runMode, assetsRoot, workspaceFolders } = ide ( ) ;
8167
82- await writeFile ( outputPath , root . toString ( ) ) ;
83- }
68+ const workspacePath =
69+ runMode === "development"
70+ ? assetsRoot
71+ : workspaceFolders ?. [ 0 ] . uri . path ?? null ;
8472
85- /**
86- * Updates the default spoken forms stored in `defaults.json` for
87- * development.
88- * @param spokenFormInfo The new value to use for default spoken forms.
89- */
90- private async updateDefaults ( spokenFormInfo : CheatsheetInfo ) {
91- const { runMode, assetsRoot, workspaceFolders } = ide ( ) ;
92-
93- const workspacePath =
94- runMode === "development"
95- ? assetsRoot
96- : workspaceFolders ?. [ 0 ] . uri . path ?? null ;
97-
98- if ( workspacePath == null ) {
99- throw new Error (
100- "Please update defaults from Cursorless workspace or running in debug" ,
101- ) ;
102- }
103-
104- const defaultsPath = path . join (
105- workspacePath ,
106- "cursorless-nx" ,
107- "libs" ,
108- "cheatsheet" ,
109- "src" ,
110- "lib" ,
111- "data" ,
112- "sampleSpokenFormInfos" ,
113- "defaults.json" ,
73+ if ( workspacePath == null ) {
74+ throw new Error (
75+ "Please update defaults from Cursorless workspace or running in debug" ,
11476 ) ;
77+ }
11578
116- const outputObject = produce ( spokenFormInfo , ( draft ) => {
117- draft . sections = sortBy ( draft . sections , "id" ) ;
118- draft . sections . forEach ( ( section ) => {
119- section . items = sortBy ( section . items , "id" ) ;
120- } ) ;
79+ const defaultsPath = path . join (
80+ workspacePath ,
81+ "cursorless-nx" ,
82+ "libs" ,
83+ "cheatsheet" ,
84+ "src" ,
85+ "lib" ,
86+ "data" ,
87+ "sampleSpokenFormInfos" ,
88+ "defaults.json" ,
89+ ) ;
90+
91+ const outputObject = produce ( spokenFormInfo , ( draft ) => {
92+ draft . sections = sortBy ( draft . sections , "id" ) ;
93+ draft . sections . forEach ( ( section ) => {
94+ section . items = sortBy ( section . items , "id" ) ;
12195 } ) ;
96+ } ) ;
12297
123- await writeFile ( defaultsPath , JSON . stringify ( outputObject , null , "\t" ) ) ;
124- }
125-
126- dispose ( ) {
127- this . disposables . forEach ( ( { dispose } ) => dispose ( ) ) ;
128- }
98+ await writeFile ( defaultsPath , JSON . stringify ( outputObject , null , "\t" ) ) ;
12999}
130100
131101// FIXME: Stop duplicating these types once we have #945
0 commit comments