diff --git a/docs/Contributing/adding-new-UI-components.md b/docs/Contributing/adding-new-UI-components.md new file mode 100644 index 0000000000..c5155a172f --- /dev/null +++ b/docs/Contributing/adding-new-UI-components.md @@ -0,0 +1,12 @@ +# Using `generate` to create boilerplate for a new Fleet UI component + +## Basic steps to create a new UI component + +1. `cd` into `fleet/frontend/components` +2. Run `./generate -n NewComponentName`, replacing `NewComponentName` with the desired name for your + new component. +3. A directory and all boilerplate for the various files of the component will be generated in + `fleet/frontend/components` + +You can also run `./generate -h` for information about the other options available (overwrite and +specifying destination) diff --git a/frontend/components/generate b/frontend/components/generate new file mode 100755 index 0000000000..14f6c68b3c --- /dev/null +++ b/frontend/components/generate @@ -0,0 +1,87 @@ +#!/usr/bin/python3 + +import argparse, os, re + +def validate_name(name: str): + return len(name) > 1 and name != name.lower() and name != name.upper() and name[0].isupper() + +def get_component_classname(name): + split_by_caps = re.findall('[A-Z][^A-Z]*', name) + return '-'.join([word.lower() for word in split_by_caps]) + +def createFleetTSXComponent(name: str, output_path: str, overwrite: bool): + target = os.path.join(output_path, name) + + # handle existing directory of the same name + try: + os.mkdir(target) + except FileExistsError: + if overwrite: + print("A directory with that name already exists, overwriting it.") + os.system(f"rm -r {target}") + os.mkdir(target) + else: + print("A directory with that name already exists, aborting.\nTo recursively overwrite existing directories of the same name, pass the '-o' flag.") + return 1 + + print (f"Creating component \"{name}\" in {os.path.abspath(output_path)}") + + base_class_name = get_component_classname(name) + react_import_ts = "import React from \"react\";" + component_import_ts = f"import {name} from \"./{name}\";" + + # write index file + os.system( + f"echo 'export {{ default }} from \"./{name}\";' > {target}/index.ts" + ) + + # write the component + base_class_ts = f"const baseClass = \"{base_class_name}\";" + interface_name_ts = f"I{name}" + interface_definition_ts = f"interface {interface_name_ts} {{\n\n}}" + component_ts = f"const {name} = ({{}}: {interface_name_ts}) => {{\n return (\n