Skip to content

Commit a978ec3

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

File tree

3 files changed

+42
-32
lines changed

3 files changed

+42
-32
lines changed

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
}

packages/neovim/src/utils/findNvim.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ describe('findNvim', () => {
127127
});
128128
});
129129

130-
it('searches in additional custom paths', () => {
130+
it('tries locations or commands given by `cmds`', () => {
131131
const customPaths = [
132132
join(process.cwd(), 'package.json'),
133133
'/custom/path/to/nvim',
134134
'/another/custom/path',
135-
].map(normalizePath);
136-
const nvimRes = findNvim({ paths: customPaths });
135+
].map(s => [ normalizePath(s) ]);
136+
const nvimRes = findNvim({ cmds: customPaths });
137137

138138
expect(nvimRes.matches.length).toBeGreaterThanOrEqual(1);
139139

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
*
@@ -144,48 +155,47 @@ function normalizePath(path: string): string {
144155
}
145156

146157
function getPlatformSearchDirs(): Set<string> {
147-
const paths = new Set<string>();
158+
const dirs = new Set<string>();
148159
const { PATH, USERPROFILE, LOCALAPPDATA, PROGRAMFILES, HOME } = process.env;
149160

150-
PATH?.split(delimiter).forEach(p => paths.add(normalizePath(p)));
161+
PATH?.split(delimiter).forEach(p => dirs.add(normalizePath(p)));
151162

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

160-
// Winget common install location
161-
// See https://github.com/microsoft/winget-cli/blob/master/doc/specs/%23182%20-%20Support%20for%20installation%20of%20portable%20standalone%20apps.md
171+
// Winget install location. https://github.com/microsoft/winget-cli/blob/master/doc/specs/%23182%20-%20Support%20for%20installation%20of%20portable%20standalone%20apps.md
162172
if (LOCALAPPDATA) {
163-
paths.add(normalizePath(`${LOCALAPPDATA}/Microsoft/WindowsApps`));
164-
paths.add(normalizePath(`${LOCALAPPDATA}/Microsoft/WinGet/Packages`));
173+
dirs.add(normalizePath(`${LOCALAPPDATA}/Microsoft/WindowsApps`));
174+
dirs.add(normalizePath(`${LOCALAPPDATA}/Microsoft/WinGet/Packages`));
165175
}
166176
if (PROGRAMFILES) {
167-
paths.add(normalizePath(`${PROGRAMFILES}/Neovim/bin`));
168-
paths.add(normalizePath(`${PROGRAMFILES} (x86)/Neovim/bin`));
169-
paths.add(normalizePath(`${PROGRAMFILES}/WinGet/Packages`));
170-
paths.add(normalizePath(`${PROGRAMFILES} (x86)/WinGet/Packages`));
177+
dirs.add(normalizePath(`${PROGRAMFILES}/Neovim/bin`));
178+
dirs.add(normalizePath(`${PROGRAMFILES} (x86)/Neovim/bin`));
179+
dirs.add(normalizePath(`${PROGRAMFILES}/WinGet/Packages`));
180+
dirs.add(normalizePath(`${PROGRAMFILES} (x86)/WinGet/Packages`));
171181
}
172182
} else {
173-
// Common paths for Unix-like systems
183+
// Common locations for Unix-like systems.
174184
[
175185
'/usr/local/bin',
176186
'/usr/bin',
177187
'/opt/homebrew/bin',
178188
'/home/linuxbrew/.linuxbrew/bin',
179189
'/snap/nvim/current/usr/bin',
180-
].forEach(p => paths.add(p));
190+
].forEach(p => dirs.add(p));
181191

182192
if (HOME) {
183-
paths.add(normalizePath(`${HOME}/bin`));
184-
paths.add(normalizePath(`${HOME}/.linuxbrew/bin`));
193+
dirs.add(normalizePath(`${HOME}/bin`));
194+
dirs.add(normalizePath(`${HOME}/.linuxbrew/bin`));
185195
}
186196
}
187197

188-
return paths;
198+
return dirs;
189199
}
190200

191201
/**
@@ -194,13 +204,13 @@ function getPlatformSearchDirs(): Set<string> {
194204
* @param opt.minVersion See {@link FindNvimOptions.minVersion}
195205
* @param opt.orderBy See {@link FindNvimOptions.orderBy}
196206
* @param opt.firstMatch See {@link FindNvimOptions.firstMatch}
197-
* @param opt.paths See {@link FindNvimOptions.paths}
207+
* @param opt.cmds See {@link FindNvimOptions.cmds}
198208
* @param opt.dirs See {@link FindNvimOptions.dirs}
199209
*/
200210
export function findNvim(opt: FindNvimOptions = {}): Readonly<FindNvimResult> {
201211
const platformDirs = getPlatformSearchDirs();
202212
const nvimExecutable = windows ? 'nvim.exe' : 'nvim';
203-
const normalizedPathsFromUser = (opt.paths ?? []).map(normalizePath);
213+
const normalizedPathsFromUser = (opt.cmds ?? []).map(a => normalizePath);
204214

205215
const allPaths = new Set<string>([
206216
...normalizedPathsFromUser,

0 commit comments

Comments
 (0)