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 fs = require("fs");
var path = require("path"); var path = require("path");
var async = require("async"); var async = require("async");
var iconv = require("iconv-lite")
var acorn; var acorn;
var ARGS = optimist var ARGS = optimist
.usage("$0 input1.js [input2.js ...] [options]\n\ .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", "Verbose")
.describe("V", "Print version number and exit.") .describe("V", "Print version number and exit.")
.describe("noerr", "Don't throw an error for unknown options in -c, -b or -m.") .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("p", "prefix")
.alias("o", "output") .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("comments")
.string("wrap") .string("wrap")
.string("p") .string("p")
.string("charset-input")
.boolean("expr") .boolean("expr")
.boolean("source-map-include-sources") .boolean("source-map-include-sources")
@ -434,15 +437,27 @@ function getOptions(x, constants) {
function read_whole_file(filename, cb) { function read_whole_file(filename, cb) {
if (filename == "-") { if (filename == "-") {
var chunks = []; var chunks = [];
process.stdin.setEncoding('utf-8'); if(!ARGS.charset_input) process.stdin.setEncoding('utf-8');
process.stdin.on('data', function (chunk) { process.stdin.on('data', function (chunk) {
chunks.push(chunk); chunks.push(chunk);
}).on('end', function () { }).on('end', function () {
cb(null, chunks.join("")); if(ARGS.charset_input) {
cb(null, iconv.decode(Buffer.concat(chunks), ARGS.charset_input));
} else {
cb(null, chunks.join(""));
}
}); });
process.openStdin(); process.openStdin();
} else { } else {
fs.readFile(filename, "utf-8", cb); 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);
}
} }
} }

View File

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