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

win32 platform support not utf8 encoding at childProcess.spawn() stdout/stderr #364

Merged
merged 2 commits into from
Jul 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 32 additions & 2 deletions src/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this, I would suggest use a config file called (codepageMapping.json) which we can put under ./msci folder as mapping files. (The folder name might be changed later.)

If can find the codepage from the mapping, just convert it. Otherwise use the default one.

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;
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid the duplication, the code is better to implement on the outputChannel side.

}

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