Skip to content

Instantly share code, notes, and snippets.

@AlexMeuer
Last active November 16, 2023 12:36
Show Gist options
  • Save AlexMeuer/9651c5c1fe2405350be8311c0bc85da6 to your computer and use it in GitHub Desktop.
Save AlexMeuer/9651c5c1fe2405350be8311c0bc85da6 to your computer and use it in GitHub Desktop.
Extract prop types and component name
#!/bin/bash
# Assumes that the component file has the prop types above the functional component.
# The prop types may or may not have a JSDoc comment block above them.
# Assumes that the functional component is exported.
if [ $# -eq 0 ]; then
printf "Attempts to extract prop types and component name from a component file.\n"
printf "\e[92mUsage: \e[0m \e[93m%s <file_path>\e[0m\n" "$(basename "${0}")"
printf "\e[92mExample:\e[0m \e[93m%s src/components/MyComponent.tsx | tee /dev/tty | pbcopy\e[0m\n" "$(basename "${0}")"
exit 1
fi
FILE_PATH="${1}"
# Extract lines from the first occurrence of `/**` or `export type *Props`
START_LINE=$(grep -nE '\/\*\*|type [A-Za-z]+Props' "${FILE_PATH}" | head -n 1 | cut -d ':' -f 1)
# Extract the line with the function name
FUNC_NAME_LINE=$(grep -nE 'export const [A-Za-z]+: React.V?FC<[^>]+>\s*=\s*\(' "${FILE_PATH}" | head -n 1 | cut -d ':' -f 1)
# Print the extracted lines
sed -n "${START_LINE},${FUNC_NAME_LINE}p" "${FILE_PATH}"
#!/bin/bash
if [ -z "$1" ]; then
printf "\e[36mOpen the selected file on github\n"
printf "\e[37mAssumes the file is in the main branch.\n"
printf "Works from any subdirectory of the repo.\n"
printf "\e[92mUsage: \e[0m \e[93m%s <file_path>\e[0m\n" "$(basename "${0}")"
exit 1
fi
open "$(ghurl "$1")"
#!/bin/bash
if [ -z "$1" ]; then
printf "\e[36mGet the github url for the selected file\n"
printf "\e[37mAssumes the file is in the main branch.\n"
printf "Works from any subdirectory of the repo.\n"
printf "\e[92mUsage: \e[0m \e[93m%s <file_path>\e[0m\n" "$(basename "${0}")"
exit 1
fi
echo "$(gh repo view --json url --jq .url)/blob/main/$(git rev-parse --show-prefix)${1}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment