|
1 | 1 | import * as child_process from "child_process";
|
| 2 | +import { promisify } from "util"; |
| 3 | +import { Readable } from "stream"; |
2 | 4 | import * as path from "path";
|
3 | 5 | import * as fs from "fs";
|
4 | 6 | import * as process from "process";
|
5 | 7 | import * as vscode from "vscode";
|
6 | 8 |
|
| 9 | +const execFile = promisify(child_process.execFile); |
| 10 | + |
7 | 11 | export async function mroFormat(
|
8 | 12 | fileContent: string,
|
9 | 13 | fileName: string,
|
@@ -82,35 +86,28 @@ async function executeMro(
|
82 | 86 | mropath: string,
|
83 | 87 | token: vscode.CancellationToken,
|
84 | 88 | ): Promise<{ stdout: string; stderr: string }> {
|
85 |
| - const execOptions = { |
| 89 | + const execOptions: child_process.ExecOptions = { |
86 | 90 | env: getMroEnv(mropath, workspacePath),
|
87 | 91 | maxBuffer: Number.MAX_SAFE_INTEGER,
|
88 | 92 | };
|
89 | 93 | const exePath = await getDefaultMroExecutablePath(workspacePath);
|
90 | 94 | if (token.isCancellationRequested) {
|
91 | 95 | return { stdout: "", stderr: "" };
|
92 | 96 | }
|
93 |
| - return await new Promise((resolve, reject) => { |
94 |
| - const proc = child_process.execFile( |
95 |
| - exePath, |
96 |
| - args, |
97 |
| - execOptions, |
98 |
| - (error: Error, stdout: string, stderr: string) => { |
99 |
| - if (!error) { |
100 |
| - resolve({ stdout, stderr }); |
101 |
| - } else { |
102 |
| - reject(error); |
103 |
| - } |
104 |
| - }, |
105 |
| - ); |
106 |
| - token.onCancellationRequested(() => proc.kill()); |
107 |
| - if (token.isCancellationRequested) { |
108 |
| - proc.kill(); |
109 |
| - return; |
| 97 | + const procPromise = execFile( |
| 98 | + exePath, |
| 99 | + args, |
| 100 | + execOptions, |
| 101 | + ); |
| 102 | + const proc = procPromise.child; |
| 103 | + token.onCancellationRequested(() => proc.kill()); |
| 104 | + if (token.isCancellationRequested) { |
| 105 | + proc.kill(); |
| 106 | + if (proc.stdin) { |
| 107 | + proc.stdin.end(); |
110 | 108 | }
|
111 |
| - // Write the file being formatted to stdin and close the stream so |
112 |
| - // that the mro process continues. |
113 |
| - proc.stdin?.write(fileContent); |
114 |
| - proc.stdin?.end(); |
115 |
| - }); |
| 109 | + } else if (proc.stdin) { |
| 110 | + Readable.from([fileContent]).pipe(proc.stdin, { end: true }); |
| 111 | + } |
| 112 | + return await procPromise; |
116 | 113 | }
|
0 commit comments