Skip to content

Commit 773f52e

Browse files
authored
fix: install location on windows
1 parent 5b4b644 commit 773f52e

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

__test__/common.spec.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ jest.mock('mkdirp');
99

1010
describe('common', () => {
1111
describe('getInstallationPath()', () => {
12-
let callback, env;
12+
let callback, _process;
1313

1414
beforeEach(() => {
1515
callback = jest.fn();
1616

17-
env = { ...process.env };
17+
_process = { ...global.process, env: { ...process.env } };
1818
});
1919

2020
afterEach(() => {
21-
process.env = env;
21+
global.process = _process;
2222
});
2323

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

32-
it('should get binaries path from env', () => {
32+
it('should get binaries path from env on windows platform', () => {
33+
childProcess.exec.mockImplementationOnce((_cmd, cb) => cb(new Error()));
34+
35+
process.platform = 'win32';
36+
process.env.npm_config_prefix = String.raw`C:\Users\John Smith\AppData\npm`;
37+
38+
common.getInstallationPath(callback);
39+
40+
expect(callback).toHaveBeenCalledWith(null, path.win32.join('C:', 'Users', 'John Smith', 'AppData', 'npm'));
41+
});
42+
43+
it('should get binaries path from env on platform different than windows', () => {
3344
childProcess.exec.mockImplementationOnce((_cmd, cb) => cb(new Error()));
3445

46+
process.platform = 'linux';
3547
process.env.npm_config_prefix = '/usr/local';
3648

3749
common.getInstallationPath(callback);

src/common.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ function getInstallationPath(callback) {
3939
const packageManager = usedPM();
4040

4141
if (env && env.npm_config_prefix) {
42-
dir = join(env.npm_config_prefix, 'bin');
42+
if (process.platform === 'win32') {
43+
// On Windows, use the installation directory itself instead of the `bin` folder.
44+
// See: https://docs.npmjs.com/cli/v6/configuring-npm/folders#executables
45+
dir = env.npm_config_prefix;
46+
} else {
47+
dir = join(env.npm_config_prefix, 'bin');
48+
}
4349
} else if (env && env.npm_config_local_prefix) {
4450
dir = join(env.npm_config_local_prefix, join('node_modules', '.bin'));
4551
} else if (packageManager.name.toLowerCase() === 'pnpm') {

0 commit comments

Comments
 (0)