From ff5757d1870f6b59342b0cc78d79d813ed86ead5 Mon Sep 17 00:00:00 2001 From: Pavol Bielik Date: Sun, 25 Jan 2015 12:19:30 +0100 Subject: [PATCH] Add colored console output --- bin/js_features.js | 38 ++++++++++++++++++++++++++++---------- lib/feature_extractor.js | 17 +++++++++-------- package.json | 3 ++- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/bin/js_features.js b/bin/js_features.js index 2b361bd9..c8145ee0 100755 --- a/bin/js_features.js +++ b/bin/js_features.js @@ -7,6 +7,7 @@ var UglifyJS = require("../tools/node"); var sys = require("util"); var yargs = require("yargs"); var fs = require("fs"); +var colors = require("colors"); var ARGS = yargs .usage("$0 input1.js \n") @@ -37,23 +38,23 @@ if (ARGS.h || ARGS.help) { var files = ARGS._.slice(); if (files.length > 1) { sys.error("WARNING: expected only single input file. Processing file '" + files[0] + "' while the rest is ignored."); -}; +} if (ARGS.features === true) { sys.error("ERROR: empty set of features."); process.exit(1); -}; +} var features = ARGS.features.split(","); for (var i = 0; i < features.length; i++) { if (features[i] != "FNAMES" && features[i] != "ASTREL" && features[i] != "FSCOPE") { sys.error("WARNING: ignoring not supported feature '" + features[i] + "'."); - }; -}; + } +} for (var i = 0; i < files.length; i++) { processFile(files[i], ARGS.print_ast, ARGS.features, ARGS.json_formatting, ARGS.skip_minified); -}; +} function stripInterpreter(code){ if (code.slice(0,2) != "#!"){ @@ -69,14 +70,27 @@ function processFile(file, print_ast, features, json_formatting, skip_minified) code = fs.readFileSync(file, "utf-8"); } catch (ex) { - sys.error("ERROR: can't read file: " + file); + sys.error("ERROR:".red + " can't read file '" + file + "'"); return; } //if it is a script, the UglifyJS parser will fail to parse it code = stripInterpreter(code); - - var output = UglifyJS.extractFeatures(code, file, print_ast, features, skip_minified); + + try { + var output = UglifyJS.extractFeatures(code, file, print_ast, features, skip_minified); + } catch (ex){ + if (ex instanceof UglifyJS.JS_Parse_Error){ + sys.error("ERROR: ".red + "cannot parse file '" + file + "'"); + } else if (ex instanceof UglifyJS.JS_Minified_Error){ + sys.error("WARN: ".yellow + "skipping minified file '" + file + "'"); + } else { + sys.error("ERROR: ".red + "'" + file + "'" + e); + } + + return; + } + if (output == null) { return; } @@ -88,13 +102,17 @@ function processFile(file, print_ast, features, json_formatting, skip_minified) //validate JSON try { JSON.parse(output); - } catch (e) { + } catch (e) { + sys.error("ERROR: ".red + "output is not valid JSON " + "'" + file + "'"); throw e; } if (removeWhitespace(output) != '{"query":[],"assign":[]}') { console.log(output); - } + sys.error("OK: ".green + "'" + file + "'"); + } else { + sys.error("EMPTY: ".yellow + "'" + file + "'"); + } } diff --git a/lib/feature_extractor.js b/lib/feature_extractor.js index 4b36bcd9..a181fc33 100644 --- a/lib/feature_extractor.js +++ b/lib/feature_extractor.js @@ -58,14 +58,14 @@ function replaceMangled(code, file) { return stream.toString(); } +function JS_Minified_Error(message) { + this.message = message; +} + function extractFeatures(code, file, print_ast, features, skip_minified) { var toplevel; - try { - toplevel = parseFile(code, file); - } catch (e) { - console.warn("Cannot parse file: '%s'", file); - return null; - } + + toplevel = parseFile(code, file); extendAst(toplevel); @@ -74,8 +74,9 @@ function extractFeatures(code, file, print_ast, features, skip_minified) { } if (skip_minified && isMinified(toplevel, code, file)){ - console.warn("Skipping minified file: '%s'", file); - return null; + throw new JS_Minified_Error("Skipping minified file"); + //console.warn("Skipping minified file: '%s'", file); + //return null; } var feature_outputter = new FeatureJsonOutputter(); diff --git a/package.json b/package.json index 8e066602..09f8ea6d 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "acorn": "~0.6.0", "escodegen": "~1.3.3", "esfuzz": "~0.3.1", - "estraverse": "~1.5.1" + "estraverse": "~1.5.1", + "colors": "~1.0.3" }, "browserify": { "transform": [ "uglify-to-browserify" ]