-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
78 lines (62 loc) · 1.81 KB
/
utils.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
const fs = require("fs");
const path = require("path");
exports.walkDir = walkDir;
exports.humanSize = humanSize;
exports.humanDuration = humanDuration;
exports.urlPath = urlPath;
function* walkDir(directory) {
for (let file of fs.readdirSync(directory, { withFileTypes: true })) {
let filePath = path.join(directory, file.name);
if (file.isFile()) {
yield filePath;
} else if (file.isDirectory()) {
yield* walkDir(filePath);
} else {
throw new Error(`unexpected file "${filePath}"`);
}
}
}
function humanSize(bytes) {
let size = bytes;
let unit = null;
for (let u of ["KiB", "MiB", "GiB"]) {
if (size < 1024) {
break;
}
size /= 1024;
unit = u;
}
return unit == null ? `${size} byte(s)` : `${size.toFixed(2)} ${unit}`;
}
function humanDuration(nanoseconds) {
let smallDuration = 0n;
let smallUnit = "ms";
let bigDuration = nanoseconds / 1_000_000n;
let bigUnit = "ms";
let seconds = bigDuration / 1_000n;
if (seconds < 60n) {
let justSeconds = Number(bigDuration) / 1000;
return `${justSeconds.toFixed(3)}s`
}
smallDuration = bigDuration % 1_000n;
smallUnit = bigUnit;
bigDuration = seconds;
bigUnit = "s";
for (let unit of ["m", "h"]) {
if (bigDuration < 60n) {
break;
}
smallDuration = bigDuration % 60n;
smallUnit = bigUnit;
bigDuration = bigDuration / 60n;
bigUnit = unit;
}
return `${bigDuration}${bigUnit} ${smallDuration}${smallUnit}`;
}
function urlPath(baseDir, p) {
let result = path.relative(baseDir, p);
if (process.platform === "win32") {
result = result.replace(/\\/g, "/");
}
return path.posix.resolve("/", result);
}