Skip to content

Commit 1df3a53

Browse files
committed
feat(findNvim)!: rename paths:string[] => cmds:string[][]
1 parent 7488014 commit 1df3a53

File tree

3 files changed

+42
-32
lines changed

3 files changed

+42
-32
lines changed

Diff for: packages/neovim/src/testUtil.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getLogger } from './utils/logger';
1010

1111
export function findNvimOrFail() {
1212
const minVersion = '0.9.5';
13-
const found = findNvim({ minVersion });
13+
const found = findNvim({ minVersion, firstMatch: true });
1414
if (found.matches.length === 0) {
1515
throw new Error(`nvim ${minVersion} not found`);
1616
}

Diff for: packages/neovim/src/utils/findNvim.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ describe('findNvim', () => {
149149
});
150150
});
151151

152-
it('searches in additional custom paths', () => {
152+
it('tries locations or commands given by `cmds`', () => {
153153
const customPaths = [
154154
join(process.cwd(), 'package.json'),
155155
'/custom/path/to/nvim',
156156
'/another/custom/path',
157-
].map(normalizePath);
158-
const nvimRes = findNvim({ paths: customPaths });
157+
].map(s => [ normalizePath(s) ]);
158+
const nvimRes = findNvim({ cmds: customPaths });
159159

160160
expect(nvimRes.matches.length).toBeGreaterThanOrEqual(1);
161161

Diff for: packages/neovim/src/utils/findNvim.ts

+38-28
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ import { join, delimiter, normalize } from 'node:path';
33
import { constants, existsSync, accessSync } from 'node:fs';
44

55
export type NvimVersion = {
6-
/** Path to `nvim` executable. */
6+
/**
7+
* @deprecated
8+
* Path to `nvim` executable.
9+
*/
710
readonly path: string;
11+
/** Nvim location or invocation command. */
12+
readonly cmd: string[];
813
/** Nvim version, or undefined if there was an error. */
914
readonly nvimVersion?: string;
1015
/** Nvim build type, or undefined if there was an error. */
@@ -36,16 +41,22 @@ export type FindNvimOptions = {
3641
*/
3742
readonly firstMatch?: boolean;
3843
/**
39-
* (Optional) Additional specific file paths to check for Nvim executables.
40-
* These paths will be checked before searching `dirs`.
41-
* Useful for allowing users to specify exact Nvim executable locations.
44+
* (Optional) Specific commands that (potentially) invoke Nvim and can receive arbitrary args.
45+
* Checked before searching `dirs`. Useful for checking a user-configured Nvim location or
46+
* unconventional wrappers such as Windows WSL.
4247
*
43-
* Example: ['/usr/local/bin/nvim', '/opt/homebrew/bin/nvim']
48+
* Example:
49+
* ```
50+
* cmds: [
51+
* ['/usr/bin/env', 'nvim'],
52+
* ['/opt/homebrew/bin/nvim'],
53+
* ],
54+
* ```
4455
*/
45-
readonly paths?: string[];
56+
readonly cmds?: string[][];
4657
/**
4758
* (Optional) Additional directories to search for Nvim executables.
48-
* These directories will be searched after checking `paths`
59+
* These directories will be searched after checking `cmds`
4960
* but before searching `$PATH` and other default locations.
5061
* Useful for including non-standard installation directories.
5162
*
@@ -148,48 +159,47 @@ function normalizePath(path: string): string {
148159
}
149160

150161
function getPlatformSearchDirs(): Set<string> {
151-
const paths = new Set<string>();
162+
const dirs = new Set<string>();
152163
const { PATH, USERPROFILE, LOCALAPPDATA, PROGRAMFILES, HOME } = process.env;
153164

154-
PATH?.split(delimiter).forEach(p => paths.add(normalizePath(p)));
165+
PATH?.split(delimiter).forEach(p => dirs.add(normalizePath(p)));
155166

156-
// Add common Neovim installation paths not always in the system's PATH.
167+
// Add common Nvim locations which may not be in the system $PATH.
157168
if (windows) {
158-
// Scoop common install location
169+
// Scoop install location.
159170
if (USERPROFILE) {
160-
paths.add(normalizePath(`${USERPROFILE}/scoop/shims`));
171+
dirs.add(normalizePath(`${USERPROFILE}/scoop/shims`));
161172
}
162-
paths.add(normalizePath('C:/ProgramData/scoop/shims'));
173+
dirs.add(normalizePath('C:/ProgramData/scoop/shims'));
163174

164-
// Winget common install location
165-
// See https://github.com/microsoft/winget-cli/blob/master/doc/specs/%23182%20-%20Support%20for%20installation%20of%20portable%20standalone%20apps.md
175+
// Winget install location. https://github.com/microsoft/winget-cli/blob/master/doc/specs/%23182%20-%20Support%20for%20installation%20of%20portable%20standalone%20apps.md
166176
if (LOCALAPPDATA) {
167-
paths.add(normalizePath(`${LOCALAPPDATA}/Microsoft/WindowsApps`));
168-
paths.add(normalizePath(`${LOCALAPPDATA}/Microsoft/WinGet/Packages`));
177+
dirs.add(normalizePath(`${LOCALAPPDATA}/Microsoft/WindowsApps`));
178+
dirs.add(normalizePath(`${LOCALAPPDATA}/Microsoft/WinGet/Packages`));
169179
}
170180
if (PROGRAMFILES) {
171-
paths.add(normalizePath(`${PROGRAMFILES}/Neovim/bin`));
172-
paths.add(normalizePath(`${PROGRAMFILES} (x86)/Neovim/bin`));
173-
paths.add(normalizePath(`${PROGRAMFILES}/WinGet/Packages`));
174-
paths.add(normalizePath(`${PROGRAMFILES} (x86)/WinGet/Packages`));
181+
dirs.add(normalizePath(`${PROGRAMFILES}/Neovim/bin`));
182+
dirs.add(normalizePath(`${PROGRAMFILES} (x86)/Neovim/bin`));
183+
dirs.add(normalizePath(`${PROGRAMFILES}/WinGet/Packages`));
184+
dirs.add(normalizePath(`${PROGRAMFILES} (x86)/WinGet/Packages`));
175185
}
176186
} else {
177-
// Common paths for Unix-like systems
187+
// Common locations for Unix-like systems.
178188
[
179189
'/usr/local/bin',
180190
'/usr/bin',
181191
'/opt/homebrew/bin',
182192
'/home/linuxbrew/.linuxbrew/bin',
183193
'/snap/nvim/current/usr/bin',
184-
].forEach(p => paths.add(p));
194+
].forEach(p => dirs.add(p));
185195

186196
if (HOME) {
187-
paths.add(normalizePath(`${HOME}/bin`));
188-
paths.add(normalizePath(`${HOME}/.linuxbrew/bin`));
197+
dirs.add(normalizePath(`${HOME}/bin`));
198+
dirs.add(normalizePath(`${HOME}/.linuxbrew/bin`));
189199
}
190200
}
191201

192-
return paths;
202+
return dirs;
193203
}
194204

195205
/**
@@ -198,13 +208,13 @@ function getPlatformSearchDirs(): Set<string> {
198208
* @param opt.minVersion See {@link FindNvimOptions.minVersion}
199209
* @param opt.orderBy See {@link FindNvimOptions.orderBy}
200210
* @param opt.firstMatch See {@link FindNvimOptions.firstMatch}
201-
* @param opt.paths See {@link FindNvimOptions.paths}
211+
* @param opt.cmds See {@link FindNvimOptions.cmds}
202212
* @param opt.dirs See {@link FindNvimOptions.dirs}
203213
*/
204214
export function findNvim(opt: FindNvimOptions = {}): Readonly<FindNvimResult> {
205215
const platformDirs = getPlatformSearchDirs();
206216
const nvimExecutable = windows ? 'nvim.exe' : 'nvim';
207-
const normalizedPathsFromUser = (opt.paths ?? []).map(normalizePath);
217+
const normalizedPathsFromUser = (opt.cmds ?? []).map(a => normalizePath);
208218

209219
const allPaths = new Set<string>([
210220
...normalizedPathsFromUser,

0 commit comments

Comments
 (0)