@@ -3,13 +3,10 @@ from __future__ import absolute_import
33
44import os
55import sys
6- import subprocess
7- import json
86
97from glob import glob
10- from click import echo , style
118
12- from sentry .lint .engine import check_files , get_js_files
9+ from sentry .lint .engine import check_files , js_format , yarn_check
1310
1411text_type = type (u'' )
1512
@@ -19,77 +16,6 @@ if 'VIRTUAL_ENV' in os.environ:
1916 '%s/lib/*/site-packages' % os .environ ['VIRTUAL_ENV' ])[0 ]
2017 sys .path .insert (0 , site_packages )
2118
22- PRETTIER_VERSION = "1.2.2"
23-
24- def yarn_check (file_list ):
25- """
26- Checks if package.json was modified WITHOUT a corresponding change in the Yarn
27- lockfile. This can happen if a user manually edited package.json without running Yarn.
28-
29- This is a user prompt right now because there ARE cases where you can touch package.json
30- without a Yarn lockfile change, e.g. Jest config changes, license changes, etc.
31- """
32- if file_list is None or os .environ .get ('SKIP_YARN_CHECK' ):
33- return False
34-
35- if 'package.json' in file_list and 'yarn.lock' not in file_list :
36- echo (style ("""
37- Warning: package.json modified without accompanying yarn.lock modifications.
38-
39- If you updated a dependency/devDependency in package.json, you must run `yarn install` to update the lockfile.
40-
41- To skip this check, run:
42-
43- $ SKIP_YARN_CHECK=1 git commit [options]""" , fg = 'yellow' ))
44- return True
45-
46- return False
47-
48-
49- def js_format (file_list = None ):
50- """
51- We only format JavaScript code as part of this pre-commit hook. It is not part
52- of the lint engine.
53- """
54- project_root = os .path .join (os .path .dirname (__file__ ), os .pardir , os .pardir )
55- prettier_path = os .path .join (project_root , 'node_modules' , '.bin' , 'prettier' )
56-
57- if not os .path .exists (prettier_path ):
58- echo ('!! Skipping JavaScript formatting because prettier is not installed.' , err = True )
59- return False
60-
61- # Get Prettier version from package.json
62- package_version = None
63- package_json_path = os .path .join (project_root , 'package.json' )
64- with open (package_json_path ) as package_json :
65- try :
66- package_version = json .load (package_json )['devDependencies' ]['prettier' ]
67- except KeyError :
68- echo ('!! Prettier missing from package.json' , err = True )
69- return False
70-
71- prettier_version = subprocess .check_output ([prettier_path , '--version' ]).rstrip ()
72- if prettier_version != package_version :
73- echo ('!! Prettier is out of date: %s (expected %s). Please run `yarn install`.' \
74- % (prettier_version , package_version ), err = True )
75- return False
76-
77- js_file_list = get_js_files (file_list )
78-
79- has_errors = False
80- if js_file_list :
81- status = subprocess .Popen ([prettier_path , '--write' , '--single-quote' ,
82- '--bracket-spacing=false' , '--print-width=90' , '--jsx-bracket-same-line=true' ] +
83- js_file_list
84- ).wait ()
85- has_errors = status != 0
86-
87- if not has_errors :
88- # Stage modifications by Prettier
89- status = subprocess .Popen (['git' , 'update-index' , '--add' ] + file_list ).wait ()
90- has_errors = status != 0
91-
92- return has_errors
9319
9420def main ():
9521 from flake8 .hooks import run
0 commit comments