Skip to content

Instantly share code, notes, and snippets.

@somoso
Last active July 11, 2018 15:19
Show Gist options
  • Save somoso/298806497de7587575ea5a0e12d9a3f6 to your computer and use it in GitHub Desktop.
Save somoso/298806497de7587575ea5a0e12d9a3f6 to your computer and use it in GitHub Desktop.
Updated gist to support React Native 0.55
#!/usr/bin/env python3
import urllib.request
import sys
import argparse
import configparser
def main(parsed_args):
url = get_rn_url(parsed_args)
response = urllib.request.urlopen(url)
data = response.read()
text = data.decode('utf-8').split("\n")
line_and_column = parsed_args['line'].split(":")
line_no = int(line_and_column[0])
column = -1
if len(line_and_column) > 1:
column = int(line_and_column[1])
line_min = line_no - parsed_args['rows']
if line_min < 0:
line_min = 0
line_max = line_no + parsed_args['rows']
if line_max > len(text):
line_max = len(text)
for i, line in enumerate(text):
if line_min <= i <= line_max:
if i == line_no and column >= 0:
print((" " * (column - 1)) + "^")
print(line)
elif (i + 1) == line_no:
print(">" + line)
else:
print(line)
if i > line_max:
break
def get_rn_url(parsed_args):
base_url = parsed_args['url']
if ":" not in base_url:
base_url += ":" + str(parsed_args['port'])
if "://" not in base_url:
base_url = "http://" + base_url
url = base_url + "/index" + ".bundle?platform=" + parsed_args['platform'] + "&dev=" + str(
not parsed_args['prod']).lower() + "&hot=" + str(parsed_args['hot']).lower() + "&minify=" + str(parsed_args['minify']).lower()
return url
def parse_args():
platform = "ios"
if sys.platform != "darwin":
platform = "android"
cli_args = read_cli_args(platform)
substitute_args_from_ini(cli_args)
return cli_args
def substitute_args_from_ini(args):
if (args['config'] is not None):
conf = configparser.ConfigParser()
conf.read(args['config'])
options = conf.options(args['config_section'])
for opt in options:
args[opt] = conf.get(args['config_section'], opt)
def read_cli_args(platform):
parser = argparse.ArgumentParser(description="Finds log information about cryptic React Native errors")
parser.add_argument("-c", "--config", help="Local .ini config file to read command line defaults (useful for loading "
"certain dev environment defaults)")
parser.add_argument("-C","--config-section", help="The section of the .ini config file to read from (useless without "
"--config param and defaults to 'default')", default="default")
parser.add_argument("-u", "--url", help="The URL where the npm server is located", default="localhost")
parser.add_argument("-P","--port", type=int, help="Port number of server (default 8081)", default=8081)
parser.add_argument("line", help="Line (and column) number of the error in LLLL:CC format")
parser.add_argument("-p", "--platform", help="Platform to target (ios or Android). Default is iOS if run on macOS, otherwise Android", default=platform)
parser.add_argument("--prod", help="Is build made for production? (i.e. dev=false in URL)", action="store_true", default=False)
parser.add_argument("--hot", help="Is build setup for hot reloading? (i.e. hot=true in URL)", action="store_true", default=False)
parser.add_argument("-m", "--minify", help="Is build setup to be minified? (i.e. minify=true)", action="store_true", default=False)
parser.add_argument("-r", "--rows", type=int, default=6)
cli_args = parser.parse_args()
cli_args = vars(cli_args)
return cli_args
if __name__ == '__main__':
args = parse_args()
main(args)
# Check out rn_bug.py -h for more details on the parameters or just look at read_cli_args() for more info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment