diff --git a/src/commands/clearStateCommand.ts b/src/commands/clearStateCommand.ts new file mode 100644 index 00000000..f4300e8f --- /dev/null +++ b/src/commands/clearStateCommand.ts @@ -0,0 +1,59 @@ +import { FileType, Uri, workspace } from 'vscode'; +import { actions } from '../data/slice'; +import { doesJobAddNewFile } from '../selectors/comparePersistedJobs'; +import { Store } from '../data'; +import { FileService } from '../components/fileService'; +import { homedir } from 'node:os'; +import { join } from 'node:path'; + +type Dependencies = Readonly<{ + store: Store; + fileService: FileService; +}>; + +export const createClearStateCommand = + ({ fileService, store }: Dependencies) => + async () => { + const state = store.getState(); + + store.dispatch(actions.clearState()); + + try { + const uris: Uri[] = []; + + for (const job of Object.values(state.job.entities)) { + if ( + !job || + !doesJobAddNewFile(job.kind) || + job.newContentUri === null || + job.newContentUri.includes('.intuita/cases') + ) { + continue; + } + + uris.push(Uri.parse(job.newContentUri)); + } + + await fileService.deleteFiles({ uris }); + } catch (error) { + console.error(error); + } + + try { + const casesDirectoryUri = Uri.parse( + join(homedir(), '.intuita', 'cases'), + ); + + const files = await workspace.fs.readDirectory(casesDirectoryUri); + + const caseDirectoryUris = files + .filter(([, fileType]) => fileType === FileType.Directory) + .map(([name]) => Uri.joinPath(casesDirectoryUri, name)); + + await fileService.deleteDirectories({ uris: caseDirectoryUris }); + } catch (error) { + console.error(error); + } + + store.dispatch(actions.onStateCleared()); + }; diff --git a/src/extension.ts b/src/extension.ts index 3e467e0b..2a8481fe 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -47,6 +47,7 @@ import { parsePrivateCodemodsEnvelope } from './data/privateCodemodsEnvelopeSche import { GlobalStateTokenStorage, UserService } from './components/userService'; import { HomeDirectoryService } from './data/readHomeDirectoryCases'; import { isLeft } from 'fp-ts/lib/Either'; +import { createClearStateCommand } from './commands/clearStateCommand'; export const enum SEARCH_PARAMS_KEYS { ENGINE = 'engine', @@ -849,46 +850,10 @@ export async function activate(context: vscode.ExtensionContext) { ); context.subscriptions.push( - vscode.commands.registerCommand('intuita.clearState', async () => { - const state = store.getState(); - - store.dispatch(actions.clearState()); - - const uris: vscode.Uri[] = []; - - for (const job of Object.values(state.job.entities)) { - if ( - !job || - !doesJobAddNewFile(job.kind) || - job.newContentUri === null || - job.newContentUri.includes('.intuita/cases') - ) { - continue; - } - - uris.push(vscode.Uri.parse(job.newContentUri)); - } - - await fileService.deleteFiles({ uris }); - - const casesDirectoryUri = vscode.Uri.parse( - join(homedir(), '.intuita', 'cases'), - ); - - const files = await vscode.workspace.fs.readDirectory( - casesDirectoryUri, - ); - - const caseDirectoryUris = files - .filter( - ([, fileType]) => fileType === vscode.FileType.Directory, - ) - .map(([name]) => vscode.Uri.joinPath(casesDirectoryUri, name)); - - await fileService.deleteDirectories({ uris: caseDirectoryUris }); - - store.dispatch(actions.onStateCleared()); - }), + vscode.commands.registerCommand( + 'intuita.clearState', + createClearStateCommand({ fileService, store }), + ), ); context.subscriptions.push(