-
Notifications
You must be signed in to change notification settings - Fork 1
/
size-limit.js
44 lines (39 loc) · 1.28 KB
/
size-limit.js
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
#!/usr/bin/env node
import fs from 'fs';
// @ts-expect-error TS7016
import { cyan, green, red } from 'kolorist';
import path from 'path';
// const args = process.argv.slice(2);
// const packageJsonPath = args[0];
const packageJsonPath = path.join(process.cwd(), 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf-8' }));
/** @type {Array<{path: string, limit: string}>} */
const arr = packageJson['size-limit'];
if (!arr) {
process.exit(0);
}
let abort = false;
for (const obj of arr) {
const filePath = path.join(packageJsonPath, '..', obj.path);
if (!fs.existsSync(filePath)) {
console.log(`${cyan('SIZE LIMIT?!')} [${green(filePath.replace(process.cwd(), '.'))}]`);
continue;
}
const nBytes = parseInt(obj.limit, 10);
const { size } = fs.statSync(filePath);
if (size > nBytes) {
console.log(
`${red('SIZE LIMIT :(')} [${green(filePath.replace(process.cwd(), '.'))}] (${cyan(size)} > ${cyan(nBytes)})`,
);
abort = true;
} else if (size === nBytes) {
console.log(`${cyan('SIZE LIMIT :|')} [${green(filePath.replace(process.cwd(), '.'))}] (${cyan(size)})`);
} else {
console.log(
`${cyan('SIZE LIMIT :)')} [${green(filePath.replace(process.cwd(), '.'))}] (${cyan(size)} < ${cyan(nBytes)})`,
);
}
}
if (abort) {
process.exit(1);
}