Skip to content

Commit

Permalink
👍 Use Deno.Command instead of Deno.run
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed May 8, 2023
1 parent 1e7b3bd commit b44321f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 42 deletions.
32 changes: 11 additions & 21 deletions denops_test/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type RunMode = "vim" | "nvim";

/** Runner option */
export interface RunOptions
extends Omit<Deno.RunOptions, "cmd" | "stdin" | "stdout" | "stderr"> {
extends Omit<Deno.CommandOptions, "cmd" | "stdin" | "stdout" | "stderr"> {
verbose?: boolean;
}

Expand All @@ -16,19 +16,20 @@ export function run(
mode: RunMode,
cmds: string[],
options: RunOptions = {},
): Deno.Process {
const cmd = [...buildArgs(mode), ...cmds.flatMap((c) => ["-c", c])];
): Deno.ChildProcess {
const [cmd, args] = buildArgs(mode);
args.unshift(...cmds.flatMap((c) => ["-c", c]));
if (options.verbose) {
cmd.unshift("--cmd", "redir >> /dev/stdout");
args.unshift("--cmd", "redir >> /dev/stdout");
}
const proc = Deno.run({
cmd,
const command = new Deno.Command(cmd, {
args,
env: options.env,
stdin: "piped",
stdout: options.verbose ? "inherit" : "null",
stderr: options.verbose ? "inherit" : "null",
});
return proc;
return command.spawn();
}

/**
Expand All @@ -44,28 +45,17 @@ export function isRunMode(mode: string): mode is RunMode {
}
}

function buildArgs(mode: RunMode): string[] {
function buildArgs(mode: RunMode): [string, string[]] {
switch (mode) {
case "vim":
return [
VIM_EXECUTABLE,
"-u",
"NONE",
"-i",
"NONE",
"-n",
"-N",
"-X",
"-e",
"-s",
["-u", "NONE", "-i", "NONE", "-n", "-N", "-X", "-e", "-s"],
];
case "nvim":
return [
NVIM_EXECUTABLE,
"--clean",
"--embed",
"--headless",
"-n",
["--clean", "--embed", "--headless", "-n"],
];
}
}
23 changes: 2 additions & 21 deletions denops_test/with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,8 @@ export async function withDenops(
},
);
} finally {
proc.stdin?.close();
await killProcess(proc);
await proc.status();
proc.close();
proc.stdin.close();
proc.kill();
conn.close();
listener.close();
}
Expand All @@ -123,20 +121,3 @@ async function newDenopsImpl(
const { DenopsImpl } = await import(url.href);
return new DenopsImpl(pluginName, meta, session);
}

async function killProcess(proc: Deno.Process): Promise<void> {
if (Deno.build.os === "windows") {
// Signal API in Deno v1.14.0 on Windows
// does not work so use `taskkill` for now
const p = Deno.run({
cmd: ["taskkill", "/pid", proc.pid.toString(), "/F"],
stdin: "null",
stdout: "null",
stderr: "null",
});
await p.status();
p.close();
} else {
proc.kill("SIGTERM");
}
}

0 comments on commit b44321f

Please sign in to comment.