-
Notifications
You must be signed in to change notification settings - Fork 30
/
analyzeTest.ts
56 lines (43 loc) · 1.61 KB
/
analyzeTest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import * as fs from 'fs';
import { parseChunked } from '@discoveryjs/json-ext';
const pluginName = process.env.PLUGIN;
const MAX_ASSET_SIZE = 17; //17 MiB
const getStatsFilePath = () => `./plugins/${pluginName}/dist/stats.json`;
const toMiB = (value: number) => value / 1024 ** 2;
const stringifyMiB = (value: ReturnType<typeof toMiB>): string =>
`${value.toFixed(2)} MB`;
const getParsedStatFile = () => {
const filePath = getStatsFilePath();
return parseChunked(fs.createReadStream(filePath, { encoding: 'utf8' }));
};
type BundleDataMap = Record<string, number>;
// [Valid Bundles, Violating Bunldes]
type GetBundleInformation = () => Promise<[BundleDataMap, BundleDataMap]>;
const getBundleInformation: GetBundleInformation = async () => {
const statsData = await getParsedStatFile();
const validAssets = {};
const violatingAssets = {};
statsData.assets.forEach((asset) => {
const assetSize = toMiB(asset.size);
const readableSize = stringifyMiB(assetSize);
if (assetSize > MAX_ASSET_SIZE) {
violatingAssets[asset.name] = readableSize;
} else {
validAssets[asset.name] = readableSize;
}
});
return [validAssets, violatingAssets];
};
const validateBuild = async () => {
const [validAssets, violatingAssets] = await getBundleInformation();
if (Object.keys(violatingAssets).length > 0) {
// eslint-disable-next-line no-console
console.error('Assets are larger than expected', violatingAssets);
process.exit(1);
} else {
// eslint-disable-next-line no-console
console.log('All assets are valid', validAssets);
process.exit(0);
}
};
validateBuild();