Skip to content

Commit

Permalink
fix: return the exe name if the file doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Feb 21, 2021
1 parent 9746992 commit d57e55a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Note that you will also need to add various entries to the `providedServices` an

### Minimal example (General LSP exe)

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).
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).

```javascript
const {AutoLanguageClient, getExePath} = require('atom-languageclient')
Expand Down
14 changes: 11 additions & 3 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { join, resolve } from 'path';
import { existsSync } from 'fs';
import {
Point,
TextBuffer,
Expand Down Expand Up @@ -114,8 +115,10 @@ export function promiseWithTimeout<T>(ms: number, promise: Promise<T>): Promise<
}


/** Finds an exe file in the package assuming it is placed under `rootPath/platform-arch/exe`
* For example on Windows x64, if the `exeName` is `serve-d`, it returns the absolute path to `./bin/win32-x64/exeName.exe`
/** Finds an exe file in the package assuming it is placed under `rootPath/platform-arch/exe`. If the exe file did not exist,
* the given name is returned.
* For example on Windows x64, if the `exeName` is `serve-d`, it returns the absolute path to `./bin/win32-x64/exeName.exe`, and
* if the file did not exist, `serve-d` is returned.
* @param exeName name of the exe file
* @param rootPath the path of the folder of the exe file. Defaults to 'join("bin", `${process.platform}-${process.arch}`)'
* @param exeExtention the extention of the exe file. Defaults to `process.platform === "win32" ? ".exe" : ""`
Expand All @@ -125,5 +128,10 @@ export function getExePath(
rootPath = join("bin", `${process.platform}-${process.arch}`),
exeExtention = process.platform === "win32" ? ".exe" : ""
): string {
return resolve(join(rootPath, `${exeName}${exeExtention}`));
const exePath = resolve(join(rootPath, `${exeName}${exeExtention}`));
if (existsSync(exePath)) {
return exePath
} else {
return exeName
}
}
22 changes: 20 additions & 2 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { createFakeEditor } from './helpers';
import { expect } from 'chai';
import { Point } from 'atom';
import { join } from 'path'
import * as fs from 'fs'
import * as sinon from 'sinon'

describe('Utils', () => {
describe('getWordAtPosition', () => {
Expand Down Expand Up @@ -34,21 +36,37 @@ describe('Utils', () => {

describe('getExePath', () => {
it('returns the exe path under bin folder by default', () => {
const exePath = Utils.getExePath('serve-d');
let expectedExe = join(process.cwd(), 'bin', `${process.platform}-${process.arch}`, 'serve-d');
if (process.platform === 'win32') {
expectedExe = expectedExe + '.exe';
}

const fsMock = sinon.mock(fs);
fsMock.expects('existsSync').withArgs(expectedExe).returns(true);

const exePath = Utils.getExePath('serve-d');
expect(exePath).eq(expectedExe);

fsMock.restore();
})
it('returns the exe path for the given root', () => {
const rootPath = join(__dirname, `${process.platform}-${process.arch}`);
const exePath = Utils.getExePath('serve-d', rootPath);
let expectedExe = join(rootPath, 'serve-d');
if (process.platform === 'win32') {
expectedExe = expectedExe + '.exe';
}

const fsMock = sinon.mock(fs);
fsMock.expects('existsSync').withArgs(expectedExe).returns(true);

const exePath = Utils.getExePath('serve-d', rootPath);
expect(exePath).eq(expectedExe);

fsMock.restore();
})
it('returns the exe name if the file does not exist under rootPath', () => {
const exePath = Utils.getExePath('python');
expect(exePath).eq('python');
})
})
});

0 comments on commit d57e55a

Please sign in to comment.