diff --git a/lib/cli.js b/lib/cli.js index 981c15f..fb3f54b 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -92,27 +92,29 @@ function parse (source) { return JSON.stringify(parsed, null, options.indent); } catch (e) { - if (options.forcePrettyPrint) { - /* From https://github.com/umbrae/jsonlintdotcom: - * If we failed to validate, run our manual formatter and then re-validate so that we - * can get a better line number. On a successful validate, we don't want to run our - * manual formatter because the automatic one is faster and probably more reliable. - */ - - try { - formatted = formatter.formatJson(source, options.indent); - // Re-parse so exception output gets better line numbers - parsed = parser.parse(formatted); - } catch (e) { + if (!options.quiet) { + if (options.forcePrettyPrint) { + /* From https://github.com/umbrae/jsonlintdotcom: + * If we failed to validate, run our manual formatter and then re-validate so that we + * can get a better line number. On a successful validate, we don't want to run our + * manual formatter because the automatic one is faster and probably more reliable. + */ + + try { + formatted = formatter.formatJson(source, options.indent); + // Re-parse so exception output gets better line numbers + parsed = parser.parse(formatted); + } catch (e) { + if (! options.compact) { + console.error(e); + } + // force the pretty print before exiting + console.log(formatted); + } + } else { if (! options.compact) { console.error(e); } - // force the pretty print before exiting - console.log(formatted); - } - } else { - if (! options.compact) { - console.error(e); } } process.exit(1); @@ -146,7 +148,8 @@ function main (args) { source += chunk.toString('utf8'); }); stdin.on('end', function () { - if (! options.quiet) {console.log(parse(source))}; + var parsed = parse(source); + if (! options.quiet) {console.log(parsed)}; }); } }