diff --git a/package.json b/package.json index c8423133..d8d53096 100644 --- a/package.json +++ b/package.json @@ -532,6 +532,7 @@ "eventemitter2": "^4.1.0", "express": "^4.14.1", "glob": "^7.1.1", + "iconv-lite": "^0.4.18", "properties": "^1.2.1", "uuid": "^3.0.1", "vscode-extension-telemetry": "0.0.6", diff --git a/src/common/util.ts b/src/common/util.ts index ef7ea6e6..00b7e549 100644 --- a/src/common/util.ts +++ b/src/common/util.ts @@ -3,6 +3,7 @@ import * as childProcess from "child_process"; import * as fs from "fs"; +import * as iconv from "iconv-lite"; import * as os from "os"; import * as path from "path"; import * as properties from "properties"; @@ -203,9 +204,38 @@ export function spawn(command: string, outputChannel: vscode.OutputChannel, args options.cwd = options.cwd || path.resolve(path.join(__dirname, "..")); const child = childProcess.spawn(command, args, options); + let codepage = "65001"; + if (os.platform() === "win32") { + codepage = childProcess.execSync("chcp").toString().split(":").pop().trim(); + } + if (outputChannel) { - child.stdout.on("data", (data) => { outputChannel.append(data.toString()); }); - child.stderr.on("data", (data) => { outputChannel.append(data.toString()); }); + child.stdout.on("data", (data: Buffer) => { + switch (codepage) { + case "932": + outputChannel.append(iconv.decode(data, "Shift_JIS")); + break; + case "936": + outputChannel.append(iconv.decode(data, "GBK")); + break; + default: + outputChannel.append(data.toString()); + break; + } + }); + child.stderr.on("data", (data: Buffer) => { + switch (codepage) { + case "932": + outputChannel.append(iconv.decode(data, "Shift_JIS")); + break; + case "936": + outputChannel.append(iconv.decode(data, "GBK")); + break; + default: + outputChannel.append(data.toString()); + break; + } + }); } child.on("error", (error) => reject({ error, stderr, stdout }));