Skip to content

Install global bin to the correct location on windows #5

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

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
20 changes: 16 additions & 4 deletions __test__/common.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ jest.mock('mkdirp');

describe('common', () => {
describe('getInstallationPath()', () => {
let callback, env;
let callback, _process;

beforeEach(() => {
callback = jest.fn();

env = { ...process.env };
_process = { ...global.process, env: { ...process.env } };
});

afterEach(() => {
process.env = env;
global.process = _process;
});

it('should get binaries path from `npm bin`', () => {
Expand All @@ -29,9 +29,21 @@ describe('common', () => {
expect(callback).toHaveBeenCalledWith(null, path.sep + path.join('usr', 'local', 'bin'));
});

it('should get binaries path from env', () => {
it('should get binaries path from env on windows platform', () => {
childProcess.exec.mockImplementationOnce((_cmd, cb) => cb(new Error()));

process.platform = 'win32';
process.env.npm_config_prefix = String.raw`C:\Users\John Smith\AppData\npm`;

common.getInstallationPath(callback);

expect(callback).toHaveBeenCalledWith(null, path.win32.join('C:', 'Users', 'John Smith', 'AppData', 'npm'));
});

it('should get binaries path from env on platform different than windows', () => {
childProcess.exec.mockImplementationOnce((_cmd, cb) => cb(new Error()));

process.platform = 'linux';
process.env.npm_config_prefix = '/usr/local';

common.getInstallationPath(callback);
Expand Down
8 changes: 7 additions & 1 deletion src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ function getInstallationPath(callback) {
const packageManager = usedPM();

if (env && env.npm_config_prefix) {
dir = join(env.npm_config_prefix, 'bin');
if (process.platform === 'win32') {
// On Windows, use the installation directory itself instead of the `bin` folder.
// See: https://docs.npmjs.com/cli/v6/configuring-npm/folders#executables
dir = env.npm_config_prefix;
} else {
dir = join(env.npm_config_prefix, 'bin');
}
} else if (env && env.npm_config_local_prefix) {
dir = join(env.npm_config_local_prefix, join('node_modules', '.bin'));
} else if (packageManager.name.toLowerCase() === 'pnpm') {
Expand Down