2023-07-23 09:33:04 +02:00
|
|
|
namespace _heimdall.helpers.misc
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
export function format_bytes(
|
|
|
|
|
bytes : int
|
|
|
|
|
) : string
|
|
|
|
|
{
|
|
|
|
|
const units : Array<{label : string; digits : int;}> = [
|
|
|
|
|
{"label": "B", "digits": 0},
|
|
|
|
|
{"label": "KB", "digits": 1},
|
|
|
|
|
{"label": "MB", "digits": 1},
|
|
|
|
|
{"label": "GB", "digits": 1},
|
|
|
|
|
{"label": "TB", "digits": 1},
|
|
|
|
|
{"label": "PB", "digits": 1},
|
|
|
|
|
]
|
|
|
|
|
let number_ : int = bytes;
|
|
|
|
|
let index : int = 0;
|
|
|
|
|
while ((number_ >= 1000) && (index < (units.length - 1))) {
|
|
|
|
|
number_ /= 1000;
|
|
|
|
|
index += 1;
|
|
|
|
|
}
|
|
|
|
|
return lib_plankton.string.coin(
|
|
|
|
|
"{{number}} {{label}}",
|
|
|
|
|
{
|
|
|
|
|
"number": number_.toFixed(units[index].digits),
|
|
|
|
|
"label": units[index].label,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-07-27 17:37:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
export type type_shell_exec_result = {return_code : int; stdout : string; stderr : string;};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see https://nodejs.org/api/child_process.html#child_processspawncommand-args-options
|
|
|
|
|
*/
|
|
|
|
|
export function shell_exec(
|
|
|
|
|
head : string,
|
|
|
|
|
args : Array<string>
|
|
|
|
|
) : Promise<type_shell_exec_result>
|
|
|
|
|
{
|
|
|
|
|
const nm_child_process = require("child_process");
|
|
|
|
|
|
|
|
|
|
return new Promise<type_shell_exec_result>(
|
|
|
|
|
(resolve, reject) => {
|
|
|
|
|
let stdout : string = "";
|
|
|
|
|
let stderr : string = "";
|
|
|
|
|
const proc = nm_child_process.spawn(
|
|
|
|
|
head,
|
|
|
|
|
args,
|
|
|
|
|
{
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
proc.stdout.on(
|
|
|
|
|
"data",
|
|
|
|
|
(data) => {
|
|
|
|
|
stdout += data;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
proc.stderr.on(
|
|
|
|
|
"data",
|
|
|
|
|
(data) => {
|
|
|
|
|
stderr += data;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
proc.on(
|
|
|
|
|
"close",
|
|
|
|
|
(return_code) => {
|
|
|
|
|
resolve(
|
|
|
|
|
{
|
|
|
|
|
"return_code": return_code,
|
|
|
|
|
"stdout": stdout,
|
|
|
|
|
"stderr": stderr,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-23 09:33:04 +02:00
|
|
|
}
|