Skip to content

Commit 6fdbd49

Browse files
committed
refactor: Use fs.writeFile instead of fs-extra
1 parent 29f9ce0 commit 6fdbd49

File tree

9 files changed

+1409
-5194
lines changed

9 files changed

+1409
-5194
lines changed

package-lock.json

+1,376-5,162
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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+
const { vol } = require('memfs');
2+
3+
module.exports = vol;
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { fs } from 'memfs';
1+
const { vol } = require('memfs');
22

3-
export default fs.promises;
3+
module.exports = 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/baseline.ts

-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import path from 'path';
2-
import fs from 'fs/promises';
32
import findCacheDir from 'find-cache-dir';
43

54
export const BASELINE_STATS_DIR =
@@ -36,15 +35,3 @@ export const getBaselineRelativePath = (
3635
const absoluteFilepath = getBaselinePath(outputPath, outputDir, filepath);
3736
return path.relative(path.join(outputPath, outputDir), absoluteFilepath);
3837
};
39-
40-
export async function readBaseline(baselineFilepath: string): Promise<object> {
41-
const file = await fs.readFile(baselineFilepath, 'utf-8');
42-
return JSON.parse(file);
43-
}
44-
45-
export async function writeBaseline(
46-
baselineFilepath: string,
47-
data: Record<string, unknown>,
48-
): Promise<void> {
49-
return fs.writeFile(baselineFilepath, JSON.stringify(data));
50-
}

packages/cli-utils/src/fs.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
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
};
25+
26+
export async function writeJSON(filepath: string, data: Record<string, unknown>): Promise<void> {
27+
return writeFile(filepath, JSON.stringify(data));
28+
}

packages/cli-utils/src/reports.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import filter from '@bundle-stats/plugin-webpack-filter';
55

66
import * as TEXT from './text';
77
import { createArtifacts } from './create-artifacts';
8-
import { getBaselinePath, getBaselineRelativePath, readBaseline } from './baseline';
8+
import { readJSONStream } from './fs';
9+
import { getBaselinePath, getBaselineRelativePath } from './baseline';
910

1011
export function getReportInfo(report: any): any {
1112
return report?.insights?.webpack?.assetsSizeTotal?.data;
@@ -110,8 +111,8 @@ export const generateReports = async (
110111

111112
if (compare) {
112113
try {
113-
const baselineStatsData = await readBaseline(baselineAbsolutePath);
114-
baselineStats = filter(baselineStatsData);
114+
const baselineStatsData = await readJSONStream(baselineAbsolutePath);
115+
baselineStats = filter(baselineStatsData as any);
115116

116117
if (!options.silent) {
117118
logger.info(`${TEXT.BASELINE_READING} ${baselinePath}`);

packages/cli/package.json

-2
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,13 @@
5757
"@bundle-stats/utils": "^4.7.0",
5858
"boxen": "^5.0.0",
5959
"core-js": "^3.21.0",
60-
"fs-extra": "^11.0.0",
6160
"listr2": "^5.0.1",
6261
"lodash": "^4.17.0",
6362
"update-notifier": "^5.0.0",
6463
"yargs": "^17.4.0"
6564
},
6665
"devDependencies": {
6766
"@playwright/test": "1.38.1",
68-
"@types/fs-extra": "^11.0.0",
6967
"@types/listr": "^0.14.4",
7068
"@types/yargs": "^17.0.20",
7169
"http-server": "14.1.1",

packages/cli/src/run.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint-disable no-console */
22
import path from 'path';
3-
import { outputFile } from 'fs-extra';
43
import { Listr } from 'listr2';
54
import { get } from 'lodash';
65
import boxen from 'boxen';
@@ -25,9 +24,8 @@ import {
2524
getBaselinePath,
2625
getBaselineRelativePath,
2726
getReportInfo,
28-
readBaseline,
2927
readJSONStream,
30-
writeBaseline,
28+
writeJSON,
3129
} from '@bundle-stats/cli-utils';
3230

3331
import LOCALES from './locales.json';
@@ -105,7 +103,7 @@ export default async function run(options: RunOptions): Promise<void> {
105103
let baselineStats = {};
106104

107105
try {
108-
baselineStats = await readBaseline(baselineAbsolutePath);
106+
baselineStats = await readJSONStream(baselineAbsolutePath);
109107
ctx.baselineStats = baselineStats;
110108
} catch (err) {
111109
return TEXT.BASELINE_MISSING;
@@ -120,7 +118,7 @@ export default async function run(options: RunOptions): Promise<void> {
120118
const stats = get(ctx, 'sources[0]webpack');
121119
const filteredWebpackStats = webpackFilter(stats) as Record<string, unknown>;
122120

123-
return writeBaseline(baselineAbsolutePath, filteredWebpackStats).then(() => {
121+
return writeJSON(baselineAbsolutePath, filteredWebpackStats).then(() => {
124122
// eslint-disable-next-line no-param-reassign
125123
task.title = `${task.title} (${baselinePath})`;
126124
});
@@ -148,7 +146,7 @@ export default async function run(options: RunOptions): Promise<void> {
148146
title: filename,
149147
task: async () => {
150148
const filepath = path.join(outDir, filename);
151-
await outputFile(filepath, output);
149+
await writeJSON(filepath, output);
152150

153151
ctx.output = [...(ctx.output ? ctx.output : []), filepath];
154152
},

0 commit comments

Comments
 (0)