/* This file is part of »ivaldi«. Copyright 2025 'Fenris Wolf' »ivaldi« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »ivaldi« 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »ivaldi«. If not, see . */ namespace _ivaldi { /** */ export function command_log( content : string ) : string { return lib_plankton.string.coin( "echo '-- {{content}}'", { "content": content, } ) } /** */ export function command_delete( path : string ) : string { return lib_plankton.string.coin( "rm --recursive --force {{path}}", { "path": path, } ) } /** */ export function command_mkdir( path : string ) : string { return lib_plankton.string.coin( "mkdir --parents {{path}}", { "path": path, } ) } /** */ export function command_cat( source_paths : Array, target_path : string, { "append": append = false, } : { append ?: boolean; } = { } ) : string { return lib_plankton.string.coin( "cat {{source_paths}} {{operator}} {{target_path}}", { "source_paths": source_paths.join(" "), "operator": (append ? ">>" : ">"), "target_path": target_path, } ) } /** */ export function command_copy( source_path : string, target_path : string ) : string { return lib_plankton.string.coin( "cp -r -u {{source_path}} {{target_path}}", { "source_path": source_path, "target_path": target_path, } ) } /** */ export function command_ptk_bundle( target : string, modules : Array ) : string { return lib_plankton.string.coin( "ptk bundle {{target}} {{modules}}", { "target": target, "modules": modules.join(" "), } ) } /** */ export function command_npm_install( modules : Array ) : string { return lib_plankton.string.coin( "npm install {{modules}}", { "modules": modules.join(" "), } ) } /** */ export function command_tsc( source_paths : Array, target_path : string, { "declaration": declaration = false, "libs": libs = [], "head": head = "tsc" } : { declaration ?: boolean; libs ?: Array; head ?: string; } = { } ) : string { return lib_plankton.string.coin( "{{head}}{{libs}} {{source_paths}} --outFile {{target_path}}{{declaration}}", { "head": head, "libs": ( libs .map( name => lib_plankton.string.coin( " --lib {{name}}", {"name": name} ), ) .join("") ), "source_paths": source_paths.join(" "), "target_path": target_path, "declaration": (declaration ? " --declaration" : ""), } ) } /** * @todo outsource */ export function execute( command : string, { "log_only": log_only = false, "working_directory": working_directory = null, } : { log_only ?: boolean; working_directory ?: (null | string); } = { } ) : Promise< { code : int; stdout : string; stderr : string; } > { const nm_child_process = require("child_process"); return ( new Promise( (resolve, reject) => { lib_plankton.log._info( "execute", { "details": { "command": command, } } ); if (log_only) { resolve( { "code": 0, "stdout": "", "stderr": "", } ); } else { nm_child_process.exec( command, { "cwd": ((working_directory === null) ? undefined : working_directory), }, (error, stdout, stderr) => { if (error) { reject(error); } else { resolve( { /** * @todo assign correct value */ "code": 0, "stdout": stdout, "stderr": stderr, } ); } } ); } } ) ); } }