Skip to content

Commit

Permalink
Merge pull request #4 from vim-denops/command
Browse files Browse the repository at this point in the history
👍 Use `Deno.Command` instead
  • Loading branch information
lambdalisue committed May 8, 2023
2 parents 63f5084 + e6f5562 commit a09b064
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- macos-latest
- ubuntu-latest
version:
- "1.28.1"
- "1.28"
- "1.x"
host_version:
- vim: "v9.0.0472"
Expand Down
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"],
];
}
}
24 changes: 3 additions & 21 deletions denops_test/with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ export async function withDenops(
},
);
} finally {
proc.stdin?.close();
await killProcess(proc);
await proc.status();
proc.close();
proc.stdin.close();
proc.kill();
await proc.status;
conn.close();
listener.close();
}
Expand All @@ -123,20 +122,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 a09b064

Please sign in to comment.