Add support for other charsets than utf-8 for input

This commit is contained in:
cjblomqvist 2014-03-27 17:54:23 +01:00
parent 14f290f8ab
commit 73abf0cb01
2 changed files with 20 additions and 4 deletions

View File

@ -9,6 +9,7 @@ var optimist = require("optimist");
var fs = require("fs");
var path = require("path");
var async = require("async");
var iconv = require("iconv-lite")
var acorn;
var ARGS = optimist
.usage("$0 input1.js [input2.js ...] [options]\n\
@ -64,6 +65,7 @@ You need to pass an argument to this option to specify the name that your module
.describe("v", "Verbose")
.describe("V", "Print version number and exit.")
.describe("noerr", "Don't throw an error for unknown options in -c, -b or -m.")
.describe("charset-input", "Charset of input (default utf-8)")
.alias("p", "prefix")
.alias("o", "output")
@ -87,6 +89,7 @@ You need to pass an argument to this option to specify the name that your module
.string("comments")
.string("wrap")
.string("p")
.string("charset-input")
.boolean("expr")
.boolean("source-map-include-sources")
@ -434,16 +437,28 @@ function getOptions(x, constants) {
function read_whole_file(filename, cb) {
if (filename == "-") {
var chunks = [];
process.stdin.setEncoding('utf-8');
if(!ARGS.charset_input) process.stdin.setEncoding('utf-8');
process.stdin.on('data', function (chunk) {
chunks.push(chunk);
}).on('end', function () {
if(ARGS.charset_input) {
cb(null, iconv.decode(Buffer.concat(chunks), ARGS.charset_input));
} else {
cb(null, chunks.join(""));
}
});
process.openStdin();
} else {
if(ARGS.charset_input) {
fs.readFile(filename, function (err, data) {
if(err) cb(err, data);
cb(err, iconv.decode(data, ARGS.charset_input));
});
} else {
fs.readFile(filename, "utf-8", cb);
}
}
}
function time_it(name, cont) {

View File

@ -18,7 +18,8 @@
"async" : "~0.2.6",
"source-map" : "~0.1.33",
"optimist" : "~0.3.5",
"uglify-to-browserify": "~1.0.0"
"uglify-to-browserify": "~1.0.0",
"iconv-lite": "*"
},
"browserify": {
"transform": [ "uglify-to-browserify" ]