Skip to content

Commit 02aff74

Browse files
committed
FIXUP
1 parent 2695dec commit 02aff74

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

packages/cli-utils/__mocks__/fs.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export { fs } from 'memfs';
1+
import { vol } from 'memfs';
2+
3+
export const { createReadStream } = vol;
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { fs } from 'memfs';
1+
import { vol } from 'memfs';
22

3-
export default fs.promises;
3+
export const { stat, writeFile } = vol.promises;

packages/cli-utils/src/__tests__/reports.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const SOURCE_BASELINE = {
2424
],
2525
};
2626

27-
describe('generateAssets', () => {
27+
describe('generateReports', () => {
2828
test('should generate reports with default options', async () => {
2929
const reports = await generateReports(SOURCE_CURRENT, {});
3030

packages/cli-utils/src/fs.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1-
import { createReadStream, promises } from 'fs';
1+
import { createReadStream } from 'fs';
2+
import { writeFile, stat } from 'fs/promises';
23
import { parser } from 'stream-json';
34
import { chain } from 'stream-chain';
45
import Asm from 'stream-json/Assembler';
56

6-
export const readJSONStream = <T = unknown>(filepath: string): Promise<T> => {
7-
const pipeline = chain([createReadStream(filepath), parser()]);
7+
export const readJSONStream = async <T = unknown>(filepath: string): Promise<T> => {
8+
// Check if the file exists and throw error before creating a stream
9+
await stat(filepath);
10+
11+
const readStream = createReadStream(filepath);
12+
const pipeline = chain([readStream, parser()]);
813
const asm = Asm.connectTo(pipeline);
914

10-
return new Promise((fulfill) => {
11-
asm.on('done', (data) => fulfill(data.current));
15+
return new Promise((resolve, reject) => {
16+
asm.on('done', (data) => {
17+
if (data.current) {
18+
resolve(data.current);
19+
} else {
20+
reject(new Error('Invalid JSON file'));
21+
}
22+
});
1223
});
1324
};
1425

1526
export async function writeJSON(filepath: string, data: Record<string, unknown>): Promise<void> {
16-
return promises.writeFile(filepath, JSON.stringify(data));
27+
return writeFile(filepath, JSON.stringify(data));
1728
}

0 commit comments

Comments
 (0)