Skip to content

Commit

Permalink
FIXUP
Browse files Browse the repository at this point in the history
  • Loading branch information
vio committed Sep 24, 2023
1 parent 2695dec commit 02aff74
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
4 changes: 3 additions & 1 deletion packages/cli-utils/__mocks__/fs.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { fs } from 'memfs';
import { vol } from 'memfs';

export const { createReadStream } = vol;
4 changes: 2 additions & 2 deletions packages/cli-utils/__mocks__/fs/promises.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { fs } from 'memfs';
import { vol } from 'memfs';

export default fs.promises;
export const { stat, writeFile } = vol.promises;
2 changes: 1 addition & 1 deletion packages/cli-utils/src/__tests__/reports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {});

Expand Down
23 changes: 17 additions & 6 deletions packages/cli-utils/src/fs.ts
Original file line number Diff line number Diff line change
@@ -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 = <T = unknown>(filepath: string): Promise<T> => {
const pipeline = chain([createReadStream(filepath), parser()]);
export const readJSONStream = async <T = unknown>(filepath: string): Promise<T> => {
// 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<string, unknown>): Promise<void> {
return promises.writeFile(filepath, JSON.stringify(data));
return writeFile(filepath, JSON.stringify(data));
}

0 comments on commit 02aff74

Please sign in to comment.