mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2025-08-14 09:27:21 +08:00
Addding no-sort-keys to pretty_format_json, this allows to disable the sort on the keys
This commit is contained in:
@ -6,10 +6,10 @@ import sys
|
||||
import simplejson
|
||||
|
||||
|
||||
def _get_pretty_format(contents, indent):
|
||||
def _get_pretty_format(contents, indent, no_sort_keys):
|
||||
return simplejson.dumps(
|
||||
simplejson.loads(contents),
|
||||
sort_keys=True,
|
||||
sort_keys=no_sort_keys,
|
||||
indent=indent
|
||||
) + "\n" # dumps don't end with a newline
|
||||
|
||||
@ -34,6 +34,13 @@ def pretty_format_json(argv=None):
|
||||
default=2,
|
||||
help='Number of indent spaces used to pretty-format files'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--no-sort-keys',
|
||||
action='store_true',
|
||||
dest='no_sort_keys',
|
||||
default=False,
|
||||
help='Do not sort the keys'
|
||||
)
|
||||
|
||||
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
||||
args = parser.parse_args(argv)
|
||||
@ -46,7 +53,7 @@ def pretty_format_json(argv=None):
|
||||
contents = f.read()
|
||||
f.close()
|
||||
|
||||
pretty_contents = _get_pretty_format(contents, args.indent)
|
||||
pretty_contents = _get_pretty_format(contents, args.indent, (not args.no_sort_keys))
|
||||
|
||||
if contents != pretty_contents:
|
||||
print("File {0} is not pretty-formatted".format(json_file))
|
||||
|
Reference in New Issue
Block a user