Skip to content

Commit

Permalink
wire up readFile
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Jul 28, 2022
1 parent 4bce54e commit fa4bc84
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
14 changes: 11 additions & 3 deletions packages/cspell-io/src/CSpellIONode.test.ts
@@ -1,6 +1,6 @@
import { CSpellIONode } from './CSpellIONode';

// const sc = expect.stringContaining;
const sc = expect.stringContaining;

describe('CSpellIONode', () => {
test('constructor', () => {
Expand All @@ -10,10 +10,18 @@ describe('CSpellIONode', () => {

test.each`
filename | expected
${__filename} | ${'Method readFile is not supported.'}
${__filename} | ${sc('This bit of text')}
`('readFile', async ({ filename, expected }) => {
const cspellIo = new CSpellIONode();
await expect(() => cspellIo.readFile(filename)).toThrow(expected);
await expect(cspellIo.readFile(filename)).resolves.toEqual(expected);
});

test.each`
filename | expected
${__filename} | ${'Unhandled Request: fs:readFileSync' /* sc('This bit of text') */}
`('readFileSync', ({ filename, expected }) => {
const cspellIo = new CSpellIONode();
expect(() => cspellIo.readFileSync(filename)).toThrow(expected);
});

// readFile(_uriOrFilename: string): Promise<string> {
Expand Down
27 changes: 23 additions & 4 deletions packages/cspell-io/src/CSpellIONode.ts
@@ -1,13 +1,32 @@
import { isServiceResponseSuccess, ServiceBus } from '@cspell/cspell-service-bus';
import { compareStats } from './common/stat';
import { CSpellIO } from './CSpellIO';
import { ErrorNotImplemented } from './errors/ErrorNotImplemented';
import { compareStats } from './common/stat';
import { registerHandlers } from './handlers/node/file';
import { Stats } from './models/Stats';
import { toURL } from './node/file/util';
import { RequestFsReadFile, RequestFsReadFileSync } from './requests';

export class CSpellIONode implements CSpellIO {
readFile(_uriOrFilename: string): Promise<string> {
throw new ErrorNotImplemented('readFile');
constructor(readonly serviceBus = new ServiceBus()) {
registerHandlers(serviceBus);
}

readFile(uriOrFilename: string): Promise<string> {
const url = toURL(uriOrFilename);
const res = this.serviceBus.dispatch(RequestFsReadFile.create({ url }));
if (!isServiceResponseSuccess(res)) {
throw res.error || new ErrorNotImplemented('readFile');
}
return res.value;
}
readFileSync(_uriOrFilename: string): string {
readFileSync(uriOrFilename: string): string {
const url = toURL(uriOrFilename);
const res = this.serviceBus.dispatch(RequestFsReadFileSync.create({ url }));
if (!isServiceResponseSuccess(res)) {
throw res.error || new ErrorNotImplemented('readFileSync');
}
return res.value;
throw new ErrorNotImplemented('readFileSync');
}
writeFile(_uriOrFilename: string, _content: string): Promise<void> {
Expand Down

0 comments on commit fa4bc84

Please sign in to comment.