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

Commit

Permalink
feat: INT-2270 connected @intuita-inc/utilities to the VSCE (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
grzpab authored Dec 7, 2023
1 parent 22c2710 commit 4cb374f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 569 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@
},
"dependencies": {
"@effect/schema": "0.30.3",
"@intuita-inc/utilities": "^1.0.1",
"@reduxjs/toolkit": "^1.9.5",
"@vscode/extension-telemetry": "^0.7.7",
"axios": "^1.2.2",
Expand Down
33 changes: 33 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 31 additions & 51 deletions src/data/readHomeDirectoryCases.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { createReadStream } from 'fs';
import { homedir } from 'os';
import { join } from 'path';
import { FileType, Uri, window, workspace } from 'vscode';
import { readSurfaceAgnosticCase } from './readSurfaceAgnosticCase';
import { Case, CaseHash, caseHashCodec } from '../cases/types';
import { Job, JobKind, jobHashCodec } from '../jobs/types';
import { parseSurfaceAgnosticCase } from './schemata/surfaceAgnosticCaseSchema';
import {
JOB_KIND,
parseSurfaceAgnosticJob,
} from './schemata/surfaceAgnosticJobSchema';
import { CodemodEntry } from '../codemods/types';
import EventEmitter from 'events';
import { MessageBus, MessageKind } from '../components/messageBus';
import { Store } from '.';
import { actions } from './slice';
import {
CaseReadingService,
JOB_KIND,
SurfaceAgnosticJob,
} from '@intuita-inc/utilities';

interface HomeDirectoryEventEmitter extends EventEmitter {
emit(event: 'start'): boolean;
Expand All @@ -35,48 +33,24 @@ const readHomeDirectoryCase = async (
codemodEntities: Record<string, CodemodEntry | undefined>,
caseDataPath: string,
) => {
const readStream = createReadStream(caseDataPath);

await new Promise<void>((resolve, reject) => {
let timedOut = false;

const timeout = setTimeout(() => {
timedOut = true;
reject(
`Opening the read stream for ${caseDataPath} timed out after 1s.`,
);
}, 1000);

readStream.once('open', () => {
if (timedOut) {
return;
}

clearTimeout(timeout);
resolve();
});
});
const caseReadingService = new CaseReadingService(caseDataPath);

let kase: Case | null = null;

const fileEventEmitter = readSurfaceAgnosticCase(readStream);

fileEventEmitter.once('case', (data: unknown) => {
const surfaceAgnosticCase = parseSurfaceAgnosticCase(data);

caseReadingService.once('case', (surfaceAgnosticCase) => {
if (
!surfaceAgnosticCase.absoluteTargetPath.startsWith(rootUri.fsPath)
) {
console.info(
'The current case does not belong to the opened workspace',
);
fileEventEmitter.emit('close');
caseReadingService.emit('finish');
return;
}

if (!caseHashCodec.is(surfaceAgnosticCase.caseHashDigest)) {
console.error('Could not validate the case hash digest');
fileEventEmitter.emit('close');
caseReadingService.emit('finish');
return;
}

Expand All @@ -94,22 +68,20 @@ const readHomeDirectoryCase = async (
homeDirectoryEventEmitter.emit('job', kase, []);
});

const jobHandler = (data: unknown) => {
const surfaceAgnosticJob = parseSurfaceAgnosticJob(data);

const jobHandler = (surfaceAgnosticJob: SurfaceAgnosticJob) => {
if (!kase) {
console.error('You need to have a case to create a job');
fileEventEmitter.emit('close');
caseReadingService.emit('finish');
return;
}

if (!jobHashCodec.is(surfaceAgnosticJob.jobHashDigest)) {
console.error('Could not validate the job hash digest');
fileEventEmitter.emit('close');
caseReadingService.emit('finish');
return;
}

if (surfaceAgnosticJob.kind === JOB_KIND.REWRITE_FILE) {
if (surfaceAgnosticJob.kind === JOB_KIND.UPDATE_FILE) {
const job: Job = {
hash: surfaceAgnosticJob.jobHashDigest,
originalNewContent: null,
Expand All @@ -118,8 +90,8 @@ const readHomeDirectoryCase = async (
caseHashDigest: kase.hash,
// variant
kind: JobKind.rewriteFile,
oldUri: Uri.file(surfaceAgnosticJob.oldUri),
newContentUri: Uri.file(surfaceAgnosticJob.newUri),
oldUri: Uri.file(surfaceAgnosticJob.pathUri),
newContentUri: Uri.file(surfaceAgnosticJob.newDataUri),
newUri: null,
};

Expand All @@ -129,7 +101,7 @@ const readHomeDirectoryCase = async (
// TODO implement more job kinds
};

fileEventEmitter.on('job', jobHandler);
caseReadingService.on('job', jobHandler);

const TIMEOUT = 120_000;

Expand All @@ -139,29 +111,29 @@ const readHomeDirectoryCase = async (
const timeout = setTimeout(() => {
timedOut = true;

fileEventEmitter.off('job', jobHandler);
fileEventEmitter.emit('close');
caseReadingService.off('job', jobHandler);
caseReadingService.emit('finish');

reject(new Error(`Reading the case timed out after ${TIMEOUT}ms`));
}, TIMEOUT);

fileEventEmitter.once('error', (error) => {
caseReadingService.once('error', (error) => {
if (timedOut) {
return;
}

fileEventEmitter.off('job', jobHandler);
caseReadingService.off('job', jobHandler);

clearTimeout(timeout);
reject(error);
});

fileEventEmitter.once('end', () => {
caseReadingService.once('finish', () => {
if (timedOut) {
return;
}

fileEventEmitter.off('job', jobHandler);
caseReadingService.off('job', jobHandler);

clearTimeout(timeout);

Expand All @@ -172,6 +144,8 @@ const readHomeDirectoryCase = async (

resolve();
});

caseReadingService.initialize().catch((error) => reject(error));
});
};

Expand Down Expand Up @@ -230,7 +204,7 @@ export const readHomeDirectoryCases = async (
.filter(([, fileType]) => fileType === FileType.Directory)
.map(([name]) => join(casesDirectoryPath, name, 'case.data'));

await Promise.allSettled(
const results = await Promise.allSettled(
caseDataPaths.map((path) =>
readHomeDirectoryCase(
eventEmitter,
Expand All @@ -240,6 +214,12 @@ export const readHomeDirectoryCases = async (
),
),
);

for (const result of results) {
if (result.status === 'rejected') {
console.error(result.reason);
}
}
} catch (error) {
console.error(error);
}
Expand Down
Loading

0 comments on commit 4cb374f

Please sign in to comment.