Skip to content

Instantly share code, notes, and snippets.

@skovhus
Created April 6, 2017 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skovhus/f79de9de3adfb9145f6faeb6cfbe299a to your computer and use it in GitHub Desktop.
Save skovhus/f79de9de3adfb9145f6faeb6cfbe299a to your computer and use it in GitHub Desktop.
Annotates redux actions files with some Flow types
# Super quick and dirty script to bootstrap our action files with some Flow boilerplate.
import re
import sys
def get_action_constant(l):
m = re.search('= \'(.*)\'', l)
if m:
return m.group(1)
def get_action_type_name(s):
return 'Action' + s.replace('_', ' ').lower().title().replace(' ', '')
def get_action_type(action_const):
action_name = get_action_type_name(action_const)
return """
type {} = {{
type: '{}',
}}
""".format(action_name, action_const)
def print_flow_types_for_action_file(file_name):
with open(file_name) as f:
action_consts = [
get_action_constant(l) for l in f.readlines()
if get_action_constant(l) is not None
]
for a in action_consts:
print get_action_type(a)
print """
export type ActionTypes =
{}
;
""".format('\n\t| '.join(map(get_action_type_name, action_consts)))
if __name__ == '__main__':
if len(sys.argv) < 2:
print 'Usage: give me a file path to an action file...'
else:
print_flow_types_for_action_file(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment