core/source/helpers/misc.ts

99 lines
1.9 KiB
TypeScript
Raw Normal View History

namespace _heimdall.helpers.misc
{
2023-08-03 08:34:33 +02:00
/**
*/
export function get_env_language(
) : (null | string)
{
const env_lang : string = process.env["LANG"];
const locale : string = env_lang.split(".")[0];
const language : string = locale.split("_")[0];
return language;
}
/**
*/
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,
}
);
}
);
}
);
}
}