|
16 | 16 |
|
17 | 17 | import Project from './validation/Project';
|
18 | 18 | import Config from './validation/Config';
|
19 |
| -import { Context, SizeMap } from './validation/Condition'; |
| 19 | +import { Context, SizeMap, FileModifier } from './validation/Condition'; |
20 | 20 | import compress from './compress';
|
21 | 21 |
|
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]> { |
26 | 23 | const conditions = [Project, Config];
|
27 | 24 | let context: Context = {
|
28 | 25 | projectPath,
|
@@ -53,3 +50,44 @@ export async function* report(
|
53 | 50 | }
|
54 | 51 | return [context.compressed, context.comparison];
|
55 | 52 | }
|
| 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 | +} |
0 commit comments