diff --git a/README.md b/README.md index 58a1aa1..e9b3a97 100644 --- a/README.md +++ b/README.md @@ -27,15 +27,16 @@ jsonlint will either report a syntax error with details or pretty print the sour file file to parse; otherwise uses stdin Options: - -v, --version print version and exit - -s, --sort-keys sort object keys - -i, --in-place overwrite the file - -t CHAR, --indent CHAR character(s) to use for indentation [ ] - -c, --compact compact error display - -V, --validate a JSON schema to use for validation - -e, --environment which specification of JSON Schema the validation file uses [json-schema-draft-03] - -q, --quiet do not print the parsed json to STDOUT [false] - -p, --pretty-print force pretty printing even if invalid + -v, --version print version and exit + -s, --sort-keys sort object keys + -i, --in-place overwrite the file + -t CHAR, --indent CHAR character(s) to use for indentation [ ] + -n, --insert-final-newline ensure JSON string ends with a newline + -c, --compact compact error display + -V, --validate a JSON schema to use for validation + -e, --environment which specification of JSON Schema the validation file uses [json-schema-draft-03] + -q, --quiet do not print the parsed json to STDOUT [false] + -p, --pretty-print force pretty printing even if invalid ## Module interface diff --git a/lib/cli.js b/lib/cli.js index 981c15f..211d008 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -36,6 +36,11 @@ var options = require("nomnom") "default": " ", help: 'character(s) to use for indentation' }, + insertFinalNewline : { + flag : true, + string: '-n, --insert-final-newline', + help: 'ensure JSON string ends with a newline' + }, compact : { flag : true, string: '-c, --compact', @@ -90,7 +95,7 @@ function parse (source) { } } - return JSON.stringify(parsed, null, options.indent); + return JSON.stringify(parsed, null, options.indent) + (options.insertFinalNewline ? "\n" : ""); } catch (e) { if (options.forcePrettyPrint) { /* From https://github.com/umbrae/jsonlintdotcom: @@ -100,7 +105,7 @@ function parse (source) { */ try { - formatted = formatter.formatJson(source, options.indent); + formatted = formatter.formatJson(source, options.indent, options.insertFinalNewline); // Re-parse so exception output gets better line numbers parsed = parser.parse(formatted); } catch (e) { diff --git a/lib/formatter.js b/lib/formatter.js index e6e2726..de398a6 100644 --- a/lib/formatter.js +++ b/lib/formatter.js @@ -16,7 +16,7 @@ var formatter = (function () { return new Array(count + 1).join(s); } - function formatJson(json, indentChars) { + function formatJson(json, indentChars, insertFinalNewline) { var i = 0, il = 0, tab = (typeof indentChars !== "undefined") ? indentChars : " ", @@ -80,6 +80,8 @@ var formatter = (function () { } } + if (insertFinalNewline) newJson += "\n"; + return newJson; }