2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 from subprocess
import CalledProcessError
, check_output
11 here
= os
.path
.dirname(os
.path
.realpath(__file__
))
12 topsrcdir
= os
.path
.join(here
, os
.pardir
, os
.pardir
)
17 "python/mozversioncontrol",
18 "testing/mozbase/mozfile",
19 "third_party/python/jsmin",
21 sys
.path
[:0] = [os
.path
.join(topsrcdir
, p
) for p
in EXTRA_PATHS
]
23 from mozversioncontrol
import InvalidRepoPath
, get_repository_object
26 def run_js_format(hooktype
, changedFiles
):
28 vcs
= get_repository_object(topsrcdir
)
29 except InvalidRepoPath
:
33 # No files have been touched
36 extensions
= (".js", ".jsx", ".jsm", ".json", ".mjs", "sjs", "html", "xhtml")
38 for filename
in sorted(changedFiles
):
39 # Ignore files unsupported in eslint and prettier
40 if filename
.endswith(extensions
):
41 path_list
.append(filename
)
44 # No files have been touched
47 arguments
= ["eslint", "--fix"] + path_list
48 # On windows we need this to call the command in a shell, see Bug 1511594
50 js_format_cmd
= ["sh", "mach"] + arguments
52 js_format_cmd
= [os
.path
.join(topsrcdir
, "mach")] + arguments
53 if "commit" in hooktype
:
54 # don't prevent commits, just display the eslint and prettier results
55 subprocess
.call(js_format_cmd
)
57 vcs
.add_remove_files(*path_list
)
60 print("warning: '{}' is not a valid js-format hooktype".format(hooktype
))
65 hooktype
= os
.path
.basename(__file__
)
66 if hooktype
== "hooks_js_format.py":
70 changedFiles
= check_output(
71 ["git", "diff", "--staged", "--diff-filter=d", "--name-only", "HEAD"],
74 # TODO we should detect if we are in a "add -p" mode and show a warning
75 return run_js_format(hooktype
, changedFiles
)
77 except CalledProcessError
:
78 print("Command to retrieve local files failed")
82 if __name__
== "__main__":