/* Copyright 2016-2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' »heimdall« is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »heimdall« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with »heimdall«. If not, see . */ namespace _heimdall.helpers.misc { /** */ export function get_env_language( ) : (null | string) { const env_lang : (undefined | string) = process.env["LANG"]; if (env_lang === undefined) { return null; } else { 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, } ); } /** */ 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 ) : Promise { const nm_child_process = require("child_process"); return new Promise( (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, } ); } ); } ); } }