-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (77 loc) · 2.3 KB
/
index.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env node
// https://twitter.com/nparashuram/status/1151931186400206849
const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");
const dir = process.argv[2] || ".";
const res = execSync(`du -ak ${dir}`);
const files = res
.toString()
.split("\n")
.filter(l => l.length > 0)
.map(l => {
const match = /(\d)\s+([^\s].*)/.exec(l);
if (!match) {
throw new Error("Invalid line " + l);
}
const size = parseInt(match[1], 10);
const relativePath = "./" + path.relative(dir, match[2]);
return [relativePath, path.dirname(relativePath), size];
});
// Last file is root.
files[files.length - 1][0] = ".";
files[files.length - 1][1] = null;
const data = [["A", "B", "C"], ...files];
const template = `
<html>
<head>
<script
type="text/javascript"
src="https://www.gstatic.com/charts/loader.js"
></script>
<script type="text/javascript">
google.charts.load('current', { packages: ['treemap'] });
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable(${JSON.stringify(
data,
null,
2
)});
tree = new google.visualization.TreeMap(
document.getElementById('chart_div'),
);
tree.draw(data, {
minColor: '#f00',
midColor: '#ddd',
maxColor: '#0d0',
headerHeight: 15,
fontColor: 'black',
showScale: true,
generateTooltip: showFullTooltip
});
function humanFileSize(size) {
const i = Math.floor(Math.log(size) / Math.log(1024));
return (
(size / Math.pow(1024, i)).toFixed(2) * 1 +
" " +
["KB", "MB", "GB", "TB"][i]
);
}
function showFullTooltip(row, size, value) {
return \`
<div style="background:#fd9; padding:10px; border-style:solid">
<span style="font-family:sans-serif">
\${data.getValue(row, 0)} (\${humanFileSize(size)})
</span>
</div>\`;
}
}
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
`;
fs.writeFileSync("./index.html", template);