Skip to content

Commit

Permalink
Merge pull request #3650 from relative-ci/3425-move-async-json-parsin…
Browse files Browse the repository at this point in the history
…g-to-cli-utils

Move async JSON  read stream to cli-utils
  • Loading branch information
vio authored Jul 29, 2023
2 parents 2f32b58 + 0a91e9c commit ba60929
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module.exports = {
'prettier/prettier': 'warn',
'no-unused-vars': ['warn', { varsIgnorePattern: '^[_]{1,}$' }],
'implicit-arrow-linebreak': 'warn',
indent: 'warn',
'react/function-component-definition': [
'warn',
{
Expand Down Expand Up @@ -87,6 +88,7 @@ module.exports = {
'react/require-default-props': 'off',
'@typescript-eslint/comma-dangle': 'warn',
'@typescript-eslint/no-unused-vars': ['warn', { varsIgnorePattern: '^[_]{1,}$' }],
'@typescript-eslint/indent': 'warn',
'react/prop-types': 'off',
// '@typescript-eslint/implicit-arrow-linebreak': 'warn',
},
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion packages/cli-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@
"@bundle-stats/utils": "^4.5.2",
"core-js": "^3.21.0",
"find-cache-dir": "^3.1.0",
"fs-extra": "^11.0.0"
"fs-extra": "^11.0.0",
"stream-chain": "^2.2.5",
"stream-json": "^1.8.0"
},
"devDependencies": {
"@types/find-cache-dir": "^3.2.1",
"@types/stream-chain": "^2.0.1",
"@types/stream-json": "^1.7.3",
"jest": "29.6.2",
"typescript": "5.1.6"
}
Expand Down
13 changes: 13 additions & 0 deletions packages/cli-utils/src/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createReadStream } from 'fs';
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()]);
const asm = Asm.connectTo(pipeline);

return new Promise((fulfill) => {
asm.on('done', (data) => fulfill(data.current));
});
};
1 change: 1 addition & 0 deletions packages/cli-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as T from './text';

export * from './fs';
export * from './baseline';
export * from './create-artifacts';
export * from './constants';
Expand Down
4 changes: 0 additions & 4 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@
"fs-extra": "^11.0.0",
"listr2": "^5.0.1",
"lodash": "^4.17.0",
"stream-chain": "^2.2.5",
"stream-json": "^1.8.0",
"update-notifier": "^5.0.0",
"yargs": "^17.4.0"
},
Expand All @@ -69,8 +67,6 @@
"@playwright/test": "1.36.2",
"@types/fs-extra": "^11.0.0",
"@types/listr": "^0.14.4",
"@types/stream-chain": "^2.0.1",
"@types/stream-json": "^1.7.3",
"@types/yargs": "^17.0.20",
"http-server": "14.1.1",
"jest": "29.6.2",
Expand Down
36 changes: 13 additions & 23 deletions packages/cli/src/run.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/* eslint-disable no-console */
import path from 'path';
import { createReadStream } from 'fs';
import { outputFile } from 'fs-extra';
import { Listr } from 'listr2';
import { get } from 'lodash';
import boxen from 'boxen';
import { parser } from 'stream-json';
import { chain } from 'stream-chain';
import Asm from 'stream-json/Assembler';
import type { StatsCompilation } from 'webpack';
import '@bundle-stats/utils/lib/polyfills';
import {
Expand All @@ -29,6 +25,7 @@ import {
getBaselineStatsFilepath,
getReportInfo,
readBaseline,
readJSONStream,
writeBaseline,
} from '@bundle-stats/cli-utils';

Expand Down Expand Up @@ -66,14 +63,7 @@ export default async function run(options: RunOptions): Promise<void> {
title: 'Read Webpack stats files',
task: async (ctx) => {
const sources = await Promise.all(
artifactFilepaths.map((filepath) => {
const pipeline = chain([createReadStream(filepath), parser()]);
const asm = Asm.connectTo(pipeline);

return new Promise<StatsCompilation>((fulfill) => {
asm.on('done', (data) => fulfill(data.current));
});
}),
artifactFilepaths.map((filepath) => readJSONStream<StatsCompilation>(filepath)),
);

sources.forEach((source, index) => {
Expand Down Expand Up @@ -150,17 +140,17 @@ export default async function run(options: RunOptions): Promise<void> {
title: 'Save reports',
task: (ctx) =>
new Listr(
Object.values(ctx.artifacts).map(({ filename, output }: any) => ({
title: filename,
task: async () => {
const filepath = path.join(outDir, filename);
await outputFile(filepath, output);

ctx.output = [...(ctx.output ? ctx.output : []), filepath];
},
})),
{ concurrent: true },
),
Object.values(ctx.artifacts).map(({ filename, output }: any) => ({
title: filename,
task: async () => {
const filepath = path.join(outDir, filename);
await outputFile(filepath, output);

ctx.output = [...(ctx.output ? ctx.output : []), filepath];
},
})),
{ concurrent: true },
),
},
]);

Expand Down

0 comments on commit ba60929

Please sign in to comment.