-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathwith.ts
More file actions
152 lines (143 loc) · 4.24 KB
/
with.ts
File metadata and controls
152 lines (143 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { channel } from "@core/streamutil";
import { tap } from "@milly/streams/transform/tap";
import { ADDR_ENV_NAME } from "./cli.ts";
import { getConfig } from "./conf.ts";
const script = new URL("./cli.ts", import.meta.url);
const origLog = console.log.bind(console);
const origError = console.error.bind(console);
const noop = () => {};
export type Fn<T> = (helper: {
reader: ReadableStream<Uint8Array>;
writer: WritableStream<Uint8Array>;
stdout: ReadableStream<string>;
stderr: ReadableStream<string>;
}) => T;
export interface WithOptions<T> {
fn: Fn<T>;
/** Print Vim messages (echomsg). */
verbose?: boolean;
/** Vim commands to be executed before the startup. */
prelude?: string[];
/** Vim commands to be executed after the startup. */
postlude?: string[];
/** Environment variables. */
env?: Record<string, string>;
}
export function withVim<T>(
options: WithOptions<T>,
): Promise<Awaited<T>> {
const conf = getConfig();
const exec = Deno.execPath();
const commands = [
...(options.prelude ?? []),
`set runtimepath^=${conf.denopsPath}`,
"let g:denops_test_channel = job_start(" +
` ['${exec}', 'run', '--allow-all', '${script}'],` +
` {'mode': 'json', 'err_mode': 'nl'}` +
")",
...(options.postlude ?? []),
];
const cmd = conf.vimExecutable;
const args = [
"-u",
"NONE", // Disable vimrc, plugins, defaults.vim
"-i",
"NONE", // Disable viminfo
"-n", // Disable swap file
"-N", // Disable compatible mode
"-X", // Disable xterm
"-e", // Start Vim in Ex mode
"-s", // Silent or batch mode ("-e" is required before)
"-V1", // Verbose level 1 (Echo messages to stderr)
"-c",
"visual", // Go to Normal mode
"-c",
"set columns=9999 shortmess-=T", // Avoid unwilling output newline/truncation
...commands.flatMap((c) => ["-c", c]),
];
return withProcess(cmd, args, { verbose: conf.verbose, ...options });
}
export function withNeovim<T>(
options: WithOptions<T>,
): Promise<Awaited<T>> {
const conf = getConfig();
const exec = Deno.execPath();
const commands = [
...(options.prelude ?? []),
`set runtimepath^=${conf.denopsPath}`,
"let g:denops_test_channel = jobstart(" +
` ['${exec}', 'run', '--allow-all', '${script}'],` +
` {'rpc': v:true}` +
")",
...(options.postlude ?? []),
];
const cmd = conf.nvimExecutable;
const args = [
"--clean",
"--headless",
"-n", // Disable swap file
"-V1", // Verbose level 1 (Echo messages to stderr)
"-c",
"set columns=9999", // Avoid unwilling output newline
...commands.flatMap((c) => ["-c", c]),
];
return withProcess(cmd, args, { verbose: conf.verbose, ...options });
}
async function withProcess<T>(
cmd: string,
args: string[],
{ fn, env, verbose }: WithOptions<T>,
): Promise<Awaited<T>> {
const aborter = new AbortController();
const { signal } = aborter;
const listener = Deno.listen({
hostname: "127.0.0.1",
port: 0, // Automatically select free port
});
const command = new Deno.Command(cmd, {
args,
stdin: "piped",
stdout: "piped",
stderr: "piped",
env: {
...env,
[ADDR_ENV_NAME]: JSON.stringify(listener.addr),
},
signal,
});
const proc = command.spawn();
let stdout = proc.stdout.pipeThrough(new TextDecoderStream(), { signal });
let stderr = proc.stderr.pipeThrough(new TextDecoderStream(), { signal });
if (verbose) {
stdout = stdout.pipeThrough(tap((s) => origLog(s)));
stderr = stderr.pipeThrough(tap((s) => origError(s)));
}
const { writer: stdoutWriter, reader: stdoutReader } = channel<string>();
stdout.pipeTo(stdoutWriter).catch(noop);
const { writer: stderrWriter, reader: stderrReader } = channel<string>();
stderr.pipeTo(stderrWriter).catch(noop);
const conn = await listener.accept();
try {
return await fn({
reader: conn.readable,
writer: conn.writable,
stdout: stdoutReader,
stderr: stderrReader,
});
} finally {
listener.close();
try {
aborter.abort("withProcess disposed");
} catch {
// Already exited, do nothing.
}
await Promise.all([
proc.stdin.close(),
proc.status,
]);
await Promise.all([
proc.stdout.cancel(),
proc.stderr.cancel(),
]);
}
}