Skip to content

Instantly share code, notes, and snippets.

@wrhall
Last active November 3, 2023 22:34
Show Gist options
  • Save wrhall/524cced4f4b5e64bca00e0b6df01fb50 to your computer and use it in GitHub Desktop.
Save wrhall/524cced4f4b5e64bca00e0b6df01fb50 to your computer and use it in GitHub Desktop.
search_exported_names() {
local export_name="$1"
local file_list
local count
count=$(rg -F --type-add 'ts:*.ts' --type-add 'tsx:*.tsx' --type 'ts' --type 'tsx' -c --no-filename "${export_name}" < /dev/null | awk '{sum += $1} END {print sum}' || echo "0")
echo "Search term: ${export_name} - Count: ${count}"
}
rg --type-add 'ts:*.ts' --type-add 'tsx:*.tsx' --type 'ts' --type 'tsx' 'export (const|function|class|type|interface) \w+' -o --no-filename | awk '{print $3}' | sort | uniq > exported_names.txt
while IFS= read -r export_name; do
search_exported_names "$export_name"
done < exported_names.txt | grep -E ' - Count: 1$'
@wrhall
Copy link
Author

wrhall commented Nov 3, 2023

You could replace the count with 2 invocations of rg -- one that finds up to 2 files, and then the other which counts occurrences in the files. But this isn't obviously faster on my data:

search_exported_names() {
  local export_name="$1"
  local file_list
  local count

  # Find files that contain the export_name, but only take the first two
  file_list=$(rg -m 1 --type-add 'ts:*.ts' --type-add 'tsx:*.tsx' --type 'ts' --type 'tsx' -lF "${export_name}" < /dev/null | head -n 2)

  # If no files are found, count is zero
  if [[ -z "$file_list" ]]; then
    count=0
  else
    # Count occurrences in up to the first two files found
    count=$(echo "${file_list}" | xargs rg -m 2 -F --no-filename -c "${export_name}" | awk '{sum += $1} END {print sum}')
  fi

  echo "Search term: ${export_name} - Count: ${count}"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment