Skip to content

Commit 0d960ef

Browse files
serialReport API
1 parent 27fd9ff commit 0d960ef

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"gzip": "3.8 kB",
6666
"none": "20 kB"
6767
},
68-
"./dist/index.js": {
68+
"./dist/api.js": {
6969
"brotli": "5 kB",
7070
"gzip": "6.5 kB"
7171
},

src/api.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616

1717
import Project from './validation/Project';
1818
import Config from './validation/Config';
19-
import { Context, SizeMap } from './validation/Condition';
19+
import { Context, SizeMap, FileModifier } from './validation/Condition';
2020
import compress from './compress';
2121

22-
export async function* report(
23-
projectPath: string,
24-
fileModifier: ((contents: string) => string) | null,
25-
): AsyncGenerator<[SizeMap, SizeMap]> {
22+
export async function* report(projectPath: string, fileModifier: FileModifier): AsyncGenerator<[SizeMap, SizeMap]> {
2623
const conditions = [Project, Config];
2724
let context: Context = {
2825
projectPath,
@@ -53,3 +50,44 @@ export async function* report(
5350
}
5451
return [context.compressed, context.comparison];
5552
}
53+
54+
export async function* serialReport(
55+
projectPath: string,
56+
fileModifier: FileModifier,
57+
): AsyncGenerator<[string, number, number, number]> {
58+
const conditions = [Project, Config];
59+
let context: Context = {
60+
projectPath,
61+
packagePath: '',
62+
packageContent: '',
63+
silent: true,
64+
originalPaths: new Map(),
65+
// Stores the result of compression <path, [...results]>
66+
compressed: new Map(),
67+
// Stores the basis of comparison.
68+
comparison: new Map(),
69+
fileModifier,
70+
fileContents: new Map(),
71+
};
72+
73+
for (const condition of conditions) {
74+
const message = await condition(context)();
75+
if (message !== null) {
76+
throw message;
77+
}
78+
}
79+
80+
const compressResults = compress(context, false);
81+
const paths: Set<string> = new Set(Array.from(context.compressed.keys()));
82+
let next = await compressResults.next();
83+
while (!next.done) {
84+
for (const filePath of paths) {
85+
const sizes = context.compressed.get(filePath);
86+
if (sizes !== undefined && sizes?.[0][0] && sizes?.[1][0] && sizes?.[2][0]) {
87+
yield [filePath, sizes?.[0][0], sizes?.[1][0], sizes?.[2][0]];
88+
}
89+
}
90+
next = await compressResults.next();
91+
}
92+
return;
93+
}

src/validation/Condition.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const SizeMapValueIndex = {
4848
export type SizeMap = Map<path, SizeMapValue>;
4949

5050
export type FileContentsMap = Map<path, string>;
51+
export type FileModifier = ((contents: string) => string) | null;
5152

5253
export interface Context {
5354
projectPath: string;
@@ -60,7 +61,7 @@ export interface Context {
6061
// Stores the basis of comparison.
6162
comparison: SizeMap;
6263
// Allows the API to specify a method that alters content before analysis.
63-
fileModifier: ((contents: string) => string) | null;
64+
fileModifier: FileModifier;
6465
// Stores the contents of files, to avoid reading from disk per compression type.
6566
fileContents: FileContentsMap;
6667
}

0 commit comments

Comments
 (0)