From fa4bc84414d566bd94fb478f1d2db147562caf18 Mon Sep 17 00:00:00 2001 From: Jason Dent Date: Tue, 26 Jul 2022 19:54:27 +0200 Subject: [PATCH] wire up readFile --- packages/cspell-io/src/CSpellIONode.test.ts | 14 ++++++++--- packages/cspell-io/src/CSpellIONode.ts | 27 ++++++++++++++++++--- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/packages/cspell-io/src/CSpellIONode.test.ts b/packages/cspell-io/src/CSpellIONode.test.ts index e5cacad2380..5c5b80994ab 100644 --- a/packages/cspell-io/src/CSpellIONode.test.ts +++ b/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', () => { @@ -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 { diff --git a/packages/cspell-io/src/CSpellIONode.ts b/packages/cspell-io/src/CSpellIONode.ts index f51c0f719db..bed5d2d3f86 100644 --- a/packages/cspell-io/src/CSpellIONode.ts +++ b/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 { - throw new ErrorNotImplemented('readFile'); + constructor(readonly serviceBus = new ServiceBus()) { + registerHandlers(serviceBus); + } + + readFile(uriOrFilename: string): Promise { + 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 {