Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 1da8030

Browse files
authored
Merge pull request #364 from lialosiu/master
win32 platform support not utf8 encoding at childProcess.spawn() stdout/stderr
2 parents d52dd49 + f3d28de commit 1da8030

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@
532532
"eventemitter2": "^4.1.0",
533533
"express": "^4.14.1",
534534
"glob": "^7.1.1",
535+
"iconv-lite": "^0.4.18",
535536
"properties": "^1.2.1",
536537
"uuid": "^3.0.1",
537538
"vscode-extension-telemetry": "0.0.6",

src/common/util.ts

+32-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import * as childProcess from "child_process";
55
import * as fs from "fs";
6+
import * as iconv from "iconv-lite";
67
import * as os from "os";
78
import * as path from "path";
89
import * as properties from "properties";
@@ -203,9 +204,38 @@ export function spawn(command: string, outputChannel: vscode.OutputChannel, args
203204
options.cwd = options.cwd || path.resolve(path.join(__dirname, ".."));
204205
const child = childProcess.spawn(command, args, options);
205206

207+
let codepage = "65001";
208+
if (os.platform() === "win32") {
209+
codepage = childProcess.execSync("chcp").toString().split(":").pop().trim();
210+
}
211+
206212
if (outputChannel) {
207-
child.stdout.on("data", (data) => { outputChannel.append(data.toString()); });
208-
child.stderr.on("data", (data) => { outputChannel.append(data.toString()); });
213+
child.stdout.on("data", (data: Buffer) => {
214+
switch (codepage) {
215+
case "932":
216+
outputChannel.append(iconv.decode(data, "Shift_JIS"));
217+
break;
218+
case "936":
219+
outputChannel.append(iconv.decode(data, "GBK"));
220+
break;
221+
default:
222+
outputChannel.append(data.toString());
223+
break;
224+
}
225+
});
226+
child.stderr.on("data", (data: Buffer) => {
227+
switch (codepage) {
228+
case "932":
229+
outputChannel.append(iconv.decode(data, "Shift_JIS"));
230+
break;
231+
case "936":
232+
outputChannel.append(iconv.decode(data, "GBK"));
233+
break;
234+
default:
235+
outputChannel.append(data.toString());
236+
break;
237+
}
238+
});
209239
}
210240

211241
child.on("error", (error) => reject({ error, stderr, stdout }));

0 commit comments

Comments
 (0)