I am reorganising a react codebase and I would like to write a bash script which I can give a path to a component which lists all the files imported from that file recursively.
The output should be filtered by unique occurrences. I am only interested in the source files (aliased to @browse) (not node modules etc).
so far I have
#!/bin/bash
list_browse_imports() {
rg "from '@browse" $1 | sed "s,.*@browse,," | sed "s/'//"
}
list_browse_imports $1
which yields:
/search/template/template
/product/button/button
I then want to feed these lines onward to rg (feel free to substitute to grep) again to match all the files in the tree that match that like
#!/bin/bash
list_browse_imports() {
rg "from '@browse" $1 | sed "s,.*@browse,," | sed "s/'//" | xargs rg -l
}
list_browse_imports $1
but this yields nothing.
I have tried to extract the second part to it's own function but get stuck there as well.
EDIT:
made some progress with this very ugly (and slow) code:
#!/bin/bash
list_imports_rec() {
declare IMPORTS=$(rg "from '@browse" $1 | sed "s,.*@browse,," | sed "s/'//")
declare FILES=$(rg -l '')
#echo "RESULT IS $IMPORTS RESULT IS"
for import in $(rg "from '@browse" $1 | sed "s,.*@browse,," | sed "s/'//")
do
for file in $(rg -l '')
do
declare import_stripped=$(echo $import | sed "s,/,,g")
declare file_stripped=$(echo $file | sed "s,/,,g")
#echo "$import_stripped and $file_stripped"
if [[ "$file_stripped" =~ .*"$import_stripped".* ]]; then
echo "$file"
list_imports_rec $file
fi
done
done
}
list_imports_rec $1