forked from mirrors/gecko-dev
Bug 1556393 - Pre 0 (m-c): Create a simple Prettier command for running format-source, r=andi
Differential Revision: https://phabricator.services.mozilla.com/D36720 --HG-- extra : source : c168aefc21dfec8a1935272201592f3781df07b0 extra : intermediate-source : 008847a148cfd171e6e23425be5d6876c6174ef0
This commit is contained in:
parent
9a679d6903
commit
fc0105c226
1 changed files with 28 additions and 0 deletions
|
|
@ -29,6 +29,7 @@ from mach.main import Mach
|
|||
from mozbuild.base import MachCommandBase
|
||||
|
||||
from mozbuild.build_commands import Build
|
||||
from mozbuild.nodeutil import find_node_executable
|
||||
|
||||
import mozpack.path as mozpath
|
||||
|
||||
|
|
@ -1327,6 +1328,33 @@ class StaticAnalysis(MachCommandBase):
|
|||
print(' '*4 + checker)
|
||||
return 0
|
||||
|
||||
@Command('prettier-format', category='misc', description='Run prettier on current changes')
|
||||
@CommandArgument('--path', '-p', nargs=1, required=True,
|
||||
help='Specify the path to reformat to stdout.')
|
||||
@CommandArgument('--assume-filename', '-a', nargs=1, required=True,
|
||||
help='This option is usually used in the context of hg-formatsource.'
|
||||
'When reading from stdin, Prettier assumes this '
|
||||
'filename to decide which style and parser to use.')
|
||||
def prettier_format(self, path, assume_filename):
|
||||
# With assume_filename we want to have stdout clean since the result of the
|
||||
# format will be redirected to stdout.
|
||||
|
||||
binary, _ = find_node_executable()
|
||||
prettier = os.path.join(self.topsrcdir, "node_modules", "prettier", "bin-prettier.js")
|
||||
path = os.path.join(self.topsrcdir, path[0])
|
||||
|
||||
# We use --stdin-filepath in order to better determine the path for
|
||||
# the prettier formatter when it is ran outside of the repo, for example
|
||||
# by the extension hg-formatsource.
|
||||
args = [binary, prettier, '--stdin-filepath', assume_filename[0]]
|
||||
|
||||
process = subprocess.Popen(args, stdin=subprocess.PIPE)
|
||||
with open(path, 'r') as fin:
|
||||
process.stdin.write(fin.read())
|
||||
process.stdin.close()
|
||||
process.wait()
|
||||
return process.returncode
|
||||
|
||||
@Command('clang-format', category='misc', description='Run clang-format on current changes')
|
||||
@CommandArgument('--show', '-s', action='store_const', const='stdout', dest='output_path',
|
||||
help='Show diff output on stdout instead of applying changes')
|
||||
|
|
|
|||
Loading…
Reference in a new issue