import * as child_process from 'child_process';

export default class CommandUtils {

	static execCommandSync(
			command: string,
			args: string[],
			logError:boolean = true,
			) {
			const { status, error, stderr, stdout } = child_process.spawnSync(command, args, {});
	  
			if (status != 0 && (command != 'robocopy' && status > 1)) {
				
				if (logError)
					console.log(`Command failed: ${command} ${args.map(x => JSON.stringify(x)).join(', ')}`);
				
				if (error) {
					if (logError)
						console.log('Error: ' + (error ? error.message : 'undefined'));
					throw error;
				} else {
					if (logError)
						console.log(`STDERR:\n${stderr}`);
					throw stderr.toString('utf8');
				}
			}
			
			return stdout.toString('utf8');
		}
		
		static execCommand(
			command: string,
			args: string[],
			callback: any,
			) {
				const child = child_process.execFile(command, args, callback);
			}
}