Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
grzpab committed Dec 7, 2023
1 parent b2e0077 commit 431a4ca
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 40 deletions.
59 changes: 59 additions & 0 deletions src/commands/clearStateCommand.ts
Original file line number Diff line number Diff line change
@@ -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());
};
45 changes: 5 additions & 40 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 431a4ca

Please sign in to comment.