npm / node-which

Like which(1) unix command. Find the first instance of an executable in the PATH.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Failed to parse past path entries on D:

johnkazer opened this issue · comments

I have two path entries 'D:....' but whichSync failed to iterate over pathEnv beyond these when trying to find the 'git' path. I changed the order so the 'D:...' entries were last and whichSync parsed everything up to but not including those moved 'D:...' entries. When moved, there were the 14th and 15th path entries.

Didn't work:
image

Worked:
image

@johnkazer Just out of curiosity, what's the output you get if you run the following script?

node that-script-below.js
"use strict";

which("git").then(output => {
	console.log(output);
});

/**
 * Locate a program file in the user's $PATH.
 *
 * If found, the returned {@link Promise} resolves to the absolute
 * pathname of the named executable. Otherwise, it resolves to the
 * empty string. Rejects with an error if the execution failed.
 *
 * @example which("curl") == "/usr/bin/curl"
 * @example which("nada") == ""
 * @param {String} name
 * @return {Promise}
 */
function which(name){
	return new Promise((resolve, reject) => {
		if(!name) return resolve("");
		const {exec} = require("child_process");
		const cmdStr = "win32" === process.platform
			? `@for %g in (ECHO ${name.replace(/%/g, "%%")}) do`
				+ " @for %e in (%PATHEXT%) do"
				+ " @for %i in (%g%e) do "
				+ ' @if NOT "%~$PATH:i"=="" echo %~$PATH:i'
			: `command -v '${name.replace(/'/g, `'"'"'`)}' 2>/dev/null`;
		exec(cmdStr, {windowsHide: true}, (error, output) => error
			? reject(error)
			: resolve(output.split(/\r?\n/).filter(Boolean)[0] || ""));
	});
}