Skip to content

Commit

Permalink
fix: resolve symbol links until their are no more symbolic links
Browse files Browse the repository at this point in the history
Depending on your npm / yarn set up a global install may have to resolve
through several symlinks before it correctly arrives the the baseDir
that actually contains the other subcommands.

The changes the resolution logic to resolve symlinks until their are no
more to resolve by using realpath which will resolve links all the way
to the final destination.
  • Loading branch information
MarshallOfSound committed Mar 22, 2019
1 parent 291fc04 commit 994d24d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
11 changes: 4 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,14 +527,11 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) {

// In case of globally installed, get the base dir where executable
// subcommand file should be located at
var baseDir,
link = fs.lstatSync(f).isSymbolicLink() ? fs.readlinkSync(f) : f;
var baseDir;

// when symbolink is relative path
if (link !== f && link.charAt(0) !== '/') {
link = path.join(dirname(f), link);
}
baseDir = dirname(link);
var resolvedLink = fs.realpathSync(f);

baseDir = dirname(resolvedLink);

// prefer local `./<bin>` to bin in the $PATH
var localBin = path.join(baseDir, bin);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/another-dir/pm
1 change: 1 addition & 0 deletions test/fixtures/other-dir/pm
7 changes: 7 additions & 0 deletions test/test.command.executableSubcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ var bin = path.join(__dirname, './fixtures/pmlink')
exec(bin + ' install', function (error, stdout, stderr) {
stdout.should.equal('install\n');
});

// when `bin` is a symbol link pointing at a symbolic for mocking global install
var bin = path.join(__dirname, './fixtures/another-dir/pm')
// success case
exec(bin + ' install', function (error, stdout, stderr) {
stdout.should.equal('install\n');
});
7 changes: 7 additions & 0 deletions test/test.command.executableSubcommandAlias.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ var bin = path.join(__dirname, './fixtures/pmlink')
exec(bin + ' i', function (error, stdout, stderr) {
stdout.should.equal('install\n');
});

// when `bin` is a symbol link pointing at a symbolic for mocking global install
var bin = path.join(__dirname, './fixtures/another-dir/pm')
// success case
exec(bin + ' install', function (error, stdout, stderr) {
stdout.should.equal('install\n');
});
7 changes: 7 additions & 0 deletions test/test.command.executableSubcommandDefault.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ var bin = path.join(__dirname, './fixtures/pmlink')
exec(bin + ' install', function (error, stdout, stderr) {
stdout.should.equal('install\n');
});

// when `bin` is a symbol link pointing at a symbolic for mocking global install
var bin = path.join(__dirname, './fixtures/another-dir/pm')
// success case
exec(bin + ' install', function (error, stdout, stderr) {
stdout.should.equal('install\n');
});

0 comments on commit 994d24d

Please sign in to comment.