uglifyjs binary: Make read_whole_file async and don't attempt to read STDIN synchronously.

This commit is contained in:
Andreas Lind Petersen
2013-03-31 11:51:43 +02:00
committed by Mihai Bazon
parent 7628bcac01
commit 69dde0462b
2 changed files with 109 additions and 103 deletions

View File

@@ -7,6 +7,7 @@ var UglifyJS = require("../tools/node");
var sys = require("util"); var sys = require("util");
var optimist = require("optimist"); var optimist = require("optimist");
var fs = require("fs"); var fs = require("fs");
var async = require("async");
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\
@@ -215,8 +216,12 @@ try {
} }
} }
files.forEach(function(file) { async.eachLimit(files, 1, function (file, cb) {
var code = read_whole_file(file); read_whole_file(file, function (err, code) {
if (err) {
sys.error("ERROR: can't read file: " + filename);
process.exit(1);
}
if (ARGS.p != null) { if (ARGS.p != null) {
file = file.replace(/^\/+/, "").split(/\/+/).slice(ARGS.p).join("/"); file = file.replace(/^\/+/, "").split(/\/+/).slice(ARGS.p).join("/");
} }
@@ -241,8 +246,9 @@ files.forEach(function(file) {
}); });
}; };
}); });
cb();
}); });
}, function () {
if (ARGS.acorn || ARGS.spidermonkey) time_it("convert_ast", function(){ if (ARGS.acorn || ARGS.spidermonkey) time_it("convert_ast", function(){
TOPLEVEL = UglifyJS.AST_Node.from_mozilla_ast(TOPLEVEL); TOPLEVEL = UglifyJS.AST_Node.from_mozilla_ast(TOPLEVEL);
}); });
@@ -319,6 +325,7 @@ if (ARGS.stats) {
})); }));
} }
} }
});
/* -----[ functions ]----- */ /* -----[ functions ]----- */
@@ -363,20 +370,18 @@ function getOptions(x, constants) {
return ret; return ret;
} }
function read_whole_file(filename) { function read_whole_file(filename, cb) {
try {
if (filename == "-") { if (filename == "-") {
var chunks = []; var chunks = [];
do { process.stdin.setEncoding('utf-8');
var chunk = fs.readFileSync("/dev/stdin", "utf8"); process.stdin.on('data', function (chunk) {
chunks.push(chunk); chunks.push(chunk);
} while (chunk.length); }).on('end', function () {
return chunks.join(""); cb(null, chunks.join(""));
} });
return fs.readFileSync(filename, "utf8"); process.openStdin();
} catch(ex) { } else {
sys.error("ERROR: can't read file: " + filename); fs.readFile(filename, "utf-8", cb);
process.exit(1);
} }
} }

View File

@@ -15,8 +15,9 @@
"url": "https://github.com/mishoo/UglifyJS2.git" "url": "https://github.com/mishoo/UglifyJS2.git"
}], }],
"dependencies": { "dependencies": {
"async" : "~0.2.6",
"source-map" : "~0.1.7", "source-map" : "~0.1.7",
"optimist" : "~0.3.5" "optimist" : "~0.3.5",
}, },
"bin": { "bin": {
"uglifyjs" : "bin/uglifyjs" "uglifyjs" : "bin/uglifyjs"