'use strict';

import fs = require('fs');
import path = require('path');

var binPathCache: { [bin: string]: string; } = {}

export function getBinPath(binname: string) {
	binname = correctBinname(binname);
    
	if (binPathCache[binname]) return binPathCache[binname];

    // clang-format.executable has a valid absolute path
    if (fs.existsSync(binname)) {
        binPathCache[binname] = binname;
        return binname;
    }
    
	if (process.env["PATH"]) {
		var pathparts = process.env["PATH"].split(path.delimiter);
		for (var i = 0; i < pathparts.length; i++) {
			let binpath = path.join(pathparts[i], binname);
			if (fs.existsSync(binpath)) {
				binPathCache[binname] = binpath;
				return binpath;
			}
		}
	}

	// Else return the binary name directly (this will likely always fail downstream) 
	binPathCache[binname] = binname;
	return binname;
}

function correctBinname(binname: string) {
	if (process.platform === 'win32') {
        if(binname.substr(binname.length - 4).toLowerCase() !== '.exe') {
		    return binname + ".exe";
        } else {
            return binname;
        }
    } else {
		return binname
    }
}
'use strict';

import fs = require('fs');
import path = require('path');

var binPathCache: { [bin: string]: string; } = {}

export function getBinPath(binname: string) {
	if (binPathCache[binname]) return binPathCache[binname];

	for (let binNameToSearch of correctBinname(binname)) {
		// clang-format.executable has a valid absolute path
		if (fs.existsSync(binNameToSearch)) {
			binPathCache[binname] = binNameToSearch;
			return binNameToSearch;
		}

		if (process.env["PATH"]) {
			var pathparts = process.env["PATH"].split(path.delimiter);
			for (var i = 0; i < pathparts.length; i++) {
				let binpath = path.join(pathparts[i], binNameToSearch);
				if (fs.existsSync(binpath)) {
					binPathCache[binname] = binpath;
					return binpath;
				}
			}
		}
	}

	// Else return the binary name directly (this will likely always fail downstream) 
	binPathCache[binname] = binname;
	return binname;
}

function correctBinname(binname: string): [string] {
	if (process.platform === 'win32') {
        return [binname + '.exe', binname + '.cmd', binname];
    } else {
		return [binname]
    }
}
Improve executable search on Windows
Improve executable search on Windows

Search for the plain provided binary as well as appending a .cmd
suffix.

This fixes issue where providing a valid path to an executable would not
work as expected because the plug-in would append `.exe`
