-
Notifications
You must be signed in to change notification settings - Fork 4
/
updateREADME.ts
55 lines (48 loc) · 1.9 KB
/
updateREADME.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
import { readFile, writeFile } from 'fs/promises';
import sloc from "sloc";
import { transformFile } from '@swc/core';
import zlib from 'zlib';
import {promisify} from 'util'
export function bytesToSize(bytes: number): string {
const sizes: string[] = ['Bytes', 'KB', 'MB', 'GB', 'TB']
if (bytes === 0) return 'n/a'
const i: number = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)).toString())
if (i === 0) return `${bytes} ${sizes[i]}`
return `${(bytes / Math.pow(1024, i)).toFixed(1)}${sizes[i]}`
}
export async function gzip(contents: string): Promise<string> {
return (await promisify(zlib.gzip)(contents)).toString()
}
async function stat(path: string) {
const contents = await readFile(path, {encoding: "utf8"})
// console.log('contents', contents)
var stats = sloc(contents,"ts");
const minified = await transformFile(path, {
minify: true,
});
const gzipped = await gzip(minified.code || '')
return {
loc: stats.source,
gzip: gzipped.length,
size: minified.code?.length||0
}
}
// updates the README file with file info
export async function updateREADME(source: string, readme: string, verbose?: true) {
const README = await readFile(readme, {encoding: "utf8"});
const stat_ = await stat(source);
if (verbose) {
console.log('=== STATS ===')
console.log('Lines of code:', stat_.loc)
console.log('Size:', bytesToSize(stat_.size))
console.log('Gzipped:', bytesToSize(stat_.gzip))
}
let s = README.replace(/under ([0-9]+) LoC/i, `under ${Math.ceil(stat_.loc/10)*10} LoC`)
.replace(/([0-9\.]+) ?Kb\*\* gzipped/i, `${bytesToSize(stat_.gzip)}** gzipped`)
.replace(/([0-9\.]+) ?Kb\*\* unpacked/i, `${bytesToSize(stat_.size)}** unpacked`)
// console.log(s.slice(0, 1000))
await writeFile(readme, s);
if (verbose) console.log('\nDone! Wrote ', readme)
}
export default updateREADME;
await updateREADME('./src/lib/Elegua.ts', './README.md', true);