|
1 | 1 | import * as cp from 'node:child_process';
|
| 2 | +// eslint-disable-next-line import/no-extraneous-dependencies |
| 3 | +import * as jest from '@jest/globals'; |
| 4 | +import * as fs from 'node:fs'; |
| 5 | +import * as path from 'node:path'; |
2 | 6 | import { NeovimClient } from './api/client';
|
3 | 7 | import { attach } from './attach';
|
4 | 8 | import { findNvim } from './utils/findNvim';
|
5 | 9 |
|
6 |
| -export function startNvim(): [cp.ChildProcessWithoutNullStreams, NeovimClient] |
7 |
| -export function startNvim(doAttach: false): [cp.ChildProcessWithoutNullStreams, undefined] |
8 |
| -export function startNvim(doAttach: true): [cp.ChildProcessWithoutNullStreams, NeovimClient] |
| 10 | +export function findNvimOrFail() { |
| 11 | + const minVersion = '0.9.5'; |
| 12 | + const found = findNvim({ minVersion }); |
| 13 | + if (found.matches.length === 0) { |
| 14 | + throw new Error(`nvim ${minVersion} not found`); |
| 15 | + } |
| 16 | + return found.matches[0].path; |
| 17 | +} |
| 18 | + |
| 19 | +const nvimPath = findNvimOrFail(); |
| 20 | + |
| 21 | +let proc: cp.ChildProcessWithoutNullStreams; |
| 22 | +let nvim: NeovimClient; |
| 23 | + |
| 24 | +export function startNvim(): [cp.ChildProcessWithoutNullStreams, NeovimClient]; |
| 25 | +export function startNvim( |
| 26 | + doAttach: false |
| 27 | +): [cp.ChildProcessWithoutNullStreams, undefined]; |
| 28 | +export function startNvim( |
| 29 | + doAttach: true |
| 30 | +): [cp.ChildProcessWithoutNullStreams, NeovimClient]; |
9 | 31 | export function startNvim(
|
10 | 32 | doAttach: boolean = true
|
11 | 33 | ): [cp.ChildProcessWithoutNullStreams, NeovimClient | undefined] {
|
12 |
| - const proc = cp.spawn('nvim', ['-u', 'NONE', '--embed', '-n', '--noplugin'], { |
| 34 | + const testFile = jest.expect.getState().testPath?.replace(/.*[\\/]/, ''); |
| 35 | + const msg = `startNvim in test: ${testFile}`; |
| 36 | + // console.log(msg); |
| 37 | + if (process.env.NVIM_NODE_LOG_FILE) { |
| 38 | + const logfile = path.resolve(process.env.NVIM_NODE_LOG_FILE); |
| 39 | + fs.writeFileSync(logfile, `${msg}\n`, { flag: 'a' }); |
| 40 | + } |
| 41 | + |
| 42 | + proc = cp.spawn(nvimPath, ['-u', 'NONE', '--embed', '-n', '--noplugin'], { |
13 | 43 | cwd: __dirname,
|
14 | 44 | });
|
15 | 45 | if (!doAttach) {
|
16 | 46 | return [proc, undefined];
|
17 | 47 | }
|
18 |
| - const nvim = attach({ proc }); |
| 48 | + nvim = attach({ proc }); |
19 | 49 | return [proc, nvim];
|
20 | 50 | }
|
21 | 51 |
|
22 | 52 | export function stopNvim(
|
23 |
| - proc_: cp.ChildProcessWithoutNullStreams | NeovimClient |
| 53 | + proc_?: cp.ChildProcessWithoutNullStreams | NeovimClient |
24 | 54 | ) {
|
| 55 | + // Stop all (proc + client). |
25 | 56 | if (!proc_) {
|
| 57 | + if (proc) { |
| 58 | + stopNvim(proc); |
| 59 | + } |
| 60 | + if (nvim) { |
| 61 | + stopNvim(nvim); |
| 62 | + } |
26 | 63 | return;
|
27 |
| - } else if (proc_ instanceof NeovimClient) { |
| 64 | + } |
| 65 | + |
| 66 | + if (proc_ instanceof NeovimClient) { |
28 | 67 | proc_.quit();
|
29 | 68 | } else if (proc_ && proc_.connected) {
|
30 | 69 | proc_.disconnect();
|
31 | 70 | }
|
32 | 71 | }
|
33 | 72 |
|
34 |
| -export function findNvimOrFail() { |
35 |
| - const minVersion = '0.9.5'; |
36 |
| - const found = findNvim({ minVersion }); |
37 |
| - if (found.matches.length === 0) { |
38 |
| - throw new Error(`nvim ${minVersion} not found`); |
39 |
| - } |
40 |
| - return found.matches[0].path; |
41 |
| -} |
| 73 | +// jest.beforeAll(async () => { |
| 74 | +// [proc, nvim] = startNvim(); |
| 75 | +// }); |
| 76 | +// jest.afterAll(() => { |
| 77 | +// stopNvim(); |
| 78 | +// }); |
0 commit comments