From 02aff7430365da02d889c7b1de8d80ca5be2655b Mon Sep 17 00:00:00 2001 From: Vio Date: Mon, 25 Sep 2023 00:02:41 +0200 Subject: [PATCH] FIXUP --- packages/cli-utils/__mocks__/fs.ts | 4 +++- packages/cli-utils/__mocks__/fs/promises.ts | 4 ++-- .../cli-utils/src/__tests__/reports.test.ts | 2 +- packages/cli-utils/src/fs.ts | 23 ++++++++++++++----- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/packages/cli-utils/__mocks__/fs.ts b/packages/cli-utils/__mocks__/fs.ts index 60e71e14c3..fa56f7a447 100644 --- a/packages/cli-utils/__mocks__/fs.ts +++ b/packages/cli-utils/__mocks__/fs.ts @@ -1 +1,3 @@ -export { fs } from 'memfs'; +import { vol } from 'memfs'; + +export const { createReadStream } = vol; diff --git a/packages/cli-utils/__mocks__/fs/promises.ts b/packages/cli-utils/__mocks__/fs/promises.ts index 916c47adc6..9991c15e17 100644 --- a/packages/cli-utils/__mocks__/fs/promises.ts +++ b/packages/cli-utils/__mocks__/fs/promises.ts @@ -1,3 +1,3 @@ -import { fs } from 'memfs'; +import { vol } from 'memfs'; -export default fs.promises; +export const { stat, writeFile } = vol.promises; diff --git a/packages/cli-utils/src/__tests__/reports.test.ts b/packages/cli-utils/src/__tests__/reports.test.ts index 41156d4a9f..c4796592d7 100644 --- a/packages/cli-utils/src/__tests__/reports.test.ts +++ b/packages/cli-utils/src/__tests__/reports.test.ts @@ -24,7 +24,7 @@ const SOURCE_BASELINE = { ], }; -describe('generateAssets', () => { +describe('generateReports', () => { test('should generate reports with default options', async () => { const reports = await generateReports(SOURCE_CURRENT, {}); diff --git a/packages/cli-utils/src/fs.ts b/packages/cli-utils/src/fs.ts index e941769e99..808302af71 100644 --- a/packages/cli-utils/src/fs.ts +++ b/packages/cli-utils/src/fs.ts @@ -1,17 +1,28 @@ -import { createReadStream, promises } from 'fs'; +import { createReadStream } from 'fs'; +import { writeFile, stat } from 'fs/promises'; import { parser } from 'stream-json'; import { chain } from 'stream-chain'; import Asm from 'stream-json/Assembler'; -export const readJSONStream = (filepath: string): Promise => { - const pipeline = chain([createReadStream(filepath), parser()]); +export const readJSONStream = async (filepath: string): Promise => { + // Check if the file exists and throw error before creating a stream + await stat(filepath); + + const readStream = createReadStream(filepath); + const pipeline = chain([readStream, parser()]); const asm = Asm.connectTo(pipeline); - return new Promise((fulfill) => { - asm.on('done', (data) => fulfill(data.current)); + return new Promise((resolve, reject) => { + asm.on('done', (data) => { + if (data.current) { + resolve(data.current); + } else { + reject(new Error('Invalid JSON file')); + } + }); }); }; export async function writeJSON(filepath: string, data: Record): Promise { - return promises.writeFile(filepath, JSON.stringify(data)); + return writeFile(filepath, JSON.stringify(data)); }