-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
54 lines (42 loc) · 1.26 KB
/
functions.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
import { $, nothrow, sleep, chalk } from 'zx';
import which from 'which';
// Returns the string response without \n at the end. For simple commands.
export async function $out(pieces, ...args) {
return (await $(pieces, ...args)).toString().replace(/\n$/, '');
}
// Disables automatic quotes. Use with caution.
export function $raw(pieces, ...args) {
let str = pieces[0];
for (let i = 0; i < args.length; i += 1) {
str += args[i] + pieces[i+1];
}
return $([str]);
}
export async function $$outRaw(pieces, ...args) {
return (await $raw(pieces, ...args)).toString().replace(/\n$/, '');
}
export function humanizeBytes(bytes, precision = null) {
const suffixes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
let value = bytes;
let iteration = 0;
while (value > 1024) {
value = value / 1024;
iteration += 1;
}
if (precision !== null) value = value.toFixed(precision);
return {
value,
suffix: suffixes[iteration],
toString() {
return `${value}${suffixes[iteration]}`;
}
};
}
export async function checkDependency(dependency) {
return which(dependency)
.catch(() => {
console.error(chalk.red(`Package ${dependency} not installed`));
process.exit(1);
});
}
export { $, nothrow, sleep };