Skip to content

Commit d57e55a

Browse files
committed
fix: return the exe name if the file doesn't exist
1 parent 9746992 commit d57e55a

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Note that you will also need to add various entries to the `providedServices` an
8585

8686
### Minimal example (General LSP exe)
8787

88-
If the LSP is a general executable (not a JavaScript file), you should use `spawn` inside `startServerProcess`. `getExePath` is a cross-platform utility function to get the exe path for the given exe name (under the `bin/platform-arch/exeName` by default).
88+
If the LSP is a general executable (not a JavaScript file), you should use `spawn` inside `startServerProcess`. `getExePath` is a cross-platform utility function to get the exe path for the given exe name (under the `bin/platform-arch/exeName` by default or `exeName` if the file doesn't exist).
8989

9090
```javascript
9191
const {AutoLanguageClient, getExePath} = require('atom-languageclient')

lib/utils.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { join, resolve } from 'path';
2+
import { existsSync } from 'fs';
23
import {
34
Point,
45
TextBuffer,
@@ -114,8 +115,10 @@ export function promiseWithTimeout<T>(ms: number, promise: Promise<T>): Promise<
114115
}
115116

116117

117-
/** Finds an exe file in the package assuming it is placed under `rootPath/platform-arch/exe`
118-
* For example on Windows x64, if the `exeName` is `serve-d`, it returns the absolute path to `./bin/win32-x64/exeName.exe`
118+
/** Finds an exe file in the package assuming it is placed under `rootPath/platform-arch/exe`. If the exe file did not exist,
119+
* the given name is returned.
120+
* For example on Windows x64, if the `exeName` is `serve-d`, it returns the absolute path to `./bin/win32-x64/exeName.exe`, and
121+
* if the file did not exist, `serve-d` is returned.
119122
* @param exeName name of the exe file
120123
* @param rootPath the path of the folder of the exe file. Defaults to 'join("bin", `${process.platform}-${process.arch}`)'
121124
* @param exeExtention the extention of the exe file. Defaults to `process.platform === "win32" ? ".exe" : ""`
@@ -125,5 +128,10 @@ export function getExePath(
125128
rootPath = join("bin", `${process.platform}-${process.arch}`),
126129
exeExtention = process.platform === "win32" ? ".exe" : ""
127130
): string {
128-
return resolve(join(rootPath, `${exeName}${exeExtention}`));
131+
const exePath = resolve(join(rootPath, `${exeName}${exeExtention}`));
132+
if (existsSync(exePath)) {
133+
return exePath
134+
} else {
135+
return exeName
136+
}
129137
}

test/utils.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { createFakeEditor } from './helpers';
33
import { expect } from 'chai';
44
import { Point } from 'atom';
55
import { join } from 'path'
6+
import * as fs from 'fs'
7+
import * as sinon from 'sinon'
68

79
describe('Utils', () => {
810
describe('getWordAtPosition', () => {
@@ -34,21 +36,37 @@ describe('Utils', () => {
3436

3537
describe('getExePath', () => {
3638
it('returns the exe path under bin folder by default', () => {
37-
const exePath = Utils.getExePath('serve-d');
3839
let expectedExe = join(process.cwd(), 'bin', `${process.platform}-${process.arch}`, 'serve-d');
3940
if (process.platform === 'win32') {
4041
expectedExe = expectedExe + '.exe';
4142
}
43+
44+
const fsMock = sinon.mock(fs);
45+
fsMock.expects('existsSync').withArgs(expectedExe).returns(true);
46+
47+
const exePath = Utils.getExePath('serve-d');
4248
expect(exePath).eq(expectedExe);
49+
50+
fsMock.restore();
4351
})
4452
it('returns the exe path for the given root', () => {
4553
const rootPath = join(__dirname, `${process.platform}-${process.arch}`);
46-
const exePath = Utils.getExePath('serve-d', rootPath);
4754
let expectedExe = join(rootPath, 'serve-d');
4855
if (process.platform === 'win32') {
4956
expectedExe = expectedExe + '.exe';
5057
}
58+
59+
const fsMock = sinon.mock(fs);
60+
fsMock.expects('existsSync').withArgs(expectedExe).returns(true);
61+
62+
const exePath = Utils.getExePath('serve-d', rootPath);
5163
expect(exePath).eq(expectedExe);
64+
65+
fsMock.restore();
66+
})
67+
it('returns the exe name if the file does not exist under rootPath', () => {
68+
const exePath = Utils.getExePath('python');
69+
expect(exePath).eq('python');
5270
})
5371
})
5472
});

0 commit comments

Comments
 (0)