Skip to content

Commit

Permalink
feat: add stopOnFirstMatch option for findNvim
Browse files Browse the repository at this point in the history
  • Loading branch information
gjf7 committed Sep 19, 2024
1 parent 21e9f77 commit de91d83
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
16 changes: 16 additions & 0 deletions packages/neovim/src/utils/findNvim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,20 @@ describe('findNvim', () => {
error: undefined,
});
});

it('stops searching on first match when stopOnFirstMatch is True', () => {
const nvimRes = findNvim({ minVersion: '0.3.0', stopOnFirstMatch: true });
expect(nvimRes).toEqual({
matches: expect.any(Array),
invalid: expect.any(Array),
});
expect(nvimRes.matches.length).toEqual(1);
expect(nvimRes.matches[0]).toEqual({
nvimVersion: expect.any(String),
path: expect.any(String),
buildType: expect.any(String),
luaJitVersion: expect.any(String),
error: undefined,
});
});
});
43 changes: 35 additions & 8 deletions packages/neovim/src/utils/findNvim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export type FindNvimOptions = {
* - Example: `['0.4.4', '0.5.0', '0.4.3']`
*/
readonly orderBy?: 'desc' | 'none';
/**
* (Optional) Stop searching after found a valid match
*/
readonly stopOnFirstMatch?: boolean;
};

export type FindNvimResult = {
Expand Down Expand Up @@ -118,14 +122,31 @@ function compareVersions(a: string, b: string): number {
*
* @param opt.minVersion See {@link FindNvimOptions.minVersion}
* @param opt.orderBy See {@link FindNvimOptions.orderBy}
* @param opt.stopOnFirstMatch See {@link FindNvimOptions.stopOnFirstMatch}
*/
export function findNvim(opt: FindNvimOptions = {}): Readonly<FindNvimResult> {
export function findNvim({
minVersion,
orderBy,
stopOnFirstMatch,
}: FindNvimOptions = {}): Readonly<FindNvimResult> {
const paths = process.env.PATH?.split(delimiter) ?? [];
const pathLength = paths.length;
paths.push(
'/usr/local/bin',
'/usr/bin',
'/opt/homebrew/bin',
'/home/linuxbrew/.linuxbrew/bin',
'/snap/nvim/current/usr/bin'
);
const home = process.env.HOME;
if (home) {
paths.push(`${home}/bin`, `${home}/.linuxbrew/bin`);
}

const matches = new Array<NvimVersion>();
const invalid = new Array<NvimVersion>();
for (let i = 0; i !== pathLength; i = i + 1) {
const nvimPath = join(paths[i], windows ? 'nvim.exe' : 'nvim');
const uniquePaths = new Set(paths);
for (const path of uniquePaths) {
const nvimPath = join(path, windows ? 'nvim.exe' : 'nvim');
if (existsSync(nvimPath)) {
try {
accessSync(nvimPath, constants.X_OK);
Expand All @@ -137,9 +158,8 @@ export function findNvim(opt: FindNvimOptions = {}): Readonly<FindNvimResult> {
const luaJitVersionMatch = luaJitVersionRegex.exec(nvimVersionFull);
if (nvimVersionMatch && buildTypeMatch && luaJitVersionMatch) {
if (
'minVersion' in opt &&
compareVersions(opt.minVersion ?? '0.0.0', nvimVersionMatch[1]) ===
1
minVersion &&
compareVersions(minVersion, nvimVersionMatch[1]) === 1
) {
invalid.push({
nvimVersion: nvimVersionMatch[1],
Expand All @@ -154,6 +174,13 @@ export function findNvim(opt: FindNvimOptions = {}): Readonly<FindNvimResult> {
buildType: buildTypeMatch[1],
luaJitVersion: luaJitVersionMatch[1],
});

if (stopOnFirstMatch) {
return {
matches,
invalid,
} as const;
}
}
}
} catch (e) {
Expand All @@ -165,7 +192,7 @@ export function findNvim(opt: FindNvimOptions = {}): Readonly<FindNvimResult> {
}
}

if (opt.orderBy === undefined || opt.orderBy === 'desc') {
if (orderBy === undefined || orderBy === 'desc') {
matches.sort((a, b) =>
compareVersions(b.nvimVersion ?? '0.0.0', a.nvimVersion ?? '0.0.0')
);
Expand Down

0 comments on commit de91d83

Please sign in to comment.