Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for [installation_folder_path]/bin in PATH #5

Merged
merged 1 commit into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ build/Release
# Dependency directories
node_modules
jspm_packages
package-lock.json

# Optional npm cache directory
.npm
Expand Down
24 changes: 21 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,31 @@ const getPathFromNpmConfig = (platform, packageName) => {

const getPathFromCmdContent = (packageName, pathToExecutable) => {
if (fs.existsSync(pathToExecutable)) {
const windowsPathRegExp = /(%~dp0[\w\\.-]+node_modules).*?"/g;
const executableContent = fs.readFileSync(pathToExecutable).toString();
const match = windowsPathRegExp.exec(executableContent);

let fullPath;

let windowsPathRegExp = /(%~dp0[\w\\.-]+node_modules).*?"/g;
let match = windowsPathRegExp.exec(executableContent);

if (match && match[1]) {
const realPath = path.normalize(match[1].replace("%~dp0", path.dirname(pathToExecutable)));
const pathToPackage = getVerifiedPath(path.join(realPath, packageName), packageName);

fullPath = path.join(realPath, packageName);
}

if (!fullPath) {
windowsPathRegExp = new RegExp(`(%~dp0[\\w\\\\.-]+?${packageName})(?:\\\\|")`, "g");
match = windowsPathRegExp.exec(executableContent);

if (match && match[1]) {
fullPath = path.normalize(match[1].replace("%~dp0", path.dirname(pathToExecutable)));
}
}

if (fullPath) {
const pathToPackage = getVerifiedPath(fullPath, packageName);

if (pathToPackage) {
return pathToPackage;
}
Expand Down
40 changes: 40 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,46 @@ describe("getPath", () => {
assert.deepEqual(path.normalize(result), expectedData);
});

it("returns correct result when where result is correct, and package is added to PATH via a .bin dir", () => {
const packageName = "test1",
executableName = "test1.cmd",
nodeModulesDirName = path.join("C:", "Users", "username", "someDir", "node_modules"),
executableDirName = path.join(nodeModulesDirName, ".bin"),
whereResult = path.join(executableDirName, executableName);

fs.existsSync = (pathToCheck) => pathToCheck !== path.join(executableDirName, "node_modules", packageName);

childProcess.execSync = (command) => {
if (command.indexOf("where") !== -1) {
return whereResult;
}

return null;
};

fs.readFileSync = (filePath) => {
if (filePath.indexOf("package.json") !== -1) {
return JSON.stringify({
"name": packageName
});
}

if (path.basename(filePath) === executableName) {
return "@\"%~dp0\\..\\test1\\bin\\test1\" %*";
}

return "";
};

const result = index.getPath(packageName, executableName);

const expectedData = process.platform === "win32" ?
path.join(path.dirname(executableDirName), packageName) :
path.join(nodeModulesDirName, ".bin\\..\\test1");

assert.deepEqual(result, expectedData);
});

it("returns correct result when where result is correct, and package is added to PATH via its bin dir", () => {
const packageName = "test1",
executableName = "test1.js",
Expand Down